-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
queued.ts
105 lines (87 loc) · 3.06 KB
/
queued.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { TokenType, Token, CharacterToken, DoctypeToken, TagToken, EOFToken, CommentToken } from '../common/token.js';
import { TokenHandler, Tokenizer, TokenizerOptions, TokenizerMode } from './index.js';
import type { ParserErrorHandler } from '../common/error-codes.js';
import type { Preprocessor } from './preprocessor.js';
const HIBERNATION_TOKEN: Token = { type: TokenType.HIBERNATION, location: null };
/** A token handler implemnetation that calls the same function for all tokens. */
export abstract class SinglePathHandler implements TokenHandler {
protected abstract handleToken(token: Token): void;
onComment(token: CommentToken): void {
this.handleToken(token);
}
onDoctype(token: DoctypeToken): void {
this.handleToken(token);
}
onStartTag(token: TagToken): void {
this.handleToken(token);
}
onEndTag(token: TagToken): void {
this.handleToken(token);
}
onEof(token: EOFToken): void {
this.handleToken(token);
}
onCharacter(token: CharacterToken): void {
this.handleToken(token);
}
onNullCharacter(token: CharacterToken): void {
this.handleToken(token);
}
onWhitespaceCharacter(token: CharacterToken): void {
this.handleToken(token);
}
}
class QueuedHandler extends SinglePathHandler {
private tokenQueue: Token[] = [];
protected handleToken(token: Token): void {
this.tokenQueue.push(token);
}
constructor(public onParseError: ParserErrorHandler | null) {
super();
}
public getNextToken(tokenizer: Tokenizer): Token {
while (this.tokenQueue.length === 0 && tokenizer.active) {
tokenizer.getNextToken();
}
if (this.tokenQueue.length === 0 && !tokenizer.active) {
this.tokenQueue.push(HIBERNATION_TOKEN);
}
return this.tokenQueue.shift()!;
}
}
export interface QueuedTokenizerOptions extends TokenizerOptions {
onParseError?: ParserErrorHandler | null;
}
/**
* Provides the same interface as the old tokenizer, while allowing users to
* read data one token at a time.
*/
export class QueuedTokenizer {
private tokenizer: Tokenizer;
private handler: QueuedHandler;
constructor(options: QueuedTokenizerOptions) {
this.handler = new QueuedHandler(options.onParseError ?? null);
this.tokenizer = new Tokenizer(options, this.handler);
}
set allowCDATA(val: boolean) {
this.tokenizer.allowCDATA = val;
}
get preprocessor(): Preprocessor {
return this.tokenizer.preprocessor;
}
get active(): boolean {
return this.tokenizer.active;
}
set state(val: typeof TokenizerMode[keyof typeof TokenizerMode]) {
this.tokenizer.state = val;
}
public write(chunk: string, isLastChunk: boolean): void {
this.tokenizer.write(chunk, isLastChunk);
}
public insertHtmlAtCurrentPos(str: string): void {
this.tokenizer.insertHtmlAtCurrentPos(str);
}
public getNextToken(): Token {
return this.handler.getNextToken(this.tokenizer);
}
}