A plain CssSheetParse no longer skips a browser-only declaration silently: it fails with a message that points shared sheets to CssSheetParseWithDiagnostics, which keeps native rules and reports the declaration. The web declaration counters on the parser and sheet are gone; packs refuse such declarations through the same parse, and the formatter counts them from the recorded declarations. The README property table is generated by css_tool properties from the parser's catalog, make check fails when it is stale, and the parse tests read every catalog row natively on its target.
460 lines
27 KiB
Ziran
460 lines
27 KiB
Ziran
// The CSS subset: what parses into which rules, and the diagnostic every
|
|
// sheet outside it gets. A sheet either parses whole or not at all.
|
|
#import "css_parser"
|
|
#import "css_sheet"
|
|
#import "css_test_support"
|
|
#import "kryon/style"
|
|
#import "kryon/style_sheet"
|
|
#import "kryon/control_props"
|
|
|
|
using StyleField;
|
|
using ButtonTone;
|
|
using MaterialKind;
|
|
using ElevationRole;
|
|
using FontRole;
|
|
using MotionEasing;
|
|
using CssTokenKind;
|
|
using CssScheme;
|
|
|
|
Rule :: (index: s32) -> StyleRule { return test_rules.items[index] }
|
|
|
|
Has :: (style: StyleData, field: u32) -> bool {
|
|
return (style.fields & field) != cast(u32)0
|
|
}
|
|
|
|
Values :: () {
|
|
if Builds("Button { color: #112233; padding-inline: 8px; padding-block: 0.5rem; gap: 4px; }", "plain rule") {
|
|
Check(test_rules.count == 1, "one rule")
|
|
style: StyleData = Rule(0).style
|
|
Check(style.foreground == cast(u32)0x112233ff, "hex color")
|
|
Check(style.padding_x == 8.0 && style.padding_y == 8.0, "px and rem lengths")
|
|
Check(Has(style, cast(u32)StyleForeground) && Has(style, cast(u32)StylePaddingX) && Has(style, cast(u32)StyleGap),
|
|
"fields marked")
|
|
Check(!Has(style, cast(u32)StyleBackground), "unset fields unmarked")
|
|
Check(Rule(0).selector.kind == StyleKindButton(), "type selector")
|
|
}
|
|
if Builds("Text { color: rebeccapurple; background: rgb(1 2 3 / 50%); border-color: hsl(120 100% 50%); outline-color: #abc; }",
|
|
"color forms") {
|
|
style: StyleData = Rule(0).style
|
|
Check(style.foreground == cast(u32)0x663399ff, "named color")
|
|
Check(style.background == cast(u32)0x01020380, "rgb with alpha")
|
|
Check(style.border == cast(u32)0x00ff00ff, "hsl")
|
|
Check(style.focus == cast(u32)0xaabbccff, "short hex")
|
|
}
|
|
if Builds("Button { background: linear-gradient(to bottom, #000000, #ffffff); translate: 3px 4px; border: 2px solid #ff0000; border-radius: 6px; opacity: 0.5; -kryon-material: glass; -kryon-icon-size: 20px; font-size: 1.5rem; letter-spacing: 1px; }",
|
|
"shorthands") {
|
|
style: StyleData = Rule(0).style
|
|
Check(style.background == cast(u32)0x000000ff && style.background_end == cast(u32)0xffffffff,
|
|
"gradient")
|
|
Check(Has(style, cast(u32)StyleBackgroundEnd), "gradient end marked")
|
|
Check(style.offset_x == 3.0 && style.offset_y == 4.0, "translate")
|
|
Check(style.border_width == 2.0 && style.border == cast(u32)0xff0000ff, "border shorthand")
|
|
Check(style.radius == 6.0 && style.opacity == 0.5, "radius and opacity")
|
|
Check(cast(s32)style.material == cast(s32)MaterialGlass, "material")
|
|
Check(style.icon_size == 20.0 && style.font_size == 24.0, "icon size and rem font")
|
|
Check(style.letter_spacing == 1.0, "letter spacing")
|
|
}
|
|
if Builds("Text { font-family: \"Inter Display\", sans-serif; }", "font family") {
|
|
Check(test_rules.items[0].style.typeface == "Inter Display", "first family")
|
|
}
|
|
if Builds("Card { min-height: 44px; line-height: 1.25; -kryon-font-role: body; -kryon-elevation: floating; -kryon-motion-duration: 120ms; -kryon-motion-multiplier: 1.5; -kryon-motion-easing: linear; -kryon-motion-reduced: reduce; }",
|
|
"typed customization") {
|
|
style: StyleData = Rule(0).style
|
|
Check(style.min_height == 44.0 && style.line_height == 1.25, "geometry")
|
|
Check(cast(s32)style.font_role == cast(s32)FontRoleBody &&
|
|
cast(s32)style.elevation == cast(s32)ElevationRoleFloating, "font and elevation")
|
|
Check(style.motion_duration == 120.0 && style.motion_multiplier == 1.5 &&
|
|
cast(s32)style.motion_easing == cast(s32)MotionEasingLinear && style.motion_reduced,
|
|
"motion")
|
|
}
|
|
// A plain native parse refuses what only the browser applies; a shared
|
|
// sheet is read with CssSheetParseWithDiagnostics, which reports it.
|
|
ParseFails("Button { width: 20%; color: red; }", "applied only by the browser", "browser-only size refused natively")
|
|
ParseFails("Button { text-transform: uppercase; }", "applied only by the browser", "browser-only property refused natively")
|
|
if Builds("Button { padding: 4px 8px; }", "padding pair") {
|
|
Check(Rule(0).style.padding_y == 4.0 && Rule(0).style.padding_x == 8.0, "padding block inline")
|
|
}
|
|
}
|
|
|
|
native_authoring_css :: #string CSS
|
|
:root { --ink: hwb(.5turn 20% 20% / 50%); --fade: 180%; --control: 2.75rem; }
|
|
Button { min-height: 32px; background: hwb(45 40% 80%); opacity: var(--fade); }
|
|
Button.primary { min-block-size: var(--control, -1px); color: var(--ink); }
|
|
Button.primary:hover { min-height: 48px; opacity: var(--missing, 75%); }
|
|
CSS;
|
|
|
|
native_authoring_angles_css :: #string CSS
|
|
Button {
|
|
min-block-size: var(--missing);
|
|
min-height: 16px;
|
|
opacity: -25%;
|
|
border: 1px solid hwb(-.5turn 0% 0%);
|
|
background: linear-gradient(hsl(200grad 100% 50%), hsla(3.141592653589793rad, 100%, 50%, .5));
|
|
}
|
|
CSS;
|
|
|
|
NativeAuthoring :: () {
|
|
if Builds(native_authoring_css, "native authoring") {
|
|
facts: StyleFacts = Facts(StyleKindButton(), CssClass("primary"))
|
|
normal: StyleData = Resolve(facts, 1)
|
|
Check(normal.min_height == 44.0 && Has(normal, cast(u32)StyleMinHeight),
|
|
"logical size resolves through the minimum-height cascade")
|
|
Check(normal.background == cast(u32)0x555555ff &&
|
|
normal.foreground == cast(u32)0x33cccc80, "HWB normalization and alpha token")
|
|
Check(normal.opacity == 1.0 && Has(normal, cast(u32)StyleOpacity),
|
|
"opacity token clamps on the typed field")
|
|
hover: StyleData = Resolve(facts, 2)
|
|
Check(hover.min_height == 48.0 && hover.opacity == 0.75,
|
|
"physical size and opacity fallback win on hover")
|
|
Check(Resolve(Facts(StyleKindButton(), 0), 2).min_height == 32.0,
|
|
"hover rule stays scoped to the class")
|
|
}
|
|
if Builds(native_authoring_angles_css, "aliases and angles") {
|
|
style: StyleData = Rule(0).style
|
|
Check(style.min_height == 16.0 && style.opacity == 0.0, "later alias clears reference and opacity clamps")
|
|
Check(style.border == cast(u32)0x00ffffff && style.background == cast(u32)0x00ffffff &&
|
|
style.background_end == cast(u32)0x00ffff80, "negative turn, grad and rad in border and gradient")
|
|
}
|
|
ParseFails("Button { min-block-size: -1px; }", "cannot be negative", "negative logical size")
|
|
BuildFails(":root { --size: -1px; } Button { min-block-size: var(--size); }",
|
|
"has an invalid value", "negative logical size from token")
|
|
BuildFails("Button { min-block-size: var(--size, -1px); }",
|
|
"has an invalid value", "negative logical size from used fallback")
|
|
ParseFails("Button { min-block-size: 50%; }", "percentages are not native lengths", "relative logical size")
|
|
ParseFails("Button { color: hwb(0, 10%, 20%); }", "hwb() uses spaces", "HWB comma syntax")
|
|
ParseFails("Button { color: hwb(0 10 20%); }", "expected a percentage", "HWB percentage subset")
|
|
ParseFails("Button { color: hsl(10% 50% 50%); }", "a hue needs an angle", "invalid hue percentage")
|
|
ParseFails("Button { color: hwb(5px 10% 20%); }", "not an angle unit", "invalid hue unit")
|
|
}
|
|
|
|
color_mix_css :: #string CSS
|
|
:root {
|
|
--hover: COLOR-MIX(IN SRGB, red, 25% blue);
|
|
--alias: var(--hover);
|
|
}
|
|
Button {
|
|
color: color-mix(in srgb, red, blue);
|
|
background: color-mix(in srgb, red 90%, blue 60%);
|
|
border: 1px solid color-mix(in srgb, #ff0000 20%, #0000ff 30%);
|
|
outline-color: color-mix(in srgb, transparent, #2468ac 25%);
|
|
}
|
|
Button:hover {
|
|
color: var(--alias);
|
|
background: var(--missing, color-mix(in srgb, #00ff00 20%, black));
|
|
}
|
|
Text {
|
|
color: color-mix(in srgb, rgb(230 20 40 / 24%) 40%, #1464dc 60%);
|
|
background: color-mix(in srgb, rgb(10.4 30.4 50.4 / .499) 80%, rgb(11.4 31.4 51.4 / .501) 20%);
|
|
border-color: color-mix(in srgb, #ff000000, rgba(0, 0, 255, 0));
|
|
outline-color: color-mix(in srgb, color-mix(in srgb, #000 70%, white) 60%, #000);
|
|
}
|
|
CSS;
|
|
|
|
ColorMix :: () {
|
|
if Builds(color_mix_css, "sRGB color mixes") {
|
|
facts: StyleFacts = Facts(StyleKindButton(), 0)
|
|
button: StyleData = Resolve(facts, 1)
|
|
Check(button.foreground == cast(u32)0x800080ff, "missing weights default to equal shares")
|
|
Check(button.background == cast(u32)0x990066ff, "weights above 100% normalize")
|
|
Check(button.border == cast(u32)0x66009980 && button.border_width == 1.0, "weights below 100% scale alpha")
|
|
Check(button.focus == cast(u32)0x2468ac40, "transparent stop preserves the other stop's hue")
|
|
hover: StyleData = Resolve(facts, 2)
|
|
Check(hover.foreground == cast(u32)0xbf0040ff, "prefix weight, inferred first share and custom property alias")
|
|
Check(hover.background == cast(u32)0x003300ff, "inferred second share in a var() fallback")
|
|
text: StyleData = Resolve(Facts(StyleKindText(), 0), 1)
|
|
Check(text.foreground == cast(u32)0x3159c3b1, "translucent stops mix with premultiplied alpha")
|
|
Check(text.background == cast(u32)0x0b1f337f, "channels and alpha keep precision until the final mix")
|
|
Check(text.border == 0 && Has(text, cast(u32)StyleBorder), "all-transparent mix resolves to transparent black")
|
|
Check(text.focus == cast(u32)0x2e2e2eff, "nested literal mix")
|
|
}
|
|
ParseFails("Button { color: color-mix(in oklab, red, blue); }", "only the srgb color space", "unsupported mix space")
|
|
ParseFails("Button { color: color-mix(red, blue); }", "requires 'in srgb'", "missing mix space")
|
|
ParseFails("Button { color: color-mix(in srgb, red, blue, green); }", "exactly two color stops", "extra mix stop")
|
|
ParseFails("Button { color: color-mix(in srgb, red); }", "requires two color stops", "missing mix stop")
|
|
ParseFails("Button { color: color-mix(in srgb, red 0%, blue 0%); }", "cannot both be 0%", "zero total mix weight")
|
|
ParseFails("Button { color: color-mix(in srgb, -1% red, blue); }", "percentages from 0% to 100%", "negative mix weight")
|
|
ParseFails("Button { color: color-mix(in srgb, red, blue 101%); }", "percentages from 0% to 100%", "excess mix weight")
|
|
ParseFails("Button { color: color-mix(in srgb, red 10px, blue); }", "percentages from 0% to 100%", "invalid mix weight unit")
|
|
ParseFails("Button { color: color-mix(in srgb, red 20%, 30% blue 40%); }", "expected ')' to close", "duplicate mix weight")
|
|
ParseFails("Button { color: color-mix(in srgb, var(--a, red), blue); }", "stops must be literal colors", "variable mix stop")
|
|
}
|
|
|
|
Selectors :: () {
|
|
if Builds("Button.hero#save:hover, Text:disabled, *[tone=Accent]:active, Toggle:checked, Button:focus, Button:selected {color: red}",
|
|
"selector list") {
|
|
Check(test_rules.count == 6, "one rule per selector")
|
|
Check(Rule(0).selector.class_name == CssClass("hero") && Rule(0).selector.id == CssClass("save") &&
|
|
Rule(0).state == 2, "class, id and hover")
|
|
Check(Rule(1).selector.kind == StyleKindText() && Rule(1).state == 5, "disabled")
|
|
Check(Rule(2).selector.kind == StyleKindAny() && Rule(2).selector.tone == cast(s32)ButtonToneAccent &&
|
|
Rule(2).state == 3, "universal, attribute, active")
|
|
Check(Rule(3).state == 7 && Rule(5).state == 7, "checked and selected")
|
|
Check(Rule(4).state == 4, "focus")
|
|
}
|
|
ParseFails("Button > Text { color: red; }", "combinators are outside Kryon CSS", "combinator")
|
|
ParseFails("Button Text { color: red; }", "combinators are outside Kryon CSS", "descendant")
|
|
ParseFails("Button#save#other { color: red; }", "one id per selector", "two ids")
|
|
ParseFails("Button::before { color: red; }", "pseudo-elements are outside Kryon CSS", "pseudo-element")
|
|
ParseFails("Button:nth-child(2) { color: red; }", "selector function", "selector function")
|
|
ParseFails("Butt0n { color: red; }", "unknown widget type", "widget name")
|
|
ParseFails("Button.a.b { color: red; }", "one class per selector", "two classes")
|
|
ParseFails("Button:hover:focus { color: red; }", "one state pseudo-class per selector", "two states")
|
|
ParseFails("Button[tone^=Acc] { color: red; }", "only '=' attribute matches", "attribute operator")
|
|
}
|
|
|
|
Diagnostics :: () {
|
|
ParseFails("Button {\n colr: red;\n}", "unknown property 'colr' at sheet.css:2:3", "location")
|
|
ParseFails("Button { color: red; }\nText { colour: blue; }", "unknown property 'colour'",
|
|
"no partial sheet")
|
|
Check(!CssSheetBuild(*test_sheet, CssDefaultEnvironment(), null, *test_rules), "failed sheet never builds")
|
|
ParseFails("// note\nButton { color: red; }", "'//' comments are not CSS", "line comment")
|
|
ParseFails("Button { color: red; } /* open", "unterminated comment", "open comment")
|
|
ParseFails("Button { color: red !important; }", "!important is outside Kryon CSS", "important")
|
|
ParseFails("Button { gap: 4; }", "a length needs a unit", "unitless length")
|
|
ParseFails("Button { gap: 2vmin; }", "is outside Kryon CSS; use px, rem, em, vw or vh", "unknown length unit")
|
|
ParseFails("Button { gap: 50%; }", "percentages are not native lengths", "percent")
|
|
ParseFails("Button.a { & .b { color: red; } }", "a nested combinator is outside Kryon CSS", "nested combinator")
|
|
ParseFails("Button.a { &.b { color: red; } }", "repeats a class, id, attribute or state", "nested second class")
|
|
ParseFails("Button.a { &:hover { &:focus { color: red; } } }", "nested rules are outside Kryon CSS", "nesting twice")
|
|
ParseFails("Button:first-child { color: red; }", "needs sibling context", "first child")
|
|
ParseFails("Button { gap: calc(4px * 2px); }", "multiply a length by a number", "length times length")
|
|
ParseFails("Button { gap: calc(4px +2px); }", "expected ')' to close calc", "calc sign needs spaces")
|
|
ParseFails("Button { gap: min(1vw, max(2vw, 4px)); }", "nested min(), max() or clamp() with vw or vh",
|
|
"nested viewport bounds")
|
|
ParseFails("Button { padding-left: 4px; }", "use padding-inline", "one side")
|
|
ParseFails("Button { padding: 1px 2px 3px 4px; }", "Kryon pads", "uneven sides")
|
|
ParseFails("Button { border: 1px dashed red; }", "borders are solid", "border style")
|
|
ParseFails("Button { background: url(a.png); }", "background images are outside Kryon CSS", "image")
|
|
ParseFails("Button { background: linear-gradient(90deg, red, blue); }", "Kryon gradients run top to bottom",
|
|
"gradient angle")
|
|
ParseFails("Button { color: #12345; }", "hex colors have 3, 4, 6 or 8 digits", "hex length")
|
|
ParseFails("Button { color: blurple; }", "unknown color", "color name")
|
|
ParseFails("Button { --x: 1px; }", "custom properties belong in :root", "custom property in rule")
|
|
ParseFails(":root { color: red; }", ":root holds only custom properties", "property in root")
|
|
ParseFails("Button { color: red; ", "expected '}'", "unclosed rule")
|
|
ParseFails("Button { Text { color: red; } }", "nested rules are outside Kryon CSS", "nesting")
|
|
ParseFails("@pack classic;", "KSS directive 'pack'", "KSS directive")
|
|
ParseFails("@font-face { src: x; }", "at-rule 'font-face' is outside Kryon CSS", "unknown at-rule")
|
|
ParseFails("@media not print { Button { color: red; } }", "'not' media queries", "not")
|
|
ParseFails("@media (colour: dark) { Button { color: red; } }", "unknown media feature", "media feature")
|
|
ParseFails("@media (prefers-reduced-motion: maybe) { Button { color: red; } }",
|
|
"unknown value 'maybe' for this media feature", "reduced-motion value")
|
|
ParseFails("Button { gap: 1e3px; }", "exponents are outside Kryon CSS", "exponent")
|
|
ParseFails("Button { font-family: \"a\\\"b\"; }", "escapes in strings", "string escape")
|
|
ParseFails("Button { line-height: -1; }", "cannot be negative", "negative line height")
|
|
ParseFails("Row { gap: 3px -2px; }", "cannot be negative", "negative column gap")
|
|
ParseFails("Row { gap: 1px 2px 3px; }", "too many values", "gap takes two values")
|
|
ParseFails("Text { font-weight: 1001; }", "from 1 to 1000", "weight range")
|
|
ParseFails("Text { font-weight: bolder; }", "from 1 to 1000", "relative weight")
|
|
ParseFails("Text { font-style: oblique 20deg; }", "too many values", "oblique angle")
|
|
ParseFails("Button { -kryon-font-role: title; }", "unknown value 'title'", "unknown font role")
|
|
ParseFails("Button { -kryon-motion-easing: fast; }", "unknown value 'fast'", "unknown easing")
|
|
}
|
|
|
|
Layers :: () {
|
|
source: string = "@layer base, app;\n@layer app { Button { color: #000001; } }\n@layer base { Button { color: #000002; } }"
|
|
if Builds(source, "layers") {
|
|
Check(Resolve(Facts(StyleKindButton(), 0), 1).foreground == cast(u32)0x000001ff,
|
|
"declared layer order, not source order")
|
|
}
|
|
if Builds("@layer base { Button { color: #000002; } }\nButton { color: #000003; }", "unlayered") {
|
|
Check(Resolve(Facts(StyleKindButton(), 0), 1).foreground == cast(u32)0x000003ff,
|
|
"unlayered rules win over layers")
|
|
}
|
|
// Within a layer, specificity before order.
|
|
if Builds("Button.hero { color: #000004; }\nButton { color: #000005; }", "specificity") {
|
|
Check(Resolve(Facts(StyleKindButton(), CssClass("hero")), 1).foreground == cast(u32)0x000004ff,
|
|
"class beats type")
|
|
Check(Resolve(Facts(StyleKindButton(), 0), 1).foreground == cast(u32)0x000005ff, "type alone")
|
|
}
|
|
if Builds("Button.hero { color: #000008; }\n#save { color: #000009; }", "id specificity") {
|
|
facts: StyleFacts = Facts(StyleKindButton(), CssClass("hero"))
|
|
facts.id = CssClass("save")
|
|
Check(Resolve(facts, 1).foreground == cast(u32)0x000009ff, "id beats type and class")
|
|
facts.id = CssClass("other")
|
|
Check(Resolve(facts, 1).foreground == cast(u32)0x000008ff, "a different id does not match")
|
|
}
|
|
if Builds("Button { color: #000006; }\nButton:hover { color: #000007; }", "state rules") {
|
|
Check(Resolve(Facts(StyleKindButton(), 0), 2).foreground == cast(u32)0x000007ff, "hover state")
|
|
Check(Resolve(Facts(StyleKindButton(), 0), 1).foreground == cast(u32)0x000006ff, "normal state")
|
|
}
|
|
ParseFails("@layer a.b { Button { color: red; } }", "nested layer names", "dotted layer")
|
|
}
|
|
|
|
Imports :: () {
|
|
if Builds("@import \"base\";\nButton { gap: 2px; }", "import") {
|
|
Check(test_rules.count == 2, "imported rule first")
|
|
Check(Rule(0).style.foreground == cast(u32)0x010203ff, "imported values")
|
|
Check(Resolve(Facts(StyleKindButton(), 0), 1).gap == 2.0, "later rule wins")
|
|
}
|
|
if Builds("@import url(\"deep\");\nTextField { gap: 1px; }", "nested import") {
|
|
Check(test_rules.count == 3, "import inside an import")
|
|
}
|
|
if Builds("@import \"tokens\";\nText { color: var(--ink); }", "imported custom property") {
|
|
Check(Rule(0).style.foreground == cast(u32)0x445566ff, "var from import")
|
|
}
|
|
ParseFails("Button { gap: 2px; }\n@import \"base\";", "@import must come before every other rule", "late import")
|
|
ParseFails("@import \"missing\";", "cannot import", "missing import")
|
|
ParseFails("@import \"loop\";", "import cycle through", "import cycle")
|
|
}
|
|
|
|
Variables :: () {
|
|
if Builds(":root { --ink: #000000; }\n@media (prefers-color-scheme: dark) { :root { --ink: #ffffff; } }\nText { color: var(--ink); }",
|
|
"scheme") {
|
|
Check(Rule(0).style.foreground == cast(u32)0x000000ff, "light value")
|
|
Check(BuildIn(":root { --ink: #000000; }\n@media (prefers-color-scheme: dark) { :root { --ink: #ffffff; } }\nText { color: var(--ink); }",
|
|
Env("dark")) && Rule(0).style.foreground == cast(u32)0xffffffff, "dark value")
|
|
}
|
|
if Builds("Button { color: var(--missing, #123456); gap: var(--none, 3px); }", "fallback") {
|
|
Check(Rule(0).style.foreground == cast(u32)0x123456ff && Rule(0).style.gap == 3.0, "fallback values")
|
|
}
|
|
if Builds(":root { --a: #111111; --b: var(--a); --pad: 0; }\nText { color: var(--b); gap: var(--pad); }", "alias") {
|
|
Check(Rule(0).style.foreground == cast(u32)0x111111ff, "alias followed")
|
|
Check(Rule(0).style.gap == 0.0, "zero serves as a length")
|
|
}
|
|
if Builds(":root { --fast: 150ms; --ratio: 0.5; --look: glass; }\nButton { opacity: var(--ratio); -kryon-material: var(--look); }",
|
|
"kinds") {
|
|
Check(Rule(0).style.opacity == 0.5 && cast(s32)Rule(0).style.material == cast(s32)MaterialGlass, "number and material")
|
|
fast: CssTokenResult = CssSheetToken(*test_sheet, "--fast")
|
|
Check(fast.found && fast.kind == cast(s32)CssTokenDuration && fast.number == 150.0, "duration in ms")
|
|
}
|
|
BuildFails("Text { color: var(--nowhere); }", "undefined custom property", "undefined var")
|
|
BuildFails(":root { --pad: 8px; }\nText { color: var(--pad); }", "is a length where the property takes another kind",
|
|
"kind mismatch")
|
|
BuildFails(":root { --a: var(--b); --b: var(--a); }\nText { color: var(--a); }", "alias cycle", "alias cycle")
|
|
ParseFails(":root { --a: 1px 2px; }", "a custom property holds one value", "two values")
|
|
// A host override beats every definition.
|
|
if Parse(":root { --ink: #000000; }\nText { color: var(--ink); }") {
|
|
overrides: CssOverrides
|
|
CssOverrideColor(*overrides, "ink", cast(u32)0x0a0b0cff)
|
|
Check(CssSheetBuild(*test_sheet, CssDefaultEnvironment(), *overrides, *test_rules) &&
|
|
Rule(0).style.foreground == cast(u32)0x0a0b0cff, "override")
|
|
}
|
|
}
|
|
|
|
Media :: () {
|
|
source: string = "@media (-kryon-platform: android) { Button { gap: 3px; } }\n@media (pointer: coarse) { Button { gap: 5px; } }\n@media (max-width: 600px) { Button { gap: 7px; } }\n@media (prefers-contrast: more), (-kryon-density: compact) { Text { gap: 9px; } }\n@media (prefers-reduced-motion: reduce) { Text { gap: 11px; } }"
|
|
if Builds(source, "media") {
|
|
Check(test_rules.count == 0, "a native environment without viewport width skips viewport rules")
|
|
}
|
|
android: CssEnvironment = CssEnvironmentWithNames(CssDefaultEnvironment(), "", "", "touch", "touch", "android")
|
|
if BuildIn(source, android) {
|
|
Check(test_rules.count == 2, "android and coarse pointer blocks apply")
|
|
} else {
|
|
Check(false, "android build")
|
|
}
|
|
narrow := CssEnvironmentWithViewportWidth(CssDefaultEnvironment(), 550.0)
|
|
Check(BuildIn(source, narrow) && test_rules.count == 1 && Rule(0).style.gap == 7.0,
|
|
"parsed viewport width matches native rules")
|
|
mixed := "@media (orientation: portrait) and (max-width: 600px), (min-width: 1000px) { Text { gap: 9px; } }"
|
|
Check(BuildIn(mixed, narrow) && test_rules.count == 0, "unsupported viewport features do not match natively")
|
|
Check(BuildIn(mixed, CssEnvironmentWithViewportWidth(narrow, 1200.0)) && test_rules.count == 1,
|
|
"supported alternative matches in a mixed query list")
|
|
bounded := "@media (min-width: 30rem) { @media (max-width: 700px) { Text { gap: 9px; } } }"
|
|
Check(BuildIn(bounded, narrow) && test_rules.count == 1, "nested width constraints intersect")
|
|
Check(BuildIn(bounded, CssEnvironmentWithViewportWidth(narrow, 450.0)) && test_rules.count == 0,
|
|
"nested query enforces outer width")
|
|
compact: CssEnvironment = CssEnvironmentWithNames(CssDefaultEnvironment(), "", "", "compact", "", "")
|
|
Check(BuildIn(source, compact) && test_rules.count == 1, "one branch of a list is enough")
|
|
themed: string = ":root { --ink: #000000; }\n@media (-kryon-theme: sky_dark) { :root { --ink: #0000ff; } }\nText { color: var(--ink); }"
|
|
Check(BuildIn(themed, Env("sky_dark")) && Rule(0).style.foreground == cast(u32)0x0000ffff, "catalog theme")
|
|
Check(BuildIn(themed, Env("ocean_dark")) && Rule(0).style.foreground == cast(u32)0x000000ff, "other theme")
|
|
Check(Env("sky_dark").scheme == cast(s32)CssSchemeDark, "dark theme names set the dark scheme")
|
|
nested: string = "@media (prefers-color-scheme: dark) { @media (-kryon-platform: web) { Text { gap: 1px; } } }"
|
|
web_dark: CssEnvironment = CssEnvironmentWithNames(CssDefaultEnvironment(), "dark", "", "", "", "web")
|
|
Check(BuildIn(nested, web_dark) && test_rules.count == 1, "nested media combine")
|
|
Check(BuildIn(nested, Env("dark")) && test_rules.count == 0, "nested media need both")
|
|
}
|
|
|
|
motion_media_css :: #string CSS
|
|
:root { --duration: 180ms; }
|
|
Button { -kryon-motion-duration: var(--duration); }
|
|
@media (prefers-reduced-motion: no-preference) {
|
|
Button { -kryon-motion-easing: linear; }
|
|
}
|
|
@media (prefers-reduced-motion) {
|
|
@media (prefers-color-scheme: dark), (-kryon-density: compact) {
|
|
:root { --duration: 0ms; }
|
|
Button { -kryon-motion-reduced: reduce; }
|
|
}
|
|
@media (prefers-reduced-motion: no-preference) {
|
|
Button { color: red; }
|
|
}
|
|
}
|
|
@media (prefers-reduced-motion: reduce) and (min-width: 1px) {
|
|
Button { opacity: 0; }
|
|
}
|
|
CSS;
|
|
|
|
MotionMedia :: () {
|
|
env: CssEnvironment = CssDefaultEnvironment()
|
|
if !BuildIn(motion_media_css, env) {
|
|
Check(false, "parse and build reduced-motion sheet")
|
|
return
|
|
}
|
|
facts: StyleFacts = Facts(StyleKindButton(), 0)
|
|
normal: StyleData = Resolve(facts, 1)
|
|
Check(normal.motion_duration == 180.0 && cast(s32)normal.motion_easing == cast(s32)MotionEasingLinear &&
|
|
!normal.motion_reduced, "native default matches no-preference")
|
|
env.reduced_motion = true
|
|
Check(CssSheetBuild(*test_sheet, env, null, *test_rules), "rebuild same sheet with reduce")
|
|
light: StyleData = Resolve(facts, 1)
|
|
Check(light.motion_duration == 180.0 && !light.motion_reduced && !Has(light, cast(u32)StyleMotionEasing),
|
|
"native reduce skips no-preference and still requires the other nested feature")
|
|
env = CssEnvironmentWithNames(env, "dark", "", "", "", "")
|
|
Check(CssSheetBuild(*test_sheet, env, null, *test_rules), "rebuild same sheet with dark and reduce")
|
|
dark: StyleData = Resolve(facts, 1)
|
|
Check(dark.motion_duration == 0.0 && dark.motion_reduced && !Has(dark, cast(u32)StyleForeground) &&
|
|
!Has(dark, cast(u32)StyleOpacity), "native nested boolean reduce cascades duration and policy")
|
|
env.reduced_motion = false
|
|
Check(CssSheetBuild(*test_sheet, env, null, *test_rules) && Resolve(facts, 1).motion_duration == 180.0,
|
|
"switching back re-resolves conditional custom properties")
|
|
}
|
|
|
|
#program_export
|
|
// Every row of the README's property table parses on its target with a
|
|
// plain native parse, which refuses anything only the browser applies.
|
|
Catalog :: () {
|
|
for index: cast(s32)0..CssCatalogCount - 1 {
|
|
entry := CssCatalogEntryAt(index)
|
|
text: [512]u8
|
|
count: s32 = 0
|
|
pieces: [6]string = .["@keyframes pulse { from { opacity: 0.5; } to { opacity: 1; } }\n",
|
|
entry.target, " { ", entry.name, ": ", entry.sample]
|
|
for piece: pieces {
|
|
for byte: piece {
|
|
text[count] = byte
|
|
count += 1
|
|
}
|
|
}
|
|
for byte: "; }" {
|
|
text[count] = byte
|
|
count += 1
|
|
}
|
|
if !Build(TextView(text[:count])) {
|
|
TestSay(" catalog row: ")
|
|
TestSay(entry.name)
|
|
TestSay(": ")
|
|
TestSay(Diagnostic())
|
|
TestSay("\n")
|
|
Check(false, "every catalog row parses natively")
|
|
}
|
|
}
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
Values()
|
|
NativeAuthoring()
|
|
ColorMix()
|
|
Selectors()
|
|
Diagnostics()
|
|
Layers()
|
|
Imports()
|
|
Variables()
|
|
Media()
|
|
MotionMedia()
|
|
Catalog()
|
|
if test_failures == 0 { TestSay("css parse behavior: PASS\n") }
|
|
return test_failures
|
|
}
|