Skip to content

Commit b75a51d

Browse files
committed
Refactored to implement with structuredText
1 parent d4d06ed commit b75a51d

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

src/nodes/html.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -134,41 +134,6 @@ export default class HTMLElement extends Node {
134134
return JSON.stringify(attr.replace(/"/g, '"'));
135135
}
136136

137-
/**
138-
* Trim all whitespace except single leading/trailing non-breaking space
139-
* @param text string to trim
140-
* @returns {string} trimmed value
141-
* @private
142-
*/
143-
private trimTextNodeWhitespace(text: string): string {
144-
let i = 0;
145-
let startPos;
146-
let endPos;
147-
148-
while (i >= 0 && i < text.length) {
149-
if (/\S/.test(text[i])) {
150-
if (startPos === undefined) {
151-
startPos = i;
152-
i = text.length;
153-
} else {
154-
endPos = i;
155-
i = void 0;
156-
}
157-
}
158-
159-
if (startPos === undefined) i++;
160-
else i--;
161-
}
162-
163-
if (startPos === undefined) startPos = 0;
164-
if (endPos === undefined) endPos = text.length - 1;
165-
166-
const hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos-1]);
167-
const hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos+1]);
168-
169-
return (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');
170-
}
171-
172137
/**
173138
* Creates an instance of HTMLElement.
174139
* @param keyAttrs id and class attribute
@@ -296,7 +261,7 @@ export default class HTMLElement extends Node {
296261
// Whitespace node, postponed output
297262
currentBlock.prependWhitespace = true;
298263
} else {
299-
let text = node.text;
264+
let text = (<TextNode>node).trimmedText;
300265
if (currentBlock.prependWhitespace) {
301266
text = ` ${text}`;
302267
currentBlock.prependWhitespace = false;
@@ -437,7 +402,7 @@ export default class HTMLElement extends Node {
437402
if ((node as TextNode).isWhitespace) {
438403
return;
439404
}
440-
node.rawText = this.trimTextNodeWhitespace(node.rawText);
405+
node.rawText = (<TextNode>node).trimmedText;
441406
} else if (node.nodeType === NodeType.ELEMENT_NODE) {
442407
(node as HTMLElement).removeWhitespace();
443408
}

src/nodes/text.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,45 @@ export default class TextNode extends Node {
1717
*/
1818
public nodeType = NodeType.TEXT_NODE;
1919

20+
private _trimmedText?: string;
21+
22+
/**
23+
* Returns text with all whitespace trimmed except single leading/trailing non-breaking space
24+
*/
25+
public get trimmedText() {
26+
if (this._trimmedText !== undefined) return this._trimmedText;
27+
28+
const text = this.rawText;
29+
let i = 0;
30+
let startPos;
31+
let endPos;
32+
33+
while (i >= 0 && i < text.length) {
34+
if (/\S/.test(text[i])) {
35+
if (startPos === undefined) {
36+
startPos = i;
37+
i = text.length;
38+
} else {
39+
endPos = i;
40+
i = void 0;
41+
}
42+
}
43+
44+
if (startPos === undefined) i++;
45+
else i--;
46+
}
47+
48+
if (startPos === undefined) startPos = 0;
49+
if (endPos === undefined) endPos = text.length - 1;
50+
51+
const hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos-1]);
52+
const hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos+1]);
53+
54+
this._trimmedText = (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');
55+
56+
return this._trimmedText;
57+
}
58+
2059
/**
2160
* Get unescaped text value of current node and its children.
2261
* @return {string} text content

test/html.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe('HTML Parser', function () {
202202

203203
const p = new HTMLElement('p', {}, '', root);
204204
p.appendChild(new HTMLElement('h5', {}, ''))
205-
.appendChild(new TextNode('123'));
205+
.appendChild(Object.assign(new TextNode('123'), { _trimmedText: '123' }));
206206

207207
root.firstChild.removeWhitespace().should.eql(p);
208208
});

0 commit comments

Comments
 (0)