Skip to content

Commit 4bffbe6

Browse files
fb55claude
andcommitted
fix(tokenizer): end pending text before a tag that is dropped at EOF
At EOF inside a tag the tag token is dropped, but the pending character token got the EOF position as its end, so its source range covered the unfinished tag. The EOF path ends pending characters at the open token's start when there is one. An inappropriate end tag dropped in RCDATA or RAWTEXT already clears `currentToken` (#1897), so it cannot serve as that boundary. The location tests check the text range before an unfinished tag at EOF. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 53d1261 commit 4bffbe6

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎packages/parse5/lib/tokenizer/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,9 @@ export class Tokenizer {
771771
location.endOffset = location.startOffset;
772772
}
773773

774-
this._emitCurrentCharacterToken(location);
774+
// A token that is still open at EOF (eof-in-tag) is dropped. Pending
775+
// characters end where that token started, rather than at EOF.
776+
this._emitCurrentCharacterToken(this.currentToken?.location ?? location);
775777
this.handler.onEof({ type: TokenType.EOF, location });
776778
this.active = false;
777779
}

‎packages/parse5/lib/tokenizer/tokenizer-location-info.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,41 @@ it('Location Info (Tokenizer)', () => {
181181
assert.ok(!tokenizer.active);
182182
}
183183
});
184+
185+
function noop(): void {
186+
// Noop
187+
}
188+
189+
it('Location Info (Tokenizer) - text before a tag that is dropped at EOF', () => {
190+
const testCases = [
191+
{ initialMode: TokenizerMode.DATA, lastStartTagName: '', html: 'text<a href="foo', textEnd: 4 },
192+
{ initialMode: TokenizerMode.RCDATA, lastStartTagName: 'title', html: 'text</title foo', textEnd: 4 },
193+
// Not an end tag, as it doesn't match the last start tag.
194+
{ initialMode: TokenizerMode.RCDATA, lastStartTagName: 'title', html: 'text</titles', textEnd: 12 },
195+
];
196+
197+
for (const { initialMode, lastStartTagName, html, textEnd } of testCases) {
198+
const endOffsets: (number | undefined)[] = [];
199+
const tokenizer = new Tokenizer(
200+
{ sourceCodeLocationInfo: true },
201+
{
202+
onComment: noop,
203+
onDoctype: noop,
204+
onStartTag: noop,
205+
onEndTag: noop,
206+
onEof: noop,
207+
onCharacter({ location }): void {
208+
endOffsets.push(location?.endOffset);
209+
},
210+
onNullCharacter: noop,
211+
onWhitespaceCharacter: noop,
212+
},
213+
);
214+
215+
tokenizer.state = initialMode;
216+
tokenizer.lastStartTagName = lastStartTagName;
217+
tokenizer.write(html, true);
218+
219+
assert.deepStrictEqual(endOffsets, [textEnd]);
220+
}
221+
});

0 commit comments

Comments
 (0)