diff --git a/src/lib/markbind/src/patches/htmlparser2.js b/src/lib/markbind/src/patches/htmlparser2.js index 185af4728d..53ba30a04d 100644 --- a/src/lib/markbind/src/patches/htmlparser2.js +++ b/src/lib/markbind/src/patches/htmlparser2.js @@ -261,6 +261,29 @@ Tokenizer.prototype._stateBeforeSpecial = function(c) { this._special = result + 1; }; +/** + * Patched self closing tag state handler that removes the special state + * if the special tag was self-closed. + */ +Tokenizer.prototype._stateInSelfClosingTag = function(c) { + if (c === ">") { + this._cbs.onselfclosingtag(); + this._state = TEXT; + this._sectionStart = this._index + 1; + /* + Allow all special tags to be self-closed. + Script and style tags are also allowed to be self-closed, + which breaks from the default html spec-compliant of behaviour of htmlparser2. + We allow this as such tags would be expanded upon re-rendering the html anyway. + ie. '' + */ + this._special = SPECIAL_NONE; + } else if (!whitespace(c)) { + this._state = BEFORE_ATTRIBUTE_NAME; + this._index--; + } +}; + /** * Processes the _special flag and _nextSpecialTagMatchIndex state variable, * returning a flag indicating whether the current special tag has finished matching or not. diff --git a/test/functional/test_site_special_tags/_markbind/plugins/testSpecialTag.js b/test/functional/test_site_special_tags/_markbind/plugins/testSpecialTag.js index 2c219106df..0b08040538 100644 --- a/test/functional/test_site_special_tags/_markbind/plugins/testSpecialTag.js +++ b/test/functional/test_site_special_tags/_markbind/plugins/testSpecialTag.js @@ -28,7 +28,7 @@ function preRender(content) { without interference from other dependencies */ function postRender(content) { - const $ = cheerio.load(content); + const $ = cheerio.load(content, { xmlMode: false }); const escapedNunjucks = $('mustache'); escapedNunjucks.each((index, element) => { const unwrappedText = $(element).text(); @@ -44,5 +44,5 @@ function postRender(content) { module.exports = { preRender, postRender, - getSpecialTags: () => ['testtag', 'mustache'], + getSpecialTags: () => ['testtag', 'testselfclosingtag', 'mustache'], }; diff --git a/test/functional/test_site_special_tags/expected/index.html b/test/functional/test_site_special_tags/expected/index.html index 1085485b37..7acbeb661f 100644 --- a/test/functional/test_site_special_tags/expected/index.html +++ b/test/functional/test_site_special_tags/expected/index.html @@ -24,7 +24,7 @@

Functional test for htmlparser2 and markdown-it patches for special tags

So far as to comply with the commonmark spec

-

There should be no text between this and the next <hr> tag in the browser, since it is a <script> tag.
There should be an alert with the value of 2 as well.

+

There should be no text between this and the next <hr> tag in the browser, since it is a <script> tag.
There should be an alert with the value of 2 as well.