Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): Align foreign content with spec #451

Merged
merged 3 commits into from Mar 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 22 additions & 28 deletions packages/parse5/lib/parser/index.test.ts
Expand Up @@ -10,41 +10,35 @@ import { isElementNode } from '../tree-adapters/default.js';

const origParseFragment = Parser.parseFragment;

generateParsingTests('parser', 'Parser', {}, (test, opts) => ({
node: test.fragmentContext
? parse5.parseFragment(test.fragmentContext, test.input, opts)
: parse5.parse(test.input, opts),
}));
generateParsingTests(
'parser',
'Parser',
{
expectErrors: [
//NOTE: Foreign content behaviour was updated in the HTML spec.
//The old test suite still tests the old behaviour.
'269.foreign-fragment',
'270.foreign-fragment',
'307.foreign-fragment',
'309.foreign-fragment',
'316.foreign-fragment',
'317.foreign-fragment',
],
},
(test, opts) => ({
node: test.fragmentContext
? parse5.parseFragment(test.fragmentContext, test.input, opts)
: parse5.parse(test.input, opts),
})
);

generateParsingTests(
'parser upstream',
'Parser',
{
withoutErrors: true,
suitePath: new URL('../../../../test/data/html5lib-tests/tree-construction', import.meta.url),
expectErrors: [
'271.foreign-fragment',
'272.foreign-fragment',
'309.foreign-fragment',
'311.foreign-fragment',
'318.foreign-fragment',
'319.foreign-fragment',
'329.foreign-fragment',
'330.foreign-fragment',
'331.foreign-fragment',
'332.foreign-fragment',
'333.foreign-fragment',
'334.foreign-fragment',
'335.foreign-fragment',
'336.foreign-fragment',
'337.foreign-fragment',
'505.search-element',
'506.search-element',
'1408.tests26',
'1409.tests26',
'1410.tests26',
'1411.tests26',
],
expectErrors: ['505.search-element', '506.search-element'],
},
(test, opts) => ({
node: test.fragmentContext
Expand Down
25 changes: 18 additions & 7 deletions packages/parse5/lib/parser/index.ts
Expand Up @@ -3460,14 +3460,18 @@ function characterInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, t
p.framesetOk = false;
}

function popUntilHtmlOrIntegrationPoint<T extends TreeAdapterTypeMap>(p: Parser<T>): void {
while (
p.treeAdapter.getNamespaceURI(p.openElements.current) !== NS.HTML &&
!p._isIntegrationPoint(p.openElements.currentTagId, p.openElements.current)
) {
p.openElements.pop();
}
}

function startTagInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToken): void {
if (foreignContent.causesExit(token) && !p.fragmentContext) {
while (
p.treeAdapter.getNamespaceURI(p.openElements.current) !== NS.HTML &&
!p._isIntegrationPoint(p.openElements.currentTagId, p.openElements.current)
) {
p.openElements.pop();
}
if (foreignContent.causesExit(token)) {
popUntilHtmlOrIntegrationPoint(p);

p._startTagOutsideForeignContent(token);
} else {
Expand All @@ -3494,6 +3498,13 @@ function startTagInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, to
}

function endTagInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToken): void {
if (token.tagID === $.P || token.tagID === $.BR) {
popUntilHtmlOrIntegrationPoint(p);

p._endTagOutsideForeignContent(token);

return;
}
for (let i = p.openElements.stackTop; i > 0; i--) {
const element = p.openElements.items[i];

Expand Down