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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#![warn(unused_must_use)]
use crate as css;
use css::css_properties::Property;
use css::{PrintErr, Printer, PropertyHandlerContext};
use css::css_values::ident::DashedIdent;
// bumpalo::Bump re-export (CSS is an arena crate)
bitflags::bitflags! {
/// A value for the [color-scheme](https://drafts.csswg.org/css-color-adjust/#color-scheme-prop) property.
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct ColorScheme: u8 {
/// Indicates that the element supports a light color scheme.
const LIGHT = 1 << 0;
/// Indicates that the element supports a dark color scheme.
const DARK = 1 << 1;
/// Forbids the user agent from overriding the color scheme for the element.
const ONLY = 1 << 2;
// Zig: __unused: u5 = 0 (padding — bitflags handles this implicitly)
}
}
impl ColorScheme {
pub(crate) fn parse(input: &mut css::Parser) -> css::Result<ColorScheme> {
let mut res = ColorScheme::empty();
let ident = input.expect_ident_cloned()?;
if let Some(value) = color_scheme_map_get(ident) {
match value {
ColorSchemeKeyword::Normal => return Ok(res),
ColorSchemeKeyword::Only => res.insert(ColorScheme::ONLY),
ColorSchemeKeyword::Light => res.insert(ColorScheme::LIGHT),
ColorSchemeKeyword::Dark => res.insert(ColorScheme::DARK),
}
}
while let Ok(i) = input.try_parse(|p| p.expect_ident_cloned()) {
if let Some(value) = color_scheme_map_get(i) {
match value {
ColorSchemeKeyword::Normal => {
return Err(input.new_custom_error(css::ParserError::invalid_value));
}
ColorSchemeKeyword::Only => {
// Only must be at the start or the end, not in the middle
if res.contains(ColorScheme::ONLY) {
return Err(input.new_custom_error(css::ParserError::invalid_value));
}
res.insert(ColorScheme::ONLY);
return Ok(res);
}
ColorSchemeKeyword::Light => res.insert(ColorScheme::LIGHT),
ColorSchemeKeyword::Dark => res.insert(ColorScheme::DARK),
}
}
}
Ok(res)
}
pub(crate) fn to_css(self, dest: &mut Printer) -> Result<(), PrintErr> {
if self == ColorScheme::empty() {
return dest.write_str("normal");
}
if self.contains(ColorScheme::LIGHT) {
dest.write_str("light")?;
if self.contains(ColorScheme::DARK) {
dest.write_char(b' ')?;
}
}
if self.contains(ColorScheme::DARK) {
dest.write_str("dark")?;
}
if self.contains(ColorScheme::ONLY) {
dest.write_str(" only")?;
}
Ok(())
}
}
// Zig: `const Map = bun.ComptimeEnumMap(enum { normal, only, light, dark });`
// ≤8 entries → plain match on bytes (per PORTING.md).
#[derive(Clone, Copy)]
enum ColorSchemeKeyword {
Normal,
Only,
Light,
Dark,
}
fn color_scheme_map_get(ident: &[u8]) -> Option<ColorSchemeKeyword> {
match ident {
b"normal" => Some(ColorSchemeKeyword::Normal),
b"only" => Some(ColorSchemeKeyword::Only),
b"light" => Some(ColorSchemeKeyword::Light),
b"dark" => Some(ColorSchemeKeyword::Dark),
_ => None,
}
}
/// A value for the [resize](https://www.w3.org/TR/2021/WD-css-ui-4-20210316/#resize) property.
// TODO(port): Zig source is `css.DefineEnumProperty(@compileError(css.todo_stuff.depth))` — intentionally unimplemented upstream.
pub struct Resize;
#[derive(Default)]
pub struct ColorSchemeHandler;
// PORT NOTE: `context.arena` was dropped from PropertyHandlerContext;
// `define_var` no longer needs an arena because `TokenList.v` is a std
// `Vec<TokenOrValue>` (LIFETIMES.tsv classification).
impl ColorSchemeHandler {
pub(crate) fn handle_property(
&mut self,
property: &Property,
dest: &mut css::DeclarationList,
context: &mut PropertyHandlerContext,
) -> bool {
match property {
Property::ColorScheme(color_scheme_) => {
let color_scheme: ColorScheme = *color_scheme_;
if !context
.targets
.is_compatible(css::compat::Feature::LightDark)
{
if color_scheme.contains(ColorScheme::LIGHT) {
dest.push(define_var(b"--buncss-light", css::Token::Ident(b"initial")));
dest.push(define_var(b"--buncss-dark", css::Token::Whitespace(b" ")));
if color_scheme.contains(ColorScheme::DARK) {
context.add_dark_rule(define_var(
b"--buncss-light",
css::Token::Whitespace(b" "),
));
context.add_dark_rule(define_var(
b"--buncss-dark",
css::Token::Ident(b"initial"),
));
}
} else if color_scheme.contains(ColorScheme::DARK) {
dest.push(define_var(b"--buncss-light", css::Token::Whitespace(b" ")));
dest.push(define_var(b"--buncss-dark", css::Token::Ident(b"initial")));
}
}
// PORT NOTE: Zig pushed `property.deepClone(arena)`; ColorScheme is
// `Copy` (bitflags u8), so reconstruct the variant directly.
dest.push(Property::ColorScheme(color_scheme));
true
}
_ => false,
}
}
pub(crate) fn finalize(
&mut self,
_: &mut css::DeclarationList<'_>,
_: &mut PropertyHandlerContext<'_>,
) {
}
}
fn define_var(name: &'static [u8], value: css::Token) -> Property {
// PORT NOTE: `name` is `&'static [u8]` because all call sites pass byte-string literals.
// `TokenList.v` is `Vec<TokenOrValue>` (std Vec — see custom.rs:320), so no arena
// threading is needed here despite Zig's `ArrayList(TokenOrValue)`.
Property::Custom(css::css_properties::custom::CustomProperty {
name: css::css_properties::custom::CustomPropertyName::Custom(DashedIdent { v: name }),
value: css::TokenList {
v: vec![css::css_properties::custom::TokenOrValue::Token(value)],
},
})
}
// ported from: src/css/properties/ui.zig