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

refactor: Several small changes #547

Merged
merged 6 commits into from May 20, 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
12 changes: 6 additions & 6 deletions packages/parse5-parser-stream/test/parser-stream.test.ts
Expand Up @@ -11,12 +11,12 @@ generateParsingTests(
expectErrors: [
//TODO(GH-448): 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',
'0.foreign-fragment',
'1.foreign-fragment',
'38.foreign-fragment',
'40.foreign-fragment',
'47.foreign-fragment',
'48.foreign-fragment',
],
},
(test, opts) => parseChunked(test, opts)
Expand Down
14 changes: 7 additions & 7 deletions packages/parse5/lib/parser/index.test.ts
Expand Up @@ -11,12 +11,12 @@ generateParsingTests(
expectErrors: [
//TODO(GH-448): 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',
'0.foreign-fragment',
'1.foreign-fragment',
'38.foreign-fragment',
'40.foreign-fragment',
'47.foreign-fragment',
'48.foreign-fragment',
],
},
(test, opts) => ({
Expand All @@ -30,7 +30,7 @@ generateParsingTests(
{
withoutErrors: true,
suitePath: new URL('../../../../test/data/html5lib-tests/tree-construction', import.meta.url),
expectErrors: ['505.search-element', '506.search-element'],
expectErrors: ['0.search-element', '1.search-element'],
},
(test, opts) => ({
node: test.fragmentContext ? parseFragment(test.fragmentContext, test.input, opts) : parse(test.input, opts),
Expand Down
44 changes: 24 additions & 20 deletions packages/parse5/lib/parser/index.ts
Expand Up @@ -1573,20 +1573,7 @@ function endTagInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToke
break;
}
case $.TEMPLATE: {
if (p.openElements.tmplCount > 0) {
p.openElements.generateImpliedEndTagsThoroughly();

if (p.openElements.currentTagId !== $.TEMPLATE) {
p._err(token, ERR.closingOfElementWithOpenChildElements);
}

p.openElements.popUntilTagNamePopped($.TEMPLATE);
p.activeFormattingElements.clearToLastMarker();
p.tmplInsertionModeStack.shift();
p._resetInsertionMode();
} else {
p._err(token, ERR.endTagWithoutMatchingOpenElement);
}
templateEndTagInHead<T>(p, token);
break;
}
default: {
Expand All @@ -1595,6 +1582,23 @@ function endTagInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToke
}
}

function templateEndTagInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToken): void {
if (p.openElements.tmplCount > 0) {
p.openElements.generateImpliedEndTagsThoroughly();

if (p.openElements.currentTagId !== $.TEMPLATE) {
p._err(token, ERR.closingOfElementWithOpenChildElements);
}

p.openElements.popUntilTagNamePopped($.TEMPLATE);
p.activeFormattingElements.clearToLastMarker();
p.tmplInsertionModeStack.shift();
p._resetInsertionMode();
} else {
p._err(token, ERR.endTagWithoutMatchingOpenElement);
}
}

function tokenInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Token): void {
p.openElements.pop();
p.insertionMode = InsertionMode.AFTER_HEAD;
Expand Down Expand Up @@ -1709,7 +1713,7 @@ function endTagAfterHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagT
break;
}
case $.TEMPLATE: {
endTagInHead(p, token);
templateEndTagInHead(p, token);
break;
}
default: {
Expand Down Expand Up @@ -2526,7 +2530,7 @@ function endTagInBody<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToke
break;
}
case $.TEMPLATE: {
endTagInHead(p, token);
templateEndTagInHead(p, token);
break;
}
default: {
Expand Down Expand Up @@ -2705,7 +2709,7 @@ function endTagInTable<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagTok
break;
}
case $.TEMPLATE: {
endTagInHead(p, token);
templateEndTagInHead(p, token);
break;
}
case $.BODY:
Expand Down Expand Up @@ -2855,7 +2859,7 @@ function endTagInColumnGroup<T extends TreeAdapterTypeMap>(p: Parser<T>, token:
break;
}
case $.TEMPLATE: {
endTagInHead(p, token);
templateEndTagInHead(p, token);
break;
}
case $.COL: {
Expand Down Expand Up @@ -3168,7 +3172,7 @@ function endTagInSelect<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagTo
break;
}
case $.TEMPLATE: {
endTagInHead(p, token);
templateEndTagInHead(p, token);
break;
}
default:
Expand Down Expand Up @@ -3275,7 +3279,7 @@ function startTagInTemplate<T extends TreeAdapterTypeMap>(p: Parser<T>, token: T

function endTagInTemplate<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagToken): void {
if (token.tagID === $.TEMPLATE) {
endTagInHead(p, token);
templateEndTagInHead(p, token);
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/parse5/lib/serializer/index.test.ts
Expand Up @@ -56,4 +56,10 @@ describe('serializer', () => {

assert.equal(serialize(svgBr), '<div></div>');
});

it('serializes unknown node to empty string', () => {
const unknown: any = {};
assert.strictEqual(serialize(unknown), '');
assert.strictEqual(serializeOuter(unknown), '');
});
});
8 changes: 8 additions & 0 deletions packages/parse5/lib/tokenizer/index.test.ts
Expand Up @@ -47,5 +47,13 @@ describe('Tokenizer methods', () => {
expect(tokenizer).toHaveProperty('paused', true);

tokenizer.resume();

assert.strictEqual(count, 4);
});

it('should throw if setting the state to an unknown value', () => {
const tokenizer = new Tokenizer(tokenizerOpts, {} as any);
tokenizer.state = -1;
expect(() => tokenizer.write('foo', true)).toThrow('Unknown state');
});
});
32 changes: 15 additions & 17 deletions packages/parse5/lib/tokenizer/index.ts
Expand Up @@ -353,9 +353,9 @@ export class Tokenizer {
this.preprocessor.retreat(count);
}

private _reconsumeInState(state: State): void {
private _reconsumeInState(state: State, cp: number): void {
this.state = state;
this._unconsume(1);
this._callState(cp);
}

private _advanceBy(count: number): void {
Expand Down Expand Up @@ -583,13 +583,11 @@ export class Tokenizer {
}

private _emitCodePoint(cp: number): void {
let type = TokenType.CHARACTER;

if (isWhitespace(cp)) {
type = TokenType.WHITESPACE_CHARACTER;
} else if (cp === $.NULL) {
type = TokenType.NULL_CHARACTER;
}
const type = isWhitespace(cp)
? TokenType.WHITESPACE_CHARACTER
: cp === $.NULL
? TokenType.NULL_CHARACTER
: TokenType.CHARACTER;

this._appendCharToCurrentCharacterToken(type, String.fromCodePoint(cp));
}
Expand Down Expand Up @@ -1001,7 +999,7 @@ export class Tokenizer {
break;
}
case State.NUMERIC_CHARACTER_REFERENCE_END: {
this._stateNumericCharacterReferenceEnd();
this._stateNumericCharacterReferenceEnd(cp);
break;
}
default: {
Expand Down Expand Up @@ -2979,7 +2977,7 @@ export class Tokenizer {
this._stateNamedCharacterReference(cp);
} else {
this._flushCodePointConsumedAsCharacterReference($.AMPERSAND);
this._reconsumeInState(this.returnState);
this._reconsumeInState(this.returnState, cp);
}
}

Expand Down Expand Up @@ -3013,7 +3011,7 @@ export class Tokenizer {
this._err(ERR.unknownNamedCharacterReference);
}

this._reconsumeInState(this.returnState);
this._reconsumeInState(this.returnState, cp);
}
}

Expand All @@ -3033,7 +3031,7 @@ export class Tokenizer {
this._err(ERR.absenceOfDigitsInNumericCharacterReference);
this._flushCodePointConsumedAsCharacterReference($.AMPERSAND);
this._flushCodePointConsumedAsCharacterReference($.NUMBER_SIGN);
this._reconsumeInState(this.returnState);
this._reconsumeInState(this.returnState, cp);
}
}

Expand Down Expand Up @@ -3066,7 +3064,7 @@ export class Tokenizer {
} else {
this._err(ERR.missingSemicolonAfterCharacterReference);
this.state = State.NUMERIC_CHARACTER_REFERENCE_END;
this._stateNumericCharacterReferenceEnd();
this._stateNumericCharacterReferenceEnd(cp);
}
}

Expand All @@ -3080,13 +3078,13 @@ export class Tokenizer {
} else {
this._err(ERR.missingSemicolonAfterCharacterReference);
this.state = State.NUMERIC_CHARACTER_REFERENCE_END;
this._stateNumericCharacterReferenceEnd();
this._stateNumericCharacterReferenceEnd(cp);
}
}

// Numeric character reference end state
//------------------------------------------------------------------
private _stateNumericCharacterReferenceEnd(): void {
private _stateNumericCharacterReferenceEnd(cp: number): void {
if (this.charRefCode === $.NULL) {
this._err(ERR.nullCharacterReference);
this.charRefCode = $.REPLACEMENT_CHARACTER;
Expand All @@ -3109,6 +3107,6 @@ export class Tokenizer {
}

this._flushCodePointConsumedAsCharacterReference(this.charRefCode);
this._reconsumeInState(this.returnState);
this._reconsumeInState(this.returnState, cp);
}
}
4 changes: 2 additions & 2 deletions test/utils/generate-parsing-tests.ts
Expand Up @@ -31,10 +31,10 @@ export function loadTreeConstructionTestData<T extends TreeAdapterTypeMap>(
const testSet = fs.readFileSync(filePath, 'utf8');
const setName = fileName.replace('.dat', '');

for (const test of parseDatFile(testSet, treeAdapter)) {
for (const [idx, test] of parseDatFile(testSet, treeAdapter).entries()) {
tests.push({
...test,
idx: tests.length,
idx,
setName,
dirName,
});
Expand Down