Skip to content

Commit

Permalink
support ingoring comments
Browse files Browse the repository at this point in the history
  • Loading branch information
henryluki committed May 19, 2017
1 parent 41964fe commit fff53b1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions dist/htmlParser.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ function tokenize(html) {
const maxTime = Date.now() + 1000;

while (string) {
if (string.indexOf("<!--") === 0) {
const lastIndex = string.indexOf("-->") + 3;
string = string.substring(lastIndex);
continue
}
if (string.indexOf("</") === 0) {
const match = string.match(ENDTAG_REX);
if (!match) continue
Expand Down
5 changes: 5 additions & 0 deletions dist/htmlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ function tokenize(html) {
var maxTime = Date.now() + 1000;

while (string) {
if (string.indexOf("<!--") === 0) {
var lastIndex = string.indexOf("-->") + 3;
string = string.substring(lastIndex);
continue;
}
if (string.indexOf("</") === 0) {
var match = string.match(ENDTAG_REX);
if (!match) continue;
Expand Down
2 changes: 1 addition & 1 deletion dist/htmlParser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export function tokenize(html) {
const maxTime = Date.now() + 1000

while (string) {
if (string.indexOf("<!--") === 0) {
const lastIndex = string.indexOf("-->") + 3
string = string.substring(lastIndex)
continue
}
if (string.indexOf("</") === 0) {
const match = string.match(ENDTAG_REX)
if (!match) continue
Expand Down
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ test('should pass the empty tag case', t=> {
t.deepEqual(div.children[0], br)
})

test('should pass the comment case', t=> {
const html = "<div><!-- hahha--></div>"
const div = htmlParser(html).children[0]
t.deepEqual(div.children.length, 0)
})

test('should pass the nested element case', t=> {
const html = "<div>a<p>b<span>c</span></p></div>"
const tree = {
Expand Down

0 comments on commit fff53b1

Please sign in to comment.