Files
css/tests/style_parse_behavior.zi
T
waozi 95387b23f5 Parse, format, and install KSS style sheets as a package over Kryon
KSS moves here from Kryon, with the classic, lightfield, and material
style packs, the pack module generator, and the parser tests and
sanitized fuzzing. Kryon keeps the rule table and style resolution;
apps that read .kss text add this package and import kss/Kss or
kss/SystemStyle.
2026-09-29 20:56:55 -03:00

109 lines
4.3 KiB
Ziran

#import "kryon/drawing_props"
#import "kryon/style"
#import "kryon/style_sheet"
#import "kss_parser"
#import "style_parse"
using KssStatus;
InstallBodyTypeface :: (bytes: []u8) -> bool {
rules: StyleRules
rules.count = 1
rules.items[0].style.typeface = TextView(bytes[:])
return InstallStyleRules(rules)
}
StyleRulesAtCapacity :: (count: s32) -> StyleRules {
result: StyleRules
if count >= 0 && count <= 320 { result.count = count }
return result
}
// A rule with this background; border, width, and radius when nonzero.
HasRule :: (rules: StyleRules, background: u32, border: u32,
border_width: float32, radius: float32) -> bool {
index: s32 = 0
while index < rules.count {
style: StyleData = rules.items[index].style
if style.background == background &&
(border == cast(u32)0 || style.border == border) &&
(border_width == 0.0 || style.border_width == border_width) &&
(radius == 0.0 || style.radius == radius) { return true }
index += 1
}
return false
}
// Parses sheets with imports, color overrides, and full rule tables, then
// installs them into Kryon's style rules.
#program_export
Answer :: () -> s32 {
parsed: StyleRulesParse = BeginStyleRules(
"@pack demo; @import <base>; Progress[role=Track] { background: #112233; border: #99aabb; border-width: 2; radius: 5; }",
"demo.kss", KssDefaultEnvironment())
parsed = ParseStyleRules(parsed)
if parsed.parser.status != cast(s32)KssStatusNeedImport ||
parsed.rules.count != 0 { return 0 }
parsed = ProvideStyleRulesImport(parsed,
"Progress[role=Fill] { background: #556677; }", "base")
parsed = ParseStyleRules(parsed)
if parsed.parser.status != cast(s32)KssStatusDone ||
parsed.overflow || parsed.rules.count != 2 { return 0 }
if !InstallParsedStyleRules(parsed) { return 0 }
// The sheet's own rule and the imported one are both installed.
installed_pair: StyleRules = ActiveStyleRules()
if installed_pair.count != 2 ||
!HasRule(installed_pair, cast(u32)0x112233ff, cast(u32)0x99aabbff, 2.0, 5.0) ||
!HasRule(installed_pair, cast(u32)0x556677ff, cast(u32)0, 0.0, 0.0) {
return 0
}
if !KssCSSNeedsPixels("width") { return 0 }
parsed = BeginStyleRules(
"@pack colors; tokens { color { canvas: #112233; } }",
"colors.kss", KssDefaultEnvironment())
parsed.parser = KssAddColorOverride(parsed.parser, "canvas",
cast(u32)0xaabbccdd)
parsed = ParseStyleRules(parsed)
if !InstallParsedStyleRules(parsed) { return 0 }
color: Color = StyleTokenColor("canvas")
if color.r != 0xaa || color.g != 0xbb || color.b != 0xcc ||
color.a != 0xdd { return 0 }
color = StyleTokenColorOr("missing", Color.{1, 2, 3, 4})
if color.r != 1 || color.g != 2 || color.b != 3 || color.a != 4 {
return 0
}
parsed = BeginStyleRules(
"@pack replacement; tokens { color { text: #445566; } }",
"replacement.kss", KssDefaultEnvironment())
parsed = ParseStyleRules(parsed)
if !InstallParsedStyleRules(parsed) { return 0 }
color = StyleTokenColor("canvas")
if color.r != 0 || color.g != 0 || color.b != 0 || color.a != 255 {
return 0
}
full_rules: StyleRules = StyleRulesAtCapacity(320)
parsed = BeginStyleRules("Progress { background: #112233; }",
"full.kss", KssDefaultEnvironment())
parsed.rules = full_rules
parsed = StepStyleRules(parsed)
if !parsed.overflow || parsed.rules.count != 320 { return 0 }
parsed = BeginStyleRules("@import <missing>;", "missing.kss",
KssDefaultEnvironment())
parsed = ParseStyleRules(parsed)
if parsed.parser.status != cast(s32)KssStatusNeedImport { return 0 }
parsed = FailStyleRulesImport(parsed)
if parsed.parser.status != cast(s32)KssStatusError { return 0 }
bytes: [4]u8 = .[98, 111, 100, 121]
if !InstallBodyTypeface(bytes[:]) { return 0 }
installed: StyleRules = ActiveStyleRules()
if installed.items[0].style.typeface != "body" { return 0 }
bytes[0] = cast(u8)120
if installed.items[0].style.typeface != "body" { return 0 }
owned_rules: StyleRules
owned_rules.count = 321
if InstallStyleRules(owned_rules) ||
ActiveStyleRules().items[0].style.typeface != "body" { return 0 }
return 42
}