diff --git a/packages/core/src/lib/markdown-it/markdown-it-escape-special-tags.js b/packages/core/src/lib/markdown-it/markdown-it-escape-special-tags.js index ab8654d39f..e512a363f0 100644 --- a/packages/core/src/lib/markdown-it/markdown-it-escape-special-tags.js +++ b/packages/core/src/lib/markdown-it/markdown-it-escape-special-tags.js @@ -12,7 +12,20 @@ function escape_plugin(md, tagsToIgnore) { const specialTagsRegex = Array.from(tagsToIgnore) .concat(['script|pre|style|variable']) .join('|'); - const startingSpecialTagRegex = new RegExp(`^<(${specialTagsRegex})(?=(\\s|>|$))`, 'i'); + + // attr_name, unquoted ... attribute patterns adapted from markdown-it/lib/common/html_re + // Construct self-closing negative lookahead for special tags, + // because if matched, the endingSpecialTagRegex will roll down to find a non-existent closing tag + const attr_name = '[a-zA-Z_:][a-zA-Z0-9:._-]*'; + const unquoted = '[^"\'=<>`\\x00-\\x20]+'; + const single_quoted = "'[^']*'"; + const double_quoted = '"[^"]*"'; + const attr_value = '(?:' + unquoted + '|' + single_quoted + '|' + double_quoted + ')'; + const attribute = '(?:\\s+' + attr_name + '(?:\\s*=\\s*' + attr_value + ')?)'; + const selfClosingNegativeLookahead = `(?!${attribute}*\\s*\/>)`; + + const startingSpecialTagRegex = new RegExp( + `^<(${specialTagsRegex})${selfClosingNegativeLookahead}(?=(\\s|>|$))`, 'i'); const endingSpecialTagRegex = new RegExp(`<\\/(${specialTagsRegex})>`, 'i'); const HTML_SEQUENCES = [