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

fix: fix <noframes> parsing #1277

Merged
merged 2 commits into from
Oct 6, 2024
Merged
Changes from 1 commit
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
Next Next commit
fix: fix <noframes> parsing
Fixes #972
nolanlawson committed Sep 30, 2024
commit 9ea5bf2eef722f1dd63ae9283c4c209f97813df7
38 changes: 38 additions & 0 deletions packages/parse5/lib/parser/index.test.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { parseFragment, parse } from 'parse5';
import { jest } from '@jest/globals';
import { generateParsingTests } from 'parse5-test-utils/utils/generate-parsing-tests.js';
import { treeAdapters } from 'parse5-test-utils/utils/common.js';
import type { Element, TextNode } from '../tree-adapters/default.js';

generateParsingTests(
'parser',
@@ -110,4 +111,41 @@ describe('parser', () => {
expect(onItemPop).toHaveBeenLastCalledWith(bodyElement.childNodes[0], bodyElement);
});
});

describe('rawtext parsing', () => {
it.each([
['iframe'],
['noembed'],
['noframes'],
['noscript'],
['script'],
['style'],
['textarea'],
['title'],
['xmp'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to find all the "raw text" tags I knew of, based on the Firefox source code.

Again, all these tests were already passing except for noframes.

])('<%s>', (tagName) => {
const html = `<r><${tagName}><math id="</${tagName}><b>should be outside</b>">`;
const fragment = parseFragment(html);

expect(fragment.childNodes.length).toBe(1);
const r = fragment.childNodes[0] as Element;
expect(r.nodeName).toBe('r');
expect(r.childNodes).toHaveLength(3);
expect(r.childNodes.map(_ => _.nodeName)).toEqual([
tagName,
'b',
'#text'
]);

const target = r.childNodes[0] as Element;
expect(target.childNodes).toHaveLength(1);
expect(target.childNodes[0].nodeName).toBe('#text');
expect((target.childNodes[0] as TextNode).value).toBe('<math id="');

const b = r.childNodes[1] as Element;
expect(b.childNodes).toHaveLength(1);
expect(b.childNodes[0].nodeName).toBe('#text');
expect((b.childNodes[0] as TextNode).value).toBe('should be outside');
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a better place to put this test. For some reason the tests in test/data/html5lib-tests/serializer do not repro the issue.

});
13 changes: 7 additions & 6 deletions packages/parse5/lib/parser/index.ts
Original file line number Diff line number Diff line change
@@ -2195,9 +2195,9 @@ function iframeStartTagInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token:
p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
}

//NOTE: here we assume that we always act as an user agent with enabled plugins, so we parse
//<noembed> as rawtext.
function noembedStartTagInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToken): void {
//NOTE: here we assume that we always act as a user agent with enabled plugins/frames, so we parse
//<noembed>/<noframes> as rawtext.
function rawTextStartTagInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToken): void {
p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
}

@@ -2449,8 +2449,9 @@ function startTagInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagTo
optgroupStartTagInBody(p, token);
break;
}
case $.NOEMBED: {
noembedStartTagInBody(p, token);
case $.NOEMBED:
case $.NOFRAMES: {
rawTextStartTagInBody(p, token);
break;
}
case $.FRAMESET: {
@@ -2463,7 +2464,7 @@ function startTagInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagTo
}
case $.NOSCRIPT: {
if (p.options.scriptingEnabled) {
noembedStartTagInBody(p, token);
rawTextStartTagInBody(p, token);
} else {
genericStartTagInBody(p, token);
}