fix: match pre, script, style and textarea as whole tag names (#4114)

This commit is contained in:
Bao Nguyen
2026-10-06 08:52:49 -06:00
committed by GitHub
parent e809386482
commit 0db74cb019
8 changed files with 142 additions and 6 deletions
+6 -6
View File
@@ -100,7 +100,7 @@ export const other = {
headingBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}#`)),
// a list item ends where a paragraph would be interrupted, so this mirrors the
// html start conditions in the paragraph rule below; type 7 is excluded there
htmlBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}(?:</?(?:${_tag})(?: +|$|/?>)|<(?:script|pre|style|textarea|!--))`, 'i')),
htmlBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}(?:</?(?:${_tag})(?: +|$|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--))`, 'i')),
blockquoteBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}>`)),
};
@@ -162,8 +162,8 @@ const html = edit(
+ '|<![A-Z][\\s\\S]*?(?:>[^\\n]*\\n*|$)' // (4)
+ '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>[^\\n]*\\n*|$)' // (5)
+ '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (6)
+ '|<(?!script|pre|style|textarea)([a-z][a-z0-9-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) open tag
+ '|</(?!script|pre|style|textarea)[a-z][a-z0-9-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) closing tag
+ '|<(?!(?:script|pre|style|textarea)(?![a-z0-9-]))([a-z][a-z0-9-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) open tag
+ '|</[a-z][a-z0-9-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) closing tag
+ ')', 'i')
.replace('comment', _comment)
.replace('tag', _tag)
@@ -178,7 +178,7 @@ const createParagraph = (listInterrupt: RegExp) => edit(_paragraph)
.replace('blockquote', ' {0,3}>')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*(?:\\n|$))|~~~)[^\\n]*(?:\\n|$)')
.replace('list', listInterrupt)
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--)')
.replace('tag', _tag) // pars can be interrupted by type (6) html blocks
.getRegex();
@@ -229,7 +229,7 @@ const gfmTable = edit(
.replace('code', '(?: {4}| {0,3}\t)[^\\n]')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*(?:\\n|$))|~~~)[^\\n]*(?:\\n|$)')
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]') // any bullet ends the table rows
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--)')
.replace('tag', _tag) // tables can be interrupted by type (6) html blocks
.getRegex();
@@ -245,7 +245,7 @@ const blockGfm: Record<BlockKeys, RegExp> = {
.replace('blockquote', ' {0,3}>')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*(?:\\n|$))|~~~)[^\\n]*(?:\\n|$)')
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]+[^ \\t\\n]') // only non-empty lists starting from 1 can interrupt
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--)')
.replace('tag', _tag) // pars can be interrupted by type (6) html blocks
.getRegex(),
};
@@ -0,0 +1,11 @@
<preview-card>
*stays raw*
</preview-card>
<style-guide>
*stays raw*
</style-guide>
<ScriptEditor>
*stays raw*
</ScriptEditor>
</pre>
*stays raw*
@@ -0,0 +1,14 @@
<preview-card>
*stays raw*
</preview-card>
<style-guide>
*stays raw*
</style-guide>
<ScriptEditor>
*stays raw*
</ScriptEditor>
</pre>
*stays raw*
@@ -0,0 +1,37 @@
<p>Paragraph
<preview-card>text</preview-card></p>
<ul>
<li>list item
<preview-card>text</preview-card></li>
</ul>
<blockquote>
<p>quote
<preview-card>text</preview-card></p>
</blockquote>
<p>Paragraph</p>
<pre
class="x">
code
</pre>
<ul>
<li>list item</li>
</ul>
<pre
class="x">
code
</pre>
<table>
<thead>
<tr>
<th>a</th>
</tr>
</thead>
<tbody>
<tr>
<td>b</td>
</tr>
<tr>
<td><preview-card>text</preview-card></td>
</tr>
</tbody>
</table>
@@ -0,0 +1,25 @@
Paragraph
<preview-card>text</preview-card>
- list item
<preview-card>text</preview-card>
> quote
<preview-card>text</preview-card>
Paragraph
<pre
class="x">
code
</pre>
- list item
<pre
class="x">
code
</pre>
| a |
| - |
| b |
<preview-card>text</preview-card>
@@ -0,0 +1,22 @@
<p>Paragraph
<preview-card>text</preview-card></p>
<ul>
<li>list item
<preview-card>text</preview-card></li>
</ul>
<blockquote>
<p>quote
<preview-card>text</preview-card></p>
</blockquote>
<p>Paragraph</p>
<pre
class="x">
code
</pre>
<ul>
<li>list item</li>
</ul>
<pre
class="x">
code
</pre>
@@ -0,0 +1,23 @@
---
gfm: false
---
Paragraph
<preview-card>text</preview-card>
- list item
<preview-card>text</preview-card>
> quote
<preview-card>text</preview-card>
Paragraph
<pre
class="x">
code
</pre>
- list item
<pre
class="x">
code
</pre>
@@ -0,0 +1,4 @@
module.exports = {
markdown: 'a\n<prefix/>'.repeat(10000),
html: '<p>' + 'a\n<prefix/>'.repeat(10000) + '</p>\n',
};