// Sheets as apps use them: a compiled pack is the same table as its CSS // parsed, app sheets layer over a pack, an install either takes whole or // leaves the previous style, and themes switch by environment. #import "file_linux" #import "css_parser" #import "css_export" #import "css_sheet" #import "css_test_support" #import "css_digest" #import "style_pack_classic" #import "style_pack_material" #import "style_pack_lightfield" #import "kryon/style" #import "kryon/style_sheet" #import "kryon/drawing_props" #import "kryon/control_props" using ElevationRole; using FontRole; using MotionEasing; using FontStyle; using LineHeightUnit; using StyleField; using StyleBoxField; parsed_sheet: CssSheet; compiled_sheet: CssSheet; parsed_rules: StyleRules; compiled_rules: StyleRules; pack_source: [262144]u8; ReadPack :: (path: string) -> string { file: FileHandle = OpenRead(path) if !file.valid { return "" } read: s64 = ReadAt(file, pack_source[:], 0) CloseFile(file) if read <= 0 || read >= cast(s64)pack_source.count { return "" } return TextView(pack_source[:cast(s32)read]) } // The committed module must build the table its CSS builds, in both schemes. SamePack :: (path: string, compiled: CssCompiledSheet, label: string) { source: string = ReadPack(path) if source.count == 0 { Check(false, label) return } CssSheetReset(*parsed_sheet) CssSheetReset(*compiled_sheet) if !CssSheetParse(*parsed_sheet, source, path, CssNoImports) { TestSay(" diagnostic: ") TestSay(CssSheetDiagnostic(*parsed_sheet, test_diagnostic[:])) TestSay("\n") Check(false, label) return } Check(CssSheetLoad(*compiled_sheet, compiled), label) for theme: cast(s32)0..1 { env: CssEnvironment = Env(ifx theme == 0 then "light" else "dark") Check(CssSheetBuild(*parsed_sheet, env, null, *parsed_rules), label) Check(CssSheetBuild(*compiled_sheet, env, null, *compiled_rules), label) Check(parsed_rules.count > 0 && parsed_rules.count == compiled_rules.count, label) Check(CssDigestTable(parsed_rules) == CssDigestTable(compiled_rules), label) } accent_parsed: CssTokenResult = CssSheetToken(*parsed_sheet, "--accent") accent_compiled: CssTokenResult = CssSheetToken(*compiled_sheet, "--accent") Check(accent_parsed.found && accent_compiled.found && accent_parsed.color == accent_compiled.color, label) } Packs :: () { SamePack("styles/classic.css", StylePackClassic(), "classic pack matches its CSS") SamePack("styles/material.css", StylePackMaterial(), "material pack matches its CSS") SamePack("styles/lightfield.css", StylePackLightfield(), "lightfield pack matches its CSS") } CssCutPack :: (pack: CssCompiledSheet) -> CssCompiledSheet { cut := pack cut.rules = pack.rules[:pack.rules.count - 1] return cut } // The packs' part metrics stay content offsets now that translate moves // the whole box: the NavigationBar panel, the Modal panel and a Segment's // label padding. PackMetricRule :: (kind: s32, x: float32, y: float32) -> bool { for index: cast(s32)0..compiled_rules.count - 1 { rule := compiled_rules.items[index] if rule.selector.kind != kind || (rule.style.fields & cast(u32)StyleContentOffset) == 0 { continue } if rule.style.offset_x == x && rule.style.offset_y == y { return true } } return false } PackMetrics :: () { CssSheetReset(*compiled_sheet) Check(CssSheetLoad(*compiled_sheet, StylePackClassic()) && CssSheetBuild(*compiled_sheet, CssDefaultEnvironment(), null, *compiled_rules), "classic pack builds") Check(PackMetricRule(StyleKindNavigationBar(), 340.0, 0.0), "the NavigationBar panel stays 340 wide") Check(PackMetricRule(StyleKindModal(), 420.0, 58.0), "the Modal panel stays 420") Check(PackMetricRule(StyleKindSegment(), 20.0, 0.0), "a Segment's label padding stays 20") moved := false for index: cast(s32)0..compiled_rules.count - 1 { if (compiled_rules.items[index].box.fields & cast(u32)StyleBoxTranslate) != 0 { moved = true } } Check(!moved, "no pack metric became a visual translate") } PackedFields :: () { words: [CssRuleWords]u32 words[0] = cast(u32)StyleKindText() words[13] = CssLayerUnlayered words[15] = cast(u32)StyleLineHeight | cast(u32)StyleRowGap | cast(u32)StyleColumnGap | cast(u32)StyleFontWeight | cast(u32)StyleFontStyle words[35] = 0x41c00000 words[42] = 0x40800000 words[43] = 0x41400000 words[44] = 650 words[45] = cast(u32)FontStyleItalic words[46] = cast(u32)LineHeightLength words[59] = cast(u32)StyleBoxMaxWidth | cast(u32)StyleBoxMarginLeft | cast(u32)StyleBoxClipChildren words[61] = 0x44160000 words[63] = 1 words[68] = 0xbf800000 pack: CssCompiledSheet pack.file = "compiled.css" pack.rule_words = 47 pack.rules = words[:47] CssSheetReset(*compiled_sheet) Check(!CssSheetLoad(*compiled_sheet, pack), "a pack in an older rule layout is refused as stale") pack.rule_words = CssRuleWords pack.rules = words[:] CssSheetReset(*compiled_sheet) Check(!CssSheetLoad(*compiled_sheet, pack), "a dense pack without its rule count is refused as stale") packed: [CssRuleMaskWords + CssRuleWords]u32 pack.rules = packed[:CssPackRule(words[:], packed[:])] pack.rule_count = 1 Check(pack.rules.count == CssRuleMaskWords + 13, "a packed rule keeps only its nonzero words") CssSheetReset(*compiled_sheet) Check(!CssSheetLoad(*compiled_sheet, CssCutPack(pack)), "a pack that ends inside a rule is refused as stale") CssSheetReset(*compiled_sheet) Check(CssSheetLoad(*compiled_sheet, pack), "current pack loads") style: StyleData = compiled_sheet.rules[0].rule.style Check(style.row_gap == 4.0 && style.column_gap == 12.0 && style.font_weight == 650 && style.font_style == cast(FontStyle)FontStyleItalic && style.line_height == 24.0 && style.line_height_unit == cast(LineHeightUnit)LineHeightLength, "current pack preserves typed fields") box := compiled_sheet.rules[0].rule.box Check(box.max_width == 600.0 && box.clip_children && box.margin_left == -1.0 && box.fields == (cast(u32)StyleBoxMaxWidth | cast(u32)StyleBoxMarginLeft | cast(u32)StyleBoxClipChildren), "current pack preserves box fields and auto margins") } PackedMotion :: () { words: [CssRuleWords]u32 words[0] = cast(u32)StyleKindText() words[13] = CssLayerUnlayered words[14] = 1 words[15] = cast(u32)StyleForeground words[17] = 0x2076c059 condition: [CssConditionWords]u32 condition[0] = 1 condition[2] = 0x17fff packed: [CssRuleMaskWords + CssRuleWords]u32 pack: CssCompiledSheet pack.file = "compiled-motion.css" pack.rule_words = CssRuleWords pack.rules = packed[:CssPackRule(words[:], packed[:])] pack.rule_count = 1 pack.conditions = condition[:] CssSheetReset(*compiled_sheet) Check(CssSheetLoad(*compiled_sheet, pack), "pack with a motion mask loads") env: CssEnvironment = CssDefaultEnvironment() Check(CssSheetBuild(*compiled_sheet, env, null, *compiled_rules) && compiled_rules.count == 0, "compiled reduce-only rule skips no-preference") env.reduced_motion = true Check(CssSheetBuild(*compiled_sheet, env, null, *compiled_rules) && compiled_rules.count == 1 && compiled_rules.items[0].style.foreground == cast(u32)0x2076c059, "compiled reduce-only rule keeps color") condition[2] = 0x7fff CssSheetReset(*compiled_sheet) Check(CssSheetLoad(*compiled_sheet, pack), "pack without motion bits loads") Check(CssSheetBuild(*compiled_sheet, env, null, *compiled_rules) && compiled_rules.count == 1, "a mask without motion bits matches reduce") env.reduced_motion = false Check(CssSheetBuild(*compiled_sheet, env, null, *compiled_rules) && compiled_rules.count == 1, "a mask without motion bits matches no-preference") } Resolved :: (kind: s32) -> StyleData { base: StyleData return ResolveInstalledStyleRules(base, StyleDefaultFacts(kind), 1) } // An app sheet appended to a pack: its var() reads the pack's custom // properties, and its unlayered rules beat the pack's layered ones. AppOverPack :: () { CssSheetReset(*test_sheet) Check(CssSheetLoad(*test_sheet, StylePackClassic()), "pack loads") app: string = ":root { --brand: #123456; }\nButton { background: var(--brand); color: var(--accent-ink); }" if !CssSheetParse(*test_sheet, app, "app.css", CssNoImports) { TestSay(" diagnostic: ") TestSay(Diagnostic()) TestSay("\n") Check(false, "app sheet parses over the pack") return } Check(CssSheetInstall(*test_sheet, Env("light"), null), "app over pack installs") button: StyleData = Resolved(StyleKindButton()) Check(button.background == cast(u32)0x123456ff, "app rule beats the pack") Check(button.foreground == cast(u32)0xffffffff, "app reads a pack custom property") Check(StyleTokenColorOr("brand", Color.{0, 0, 0, 0}).r == 0x12, "app color custom property installed") } // A sheet that fails to build leaves the installed rules and colors alone. AtomicInstall :: () { CssSheetReset(*test_sheet) good: string = ":root { --ink: #102030; }\nText { color: var(--ink); }" Check(CssSheetParse(*test_sheet, good, "good.css", CssNoImports), "good sheet parses") Check(CssSheetInstall(*test_sheet, CssDefaultEnvironment(), null), "good sheet installs") Check(Resolved(StyleKindText()).foreground == cast(u32)0x102030ff, "good sheet styles text") CssSheetReset(*test_sheet) bad: string = ":root { --ink: #ff0000; }\nText { color: var(--missing); }" Check(CssSheetParse(*test_sheet, bad, "bad.css", CssNoImports), "bad sheet parses") Check(!CssSheetInstall(*test_sheet, CssDefaultEnvironment(), null), "bad sheet refuses to install") Check(Contains(Diagnostic(), "undefined custom property '--missing' at bad.css:2"), "bad sheet says why") Check(Resolved(StyleKindText()).foreground == cast(u32)0x102030ff, "old rules stay") Check(StyleTokenColorOr("ink", Color.{0, 0, 0, 0}).r == 0x10, "old colors stay") // Too many color custom properties fails before anything changes. CssSheetReset(*test_sheet) many: [8192]u8 count: s32 = 0 head: string = ":root {" for byte: head { many[count] = byte count += 1 } for index: cast(s32)0..129 { line: string = " --c000: #000000;" for byte: line { many[count] = byte count += 1 } many[count - 13] = cast(u8)(48 + index / 100) many[count - 12] = cast(u8)(48 + (index / 10) % 10) many[count - 11] = cast(u8)(48 + index % 10) } many[count] = #char "}" count += 1 Check(CssSheetParse(*test_sheet, TextView(many[:count]), "many.css", CssNoImports), "many colors parse") Check(!CssSheetInstall(*test_sheet, CssDefaultEnvironment(), null), "too many colors refuse") Check(Contains(Diagnostic(), "more than 128 color custom properties"), "too many colors say why") Check(Resolved(StyleKindText()).foreground == cast(u32)0x102030ff, "rules stay after a full color table") } Themes :: () { source: string = ":root { --ink: #111111; }\n@media (prefers-color-scheme: dark) { :root { --ink: #eeeeee; } }\nText { color: var(--ink); }" CssSheetReset(*test_sheet) Check(CssSheetParse(*test_sheet, source, "theme.css", CssNoImports), "theme sheet parses") Check(CssSheetInstall(*test_sheet, Env("light"), null), "light installs") Check(Resolved(StyleKindText()).foreground == cast(u32)0x111111ff, "light value") Check(CssSheetInstall(*test_sheet, Env("dark"), null), "dark installs") Check(Resolved(StyleKindText()).foreground == cast(u32)0xeeeeeeff, "dark value") // A host override beats every definition. overrides: CssOverrides CssOverrideColor(*overrides, "--ink", cast(u32)0x336699ff) Check(CssSheetInstall(*test_sheet, Env("dark"), *overrides), "override installs") Check(Resolved(StyleKindText()).foreground == cast(u32)0x336699ff, "override value") } TypedCustomProperties :: () { source: string = ":root { --height: 44px; --leading: 1.3; --duration: 120ms; --multiplier: 1.5; --role: body; --elevation: floating; --easing: linear; --reduced: reduce; }\nCard { min-height: var(--height); line-height: var(--leading); -kryon-motion-duration: var(--duration); -kryon-motion-multiplier: var(--multiplier); -kryon-font-role: var(--role); -kryon-elevation: var(--elevation); -kryon-motion-easing: var(--easing); -kryon-motion-reduced: var(--reduced); }" CssSheetReset(*test_sheet) Check(CssSheetParse(*test_sheet, source, "typed.css", CssNoImports), "typed custom properties parse") Check(CssSheetBuild(*test_sheet, CssDefaultEnvironment(), null, *test_rules), "typed custom properties build") if test_rules.count == 1 { style: StyleData = test_rules.items[0].style Check(style.min_height == 44.0 && style.line_height == 1.3, "custom geometry") Check(style.motion_duration == 120.0 && style.motion_multiplier == 1.5, "custom motion numbers") Check(cast(s32)style.font_role == cast(s32)FontRoleBody && cast(s32)style.elevation == cast(s32)ElevationRoleFloating && cast(s32)style.motion_easing == cast(s32)MotionEasingLinear && style.motion_reduced, "custom motion and role keywords") } BuildFails(":root { --leading: -1; }\nText { line-height: var(--leading); }", "has an invalid value", "negative custom line height refuses") BuildFails(":root { --elevation: floating; }\nText { -kryon-font-role: var(--elevation); }", "takes another kind", "typed custom properties cannot change kind") } layout_and_typography_css :: #string CSS :root { --gutter: 0.75rem; --leading: 125%; --tight: 1.5rem; --weight: bold; --slant: italic; --normal: normal; } @layer base { Row, Column { gap: 4px var(--gutter); padding: 2px 6px; } Row.compact { row-gap: 20px; column-gap: 30px; } } Row { gap: 7px 8px; row-gap: 3px; } Row.compact { column-gap: 0; } Column { column-gap: 18px; gap: 6px var(--gutter); } Button { gap: 5px; } Text { font-weight: var(--weight); font-style: var(--slant); letter-spacing: -0.5px; line-height: var(--leading); } Text.compact { font-weight: 612.6; font-style: oblique; letter-spacing: var(--normal); line-height: var(--tight); } Text#reset { font-weight: var(--normal); font-style: normal; line-height: var(--normal); } Text#zero { font-weight: var(--missing, 1); line-height: var(--missing, 0); } CSS; LayoutAndTypography :: () { CssSheetReset(*test_sheet) Check(CssSheetParse(*test_sheet, layout_and_typography_css, "layout-and-type.css", CssNoImports), "layout and typography parse") Check(CssSheetInstall(*test_sheet, CssDefaultEnvironment(), null), "layout and typography install") empty: StyleData row: StyleFacts = StyleDefaultFacts(StyleKindRow()) row.class_name = StyleClass("compact") style: StyleData = ResolveInstalledStyleRules(empty, row, 1) Check(style.row_gap == 3.0 && style.column_gap == 0.0 && style.padding_x == 6.0 && style.padding_y == 2.0, "row axes cascade independently") style = Resolved(StyleKindColumn()) Check(style.row_gap == 6.0 && style.column_gap == 12.0, "column shorthand resets earlier longhand") Check(Resolved(StyleKindButton()).gap == 5.0, "uniform control gap") text: StyleFacts = StyleDefaultFacts(StyleKindText()) style = ResolveInstalledStyleRules(empty, text, 1) Check(style.font_weight == 700 && style.font_style == cast(FontStyle)FontStyleItalic && style.letter_spacing == -0.5 && style.line_height == 1.25 && style.line_height_unit == cast(LineHeightUnit)LineHeightFactor, "text custom properties and signed tracking") text.class_name = StyleClass("compact") style = ResolveInstalledStyleRules(empty, text, 1) Check(style.font_weight == 613 && style.font_style == cast(FontStyle)FontStyleOblique && style.letter_spacing == 0.0 && style.line_height == 24.0 && style.line_height_unit == cast(LineHeightUnit)LineHeightLength, "text length leading and weight rounding") text.id = StyleId("reset") style = ResolveInstalledStyleRules(empty, text, 1) Check(style.font_weight == 400 && style.font_style == cast(FontStyle)FontStyleNormal && style.line_height == 0.0 && style.line_height_unit == cast(LineHeightUnit)LineHeightNormal, "higher-specificity normal resets text metrics") text.id = StyleId("zero") style = ResolveInstalledStyleRules(empty, text, 1) Check(style.font_weight == 1 && style.line_height == 0.0 && style.line_height_unit == cast(LineHeightUnit)LineHeightFactor, "fallback preserves explicit zero factor") BuildFails(":root { --weight: 400%; }\nText { font-weight: var(--weight); }", "takes another kind", "percentage does not become a weight") BuildFails(":root { --weight: 1001; }\nText { font-weight: var(--weight); }", "has an invalid value", "custom weight range") BuildFails(":root { --gap: -1px; }\nColumn { row-gap: var(--gap); }", "has an invalid value", "custom gap range") } DiagnosticFixturePath :: "tests/css_diagnostics.css" DiagnosticExpectation :: struct { line: s32 column: s32 kind: CssIssueKind name: string web_only: bool } DiagnosticIssueCount :: 14 ObserveIssue :: (context: *void, issue: CssIssue) { diagnostic_expectations: [DiagnosticIssueCount]DiagnosticExpectation = .[ .{4, 3, CssIssueKind.Declaration, "gap", false}, .{5, 3, CssIssueKind.Declaration, "text-transform", true}, .{6, 3, CssIssueKind.Declaration, "colour", false}, .{7, 3, CssIssueKind.Declaration, "broken-name", false}, .{8, 3, CssIssueKind.Selector, "& > Text", false}, .{10, 3, CssIssueKind.Declaration, "opacity", false}, .{13, 1, CssIssueKind.Selector, "Row.fixture > Text", false}, .{14, 1, CssIssueKind.AtRule, "@supports", false}, .{15, 1, CssIssueKind.AtRule, "@media", true}, .{16, 8, CssIssueKind.Declaration, "width", true}, .{16, 48, CssIssueKind.Declaration, "height", true}, .{17, 41, CssIssueKind.Declaration, "text-transform", true}, .{18, 8, CssIssueKind.Declaration, "font-family", true}, .{19, 38, CssIssueKind.Declaration, "border-left", true}] count: *s32 = cast(*s32)context index: s32 = <= DiagnosticIssueCount { Check(false, "unexpected source diagnostic") return } expect: DiagnosticExpectation = diagnostic_expectations[index] Check(issue.path == DiagnosticFixturePath && issue.line == expect.line && issue.column == expect.column && issue.kind == expect.kind && issue.name == expect.name && issue.web_only == expect.web_only && issue.reason.count > 0, "issue includes source, category, name and reason") } ImportedDiagnostics :: (name: string) -> CssImportAnswer { if name == "nested.css" { return .{true, "Text { text-transform: none; }"} } return .{} } ObserveImportIssue :: (context: *void, issue: CssIssue) { count: *s32 = cast(*s32)context if < s32 { TransformOptIn() Packs() PackedFields() PackMetrics() PackedMotion() AppOverPack() AtomicInstall() Themes() TypedCustomProperties() LayoutAndTypography() DiagnosticRecovery() Capacity() if test_failures == 0 { TestSay("css sheet behavior: PASS\n") } return test_failures }