From bc309075e8a2771ca993609c21b49a549ed209dc Mon Sep 17 00:00:00 2001 From: Ze Yu Date: Sun, 6 Dec 2020 11:14:04 +0800 Subject: [PATCH] Ignore self-closing special tags for markdown-it The markdown-it patch for special tags does not blacklist self-closed special tags from the starting regex pattern. Thus, the starting regex pattern incorrectly matches such a self-closing special tag, and the ending regex pattern incorrectly seeks for a corresponding closing special tag until EOF. This is not apparent in the functional tests which have such tags (), because puml tags are processed before markdown. Let's add a negative lookahead to the starting pattern to blacklist such tags. --- .../markdown-it-escape-special-tags.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 = [