Skip to content

Restrict HTML tag whitespace to spaces, tabs and line endings - #316

Open
anubhavlal07 wants to merge 1 commit into
commonmark:masterfrom
anubhavlal07:fix/html-tag-ascii-whitespace
Open

anubhavlal07 wants to merge 1 commit into
commonmark:masterfrom
anubhavlal07:fix/html-tag-ascii-whitespace

Conversation

@anubhavlal07

Copy link
Copy Markdown

The HTML tag regexes in lib/common.js use JS \s, which also matches U+00A0 and other Unicode spaces. UNQUOTEDVALUE ([^"'=<>\x00-\x20]+) matches U+00A0 too. So after <a x=, a run of NBSP can be split three ways: \s*after=, the unquoted value, and the trailing \s*before/?>. When the closing > is missing, the anchored type-7 block-start regex (^(?:OPENTAG|CLOSETAG)\s*$) and the inline reHtmlTag` try every split, which is cubic:

NBSP after <a x= before after
500 396 ms 0 ms
1000 2478 ms 0 ms
2000 26680 ms 1 ms

\s is also wrong per spec 0.31.2. An attribute, an attribute value specification, an open tag and a closing tag all allow only "spaces, tabs, and up to one line ending" as whitespace. Start condition 7 for HTML blocks allows only spaces and tabs between the tag and the end of the line.

Fix

  • lib/common.js: add TAGWHITESPACE = "[ \t\r\n]" and use it in place of \s in ATTRIBUTEVALUESPEC, ATTRIBUTE, OPENTAG and CLOSETAG. An NBSP run after = can now match only the unquoted value, so the split ambiguity is gone.
  • lib/blocks.js: change the trailing \s*$ in reHtmlBlockOpen[7] to [ \t]*$.

The fix changes how a few inputs render, all to match the spec:

  • <a + NBSP + x=1> is no longer a tag, in blocks or inline.
  • <a> + NBSP no longer starts a type-7 HTML block.
  • <a x= + NBSP + > is still a tag, because NBSP is a valid unquoted value character.

dist/ is not touched.

Tests

  • test/regression.txt: four examples: the three cases above and the unclosed <a x= + NBSP case from the issue. Without the fix, the <a + NBSP + x=1> example and the <a> + NBSP example fail.
  • test/test.js: a pathological case for <a x= followed by 1000 and 10000 NBSP. Without the fix, the 10000 case does not finish within 2 minutes. With the fix it runs in a few ms.

make test reports 760 passed, 0 failed, and make lint is clean.

make bench on bench/samples/README.md was noisy on my machine, with readings between 1,300 and 3,900 ops/sec for both versions. Alternating runs gave before 3,828 / 3,972 / 3,303 ops/sec and after 3,161 / 3,247 / 3,477 ops/sec. To reduce the noise, I loaded both builds into one process and alternated them over 8 rounds of 300 parses each. Median time was 79 ms before and 78 ms after on README.md, and 312 ms before and 304 ms after on all of bench/samples/*.md.

Fixes #315

Avoids cubic backtracking on NBSP runs after an attribute's = (commonmark#315).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cubic time on an HTML block start with Unicode spaces after an attribute's =

1 participant