fix: match html block start conditions when ending a list item - #4072
Open
giaBaoJS wants to merge 3 commits into
Open
fix: match html block start conditions when ending a list item#4072giaBaoJS wants to merge 3 commits into
giaBaoJS wants to merge 3 commits into
Conversation
htmlBeginRegex only matched an opening tag with a closing `>` on the same line, so `</div>` after a list item was swallowed into the `<li>`. It also matched any tag name, so a type 7 block such as `<b>` ended the list even though type 7 cannot interrupt a paragraph. Reuse the start conditions the paragraph and html rules already share.
|
@giaBaoJS is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
UziTech
reviewed
Aug 29, 2026
Member
There was a problem hiding this comment.
I think spec tests should be enough. We don't need lexer tests as well
Author
There was a problem hiding this comment.
Removed. The one case the specs don't cover is a block tag with no > on the line (<div with its attributes on the next line) ending a list item; want that as a spec test, or leave it?
Member
There was a problem hiding this comment.
Ya you could add it as a spec test
Author
There was a problem hiding this comment.
Added as html_multiline_tag_following_list.
The spec tests already cover the closing tag and type 7 cases.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Marked version: 18.0.11 (master, 53cb13f)
Markdown flavor: CommonMark
Description
No issue for this one. #3437 reported that an HTML tag on the line after a list item ends up inside the
<li>, and #3444 fixed it for opening tags. The closing-tag half was never fixed, and the same regex has a second problem in the other direction.src/rules.tsline 98:Tokenizer.ts:354uses it to decide whether the next line ends the current list item. It never matches</..., and[a-z].*>matches every tag name including ones that start a type 7 HTML block.Expectation
Result
The
</div>is pulled inside the<li>, so the caller's wrapper is never closed and the browser closes it at the</li>instead. Minimal repro:marked.parse('- a\n</div>\n')returns<ul>\n<li>a</div></li>\n</ul>\n.The second direction, same regex:
marked returns
<ul>\n<li>list item</li>\n</ul>\n<p><b>bold</b></p>\n. The<b>ends the list. CommonMark 0.31.2 keeps it in the item, because a type 7 block cannot interrupt the paragraph inside the list item.What was attempted
Both cases are the same start-condition test. Spec 0.31.2, "HTML blocks":
and start condition 6:
(source: https://github.com/commonmark/commonmark-spec/blob/0.31.2/spec.txt, lines 2396 to 2408 and 2449 to 2450)
marked already encodes exactly that set: the paragraph rule at
src/rules.ts:174uses'</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)'with the comment "pars can be interrupted by type (6) html blocks". This PR reuses the same expression forhtmlBeginRegexinstead of inventing a new one. The only change is\nbecomes$, becausehtmlBeginRegexis tested against a single line with the newline already stripped, and the spec condition includes "the end of the line".Two behaviours it deliberately does not change, to stay consistent with the
htmlrule rather than be locally more correct:<div\t>) still does not match, becausehtmland the paragraph rule both use+Blast radius
I generated 57,456 documents (7 list markers x 4 leading indents x 6 continuation indents x 56 continuation lines x 6 tails, over 5 nesting shapes) and diffed master against this branch, using the
commonmark0.31.2 package already in devDependencies as the reference. Comparison ignores whitespace, so pure newline differences do not count.Of the 15,692 documents whose output changes, 13,292 go from disagreeing with the reference to matching it, and 0 go the other way. The remaining 2,400 disagree with the reference both before and after, for reasons this regex does not control (list tightness whitespace, tab and 4-space continuation indents, task-list markers).
Narrowing to one variable,
- one\n<LINE>\n, 18 of the 56 continuation lines change behaviour:17 move to matching the reference, none regress, and
<div\t>is the tab case noted above.For contrast, the one-character fix of just adding
/?to the existing pattern (^ {0,n}</?(?:[a-z].*>|!--)) produces 4,316 regressions on the same corpus, across</script>,</pre>,</style>,</b>,</span>,</em>,</a>and</x-widget>: a closing type 7 tag would start ending list items. That is why this uses the tag list rather than[a-z].CommonMark and GFM spec completion tables are identical before and after.
ReDoS
npm run test:redosskips this regex (test/recheck.tshas// TODO: skip functions for nowfor thecachedIndentRegexentries), so I ranrecheckagainst it directly at all four indent valuescachedIndentRegexcan produce:The new pattern is an alternation of literal prefixes with no nested quantifier; it also removes the
.*the old one had.Tests
Two spec files in
test/specs/newplus a unit test on the regex. They fail on different mutations:html_closing_tag_following_listhtml_inline_tag_in_listhtml_following_list(existing, unchanged)/?only</?, keep the tag listhtml_following_listis the control from #3444: an opening block tag must still end the list, and it does.Local run: 1789 to 1793 spec tests, 191 to 192 unit tests, 0 failures.
test:lint,test:types,test:umdandtest:cjspass.Contributor
Committer
In most cases, this should be a different person than the contributor.