diff --git a/changelog.md b/changelog.md
index 97e184b..b700437 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,6 @@
## Unreleased
+* Do not emit end tags after a `` element outside SVG/MathML. In HTML such an element turns the rest of the document into text, so posthtml-parser 0.13 (htmlparser2 12) returns everything after it as its content and the source cannot contain those end tags. Rendering now reproduces the source instead of appending ``.
* Replace the legacy build, lint and test toolchain with TypeScript, ESLint and native Node.js tests; retain Node.js 12 runtime support.
* Check the full suite and CJS/ESM consumers against a built npm tarball across the runtime matrix.
* Make declarations self-contained, including mixed nested content arrays, and check TypeScript 4.3.5 and current consumers.
diff --git a/src/index.ts b/src/index.ts
index f91071d..9d48142 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -83,6 +83,19 @@ export type Options = {
quoteStyle?: quoteStyleEnum;
};
+const FOREIGN_CONTENT_TAGS = new Set(['svg', 'math']);
+const HTML_INTEGRATION_TAGS = new Set([
+ 'mi',
+ 'mo',
+ 'mn',
+ 'ms',
+ 'mtext',
+ 'annotation-xml',
+ 'foreignObject',
+ 'desc',
+ 'title',
+]);
+
const SINGLE_TAGS: Array = [
'area',
'base',
@@ -145,9 +158,14 @@ export function render(tree?: Node | Node[], options: Options = {}): string {
tree = [tree];
}
- return html(tree);
+ // Outside SVG/MathML, switches the HTML tokenizer to plain text
+ // until the end of the document, so no end tag can follow it in the source.
+ // Mirrors htmlparser2: only exact `svg`/`math` start foreign content.
+ let inPlaintext = false;
+
+ return html(tree, false);
- function html(tree: Array) {
+ function html(tree: Array, foreign: boolean) {
let result = '';
for (const node of tree) {
@@ -168,7 +186,7 @@ export function render(tree?: Node | Node[], options: Options = {}): string {
continue;
}
- result += html(node);
+ result += html(node, foreign);
continue;
}
@@ -188,12 +206,18 @@ export function render(tree?: Node | Node[], options: Options = {}): string {
}
if (node.tag === false) {
- result += html(node.content);
+ result += html(node.content, foreign);
continue;
}
const tag = typeof node.tag === 'string' ? node.tag : 'div';
+ const contentForeign = FOREIGN_CONTENT_TAGS.has(tag)
+ || (foreign && !HTML_INTEGRATION_TAGS.has(tag));
+
+ if (!foreign && tag.toLowerCase() === 'plaintext') {
+ inPlaintext = true;
+ }
result += `<${tag}`;
@@ -228,15 +252,19 @@ export function render(tree?: Node | Node[], options: Options = {}): string {
}
if (node.content) {
- result += html(node.content);
+ result += html(node.content, contentForeign);
}
} else if (closingSingleTag === closingSingleTagOptionEnum.closeAs && node.closeAs) {
const type = node.closeAs
? closingSingleTagTypeEnum[node.closeAs]
: closingSingleTagTypeEnum.default;
- result += `${closeAs[type]}${html(node.content)}`;
+ result += `${closeAs[type]}${html(node.content, contentForeign)}`;
} else {
- result += `>${html(node.content)}${tag}>`;
+ result += `>${html(node.content, contentForeign)}`;
+
+ if (!inPlaintext) {
+ result += `${tag}>`;
+ }
}
}
diff --git a/test/suite.cjs b/test/suite.cjs
index 151a4d2..83e133a 100644
--- a/test/suite.cjs
+++ b/test/suite.cjs
@@ -347,6 +347,51 @@ module.exports = function suite(test, renderer, parser) {
assert.equal(render(tree), html);
});
+ test('{Tags} {plaintext} omits end tags that cannot follow it', () => {
+ // What posthtml-parser 0.13 (htmlparser2 12) returns for
+ //
a
x
+ const tree = [{tag: 'div', content: [{tag: 'plaintext', content: ['ax']}]}];
+ assert.equal(render(tree), '