Skip to content

Commit

Permalink
feat(stringify): Add textContent and innerText functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Jun 6, 2021
1 parent 81ac52f commit 2cf0814
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/stringify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getInnerHTML, getOuterHTML } from "./";
import {
getInnerHTML,
getOuterHTML,
getText,
textContent,
innerText,
} from "./";
import fixture from "./__fixtures__/fixture";

describe("stringify", () => {
Expand All @@ -17,4 +23,22 @@ describe("stringify", () => {
);
});
});

describe("getText", () => {
it("Correctly renders the text content", () => {
expect(getText(fixture[1])).toBe(" text text ");
});
});

describe("textContent", () => {
it("Correctly renders the text content", () => {
expect(textContent(fixture[1])).toBe(" text text ");
});
});

describe("innerText", () => {
it("Correctly renders the text content", () => {
expect(innerText(fixture[1])).toBe(" text ");
});
});
});
36 changes: 35 additions & 1 deletion src/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isTag, isCDATA, isText, hasChildren, Node } from "domhandler";
import renderHTML, { DomSerializerOptions } from "dom-serializer";
import { ElementType } from "domelementtype";

/**
* @param node Node to get the outer HTML of.
Expand Down Expand Up @@ -30,8 +31,9 @@ export function getInnerHTML(
}

/**
* Get a node's inner text.
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags.
*
* @deprecated Use `textContent` instead.
* @param node Node to get the inner text of.
* @returns `node`'s inner text.
*/
Expand All @@ -42,3 +44,35 @@ export function getText(node: Node | Node[]): string {
if (isText(node)) return node.data;
return "";
}

/**
* Get a node's text content.
*
* @param node Node to get the text content of.
* @returns `node`'s text content.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent}
*/
export function textContent(node: Node | Node[]): string {
if (Array.isArray(node)) return node.map(textContent).join("");
if (isTag(node)) return textContent(node.children);
if (isCDATA(node)) return textContent(node.children);
if (isText(node)) return node.data;
return "";
}

/**
* Get a node's inner text.
*
* @param node Node to get the inner text of.
* @returns `node`'s inner text.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText}
*/
export function innerText(node: Node | Node[]): string {
if (Array.isArray(node)) return node.map(innerText).join("");
if (hasChildren(node) && node.type === ElementType.Tag) {
return innerText(node.children);
}
if (isCDATA(node)) return innerText(node.children);
if (isText(node)) return node.data;
return "";
}

0 comments on commit 2cf0814

Please sign in to comment.