Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9351e96
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
f0c0845
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
dbff870
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
5eeecd7
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
f04d8ed
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
7f6426f
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
209e293
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
fbcc763
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
dbca621
fix: accept newline-delimited HTML block tag names
hakarrr Nov 17, 2026
d4e2980
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
364e97e
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
b8041a1
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
1a16167
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
1842698
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
8524096
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
15ba79e
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
f8046ed
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
f72a7c0
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
c1af983
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
3863d79
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
b96e37c
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
913b0b8
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
43d1c58
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
d0bc5f5
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
cc3c17a
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
621cc55
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
4ed9d36
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
1740196
fix: recognize HTML block tag names at end of input
hakarrr Nov 17, 2026
f295cbf
test: remove redundant fixture html_type6_newline_interrupt_commonmar…
hakarrr Nov 28, 2026
4522e07
test: remove redundant fixture html_type6_newline_interrupt_commonmar…
hakarrr Nov 28, 2026
7d9d03a
test: remove redundant fixture html_type6_newline_pedantic_control.html
hakarrr Nov 28, 2026
181c560
test: remove redundant fixture html_type6_newline_pedantic_control.md
hakarrr Nov 28, 2026
c096b17
test: add fixture html_type6_eof_interrupt.md
hakarrr Nov 28, 2026
fe717a6
test: add fixture html_type6_eof_interrupt.html
hakarrr Nov 28, 2026
5957328
test: fix EOF fixture content html_type6_eof_interrupt.md
hakarrr Nov 28, 2026
dea9355
test: remove malformed fixture html_type6_eof_interrupt.html
hakarrr Nov 28, 2026
0f3d22e
test: remove malformed fixture html_type6_eof_interrupt.md
hakarrr Nov 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,13 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
const cap = this.rules.block.html.exec(src);
if (cap) {
// an html block of type 7 cannot interrupt a paragraph, so a lazy
// continuation line that starts one continues the paragraph instead
if (this.lexer.state.lastToken?.type === 'paragraph' && !this.rules.other.htmlBeginRegex(4).test(src)) {
// continuation line that starts one continues the paragraph instead.
// Capture 3 identifies a type 6 block, including a tag name followed by
// a newline, which htmlBeginRegex (also used on list lines) does not match.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One case this still misses: the tag name at the very end of the input, with no final newline. The fixture files end with \n, so the EOF case in the tests is really <div\n.

marked.parse('a\n<div')
// this PR:   '<p>a\n&lt;div</p>\n'
// commonmark: '<p>a</p>\n<div\n'

This input never reaches html(): the paragraph regex swallows the line first, because _htmlInterrupt in src/rules.ts accepts [ \t]+, \n or /?> after the tag name, but not end of input. It matters for streaming renderers that re-parse partial text after each chunk.

Adding $ there fixes it:

const _htmlInterrupt = edit(' {0,3}(?:</?(?:tag)(?:[ \\t]+|\\n|$|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--)|<\\?|<![A-Za-z]|<!\\[CDATA\\[)')

On this branch with that change, npm run test:specs (2043) and test:unit (256) still pass. In a small matrix (8 tags x 8 contexts, no trailing newline) it fixes 24 cases vs commonmark.js 0.31.2. The only new mismatches are a\n<meta, because meta is still in _tag. That is the same wrong result master already gives for a\n<meta (with a trailing space), and #4239 drops meta from the interrupt list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. I reproduced a\n<div and a\n</div on the original PR head. You are right about the fixture: it ended with a newline, so the EOF coverage claim was too broad.

The follow-up handles tag names at true end of input in the modern interruption rule and excludes meta from that new EOF path. It preserves all compiled pedantic block rules and leaves the shared list helper unchanged. Eight new exact API assertions use strings with no final newline and fail without the follow-up. I used string assertions because the web editor adds final newlines to fixture files.

On the updated head, full npm test passes: 2,043 spec tests and 264 unit tests, plus UMD/CJS/types/lint. The 54,600-case tag/delimiter/context comparison has 1,984 changed outputs, all new normalized CommonMark matches; 240,000 generated modern parses have zero match-to-mismatch regressions. Existing META and nested-container gaps remain outside this change. I also corrected the description's EOF claim.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked 1740196 against commonmark.js 0.31.2: a\n<div, a\n</div, a\n<table, a\n<p, > a\n<div and - a\n<div with no final newline now match, and a\n<meta stays paragraph text. npm test passes for me. Thanks. (Upper-case names like a\n<DIV> still stay in the paragraph, but that is the same on master, so not for this PR.)

// Keep the existing guard for pedantic grammar and the legacy meta tag.
if (this.lexer.state.lastToken?.type === 'paragraph'
&& (this.options.pedantic || !cap[3] || cap[3].toLowerCase() === 'meta')
&& !this.rules.other.htmlBeginRegex(4).test(src)) {
return;
}

Expand Down
11 changes: 9 additions & 2 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,15 @@ const _tag = 'address|article|aside|base|basefont|blockquote|body|caption'
+ '|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title'
+ '|tr|track|ul';
// HTML blocks of types (1) to (6) can interrupt a paragraph, type (7) cannot
const _htmlInterrupt = edit(' {0,3}(?:</?(?:tag)(?:[ \\t]+|\\n|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--)|<\\?|<![A-Za-z]|<!\\[CDATA\\[)')
const _htmlInterruptCore = ' {0,3}(?:</?(?:tag)(?:[ \\t]+|\\n|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--)|<\\?|<![A-Za-z]|<!\\[CDATA\\[)';
const _htmlInterruptLegacy = edit(_htmlInterruptCore)
.replace('tag', _tag)
.getRegex();
// At EOF, meta is an inline tag rather than a type 6 block start.
const _htmlInterrupt = edit(_htmlInterruptCore)
.replace('tag', '(?!meta$)(?:' + _tag + ')')
.replace('|\\n|', '|\\n|$|')
.getRegex();
// The underline may end with spaces or tabs (CommonMark §4.3). Internal
// spaces or tabs still fail to match, and more than three spaces of indent
// still do not.
Expand Down Expand Up @@ -280,6 +286,7 @@ const blockGfm: Record<BlockKeys, RegExp> = {

const blockPedantic: Record<BlockKeys, RegExp> = {
...blockNormal,
blockquote: edit(blockquote.source).replace(_htmlInterrupt.source, _htmlInterruptLegacy).getRegex(),
html: edit(
'^ *(?:comment(?: *\\n|\\s*$)'
+ '|<(tag)[\\s\\S]+?</\\1>(?: *\\n{2,}|\\s*$)' // closed tag
Expand All @@ -303,7 +310,7 @@ const blockPedantic: Record<BlockKeys, RegExp> = {
.replace('|list', '')
.replace('|html', '')
.replace('|tag', '')
.replace('lheading', lheading) // last, so the replacements above do not match inside it
.replace('lheading', lheading.source.replaceAll(_htmlInterrupt.source, _htmlInterruptLegacy.source)) // last, so the replacements above do not match inside it
.getRegex(),
};

Expand Down
37 changes: 37 additions & 0 deletions test/specs/new/html_type6_newline_interrupt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<p>a</p>
<div
class="x">
x
</div><p>a</p>
</div
><p>a</p>
<table
class="x">
</table><blockquote>
<p>a</p>
</blockquote>
<div
class="x">
x
</div><ul>
<li>a</li>
</ul>
<div
class="x">
x
</div><p>a
<span
x></p>
<p>a
<x-widget
x></p>
<p>a
<divx
x></p>
<p>a</p>
</div
x<p>a</p>
<div
===
</div><p>a</p>
<div
54 changes: 54 additions & 0 deletions test/specs/new/html_type6_newline_interrupt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
renderExact: true
gfm: true
---
a
<div
class="x">
x
</div>

a
</div
>

a
<table
class="x">
</table>

> a
<div
class="x">
x
</div>

- a
<div
class="x">
x
</div>

a
<span
x>

a
<x-widget
x>

a
<divx
x>

a
</div
x

a
<div
===
</div>

a
<div
8 changes: 8 additions & 0 deletions test/specs/new/html_type6_newline_legacy_control.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>a
&lt;meta
x</p>
<blockquote>
<p>a</p>
</blockquote>
<meta
x
11 changes: 11 additions & 0 deletions test/specs/new/html_type6_newline_legacy_control.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
renderExact: true
gfm: true
---
a
<meta
x

> a
<meta
x
10 changes: 10 additions & 0 deletions test/unit/marked.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ describe('marked unit', () => {
setOptions(getDefaults());
});

describe('type 6 html tag names at end of input', () => {
for (const gfm of [false, true]) {
for (const tag of ['<div', '</div', '<table', '</table']) {
it(`interrupts a paragraph before ${tag} without a final newline (gfm=${gfm})`, () => {
assert.strictEqual(marked.parse(`a\n${tag}`, { gfm }), `<p>a</p>\n${tag}`);
});
}
}
});

describe('Test paragraph token type', () => {
it('should use the "paragraph" type on top level', () => {
const md = 'A Paragraph.\n\n> A blockquote\n\n- list item\n';
Expand Down