Skip to content

Commit

Permalink
fix: TextNode text property is not decoded (fixes #134)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Jun 30, 2021
1 parent e35e4ff commit 7dea8d7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ export default class HTMLElement extends Node {
if ((node as TextNode).isWhitespace) {
return;
}
node.rawText = (<TextNode>node).trimmedText;
node.rawText = (<TextNode>node).trimmedRawText;
} else if (node.nodeType === NodeType.ELEMENT_NODE) {
(node as HTMLElement).removeWhitespace();
}
Expand Down
62 changes: 31 additions & 31 deletions src/nodes/text.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import NodeType from './type';
import Node from './node';
import HTMLElement from './html';
import { trimText } from '../utils';
import { decode } from 'he';


/**
* TextNode to contain a text element in DOM tree.
* @param {string} value [description]
*/
export default class TextNode extends Node {
public constructor(public rawText: string, parentNode: HTMLElement) {
public constructor(rawText: string, parentNode: HTMLElement) {
super(parentNode);
this._rawText = rawText;
}

/**
Expand All @@ -17,42 +21,38 @@ export default class TextNode extends Node {
*/
public nodeType = NodeType.TEXT_NODE;

private _rawText: string;
private _trimmedRawText?: string;
private _trimmedText?: string;

public get rawText() {
return this._rawText;
}

/**
* Set rawText and invalidate trimmed caches
*/
public set rawText(text: string) {
this._rawText = text;
this._trimmedRawText = void 0;
this._trimmedText = void 0;
}

/**
* Returns raw text with all whitespace trimmed except single leading/trailing non-breaking space
*/
public get trimmedRawText() {
if (this._trimmedRawText !== undefined) return this._trimmedRawText;
this._trimmedRawText = trimText(this.rawText);
return this._trimmedRawText;
}

/**
* Returns text with all whitespace trimmed except single leading/trailing non-breaking space
*/
public get trimmedText() {
if (this._trimmedText !== undefined) return this._trimmedText;

const text = this.rawText;
let i = 0;
let startPos;
let endPos;

while (i >= 0 && i < text.length) {
if (/\S/.test(text[i])) {
if (startPos === undefined) {
startPos = i;
i = text.length;
} else {
endPos = i;
i = void 0;
}
}

if (startPos === undefined) i++;
else i--;
}

if (startPos === undefined) startPos = 0;
if (endPos === undefined) endPos = text.length - 1;

const hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos-1]);
const hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos+1]);

this._trimmedText = (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');

this._trimmedText = trimText(this.text);
return this._trimmedText;
}

Expand All @@ -61,7 +61,7 @@ export default class TextNode extends Node {
* @return {string} text content
*/
public get text() {
return this.rawText;
return decode(this.rawText);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Trim whitespace except single leading/trailing non-breaking space
*/
export function trimText(text: string): string {
let i = 0;
let startPos;
let endPos;

while (i >= 0 && i < text.length) {
if (/\S/.test(text[i])) {
if (startPos === undefined) {
startPos = i;
i = text.length;
} else {
endPos = i;
i = void 0;
}
}

if (startPos === undefined) i++;
else i--;
}

if (startPos === undefined) startPos = 0;
if (endPos === undefined) endPos = text.length - 1;

const hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos-1]);
const hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos+1]);

return (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');
}

0 comments on commit 7dea8d7

Please sign in to comment.