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
9 changes: 5 additions & 4 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
- Correct CSS namespace URI serialization in pretty-printed stylesheets while
preserving compact `url()` representation.
- Fix fractional alpha in the numeric `Color` constructor and normalize CSS
short and eight-digit hex colors, with validation of malformed input.
short and eight-digit hex colors, with validation of malformed input and
unknown color names.
`Color.css` also accepts named colors and `transparent`, and
`Rgba.fromString`/`Hsla.fromString` now parse their argument only once.
- `Color.css` now reads the `hsl()`/`hsla()` hue as a CSS angle (degrees by
default, or `deg`, `grad`, `rad` and `turn`) instead of a 0..1 fraction, and
accepts percentages for saturation, lightness, RGB channels and alpha.
Functional colors with the wrong number of components or non-finite values
now throw a `FormatException`.
Functional colors with the wrong number of components or invalid or
non-finite values now throw a `FormatException` whose source is the color.
- Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the
start of `selector()` and `parseSelectorGroup()` input and of style rules.
- Use native integer hexadecimal formatting for named CSS colors, reducing
temporary allocations and avoiding hangs on negative conversion inputs.
- Speed up named CSS color lookups by indexing the color table.
- Recognize the `@left-bottom` page-margin directive, and stop parsing
`@right-bottom` as `@left-bottom`.
- Recognize the `rebeccapurple` color keyword.
- Recognize the `rebeccapurple` color keyword and add `Color.rebeccaPurple`.
- Reduce repeated CSS unit, directive, and media keyword scans by indexing
keyword tables by length while retaining allocation-free token matching.
- Reduce allocations when tokenizing ordinary CSS identifiers; add a parsing
Expand Down
7 changes: 4 additions & 3 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ class Color implements _StyleProperty, ColorBase {
}

double parseNumber(String text) {
final number = double.parse(text.trim());
if (!number.isFinite) {
throw FormatException('Non-finite CSS color component', value);
final number = double.tryParse(text.trim());
if (number == null || !number.isFinite) {
throw FormatException('Invalid CSS color component', value);
}
return number;
}
Expand Down Expand Up @@ -540,6 +540,7 @@ class Color implements _StyleProperty, ColorBase {
static final Color plum = const Color.hex('dda0dd');
static final Color powderBlue = const Color.hex('b0e0e6');
static final Color purple = const Color.hex('800080');
static final Color rebeccaPurple = const Color.hex('663399');
static final Color red = const Color.hex('ff0000');
static final Color rosyBrown = const Color.hex('bc8f8f');
static final Color royalBlue = const Color.hex('4169e1');
Expand Down
11 changes: 8 additions & 3 deletions pkgs/csslib/lib/src/token_kind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,11 @@ class TokenKind {
String text,
int offset,
int length,
) =>
matchList(groups[length] ?? const <Map<String, Object>>[], field, text,
offset, length);
) {
final group = groups[length];
if (group == null) return -1;
return matchList(group, field, text, offset, length);
}

/// Return the token that matches the unit ident found.
static int matchUnits(String text, int offset, int length) =>
Expand Down Expand Up @@ -607,6 +609,9 @@ class TokenKind {
static String? hexToColorName(Object hexValue) =>
_colorNamesByValue[hexValue];

/// Converts [number] to lowercase hexadecimal, zero-padded to [minDigits].
///
/// A negative [number] keeps its sign in front; only the digits are padded.
static String decimalToHex(int number, [int minDigits = 1]) {
final hex = number.toRadixString(16);
if (number < 0) {
Expand Down
5 changes: 3 additions & 2 deletions pkgs/csslib/lib/src/tokenizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,13 @@ class Tokenizer extends TokenizerBase {

// Decode CSS escapes without changing the raw source span.
if (ch == 92 /*\*/ && _inString) {
decoded ??= StringBuffer()..write(_text.substring(_startIndex, _index));
decoded ??= StringBuffer(_text.substring(_startIndex, _index));
final startHex = ++_index;
eatHexDigits(startHex + 6);
if (_index != startHex) {
final hexVal =
int.tryParse('0x${_text.substring(startHex, _index)}') ?? 0xFFFD;
int.tryParse(_text.substring(startHex, _index), radix: 16) ??
0xFFFD;
decoded.writeCharCode(hexVal <= 0x10FFFF ? hexVal : 0xFFFD);

if (_index == _text.length) break;
Expand Down
15 changes: 11 additions & 4 deletions pkgs/csslib/test/color_conversion_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ void main() {
expect(Rgba(0xff, 0, 0, 1).argbValue, 0xffff0000);
});

test('ARGB round-trips retain each alpha byte, including 0x7f', () {
expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233');
expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080');
});
test(
'ARGB round-trips retain each alpha byte, including 0x7f and >= 0x80',
() {
expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233');
expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080');
expect(Hsla.fromArgbValue(0x80808080).toHexArgbString(), '80808080');
expect(Hsla.fromArgbValue(0xff808080).toHexArgbString(), 'ff808080');
expect(Rgba.fromArgbValue(0xff808080).argbValue, 0xff808080);
expect(Rgba.fromArgbValue(0x80808080).argbValue, 0x80808080);
},
);

test('color string factories decode CSS colors only once', () {
expect(Rgba.fromString('rgba(255,0,0,0.5)').toHexArgbString(), '80ff0000');
Expand Down
8 changes: 8 additions & 0 deletions pkgs/csslib/test/color_function_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ void main() {
expect(() => Color.css('rgba(10,20,30)'), throwsFormatException);
expect(() => Color.css('rgb(NaN,20,30)'), throwsFormatException);
});

test('component errors report the whole color as their source', () {
expect(
() => Color.css('rgb(1x,20,30)'),
throwsA(isA<FormatException>()
.having((e) => e.source, 'source', 'rgb(1x,20,30)')),
);
});
}
4 changes: 4 additions & 0 deletions pkgs/csslib/test/color_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ void main() {
expect(() => Color.css('#-12345'), throwsFormatException);
expect(() => Color.css('# 12345'), throwsFormatException);
});

test('unknown color names throw a FormatException', () {
expect(() => Color.css('notacolor'), throwsFormatException);
});
});

group('named colors', () {
Expand Down
1 change: 1 addition & 0 deletions pkgs/csslib/test/token_lookup_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void main() {
);
expect(TokenKind.colorValue(TokenKind.matchColorName('rebeccapurple')!),
0x663399);
expect(Color.rebeccaPurple.argbValue, 0x663399);
expect(TokenKind.matchColorName('not-a-color'), isNull);
});

Expand Down
Loading