Skip to content

Commit

Permalink
style: Normalize indents to tab
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed May 22, 2021
1 parent b75a51d commit 0195dd3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
56 changes: 28 additions & 28 deletions src/nodes/text.ts
Expand Up @@ -19,42 +19,42 @@ export default class TextNode extends Node {

private _trimmedText?: string;

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

const text = this.rawText;
let i = 0;
let startPos;
let endPos;
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;
}
}
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) i++;
else i--;
}

if (startPos === undefined) startPos = 0;
if (endPos === undefined) endPos = text.length - 1;
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]);
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 = (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');

return this._trimmedText;
}
return this._trimmedText;
}

/**
* Get unescaped text value of current node and its children.
Expand Down
8 changes: 4 additions & 4 deletions test/html.js
Expand Up @@ -208,10 +208,10 @@ describe('HTML Parser', function () {
});

it('should preserve legitimate leading/trailing whitespace in TextNode', function () {
parseHTML('<p>Hello <em>World</em>!</p>').removeWhitespace().firstChild.text.should.eql('Hello World!');
parseHTML('<p>\t\nHello\n\t<em>World</em>!</p>').removeWhitespace().firstChild.text.should.eql('HelloWorld!');
parseHTML('<p>\t\n Hello \n\t<em>World</em>!</p>').removeWhitespace().firstChild.text.should.eql(' Hello World!');
});
parseHTML('<p>Hello <em>World</em>!</p>').removeWhitespace().firstChild.text.should.eql('Hello World!');
parseHTML('<p>\t\nHello\n\t<em>World</em>!</p>').removeWhitespace().firstChild.text.should.eql('HelloWorld!');
parseHTML('<p>\t\n Hello \n\t<em>World</em>!</p>').removeWhitespace().firstChild.text.should.eql(' Hello World!');
});
});

describe('#rawAttributes', function () {
Expand Down

0 comments on commit 0195dd3

Please sign in to comment.