Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.0.3-wip

- Avoid recursive tokenizer calls when skipping CSS comments and whitespace,
preventing stack overflow on comment-heavy stylesheets.
- End HTML comments at the first `-->` even when more dashes precede it, and
don't end comments at a NUL character.
- `Font.scale` keeps the line height (scaling pixel heights and keeping
relative ratios) and no longer throws when the font size is unspecified.
- Fix `Font.cssExpression` output for partial fonts and line heights.
Expand Down
99 changes: 69 additions & 30 deletions pkgs/csslib/lib/src/tokenizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,62 @@ class Tokenizer extends TokenizerBase {

@override
Token next({bool unicodeRange = false}) {
// keep track of our starting position
// Skip whitespace, comments and CDATA markers in a loop rather than by
// recursing into next(), so long runs of them can't overflow the stack.
if (_inString) {
final skipStart = _index;
var trailingWhitespaceStart = -1;
while (_index < _text.length) {
final ch = _text.codeUnitAt(_index);
if (ch == TokenChar.NEWLINE ||
ch == TokenChar.RETURN ||
ch == TokenChar.SPACE ||
ch == TokenChar.TAB) {
trailingWhitespaceStart = _index;
_index++;
while (_index < _text.length) {
final next = _text.codeUnitAt(_index);
if (next != TokenChar.NEWLINE &&
next != TokenChar.RETURN &&
next != TokenChar.SPACE &&
next != TokenChar.TAB) {
break;
}
_index++;
}
continue;
}

if (ch == TokenChar.SLASH &&
_index + 1 < _text.length &&
_text.codeUnitAt(_index + 1) == TokenChar.ASTERISK) {
final end = _text.indexOf('*/', _index + 2);
if (end < 0) break; // Preserve incomplete-comment errors.
_index = end + 2;
} else if (ch == TokenChar.LESS && _text.startsWith('<!--', _index)) {
final end = _text.indexOf('-->', _index + 4);
if (end < 0) break;
_index = end + 3;
} else if (ch == TokenChar.LESS &&
_text.startsWith('<![CDATA[', _index)) {
_index += 9;
} else if (ch == TokenChar.RBRACK && _text.startsWith(']]>', _index)) {
_index += 3;
} else {
break;
}
trailingWhitespaceStart = -1;
}
if (_index == _text.length && trailingWhitespaceStart >= 0) {
// Preserve the historical EOF span after trailing whitespace.
_startIndex = trailingWhitespaceStart;
return _finishToken(TokenKind.END_OF_FILE);
}
// A unicode range can't span whitespace or comments.
if (_index != skipStart) unicodeRange = false;
}

// Keep track of our starting position.
_startIndex = _index;

int ch;
Expand Down Expand Up @@ -402,41 +457,25 @@ class Tokenizer extends TokenizerBase {
}

Token finishHtmlComment() {
while (true) {
var ch = _nextChar();
if (ch == 0) {
return _finishToken(TokenKind.INCOMPLETE_COMMENT);
} else if (ch == TokenChar.MINUS) {
/* Check if close part of Comment Definition --> (CDC). */
if (_maybeEatChar(TokenChar.MINUS)) {
if (_maybeEatChar(TokenChar.GREATER)) {
if (_inString) {
return next();
} else {
return _finishToken(TokenKind.HTML_COMMENT);
}
}
}
}
// The comment ends at the first `-->`, even if more dashes precede it.
final end = _text.indexOf('-->', _index);
if (end < 0) {
_index = _text.length;
return _finishToken(TokenKind.INCOMPLETE_COMMENT);
}
_index = end + 3;
return _finishToken(TokenKind.HTML_COMMENT);
}

@override
Token finishMultiLineComment() {
while (true) {
var ch = _nextChar();
if (ch == 0) {
return _finishToken(TokenKind.INCOMPLETE_COMMENT);
} else if (ch == 42 /*'*'*/) {
if (_maybeEatChar(47 /*'/'*/)) {
if (_inString) {
return next();
} else {
return _finishToken(TokenKind.COMMENT);
}
}
}
final end = _text.indexOf('*/', _index);
if (end < 0) {
_index = _text.length;
return _finishToken(TokenKind.INCOMPLETE_COMMENT);
}
_index = end + 2;
return _finishToken(TokenKind.COMMENT);
}
}

Expand Down
59 changes: 59 additions & 0 deletions pkgs/csslib/test/comment_skipping_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:csslib/parser.dart';
import 'package:source_span/source_span.dart';
import 'package:test/test.dart';

void main() {
test('thousands of skipped comments cannot overflow the tokenizer stack', () {
final comments = List.filled(20000, '/* note */ \n').join();
final errors = <Message>[];
final tree = parse('$comments.x { color: red; }', errors: errors);
expect(errors, isEmpty);
expect(tree.topLevels, hasLength(1));
});

test('HTML comment, CDATA and block comments preserve following rules', () {
final errors = <Message>[];
final tree = parse(
'<!-- header --> <![CDATA[ /* ignored */ ]]> '
'.entry { color: red; }',
errors: errors,
);
expect(errors, isEmpty);
expect(tree.topLevels, hasLength(1));
});

test('comments before EOF do not produce empty rules', () {
final errors = <Message>[];
final tree = parse('/* leading */ \n /* trailing */', errors: errors);
expect(errors, isEmpty);
expect(tree.topLevels, isEmpty);
});

test('HTML comment ends at the first -->', () {
final errors = <Message>[];
final tree =
parse('<!-- header ---> .entry { color: red; }', errors: errors);
expect(errors, isEmpty);
expect(tree.topLevels, hasLength(1));
});

test('NUL character does not end a comment', () {
final errors = <Message>[];
final tree =
parse('/* a \u0000 b */ .entry { color: red; }', errors: errors);
expect(errors, isEmpty);
expect(tree.topLevels, hasLength(1));
});

test('unicode range does not continue after whitespace or comments', () {
for (final input in [' 10', '/**/10']) {
final tokenizer = Tokenizer(SourceFile.fromString(input), input, true);
expect(tokenizer.next(unicodeRange: true).kind, TokenKind.INTEGER,
reason: input);
}
});
}
Loading