htmllint / htmllint

Unofficial html5 linter and validator.
htmllint.github.io
ISC License
376 stars 55 forks source link

"Cannot get options for index" error when heading is inside paragraph #175

Open jonatanpedersen opened 7 years ago

jonatanpedersen commented 7 years ago

Thanks for creating this awesome library. We are replacing all our old mocha/cheerio tests with htmllint + a plugin that we are making with some domain specific rules.

On of the problems that we have run in to is that we get this "Cannot get options for index" error when heading elements are nested inside paragraph elements:

<html><body><p><h2>Heading 2</h2></p><body></html>

We encounter this error:

 Error: Cannot get options for index 19 when index 19 has already been checked
      at inlineConfig.getOptsAtIndex (node_modules\htmllint\lib\inline_config.js:83:15)
      at getIssues (node_modules\htmllint\lib\rules\dom.js:21:23)
      at node_modules\htmllint\lib\rules\dom.js:28:34
      at Array.forEach (native)
      at getIssues (node_modules\htmllint\lib\rules\dom.js:27:30)
      at node_modules\htmllint\lib\rules\dom.js:28:34
      at Array.forEach (native)
      at getIssues (node_modules\htmllint\lib\rules\dom.js:27:30)
      at node_modules\htmllint\lib\rules\dom.js:28:34
      at Array.forEach (native)
      at getIssues (node_modules\htmllint\lib\rules\dom.js:27:30)
      at Array.map (native)
      at Object.module.exports.lint (node_modules\htmllint\lib\rules\dom.js:34:35)
      at Linter.lintDom (node_modules\htmllint\lib\linter.js:85:38)
      at Linter.lint (node_modules\htmllint\lib\linter.js:49:33)
      at Linter.lint (node_modules\promise\lib\node-extensions.js:103:17)
      at Context.<anonymous> (test\rules.spec.js:43:24)

Of course headings should not be nested inside paragraphs. But that is a different issue :)

I also checked a couple of other things like putting a list inside a paragraph:

<html><body><p><ul><li>foo</li></ul></p><body></html>

and a heading inside a table:

<html><body><table><h1>Heading 1</h1></table><body></html>

none of which produces the error.

It looks like it is somehow specific to the combination of paragraphs and headings, and something to do with inline configurations.

Best regards, Jonatan

mlochbaum commented 7 years ago

At a glance it looks like the problem is that a <h1> tag closes a <p> tag, that is, <p>par...<h1>Heading</h1> is valid html and the <p> tag will extend to the beginning of the heading. Then the </p> tag comes later and the parser does something weird. I'll look into this later today.

jonatanpedersen commented 7 years ago

When this error happens it also never calls the end function on all the rules which can cause problems the next time you call the lint function.