Report recoverable and browser-only native CSS issues

This commit is contained in:
waozi committed 2026-11-22 15:55:25 -03:00
1 parent e2d73fcdd4
commit e87bd75a45
13 files changed
+492 -29

No files matched your search

+42 -3
View File
@@ -28,9 +28,8 @@ using LineHeightUnit;
// rule lowered to Kryon's typed style table, each :root custom property and
// the media condition both apply under. var() references stay symbolic until
// the sheet installs, so a theme switch only swaps custom property values.
// Anything outside the subset is a diagnostic with file, line and column;
// a sheet never installs partly. Hosts own I/O: they feed source text in and
// answer @import requests.
// The default parser rejects unsupported syntax. Hosts can opt into recovery
// with source diagnostics. Hosts own I/O and answer @import requests.
CssStatus :: enum {
CssStatusContinue :: 0
@@ -140,6 +139,34 @@ CssName :: struct {
length: s32
}
CssIssueKind :: enum {
Stylesheet
Declaration
Selector
AtRule
}
// Borrowed text is valid only during the callback. Positions are one-based
// byte columns, like the original parser diagnostics.
CssIssue :: struct {
path: string
line: s32
column: s32
kind: CssIssueKind
name: string
reason: string
web_only: bool
}
CssIssueSink :: #type (context: *void, issue: CssIssue) -> void;
CssDiagnostics :: struct {
report: CssIssueSink
context: *void
// Fail the sheet after collecting all issues, including browser-only CSS.
strict: bool
}
CssCursor :: struct {
source: string
pos: s32
@@ -375,6 +402,13 @@ CssParser :: struct {
pending_end: CssCursor
diagnostic: [256]u8
diagnostic_length: s32
diagnostic_reason_length: s32
diagnostic_cursor: CssCursor
diagnostics: CssDiagnostics
collect_issues: bool
issue_count: s32
report_path: string
statement_origin: CssCursor
rule_total: s32
}
@@ -480,6 +514,11 @@ CssBegin :: (p: *CssParser, source: string, path: string) {
p.declaration_count = 0
p.web_declarations = 0
p.diagnostic_length = 0
p.diagnostic_reason_length = 0
p.diagnostics = CssDiagnostics.{}
p.collect_issues = false
p.issue_count = 0
p.report_path = ""
p.rule_total = 0
}
+35 -1
View File
@@ -392,24 +392,58 @@ css_sheet_parser: CssParser;
#program_export
CssSheetParse :: (sheet: *CssSheet, source: string, path: string, resolve: CssImportResolver) -> bool {
return CssSheetParseSource(sheet, source, path, resolve, CssDiagnostics.{}, false)
}
// Collect source issues through a caller-supplied sink. In lenient mode a
// recoverable rejected item is skipped and the supported rules remain usable.
// A strict native check still scans the whole source before it fails.
#program_export
CssSheetParseWithDiagnostics :: (sheet: *CssSheet, source: string, path: string,
resolve: CssImportResolver, diagnostics: CssDiagnostics) -> bool {
return CssSheetParseSource(sheet, source, path, resolve, diagnostics, true)
}
CssSheetParseSource :: (sheet: *CssSheet, source: string, path: string, resolve: CssImportResolver,
diagnostics: CssDiagnostics, collect: bool) -> bool {
if sheet.failed { return false }
p: *CssParser = *css_sheet_parser
CssBegin(p, source, path)
p.diagnostics = diagnostics
p.collect_issues = collect
p.report_path = path
while true {
status: s32 = CssStep(p)
if status == cast(s32)CssStatusRule || status == cast(s32)CssStatusProperty ||
status == cast(s32)CssStatusKeyframes {
CssSheetTake(sheet, p)
if sheet.failed { return false }
if sheet.failed {
if CssCollectIssues(p) {
CssReportIssue(p, p.cursor, CssIssueKind.Stylesheet, "",
TextView(sheet.diagnostic[:sheet.diagnostic_length]), false)
}
return false
}
} else if status == cast(s32)CssStatusNeedImport {
target: [64]u8
name: string = CssPendingImport(p, target[:])
answer: CssImportAnswer = resolve(name)
if answer.found { CssProvideImport(p, answer.source, name) }
else { CssFailImport(p) }
if CssFailed(p) && CssCollectIssues(p) {
CssReportFailure(p, p.statement_origin, CssIssueKind.AtRule, "@import")
CssClearFailure(p)
}
} else if status == cast(s32)CssStatusDone {
if diagnostics.strict && p.issue_count > 0 {
CssSheetFail(sheet, "stylesheet has native CSS diagnostics")
return false
}
return true
} else if status == cast(s32)CssStatusError {
if CssCollectIssues(p) {
CssReportFailure(p, p.diagnostic_cursor, CssIssueKind.Stylesheet, "")
}
sheet.failed = true
sheet.diagnostic_length = 0
for index: cast(s32)0..p.diagnostic_length - 1 {
+115
View File
@@ -252,6 +252,8 @@ CssDiagnosticAppendInt :: (p: *CssParser, value: s32) {
}
CssDiagnosticAt :: (p: *CssParser) {
p.diagnostic_reason_length = p.diagnostic_length
p.diagnostic_cursor = p.cursor
CssDiagnosticAppend(p, " at ")
file: s32 = p.cursor.file
if file >= 0 && file < p.file_count && p.files[file].length > 0 {
@@ -284,6 +286,119 @@ CssFailName :: (p: *CssParser, message: string, name: CssName, suffix: string) {
p.status = cast(s32)CssStatusError
}
CssCollectIssues :: (p: *CssParser) -> bool {
return p.collect_issues
}
CssReportIssue :: (p: *CssParser, origin: CssCursor, kind: CssIssueKind,
name: string, reason: string, web_only: bool) {
p.issue_count += 1
if p.diagnostics.report == null { return }
path: string = p.report_path
if origin.file > 0 && origin.file < p.file_count {
path = TextView(p.files[origin.file].name[:p.files[origin.file].length])
}
p.diagnostics.report(p.diagnostics.context,
CssIssue.{path, origin.line, origin.column, kind, name, reason, web_only})
}
CssReportFailure :: (p: *CssParser, origin: CssCursor, kind: CssIssueKind, name: string, web_only: bool = false) {
reason: string = "value has no native CSS mapping"
if CssFailed(p) && p.diagnostic_reason_length > 0 {
reason = TextView(p.diagnostic[:p.diagnostic_reason_length])
}
CssReportIssue(p, origin, kind, name, reason, web_only)
}
CssClearFailure :: (p: *CssParser) {
p.status = cast(s32)CssStatusContinue
p.diagnostic_length = 0
}
// Scan a rejected statement without interpreting its contents. Delimiters
// inside strings, comments, functions, and nested blocks belong to that item.
// A containing '}' stays for the caller. Return the end of the item header
// so the diagnostic can display an unsupported selector or nested rule.
CssSkipRejected :: (p: *CssParser) -> s32 {
quote: u8 = 0
nesting: s32 = 0
braces: s32 = 0
head_end: s32 = -1
separator: CssCursor = p.cursor
has_separator: bool = false
while !CssAtEnd(p) {
byte: u8 = CssPeek(p, 0)
if quote != 0 {
if byte == #char "\\" { CssAdvance(p, 2); continue }
if byte == quote || byte == 10 || byte == 13 { quote = 0 }
} else if byte == #char "\"" || byte == #char "'" {
quote = byte
} else if byte == #char "\\" {
CssAdvance(p, 2)
continue
} else if byte == #char "/" && CssPeek(p, 1) == #char "*" {
CssAdvance(p, 2)
while !CssAtEnd(p) && !(CssPeek(p, 0) == #char "*" && CssPeek(p, 1) == #char "/") {
CssAdvance(p, 1)
}
CssAdvance(p, 2)
continue
} else if byte == #char "(" || byte == #char "[" {
nesting += 1
} else if byte == #char ")" || byte == #char "]" {
if nesting > 0 { nesting -= 1 }
if nesting == 0 { has_separator = false }
} else if byte == #char "{" && (nesting == 0 || braces > 0) {
if braces == 0 { head_end = p.cursor.pos }
braces += 1
} else if byte == #char "}" {
if braces == 0 { break }
braces -= 1
if braces == 0 {
CssAdvance(p, 1)
return head_end
}
} else if byte == #char ";" && braces == 0 {
if nesting == 0 {
head_end = p.cursor.pos
CssAdvance(p, 1)
return head_end
}
if !has_separator {
separator = p.cursor
has_separator = true
}
}
CssAdvance(p, 1)
}
if braces == 0 && has_separator {
p.cursor = separator
CssAdvance(p, 1)
return separator.pos
}
return ifx head_end >= 0 then head_end else p.cursor.pos
}
CssRecoverItem :: (p: *CssParser, origin: CssCursor, kind: CssIssueKind, name: string = "") -> bool {
if !CssCollectIssues(p) { return false }
p.cursor = origin
end: s32 = CssSkipRejected(p)
label: string = name
if label.count == 0 {
if kind == CssIssueKind.AtRule || kind == CssIssueKind.Declaration {
end = origin.pos
if kind == CssIssueKind.AtRule { end += 1 }
while end < cast(s32)origin.source.count && CssNameChar(origin.source[end]) { end += 1 }
}
while end > origin.pos && CssIsSpace(origin.source[end - 1]) { end -= 1 }
label = origin.source[origin.pos:end]
}
CssReportFailure(p, origin, kind, label)
if p.cursor.pos == origin.pos && !CssAtEnd(p) { CssAdvance(p, 1) }
CssClearFailure(p)
return true
}
// The diagnostic, copied into `output`.
#program_export
CssDiagnostic :: (p: *CssParser, output: []u8) -> string {
+15 -2
View File
@@ -272,6 +272,7 @@ CssReadMotionProperty :: (p: *CssParser, name: CssName, end: s32) -> bool {
animation = text.count > 10 && CssNameEquals(CssMakeName(text[:10]), "animation-")
}
if !transition && !animation { return false }
unsupported: bool = false
if transition {
value: CssTransition = p.motion.transition
ok: bool
@@ -279,6 +280,7 @@ CssReadMotionProperty :: (p: *CssParser, name: CssName, end: s32) -> bool {
else { ok = CssTransitionLonghand(p, name, end, *value) }
if !ok || CssFailed(p) { value = CssTransition.{.set = true, .unsupported = true} }
p.motion.transition = value
unsupported = !ok || CssFailed(p)
} else {
value: CssAnimation = p.motion.animation
ok: bool
@@ -286,9 +288,12 @@ CssReadMotionProperty :: (p: *CssParser, name: CssName, end: s32) -> bool {
else { ok = CssAnimationLonghand(p, name, end, *value) }
if !ok || CssFailed(p) { value = CssAnimation.{.set = true, .unsupported = true} }
p.motion.animation = value
unsupported = !ok || CssFailed(p)
}
p.status = cast(s32)CssStatusContinue
p.diagnostic_length = 0
if unsupported && CssCollectIssues(p) {
CssReportFailure(p, p.pending_name_cursor, CssIssueKind.Declaration, text, true)
}
CssClearFailure(p)
return true
}
@@ -309,6 +314,10 @@ CssReadKeyframeStyle :: (p: *CssParser, track: *CssParsedKeyframes) -> StyleData
head: CssDeclarationResult = CssReadDeclarationHead(p)
if !head.ok { break }
if CssAnimatedProperty(head.name) == 0 {
if CssCollectIssues(p) {
CssReportIssue(p, p.pending_name_cursor, CssIssueKind.Declaration, TextView(head.name.bytes[:head.name.length]),
"property cannot be animated by native keyframes", true)
}
track.unsupported = true
} else {
previous: StyleData = p.template
@@ -319,6 +328,10 @@ CssReadKeyframeStyle :: (p: *CssParser, track: *CssParsedKeyframes) -> StyleData
(p.template.fields & ~CssAnimatedFields()) == 0 {
p.template = MergeValues(previous, p.template)
} else {
if CssCollectIssues(p) {
CssReportFailure(p, p.pending_name_cursor, CssIssueKind.Declaration,
TextView(head.name.bytes[:head.name.length]), true)
}
p.template = previous
track.unsupported = true
p.rule_ref_count = 0
+22
View File
@@ -391,6 +391,13 @@ CssReadTypeface :: (p: *CssParser, end: s32) {
CssFail(p, "expected a font family")
return
}
if CssCollectIssues(p) {
CssSkipSpace(p)
if CssAtByte(p, #char ",") {
CssReportIssue(p, p.pending_name_cursor, CssIssueKind.Declaration, "font-family",
"native text uses only the first font family", true)
}
}
p.template.typeface = p.cursor.source[start:start + length]
p.template.fields |= cast(u32)StyleTypeface
p.cursor.column += end - p.cursor.pos
@@ -489,6 +496,19 @@ CssReadGridTracks :: (p: *CssParser, end: s32) -> (tracks: s32, native: bool) {
return ifx auto_fit then -minimum else columns, !CssMoreValue(p, end)
}
CssCheckLayoutTarget :: (p: *CssParser, name: string, grid_only: bool) {
if !CssCollectIssues(p) { return }
for index: cast(s32)0..p.selector_count - 1 {
kind: s32 = p.selectors[index].selector.kind
if kind == StyleKindAny() || kind == StyleKindGrid() { continue }
if !grid_only && (kind == StyleKindRow() || kind == StyleKindColumn()) { continue }
reason: string = "explicit sizes apply only to native Row, Column and Grid"
if grid_only { reason = "grid tracks apply only to native Grid" }
CssReportIssue(p, p.pending_name_cursor, CssIssueKind.Declaration, name, reason, true)
return
}
}
CssPassWebDeclaration :: (p: *CssParser) -> s32 {
p.cursor = p.pending_end
p.web_declarations += 1
@@ -634,6 +654,7 @@ CssApplyProperty :: (p: *CssParser, name: CssName, end: s32) -> s32 {
} else if CssNameEquals(name, "width") || CssNameEquals(name, "height") {
extent, native := CssReadNativeSize(p, end)
if !native { return CssPassWebDeclaration(p) }
CssCheckLayoutTarget(p, TextView(name.bytes[:name.length]), false)
if CssNameEquals(name, "width") {
p.template.fields |= cast(u32)StyleWidth
p.template.width = extent
@@ -650,6 +671,7 @@ CssApplyProperty :: (p: *CssParser, name: CssName, end: s32) -> s32 {
p.diagnostic_length = 0
return CssPassWebDeclaration(p)
}
CssCheckLayoutTarget(p, "grid-template-columns", true)
p.template.fields |= cast(u32)StyleGridTracks
p.template.grid_tracks = tracks
return CssScopeNative
+74 -11
View File
@@ -99,6 +99,16 @@ CssReadMedia :: (p: *CssParser) {
while length > 0 && CssIsSpace(p.cursor.source[start + length - 1]) { length -= 1 }
combined: CssCondition = CssConditionAnd(p, CssCurrentCondition(p), condition)
if CssFailed(p) { return }
if CssCollectIssues(p) {
for index: cast(s32)0..condition.count - 1 {
branch: *CssBranch = *condition.branches[index]
if branch.viewport_count != branch.width_count {
CssReportIssue(p, p.statement_origin, CssIssueKind.AtRule, "@media",
"media condition is evaluated only by the browser", true)
break
}
}
}
CssAdvance(p, 1)
p.imports_open = false
unused CssOpenBlock(p, CssBlockMedia, combined, start, length)
@@ -230,9 +240,11 @@ CssReadAtRule :: (p: *CssParser) -> bool {
p.cursor = body
p.cursor.source = body.source[:end.pos]
p.keyframes = CssReadKeyframes(p, name)
if CssCollectIssues(p) && p.keyframes.unsupported && p.keyframes.count == 0 {
CssReportFailure(p, p.statement_origin, CssIssueKind.AtRule, "@keyframes", true)
}
p.cursor = end
p.status = cast(s32)CssStatusContinue
p.diagnostic_length = 0
CssClearFailure(p)
p.layer_of_rule = p.layer
p.raw_start = start
p.raw_length = p.cursor.pos - start
@@ -260,6 +272,7 @@ CssReadRuleBody :: (p: *CssParser) -> bool {
p.declaration_count = 0
p.web_declarations = 0
p.rule_span.body_start = p.cursor.pos
collect: bool = CssCollectIssues(p)
while true {
CssSkipSpace(p)
if CssFailed(p) { return false }
@@ -276,19 +289,50 @@ CssReadRuleBody :: (p: *CssParser) -> bool {
CssAdvance(p, 1)
continue
}
origin: CssCursor = p.cursor
if CssAtByte(p, #char "{") || CssAtByte(p, #char "@") || CssAtByte(p, #char "&") ||
CssAtByte(p, #char ".") || CssAtByte(p, #char "#") || CssAtByte(p, #char "[") ||
CssAtByte(p, #char "*") || CssAtByte(p, #char ">") || CssAtByte(p, #char ":") {
CssFail(p, "nested rules are outside Kryon CSS")
kind: CssIssueKind = ifx CssAtByte(p, #char "@") then CssIssueKind.AtRule else CssIssueKind.Selector
if CssRecoverItem(p, origin, kind) { continue }
return false
}
head: CssDeclarationResult = CssReadDeclarationHead(p)
if !head.ok { return false }
if !head.ok {
if CssRecoverItem(p, origin, CssIssueKind.Declaration) { continue }
return false
}
previous: StyleData
motion: CssRuleMotion
refs: [24]CssLocalRef
ref_count: s32 = p.rule_ref_count
name_count: s32 = p.name_count
web_count: s32 = p.web_declarations
if collect {
previous = p.template
motion = p.motion
for index: cast(s32)0..ref_count - 1 { refs[index] = p.rule_refs[index] }
}
scope: s32 = CssApplyProperty(p, head.name, head.end)
if scope < 0 { return false }
CssRecordDeclaration(p, p.pending_name_start, p.pending_name_length,
p.pending_value_start, p.pending_value_length, scope)
if CssFailed(p) { return false }
if scope >= 0 {
CssRecordDeclaration(p, p.pending_name_start, p.pending_name_length,
p.pending_value_start, p.pending_value_length, scope)
}
if scope < 0 || CssFailed(p) {
if !collect { return false }
CssReportFailure(p, origin, CssIssueKind.Declaration, TextView(head.name.bytes[:head.name.length]))
p.template = previous
p.motion = motion
p.rule_ref_count = ref_count
p.name_count = name_count
p.web_declarations = web_count
for index: cast(s32)0..ref_count - 1 { p.rule_refs[index] = refs[index] }
CssClearFailure(p)
} else if collect && scope == CssScopeWeb {
CssReportIssue(p, origin, CssIssueKind.Declaration, TextView(head.name.bytes[:head.name.length]),
"declaration is applied only by the browser", true)
}
CssFinishDeclaration(p)
}
return false
@@ -371,6 +415,7 @@ CssStep :: (p: *CssParser) -> s32 {
p.status = cast(s32)CssStatusDone
return p.status
}
origin: CssCursor = p.cursor
byte: u8 = p.cursor.source[p.cursor.pos]
if CssInRoot(p) {
if byte == #char "}" {
@@ -383,12 +428,13 @@ CssStep :: (p: *CssParser) -> s32 {
continue
}
if CssReadDefinitionItem(p) { return p.status }
if CssFailed(p) { return p.status }
if CssFailed(p) && !CssRecoverItem(p, origin, CssIssueKind.Declaration) { return p.status }
continue
}
if byte == #char "}" {
if p.depth == 0 {
CssFail(p, "unexpected '}'")
if CssRecoverItem(p, origin, CssIssueKind.Stylesheet, "}") { continue }
return p.status
}
CssAdvance(p, 1)
@@ -397,11 +443,19 @@ CssStep :: (p: *CssParser) -> s32 {
}
if byte == #char ";" {
CssFail(p, "unexpected ';'")
if CssRecoverItem(p, origin, CssIssueKind.Stylesheet, ";") { continue }
return p.status
}
if byte == #char "@" {
p.statement_origin = origin
layers: s32 = p.layer_count
themes: s32 = p.theme_count
if CssReadAtRule(p) { return p.status }
if CssFailed(p) { return p.status }
if CssFailed(p) {
if !CssRecoverItem(p, origin, CssIssueKind.AtRule) { return p.status }
p.layer_count = layers
p.theme_count = themes
}
continue
}
p.imports_open = false
@@ -411,12 +465,21 @@ CssStep :: (p: *CssParser) -> s32 {
if CssFailed(p) { return p.status }
continue
}
if CssFailed(p) { return p.status }
if CssFailed(p) {
if CssRecoverItem(p, origin, CssIssueKind.Selector) { continue }
return p.status
}
p.origin.file = p.cursor.file
p.origin.line = p.cursor.line
p.origin.column = p.cursor.column
p.rule_span.file = p.cursor.file
if !CssReadSelectorList(p) { return p.status }
if !CssReadSelectorList(p) {
if CssRecoverItem(p, origin, CssIssueKind.Selector) {
p.selector_count = 0
continue
}
return p.status
}
p.rule_span.selector_start = p.selector_start
p.rule_span.selector_length = p.selector_length
CssAdvance(p, 1)