Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Do not emit end tags after a `<plaintext>` 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 `</plaintext></div>`.
* 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.
Expand Down
42 changes: 35 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | RegExp> = [
'area',
'base',
Expand Down Expand Up @@ -145,9 +158,14 @@ export function render(tree?: Node | Node[], options: Options = {}): string {
tree = [tree];
}

return html(tree);
// Outside SVG/MathML, <plaintext> 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<Node | Node[] | false | null | undefined>) {
function html(tree: Array<Node | Node[] | false | null | undefined>, foreign: boolean) {
let result = '';

for (const node of tree) {
Expand All @@ -168,7 +186,7 @@ export function render(tree?: Node | Node[], options: Options = {}): string {
continue;
}

result += html(node);
result += html(node, foreign);

continue;
}
Expand All @@ -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}`;

Expand Down Expand Up @@ -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}>`;
}
}
}

Expand Down
45 changes: 45 additions & 0 deletions test/suite.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// <div><plaintext>a</plaintext></div><b>x</b>
const tree = [{tag: 'div', content: [{tag: 'plaintext', content: ['a</plaintext></div><b>x</b>']}]}];
assert.equal(render(tree), '<div><plaintext>a</plaintext></div><b>x</b>');
});

test('{Tags} {plaintext} is case-insensitive', () => {
assert.equal(render([{tag: 'PLAINTEXT', content: ['a</PLAINTEXT>']}]), '<PLAINTEXT>a</PLAINTEXT>');
});

test('{Tags} {plaintext} omits end tags of later nodes', () => {
const tree = [{tag: 'plaintext', content: ['a']}, {tag: 'b', content: ['x']}];
assert.equal(render(tree), '<plaintext>a<b>x');
});

test('{Tags} {plaintext} is a regular element inside SVG and MathML', () => {
const tree = [
{tag: 'svg', content: [{tag: 'plaintext', content: ['a']}, {tag: 'b', content: ['x']}]},
{tag: 'math', content: [{tag: 'plaintext', content: ['b']}]},
{tag: 'i', content: ['y']},
];
assert.equal(
render(tree),
'<svg><plaintext>a</plaintext><b>x</b></svg><math><plaintext>b</plaintext></math><i>y</i>',
);
});

test('{Tags} {plaintext} switches to text inside HTML integration points', () => {
const tree = [{tag: 'svg', content: [{tag: 'foreignObject', content: [
{tag: 'plaintext', content: ['a</plaintext></foreignObject></svg>']},
]}]}];
assert.equal(render(tree), '<svg><foreignObject><plaintext>a</plaintext></foreignObject></svg>');
});

test('{Tags} {plaintext} keeps foreign detection case-sensitive like htmlparser2', () => {
const tree = [{tag: 'SVG', content: [{tag: 'plaintext', content: ['a</plaintext></SVG>']}]}];
assert.equal(render(tree), '<SVG><plaintext>a</plaintext></SVG>');
});

test('{Tags} {plaintext} state does not leak between render calls', () => {
render([{tag: 'plaintext', content: ['a']}]);
assert.equal(render([{tag: 'b', content: ['x']}]), '<b>x</b>');
});

test('{Options} {singleTag} Defaults', () => {
const SINGLE_TAGS_LOWERCASE = [
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr',
Expand Down