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: stack exceeding for css tokenizer #1862

Merged
merged 2 commits into from
May 30, 2019
Merged
Changes from all commits
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
39 changes: 28 additions & 11 deletions src/css/syntax/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export class Tokenizer {
}

write(chunk: string) {
this._value.push(...toCodePoints(chunk));
this._value = this._value.concat(toCodePoints(chunk));
}

read(): CSSToken[] {
Expand Down Expand Up @@ -370,7 +370,6 @@ export class Tokenizer {
return this.consumeNumericToken();
}
break;
break;
case COMMA:
return COMMA_TOKEN;
case HYPHEN_MINUS:
Expand Down Expand Up @@ -472,7 +471,6 @@ export class Tokenizer {
}
this.reconsumeCodePoint(codePoint);
return this.consumeIdentLikeToken();
break;
case VERTICAL_LINE:
if (this.peekCodePoint(0) === EQUALS_SIGN) {
this.consumeCodePoint();
Expand Down Expand Up @@ -655,32 +653,51 @@ export class Tokenizer {
}
}

private consumeStringSlice(count: number): string {
const SLICE_STACK_SIZE = 60000;
let value = '';
while (count > 0) {
const amount = Math.min(SLICE_STACK_SIZE, count);
value += fromCodePoint(...this._value.splice(0, amount));
count -= amount;
}
this._value.shift();

return value;
}

private consumeStringToken(endingCodePoint: number): StringValueToken | Token {
let value = '';
let i = 0;

do {
const codePoint = this.consumeCodePoint();
if (codePoint === EOF || codePoint === endingCodePoint) {
const codePoint = this._value[i];
if (codePoint === EOF || codePoint === undefined || codePoint === endingCodePoint) {
value += this.consumeStringSlice(i);
return {type: TokenType.STRING_TOKEN, value};
}

if (codePoint === LINE_FEED) {
this.reconsumeCodePoint(codePoint);
this._value.splice(0, i);
return BAD_STRING_TOKEN;
}

if (codePoint === REVERSE_SOLIDUS) {
const next = this.peekCodePoint(0);
if (next !== EOF) {
const next = this._value[i + 1];
if (next !== EOF && next !== undefined) {
if (next === LINE_FEED) {
this.consumeCodePoint();
value += this.consumeStringSlice(i);
i = -1;
this._value.shift();
} else if (isValidEscape(codePoint, next)) {
value += this.consumeStringSlice(i);
value += fromCodePoint(this.consumeEscapedCodePoint());
i = -1;
}
}
} else {
value += fromCodePoint(codePoint);
}

i++;
} while (true);
}

Expand Down