diff --git a/CHANGELOG.md b/CHANGELOG.md index 625d35f..29b9391 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Add recoverable native stylesheet diagnostics and `css_tool check --strict` + with source locations for rejected and browser-only CSS. + - Resolve native viewport-width media queries and responsive equal-width grid tracks in parsed sheets and compiled packs while preserving browser export. diff --git a/Makefile b/Makefile index f7fbe07..1016ffc 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ STYLE_PACKS := classic material lightfield CSS_TOOL := $(BUILD)/tools/css_tool INCLUDE = -I$$($(ZIRAN) pkg path ziran)/include -.PHONY: check test conformance style-packs style-pack-check format export css-dom-test motion-test fuzz clean +.PHONY: check test conformance style-packs style-pack-check diagnostics-test format export css-dom-test motion-test fuzz clean check: test conformance style-pack-check css-dom-test motion-test fuzz @@ -21,7 +21,7 @@ define native_test $(BUILD)/$(1) endef -test: $(BUILD)/layout-pack/style_pack_layout.zi +test: diagnostics-test $(BUILD)/layout-pack/style_pack_layout.zi $(ZIRAN) check --project src/Css/module.zi $(ZIRAN) check --project src/css_lexer_laws.zi $(call native_test,css_parse_behavior) @@ -53,6 +53,14 @@ $(CSS_TOOL): tools/css_tool.zi src/*.zi src/parser/*.zi $(ZIRAN) build --project --target=c --exe --entry css_tool:main -o $@-c tools/css_tool.zi $(CC) -std=c11 -O2 $(INCLUDE) -I$@-c $@-c/*.c -o $@ -lm +diagnostics-test: $(CSS_TOOL) + $(CSS_TOOL) check --strict styles/classic.css + $(CSS_TOOL) check tests/css_diagnostics.css 2>$(BUILD)/css-diagnostics.log + @if $(CSS_TOOL) check --strict tests/css_diagnostics.css 2>$(BUILD)/css-diagnostics-strict.log; then exit 1; else test $$? -eq 1; fi + cmp $(BUILD)/css-diagnostics.log $(BUILD)/css-diagnostics-strict.log + grep -Fx "tests/css_diagnostics.css:5:3: web-only declaration 'box-shadow': declaration is applied only by the browser" $(BUILD)/css-diagnostics.log + grep -Fx "tests/css_diagnostics.css:14:1: skipped at-rule '@supports': at-rule 'supports' is outside Kryon CSS" $(BUILD)/css-diagnostics.log + # Packs are compiled into rule tables apps load without parsing. The # modules are committed, because apps import this package without running # its Makefile; style-pack-check fails when one no longer matches its CSS. diff --git a/README.md b/README.md index e22bb1c..d2ae7a4 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,26 @@ is installed separately; no style pack is restored automatically. Imports go through a resolver you pass: `(name: string) -> CssImportAnswer`. Hosts without imports pass `CssNoImports`. +## Check styles for native support + +Build the CLI with `make build/tools/css_tool`. Run `build/tools/css_tool check app.css` +to list issues with filename, line, byte column, item name and reason. The command +reports each recoverable issue it encounters, including browser-only declarations +and media conditions. A rejected selector or at-rule skips its entire block. +`build/tools/css_tool check --strict app.css` exits with status 1 if any issue is +reported. Both modes exit with status 1 when the stylesheet cannot be recovered. +The CLI checks the given source and treats imports as empty sheets. + +Apps can use `CssSheetParseWithDiagnostics(sheet, source, path, resolve, options)` +from `css/Css` to recover while parsing. Set `options.report` to a `CssIssueSink` +with signature `(context: *void, issue: CssIssue) -> void`. Each issue supplies +its origin, item kind, name, reason and `web_only` flag. Copy any text you need +after the callback returns. Imported issues use the paths from the host's +resolver. Set `options.strict = true` to collect the same issues and reject the +sheet. The ordinary `CssSheetParse` preserves its fail-fast behavior and browser +export support. `CssSheetBuild` and installation still validate values after +resolving custom properties in the chosen environment. + ## The subset Each selector can combine a widget type (`Button`, `TextField`, `Row`, diff --git a/src/css_parser.zi b/src/css_parser.zi index 2f99aef..da071ab 100644 --- a/src/css_parser.zi +++ b/src/css_parser.zi @@ -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 } diff --git a/src/css_sheet.zi b/src/css_sheet.zi index 678e45c..e7d7542 100644 --- a/src/css_sheet.zi +++ b/src/css_sheet.zi @@ -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 { diff --git a/src/parser/lexer.zi b/src/parser/lexer.zi index d9387c3..aa1a185 100644 --- a/src/parser/lexer.zi +++ b/src/parser/lexer.zi @@ -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 { diff --git a/src/parser/motion.zi b/src/parser/motion.zi index e0f57b0..d4538d4 100644 --- a/src/parser/motion.zi +++ b/src/parser/motion.zi @@ -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 diff --git a/src/parser/properties.zi b/src/parser/properties.zi index 0332b76..9ee8f90 100644 --- a/src/parser/properties.zi +++ b/src/parser/properties.zi @@ -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 diff --git a/src/parser/statements.zi b/src/parser/statements.zi index 267a2a7..0cb35c8 100644 --- a/src/parser/statements.zi +++ b/src/parser/statements.zi @@ -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) diff --git a/tests/css_diagnostics.css b/tests/css_diagnostics.css new file mode 100644 index 0000000..a089dec --- /dev/null +++ b/tests/css_diagnostics.css @@ -0,0 +1,18 @@ +:root { --gap: 7px; } +Row.fixture { + gap: var(--gap); + gap: 13px -2px; + box-shadow: 0 1px 2px black; + colour: red; + broken-name red; + & > Text { color: blue; } + color: #123456; + opacity: calc(1; + padding: 4px; +} +Row.fixture > Text { color: red; } +@supports (display: grid) { Text { color: red; } } +@media (orientation: landscape) { Text { color: red; } } +Text { width: 100vw; transition: transform 1s; height: 24px; } +@keyframes flash { from { opacity: 0.2; box-shadow: none; } to { opacity: 1; } } +Text { font-family: "Inter", sans-serif; } diff --git a/tests/css_fuzz.zi b/tests/css_fuzz.zi index 0982c1a..65e2abb 100644 --- a/tests/css_fuzz.zi +++ b/tests/css_fuzz.zi @@ -344,6 +344,11 @@ CheckCss :: (source: string, must_parse: bool) { } else if CssSheetDiagnostic(*fuzz_sheet, fuzz_diagnostic[:]).count == 0 { Fail("a parse failed without a diagnostic") } + CssSheetReset(*fuzz_sheet) + if !CssSheetParseWithDiagnostics(*fuzz_sheet, source, "fuzz.css", FuzzImports, CssDiagnostics.{}) && + CssSheetDiagnostic(*fuzz_sheet, fuzz_diagnostic[:]).count == 0 { + Fail("a recovering parse failed without a diagnostic") + } // The formatter answers imports with empty sheets. CssSheetReset(*fuzz_sheet) parses: bool = CssSheetParse(*fuzz_sheet, source, "fuzz.css", EmptyImports) diff --git a/tests/css_sheet_behavior.zi b/tests/css_sheet_behavior.zi index 6023cd8..e3763c8 100644 --- a/tests/css_sheet_behavior.zi +++ b/tests/css_sheet_behavior.zi @@ -303,6 +303,102 @@ LayoutAndTypography :: () { "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, "box-shadow", 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, 22, CssIssueKind.Declaration, "transition", true}, + .{16, 48, CssIssueKind.Declaration, "height", true}, + .{17, 41, CssIssueKind.Declaration, "box-shadow", true}, + .{18, 8, CssIssueKind.Declaration, "font-family", 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 { box-shadow: none; }"} } + return .{} +} + +ObserveImportIssue :: (context: *void, issue: CssIssue) { + count: *s32 = cast(*s32)context + if < s32 { Themes() TypedCustomProperties() LayoutAndTypography() + DiagnosticRecovery() Capacity() if test_failures == 0 { TestSay("css sheet behavior: PASS\n") } return test_failures diff --git a/tools/css_tool.zi b/tools/css_tool.zi index 0234100..ccc1813 100644 --- a/tools/css_tool.zi +++ b/tools/css_tool.zi @@ -2,7 +2,7 @@ // // css_tool export IN.css OUT.css write browser CSS for the DOM host and // check the result parses -// css_tool check FILE.css parse a sheet; imports count as empty +// css_tool check [--strict] FILE.css report native CSS issues; imports count as empty // css_tool format FILE.css rewrite a sheet in the standard layout // css_tool format-check FILE.css fail when a sheet is not formatted // css_tool pack NAME compile styles/NAME.css into @@ -81,13 +81,33 @@ EmptyImport :: (name: string) -> CssImportAnswer { return answer } -CheckSheet :: (path: string, source: string) -> s32 { - CssSheetReset(*sheet) - if !CssSheetParse(*sheet, source, path, EmptyImport) { - text: [256]u8 - return Fail(path, CssSheetDiagnostic(*sheet, text[:])) +PrintIssue :: (context: *void, issue: CssIssue) { + output_length = 0 + unused EmitNumber(issue.line) + unused Emit(":") + unused EmitNumber(issue.column) + WriteText(2, issue.path) + WriteText(2, ":") + WriteText(2, TextView(output_bytes[:output_length])) + WriteText(2, ifx issue.web_only then ": web-only " else ": skipped ") + if issue.kind == CssIssueKind.Declaration { WriteText(2, "declaration") } + else if issue.kind == CssIssueKind.AtRule { WriteText(2, "at-rule") } + else if issue.kind == CssIssueKind.Selector { WriteText(2, "selector") } + else { WriteText(2, "stylesheet") } + if issue.name.count > 0 { + WriteText(2, " '") + WriteText(2, issue.name) + WriteText(2, "'") } - return 0 + WriteText(2, ": ") + WriteText(2, issue.reason) + WriteText(2, "\n") +} + +CheckSheet :: (path: string, source: string, strict: bool) -> s32 { + CssSheetReset(*sheet) + diagnostics: CssDiagnostics = .{.report = PrintIssue, .strict = strict} + return ifx CssSheetParseWithDiagnostics(*sheet, source, path, EmptyImport, diagnostics) then 0 else 1 } // Generated module text ------------------------------------------------ @@ -491,15 +511,21 @@ Argument :: (argv: **s8, index: s32) -> string { #program_export main :: (argc: s32, argv: **s8) -> s32 { if argc < 3 { - WriteText(2, "usage: css_tool export IN.css OUT.css | check FILE.css | format FILE.css | format-check FILE.css | pack NAME | pack-file NAME IN.css OUT.zi | pack-check NAME\n") + WriteText(2, "usage: css_tool export IN.css OUT.css | check [--strict] FILE.css | format FILE.css | format-check FILE.css | pack NAME | pack-file NAME IN.css OUT.zi | pack-check NAME\n") return 2 } mode: string = Argument(argv, 1) if mode == "check" { - path: string = Argument(argv, 2) + strict: bool = Argument(argv, 2) == "--strict" + index: s32 = ifx strict then 3 else 2 + if argc != index + 1 { + WriteText(2, "usage: css_tool check [--strict] FILE.css\n") + return 2 + } + path: string = Argument(argv, index) count: s32 = ReadFile(path) if count < 0 { return Fail(path, "cannot read") } - return CheckSheet(path, TextView(source_bytes[:count])) + return CheckSheet(path, TextView(source_bytes[:count]), strict) } if mode == "format" || mode == "format-check" { path: string = Argument(argv, 2)