forked from ashi009/node-fast-html-parser
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from nonara/fix-137
feat: Add range to nodes and fix whitespace issue (fixes #137)
- Loading branch information
Showing
8 changed files
with
176 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const { parse, HTMLElement, TextNode, CommentNode } = require('../dist'); | ||
const hp2 = require('htmlparser2') | ||
const mochaEach = require('mocha-each'); | ||
|
||
// Use https://astexplorer.net/ to compare | ||
const html = ` | ||
Leading text | ||
<div> | ||
<p>Text Content</p> | ||
Goes Here | ||
</div> | ||
<input name="hello"> | ||
<!-- comment --> | ||
<style> | ||
.abc { | ||
display: none | ||
} | ||
</style> | ||
<pre> | ||
block Text | ||
</pre> | ||
<span>The space between us</span> <span>is vast</span> | ||
Closing text | ||
`; | ||
|
||
function prepare() { | ||
const nodeMeta = []; | ||
const abbreviate = (s, maxLen = 8) => | ||
(s.length > maxLen ? s.slice(0, maxLen) + '...' : s).replace(/(\r?\n)/g, '\\n'); | ||
|
||
// Parse AST | ||
const hp2ast = hp2.parseDocument(html, { withEndIndices: true, withStartIndices: true }); | ||
const ast = parse(html, { comment: true }); | ||
|
||
// Prepare flatNodes | ||
ast.childNodes.forEach((n, idx, arr) => walk(arr, idx, hp2ast.childNodes)); | ||
|
||
return { nodeMeta, ast, hp2ast }; | ||
|
||
function walk(nodeArr, idx, mirrorArr) { | ||
const node = nodeArr[idx]; | ||
const mirrorNode = mirrorArr[idx]; | ||
|
||
const label = mirrorNode.type !== 'tag' ? `<${mirrorNode.type}: '${abbreviate(node.text)}'>` : node.tagName; | ||
nodeMeta.push([ label, node, mirrorNode ]); | ||
|
||
node.childNodes.forEach((n, idx, arr) => walk(arr, idx, mirrorNode.childNodes)); | ||
} | ||
} | ||
|
||
// See: https://github.com/taoqf/node-html-parser/issues/137 | ||
describe(`Elements ranges`, function () { | ||
const { nodeMeta, ast } = prepare(); | ||
|
||
before(() => { | ||
// Pre-check to make sure configured html is not altered | ||
ast.childNodes.length.should.be.greaterThan(2); | ||
}); | ||
|
||
describe(`parsed elements created with proper ranges`, () => { | ||
mochaEach(nodeMeta).it(`%s`, (label, node, hp2Node) => { | ||
/* Ensure we have the right node mapping */ | ||
const expectedProto = hp2Node.type === 'comment' ? CommentNode : | ||
hp2Node.type === 'text' ? TextNode : | ||
HTMLElement; | ||
Object.getPrototypeOf(node).constructor.should.eql(expectedProto); | ||
if (expectedProto === HTMLElement) node.tagName.toLocaleLowerCase().should.eql(hp2Node.name.toLocaleLowerCase()); | ||
|
||
// Check range | ||
node.range.should.eql([ hp2Node.startIndex, hp2Node.endIndex + 1 ]); | ||
}); | ||
}); | ||
|
||
it(`new nodes are created with [ -1, -1 ] range by default`, () => { | ||
const nodes = [ | ||
new HTMLElement('B', {}, '', null), | ||
new TextNode('text', null), | ||
new CommentNode('text', null) | ||
]; | ||
|
||
for (const node of nodes) node.range.should.eql([ -1, -1 ]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters