Skip to content

Commit

Permalink
Refactored to implement with structuredText
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed May 22, 2021
1 parent d4d06ed commit b75a51d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
39 changes: 2 additions & 37 deletions src/nodes/html.ts
Expand Up @@ -134,41 +134,6 @@ export default class HTMLElement extends Node {
return JSON.stringify(attr.replace(/"/g, '"'));
}

/**
* Trim all whitespace except single leading/trailing non-breaking space
* @param text string to trim
* @returns {string} trimmed value
* @private
*/
private trimTextNodeWhitespace(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 ? ' ' : '');
}

/**
* Creates an instance of HTMLElement.
* @param keyAttrs id and class attribute
Expand Down Expand Up @@ -296,7 +261,7 @@ export default class HTMLElement extends Node {
// Whitespace node, postponed output
currentBlock.prependWhitespace = true;
} else {
let text = node.text;
let text = (<TextNode>node).trimmedText;
if (currentBlock.prependWhitespace) {
text = ` ${text}`;
currentBlock.prependWhitespace = false;
Expand Down Expand Up @@ -437,7 +402,7 @@ export default class HTMLElement extends Node {
if ((node as TextNode).isWhitespace) {
return;
}
node.rawText = this.trimTextNodeWhitespace(node.rawText);
node.rawText = (<TextNode>node).trimmedText;
} else if (node.nodeType === NodeType.ELEMENT_NODE) {
(node as HTMLElement).removeWhitespace();
}
Expand Down
39 changes: 39 additions & 0 deletions src/nodes/text.ts
Expand Up @@ -17,6 +17,45 @@ export default class TextNode extends Node {
*/
public nodeType = NodeType.TEXT_NODE;

private _trimmedText?: string;

/**
* 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 ? ' ' : '');

return this._trimmedText;
}

/**
* Get unescaped text value of current node and its children.
* @return {string} text content
Expand Down
2 changes: 1 addition & 1 deletion test/html.js
Expand Up @@ -202,7 +202,7 @@ describe('HTML Parser', function () {

const p = new HTMLElement('p', {}, '', root);
p.appendChild(new HTMLElement('h5', {}, ''))
.appendChild(new TextNode('123'));
.appendChild(Object.assign(new TextNode('123'), { _trimmedText: '123' }));

root.firstChild.removeWhitespace().should.eql(p);
});
Expand Down

0 comments on commit b75a51d

Please sign in to comment.