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
use glory_style::sheet::{Error, ErrorKind, Relation, State, Term, identifier};
use glory_style::{Css, Profile, Sheet};
/// Compile checked semantic selectors into the shared HTML mapping. Rule order,
/// duplicate declarations and class/state specificity are preserved. Layout and
/// cascade remain the consumer engine's responsibility.
pub fn sheet(sheet: &Sheet, profile: Profile) -> Result<String, Error> {
if profile.css != Css::Html {
return Err(Error {
line: 1,
column: 1,
kind: ErrorKind::Target(profile.name),
});
}
sheet.check(profile)?;
let mut css = String::new();
for rule in sheet.rules() {
for (index, selector) in rule.selectors().iter().enumerate() {
if index > 0 {
css.push(',');
}
for compound in selector.compounds() {
match compound.relation() {
Some(Relation::Child) => css.push('>'),
Some(Relation::Descendant) => css.push(' '),
None => {}
}
// Internal parts and user-owned document elements must not
// become semantic selector targets. :where adds no specificity.
css.push_str(":where([data-gly-kind])");
for term in compound.terms() {
match term {
Term::Class(name) => {
css.push('.');
css.push_str(&identifier(name));
}
Term::State(state) => css.push_str(match state {
State::Hover => ":hover",
// Check's label owns the semantic class; only its
// internal input receives physical keyboard focus.
State::Focus => {
":is(:focus,:where([data-gly-kind=check]):focus-within)"
}
State::Enabled => "[data-gly-enabled]",
State::Disabled => "[data-gly-disabled]",
State::Checked => "[data-gly-checked]",
}),
}
}
}
}
css.push('{');
for declaration in rule.declarations() {
css.push_str(&crate::style(declaration.style()));
}
css.push('}');
}
Ok(css)
}