@chikx/compiler 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/emitter/index.d.ts +96 -0
- package/dist/emitter/index.d.ts.map +1 -0
- package/dist/emitter/index.js +415 -0
- package/dist/emitter/index.js.map +1 -0
- package/dist/formatter/index.d.ts +53 -0
- package/dist/formatter/index.d.ts.map +1 -0
- package/dist/formatter/index.js +464 -0
- package/dist/formatter/index.js.map +1 -0
- package/dist/host/node.d.ts +34 -0
- package/dist/host/node.d.ts.map +1 -0
- package/dist/host/node.js +80 -0
- package/dist/host/node.js.map +1 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +792 -0
- package/dist/index.js.map +1 -0
- package/dist/ir/factories.d.ts +35 -0
- package/dist/ir/factories.d.ts.map +1 -0
- package/dist/ir/factories.js +47 -0
- package/dist/ir/factories.js.map +1 -0
- package/dist/ir/index.d.ts +13 -0
- package/dist/ir/index.d.ts.map +1 -0
- package/dist/ir/index.js +2 -0
- package/dist/ir/index.js.map +1 -0
- package/dist/ir/types.d.ts +139 -0
- package/dist/ir/types.d.ts.map +1 -0
- package/dist/ir/types.js +2 -0
- package/dist/ir/types.js.map +1 -0
- package/dist/lexer/at-keywords.d.ts +27 -0
- package/dist/lexer/at-keywords.d.ts.map +1 -0
- package/dist/lexer/at-keywords.js +35 -0
- package/dist/lexer/at-keywords.js.map +1 -0
- package/dist/lexer/characters.d.ts +38 -0
- package/dist/lexer/characters.d.ts.map +1 -0
- package/dist/lexer/characters.js +74 -0
- package/dist/lexer/characters.js.map +1 -0
- package/dist/lexer/comments.d.ts +22 -0
- package/dist/lexer/comments.d.ts.map +1 -0
- package/dist/lexer/comments.js +20 -0
- package/dist/lexer/comments.js.map +1 -0
- package/dist/lexer/diagnostics.d.ts +24 -0
- package/dist/lexer/diagnostics.d.ts.map +1 -0
- package/dist/lexer/diagnostics.js +29 -0
- package/dist/lexer/diagnostics.js.map +1 -0
- package/dist/lexer/dollar-refs.d.ts +13 -0
- package/dist/lexer/dollar-refs.d.ts.map +1 -0
- package/dist/lexer/dollar-refs.js +22 -0
- package/dist/lexer/dollar-refs.js.map +1 -0
- package/dist/lexer/index.d.ts +32 -0
- package/dist/lexer/index.d.ts.map +1 -0
- package/dist/lexer/index.js +26 -0
- package/dist/lexer/index.js.map +1 -0
- package/dist/lexer/names.d.ts +23 -0
- package/dist/lexer/names.d.ts.map +1 -0
- package/dist/lexer/names.js +48 -0
- package/dist/lexer/names.js.map +1 -0
- package/dist/lexer/numbers.d.ts +22 -0
- package/dist/lexer/numbers.d.ts.map +1 -0
- package/dist/lexer/numbers.js +60 -0
- package/dist/lexer/numbers.js.map +1 -0
- package/dist/lexer/scanner.d.ts +30 -0
- package/dist/lexer/scanner.d.ts.map +1 -0
- package/dist/lexer/scanner.js +158 -0
- package/dist/lexer/scanner.js.map +1 -0
- package/dist/lexer/strings.d.ts +25 -0
- package/dist/lexer/strings.d.ts.map +1 -0
- package/dist/lexer/strings.js +41 -0
- package/dist/lexer/strings.js.map +1 -0
- package/dist/lexer/urls.d.ts +28 -0
- package/dist/lexer/urls.d.ts.map +1 -0
- package/dist/lexer/urls.js +54 -0
- package/dist/lexer/urls.js.map +1 -0
- package/dist/lowering/expand-groups.d.ts +61 -0
- package/dist/lowering/expand-groups.d.ts.map +1 -0
- package/dist/lowering/expand-groups.js +117 -0
- package/dist/lowering/expand-groups.js.map +1 -0
- package/dist/lowering/group.d.ts +30 -0
- package/dist/lowering/group.d.ts.map +1 -0
- package/dist/lowering/group.js +109 -0
- package/dist/lowering/group.js.map +1 -0
- package/dist/lowering/index.d.ts +26 -0
- package/dist/lowering/index.d.ts.map +1 -0
- package/dist/lowering/index.js +23 -0
- package/dist/lowering/index.js.map +1 -0
- package/dist/lowering/lower.d.ts +21 -0
- package/dist/lowering/lower.d.ts.map +1 -0
- package/dist/lowering/lower.js +296 -0
- package/dist/lowering/lower.js.map +1 -0
- package/dist/lowering/media.d.ts +63 -0
- package/dist/lowering/media.d.ts.map +1 -0
- package/dist/lowering/media.js +237 -0
- package/dist/lowering/media.js.map +1 -0
- package/dist/lowering/merge.d.ts +35 -0
- package/dist/lowering/merge.d.ts.map +1 -0
- package/dist/lowering/merge.js +146 -0
- package/dist/lowering/merge.js.map +1 -0
- package/dist/lowering/property-families.d.ts +50 -0
- package/dist/lowering/property-families.d.ts.map +1 -0
- package/dist/lowering/property-families.js +175 -0
- package/dist/lowering/property-families.js.map +1 -0
- package/dist/lowering/spans.d.ts +36 -0
- package/dist/lowering/spans.d.ts.map +1 -0
- package/dist/lowering/spans.js +100 -0
- package/dist/lowering/spans.js.map +1 -0
- package/dist/naming/index.d.ts +37 -0
- package/dist/naming/index.d.ts.map +1 -0
- package/dist/naming/index.js +34 -0
- package/dist/naming/index.js.map +1 -0
- package/dist/naming/mapgen.d.ts +70 -0
- package/dist/naming/mapgen.d.ts.map +1 -0
- package/dist/naming/mapgen.js +153 -0
- package/dist/naming/mapgen.js.map +1 -0
- package/dist/naming/merge.d.ts +62 -0
- package/dist/naming/merge.d.ts.map +1 -0
- package/dist/naming/merge.js +77 -0
- package/dist/naming/merge.js.map +1 -0
- package/dist/naming/rewrite.d.ts +14 -0
- package/dist/naming/rewrite.d.ts.map +1 -0
- package/dist/naming/rewrite.js +191 -0
- package/dist/naming/rewrite.js.map +1 -0
- package/dist/naming/scan.d.ts +80 -0
- package/dist/naming/scan.d.ts.map +1 -0
- package/dist/naming/scan.js +193 -0
- package/dist/naming/scan.js.map +1 -0
- package/dist/naming/table.d.ts +68 -0
- package/dist/naming/table.d.ts.map +1 -0
- package/dist/naming/table.js +132 -0
- package/dist/naming/table.js.map +1 -0
- package/dist/naming/types.d.ts +94 -0
- package/dist/naming/types.d.ts.map +1 -0
- package/dist/naming/types.js +2 -0
- package/dist/naming/types.js.map +1 -0
- package/dist/output/import-path.d.ts +24 -0
- package/dist/output/import-path.d.ts.map +1 -0
- package/dist/output/import-path.js +30 -0
- package/dist/output/import-path.js.map +1 -0
- package/dist/output/index.d.ts +40 -0
- package/dist/output/index.d.ts.map +1 -0
- package/dist/output/index.js +138 -0
- package/dist/output/index.js.map +1 -0
- package/dist/output/manifest.d.ts +17 -0
- package/dist/output/manifest.d.ts.map +1 -0
- package/dist/output/manifest.js +22 -0
- package/dist/output/manifest.js.map +1 -0
- package/dist/output/paths.d.ts +54 -0
- package/dist/output/paths.d.ts.map +1 -0
- package/dist/output/paths.js +183 -0
- package/dist/output/paths.js.map +1 -0
- package/dist/output/resolve.d.ts +100 -0
- package/dist/output/resolve.d.ts.map +1 -0
- package/dist/output/resolve.js +313 -0
- package/dist/output/resolve.js.map +1 -0
- package/dist/output/validate.d.ts +82 -0
- package/dist/output/validate.d.ts.map +1 -0
- package/dist/output/validate.js +128 -0
- package/dist/output/validate.js.map +1 -0
- package/dist/parser/cursor.d.ts +55 -0
- package/dist/parser/cursor.d.ts.map +1 -0
- package/dist/parser/cursor.js +120 -0
- package/dist/parser/cursor.js.map +1 -0
- package/dist/parser/declaration.d.ts +21 -0
- package/dist/parser/declaration.d.ts.map +1 -0
- package/dist/parser/declaration.js +201 -0
- package/dist/parser/declaration.js.map +1 -0
- package/dist/parser/diagnostics.d.ts +52 -0
- package/dist/parser/diagnostics.d.ts.map +1 -0
- package/dist/parser/diagnostics.js +55 -0
- package/dist/parser/diagnostics.js.map +1 -0
- package/dist/parser/group.d.ts +26 -0
- package/dist/parser/group.d.ts.map +1 -0
- package/dist/parser/group.js +219 -0
- package/dist/parser/group.js.map +1 -0
- package/dist/parser/index.d.ts +30 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +48 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/modifier.d.ts +25 -0
- package/dist/parser/modifier.d.ts.map +1 -0
- package/dist/parser/modifier.js +41 -0
- package/dist/parser/modifier.js.map +1 -0
- package/dist/parser/recovery.d.ts +23 -0
- package/dist/parser/recovery.d.ts.map +1 -0
- package/dist/parser/recovery.js +80 -0
- package/dist/parser/recovery.js.map +1 -0
- package/dist/parser/rule.d.ts +39 -0
- package/dist/parser/rule.d.ts.map +1 -0
- package/dist/parser/rule.js +337 -0
- package/dist/parser/rule.js.map +1 -0
- package/dist/parser/statement.d.ts +21 -0
- package/dist/parser/statement.d.ts.map +1 -0
- package/dist/parser/statement.js +48 -0
- package/dist/parser/statement.js.map +1 -0
- package/dist/parser/statements.d.ts +33 -0
- package/dist/parser/statements.d.ts.map +1 -0
- package/dist/parser/statements.js +605 -0
- package/dist/parser/statements.js.map +1 -0
- package/dist/parser/value.d.ts +57 -0
- package/dist/parser/value.d.ts.map +1 -0
- package/dist/parser/value.js +226 -0
- package/dist/parser/value.js.map +1 -0
- package/dist/resolver/graph.d.ts +20 -0
- package/dist/resolver/graph.d.ts.map +1 -0
- package/dist/resolver/graph.js +382 -0
- package/dist/resolver/graph.js.map +1 -0
- package/dist/resolver/groups.d.ts +70 -0
- package/dist/resolver/groups.d.ts.map +1 -0
- package/dist/resolver/groups.js +143 -0
- package/dist/resolver/groups.js.map +1 -0
- package/dist/resolver/index.d.ts +46 -0
- package/dist/resolver/index.d.ts.map +1 -0
- package/dist/resolver/index.js +46 -0
- package/dist/resolver/index.js.map +1 -0
- package/dist/resolver/keyframes.d.ts +56 -0
- package/dist/resolver/keyframes.d.ts.map +1 -0
- package/dist/resolver/keyframes.js +102 -0
- package/dist/resolver/keyframes.js.map +1 -0
- package/dist/resolver/paths.d.ts +37 -0
- package/dist/resolver/paths.d.ts.map +1 -0
- package/dist/resolver/paths.js +135 -0
- package/dist/resolver/paths.js.map +1 -0
- package/dist/resolver/rules.d.ts +34 -0
- package/dist/resolver/rules.d.ts.map +1 -0
- package/dist/resolver/rules.js +52 -0
- package/dist/resolver/rules.js.map +1 -0
- package/dist/resolver/scopes.d.ts +44 -0
- package/dist/resolver/scopes.d.ts.map +1 -0
- package/dist/resolver/scopes.js +213 -0
- package/dist/resolver/scopes.js.map +1 -0
- package/dist/resolver/types.d.ts +238 -0
- package/dist/resolver/types.d.ts.map +1 -0
- package/dist/resolver/types.js +11 -0
- package/dist/resolver/types.js.map +1 -0
- package/dist/selectors/flatten.d.ts +95 -0
- package/dist/selectors/flatten.d.ts.map +1 -0
- package/dist/selectors/flatten.js +384 -0
- package/dist/selectors/flatten.js.map +1 -0
- package/dist/selectors/index.d.ts +15 -0
- package/dist/selectors/index.d.ts.map +1 -0
- package/dist/selectors/index.js +14 -0
- package/dist/selectors/index.js.map +1 -0
- package/dist/selectors/scan.d.ts +54 -0
- package/dist/selectors/scan.d.ts.map +1 -0
- package/dist/selectors/scan.js +191 -0
- package/dist/selectors/scan.js.map +1 -0
- package/dist/selectors/specificity.d.ts +28 -0
- package/dist/selectors/specificity.d.ts.map +1 -0
- package/dist/selectors/specificity.js +297 -0
- package/dist/selectors/specificity.js.map +1 -0
- package/dist/setup-list.d.ts +41 -0
- package/dist/setup-list.d.ts.map +1 -0
- package/dist/setup-list.js +122 -0
- package/dist/setup-list.js.map +1 -0
- package/dist/sourcemaps/index.d.ts +14 -0
- package/dist/sourcemaps/index.d.ts.map +1 -0
- package/dist/sourcemaps/index.js +13 -0
- package/dist/sourcemaps/index.js.map +1 -0
- package/dist/sourcemaps/mappings.d.ts +77 -0
- package/dist/sourcemaps/mappings.d.ts.map +1 -0
- package/dist/sourcemaps/mappings.js +124 -0
- package/dist/sourcemaps/mappings.js.map +1 -0
- package/dist/sourcemaps/vlq.d.ts +12 -0
- package/dist/sourcemaps/vlq.d.ts.map +1 -0
- package/dist/sourcemaps/vlq.js +29 -0
- package/dist/sourcemaps/vlq.js.map +1 -0
- package/dist/types/ast.d.ts +291 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/ast.js +2 -0
- package/dist/types/ast.js.map +1 -0
- package/dist/types/diagnostics.d.ts +55 -0
- package/dist/types/diagnostics.d.ts.map +1 -0
- package/dist/types/diagnostics.js +61 -0
- package/dist/types/diagnostics.js.map +1 -0
- package/dist/types/options.d.ts +274 -0
- package/dist/types/options.d.ts.map +1 -0
- package/dist/types/options.js +11 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/result.d.ts +112 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +2 -0
- package/dist/types/result.js.map +1 -0
- package/dist/types/span.d.ts +19 -0
- package/dist/types/span.d.ts.map +1 -0
- package/dist/types/span.js +2 -0
- package/dist/types/span.js.map +1 -0
- package/dist/types/token.d.ts +68 -0
- package/dist/types/token.d.ts.map +1 -0
- package/dist/types/token.js +2 -0
- package/dist/types/token.js.map +1 -0
- package/dist/variables/ast.d.ts +56 -0
- package/dist/variables/ast.d.ts.map +1 -0
- package/dist/variables/ast.js +167 -0
- package/dist/variables/ast.js.map +1 -0
- package/dist/variables/expand.d.ts +109 -0
- package/dist/variables/expand.d.ts.map +1 -0
- package/dist/variables/expand.js +213 -0
- package/dist/variables/expand.js.map +1 -0
- package/dist/variables/index.d.ts +36 -0
- package/dist/variables/index.d.ts.map +1 -0
- package/dist/variables/index.js +33 -0
- package/dist/variables/index.js.map +1 -0
- package/dist/variables/naming.d.ts +39 -0
- package/dist/variables/naming.d.ts.map +1 -0
- package/dist/variables/naming.js +14 -0
- package/dist/variables/naming.js.map +1 -0
- package/dist/variables/runtime.d.ts +81 -0
- package/dist/variables/runtime.d.ts.map +1 -0
- package/dist/variables/runtime.js +228 -0
- package/dist/variables/runtime.js.map +1 -0
- package/dist/variables/substitute.d.ts +65 -0
- package/dist/variables/substitute.d.ts.map +1 -0
- package/dist/variables/substitute.js +73 -0
- package/dist/variables/substitute.js.map +1 -0
- package/dist/variables/walk.d.ts +37 -0
- package/dist/variables/walk.d.ts.map +1 -0
- package/dist/variables/walk.js +113 -0
- package/dist/variables/walk.js.map +1 -0
- package/package.json +35 -4
- package/src/emitter/index.ts +658 -0
- package/src/formatter/index.ts +544 -0
- package/src/host/node.ts +85 -0
- package/src/index.ts +1021 -0
- package/src/ir/factories.ts +85 -0
- package/src/ir/index.ts +28 -0
- package/src/ir/types.ts +145 -0
- package/src/lexer/at-keywords.ts +35 -0
- package/src/lexer/characters.ts +82 -0
- package/src/lexer/comments.ts +27 -0
- package/src/lexer/diagnostics.ts +42 -0
- package/src/lexer/dollar-refs.ts +23 -0
- package/src/lexer/index.ts +37 -0
- package/src/lexer/names.ts +49 -0
- package/src/lexer/numbers.ts +59 -0
- package/src/lexer/scanner.ts +199 -0
- package/src/lexer/strings.ts +52 -0
- package/src/lexer/urls.ts +63 -0
- package/src/lowering/expand-groups.ts +206 -0
- package/src/lowering/group.ts +119 -0
- package/src/lowering/index.ts +43 -0
- package/src/lowering/lower.ts +441 -0
- package/src/lowering/media.ts +314 -0
- package/src/lowering/merge.ts +163 -0
- package/src/lowering/property-families.ts +194 -0
- package/src/lowering/spans.ts +118 -0
- package/src/naming/index.ts +44 -0
- package/src/naming/mapgen.ts +252 -0
- package/src/naming/merge.ts +114 -0
- package/src/naming/rewrite.ts +333 -0
- package/src/naming/scan.ts +267 -0
- package/src/naming/table.ts +153 -0
- package/src/naming/types.ts +100 -0
- package/src/output/import-path.ts +30 -0
- package/src/output/index.ts +213 -0
- package/src/output/manifest.ts +28 -0
- package/src/output/paths.ts +200 -0
- package/src/output/resolve.ts +401 -0
- package/src/output/validate.ts +251 -0
- package/src/parser/cursor.ts +138 -0
- package/src/parser/declaration.ts +247 -0
- package/src/parser/diagnostics.ts +75 -0
- package/src/parser/group.ts +303 -0
- package/src/parser/index.ts +50 -0
- package/src/parser/modifier.ts +60 -0
- package/src/parser/recovery.ts +88 -0
- package/src/parser/rule.ts +370 -0
- package/src/parser/statement.ts +47 -0
- package/src/parser/statements.ts +667 -0
- package/src/parser/value.ts +265 -0
- package/src/resolver/graph.ts +467 -0
- package/src/resolver/groups.ts +177 -0
- package/src/resolver/index.ts +68 -0
- package/src/resolver/keyframes.ts +131 -0
- package/src/resolver/paths.ts +142 -0
- package/src/resolver/rules.ts +54 -0
- package/src/resolver/scopes.ts +342 -0
- package/src/resolver/types.ts +259 -0
- package/src/selectors/flatten.ts +448 -0
- package/src/selectors/index.ts +14 -0
- package/src/selectors/scan.ts +227 -0
- package/src/selectors/specificity.ts +319 -0
- package/src/setup-list.ts +172 -0
- package/src/sourcemaps/index.ts +25 -0
- package/src/sourcemaps/mappings.ts +190 -0
- package/src/sourcemaps/vlq.ts +29 -0
- package/src/types/ast.ts +318 -0
- package/src/types/diagnostics.ts +124 -0
- package/src/types/options.ts +286 -0
- package/src/types/result.ts +121 -0
- package/src/types/span.ts +18 -0
- package/src/types/token.ts +77 -0
- package/src/variables/ast.ts +182 -0
- package/src/variables/expand.ts +319 -0
- package/src/variables/index.ts +38 -0
- package/src/variables/naming.ts +46 -0
- package/src/variables/runtime.ts +336 -0
- package/src/variables/substitute.ts +148 -0
- package/src/variables/walk.ts +147 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { mediaConditionIr } from "../ir/factories.js";
|
|
2
|
+
import type { MediaConditionIr } from "../ir/types.js";
|
|
3
|
+
import type { Stylesheet } from "../types/ast.js";
|
|
4
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
5
|
+
import { error } from "../types/diagnostics.js";
|
|
6
|
+
import type { Span } from "../types/span.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Media alias resolution (SPECIFICATION.md §4, ADR-0006).
|
|
10
|
+
*
|
|
11
|
+
* Definitions merge **by key** across three layers: built-in defaults, then
|
|
12
|
+
* project configuration (`options.media`), then file-level `@media-types` in
|
|
13
|
+
* document order. A later definition of the same key replaces the earlier one;
|
|
14
|
+
* distinct keys accumulate. Definitions are validated when a condition is
|
|
15
|
+
* resolved, not when the table is built, so an unused invalid definition never
|
|
16
|
+
* produces a diagnostic.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Built-in alias table of SPECIFICATION.md §4.1. Frozen; never mutated. */
|
|
20
|
+
export const BUILTIN_MEDIA_ALIASES: Readonly<Record<string, string>> = Object.freeze({
|
|
21
|
+
sm: "(min-width: 640px)",
|
|
22
|
+
md: "(min-width: 768px)",
|
|
23
|
+
lg: "(min-width: 1024px)",
|
|
24
|
+
xl: "(min-width: 1280px)",
|
|
25
|
+
"2xl": "(min-width: 1536px)",
|
|
26
|
+
dark: "(prefers-color-scheme: dark)",
|
|
27
|
+
light: "(prefers-color-scheme: light)",
|
|
28
|
+
portrait: "(orientation: portrait)",
|
|
29
|
+
landscape: "(orientation: landscape)",
|
|
30
|
+
print: "print",
|
|
31
|
+
reduced: "(prefers-reduced-motion: reduce)",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** The merged alias table used to resolve one stylesheet's conditions. */
|
|
35
|
+
export interface MediaAliasTable {
|
|
36
|
+
/** Alias name to raw media-query definition; later layers already applied. */
|
|
37
|
+
readonly definitions: ReadonlyMap<string, string>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Build the merged alias table for one stylesheet.
|
|
42
|
+
*
|
|
43
|
+
* Order is fixed by ADR-0006: built-ins, then `project` (`options.media`), then
|
|
44
|
+
* every top-level `@media-types` block in document order. Only top-level blocks
|
|
45
|
+
* are considered because the grammar restricts `@media-types` to the file
|
|
46
|
+
* level. The AST is not mutated.
|
|
47
|
+
*/
|
|
48
|
+
export function buildMediaAliasTable(
|
|
49
|
+
ast: Stylesheet,
|
|
50
|
+
project?: Readonly<Record<string, string>>,
|
|
51
|
+
): MediaAliasTable {
|
|
52
|
+
const definitions = new Map<string, string>(Object.entries(BUILTIN_MEDIA_ALIASES));
|
|
53
|
+
if (project !== undefined) {
|
|
54
|
+
for (const [alias, definition] of Object.entries(project)) definitions.set(alias, definition);
|
|
55
|
+
}
|
|
56
|
+
for (const item of ast.items) {
|
|
57
|
+
if (item.kind !== "media-types") continue;
|
|
58
|
+
for (const alias of item.aliases) definitions.set(alias.name, alias.media);
|
|
59
|
+
}
|
|
60
|
+
return Object.freeze({ definitions });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One alias reference with the exact source range it was authored at. */
|
|
64
|
+
export interface ConditionAlias {
|
|
65
|
+
/** Alias name without the `@` sigil. */
|
|
66
|
+
readonly alias: string;
|
|
67
|
+
/** Exact span of the authored alias text, used for diagnostics. */
|
|
68
|
+
readonly span: Span;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Resolution outcome: a single query plus its keys, or one blocking diagnostic. */
|
|
72
|
+
export type ConditionResolution =
|
|
73
|
+
| {
|
|
74
|
+
readonly ok: true;
|
|
75
|
+
readonly condition: MediaConditionIr;
|
|
76
|
+
/** Deduplicated alias references in authored order (first occurrence wins). */
|
|
77
|
+
readonly aliases: readonly ConditionAlias[];
|
|
78
|
+
}
|
|
79
|
+
| { readonly ok: false; readonly diagnostic: Diagnostic };
|
|
80
|
+
|
|
81
|
+
/** Media types recognised at the start of a definition (CSS Media Queries). */
|
|
82
|
+
const MEDIA_TYPES: ReadonlySet<string> = new Set([
|
|
83
|
+
"all",
|
|
84
|
+
"aural",
|
|
85
|
+
"braille",
|
|
86
|
+
"embossed",
|
|
87
|
+
"handheld",
|
|
88
|
+
"print",
|
|
89
|
+
"projection",
|
|
90
|
+
"screen",
|
|
91
|
+
"speech",
|
|
92
|
+
"tty",
|
|
93
|
+
"tv",
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
interface ExpressionScan {
|
|
97
|
+
readonly balanced: boolean;
|
|
98
|
+
readonly comma: boolean;
|
|
99
|
+
readonly words: readonly string[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Scan a media-query definition at top level only.
|
|
104
|
+
*
|
|
105
|
+
* Words and commas inside parentheses, brackets and quoted strings are ignored
|
|
106
|
+
* so `(min-width: 640px)` and `url("a,b")` never look like a list or a bare
|
|
107
|
+
* media type.
|
|
108
|
+
*/
|
|
109
|
+
function scanExpression(text: string): ExpressionScan {
|
|
110
|
+
let depth = 0;
|
|
111
|
+
let balanced = true;
|
|
112
|
+
let comma = false;
|
|
113
|
+
let quote: string | null = null;
|
|
114
|
+
const words: string[] = [];
|
|
115
|
+
let word = "";
|
|
116
|
+
|
|
117
|
+
const flushWord = (): void => {
|
|
118
|
+
if (word !== "") {
|
|
119
|
+
words.push(word);
|
|
120
|
+
word = "";
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
for (let index = 0; index < text.length; index++) {
|
|
125
|
+
const character = text[index] as string;
|
|
126
|
+
if (quote !== null) {
|
|
127
|
+
if (character === "\\") {
|
|
128
|
+
index++;
|
|
129
|
+
} else if (character === quote) {
|
|
130
|
+
quote = null;
|
|
131
|
+
}
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (character === '"' || character === "'") {
|
|
135
|
+
flushWord();
|
|
136
|
+
quote = character;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (character === "(" || character === "[") {
|
|
140
|
+
flushWord();
|
|
141
|
+
depth++;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (character === ")" || character === "]") {
|
|
145
|
+
flushWord();
|
|
146
|
+
depth--;
|
|
147
|
+
if (depth < 0) balanced = false;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (depth === 0) {
|
|
151
|
+
if (character === ",") {
|
|
152
|
+
comma = true;
|
|
153
|
+
flushWord();
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (/[A-Za-z0-9-]/.test(character)) {
|
|
157
|
+
word += character;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
flushWord();
|
|
162
|
+
}
|
|
163
|
+
flushWord();
|
|
164
|
+
|
|
165
|
+
if (quote !== null || depth !== 0) balanced = false;
|
|
166
|
+
return { balanced, comma, words };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
type ExpressionClass =
|
|
170
|
+
| { readonly ok: true; readonly kind: "feature" | "type" | "modifier"; readonly hasOr: boolean }
|
|
171
|
+
| { readonly ok: false; readonly reason: string };
|
|
172
|
+
|
|
173
|
+
/** Classify one alias definition and reject what cannot form valid CSS. */
|
|
174
|
+
function classifyExpression(text: string): ExpressionClass {
|
|
175
|
+
const scan = scanExpression(text);
|
|
176
|
+
if (!scan.balanced) return { ok: false, reason: "unbalanced parentheses" };
|
|
177
|
+
if (scan.comma) {
|
|
178
|
+
return { ok: false, reason: "comma-separated media query lists are not supported" };
|
|
179
|
+
}
|
|
180
|
+
const trimmed = text.trim();
|
|
181
|
+
if (trimmed === "") return { ok: false, reason: "the definition is empty" };
|
|
182
|
+
if (/(^|\s)(and|or|not|only)$/i.test(trimmed)) {
|
|
183
|
+
return { ok: false, reason: "the definition ends with a combinator" };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const hasOr = scan.words.some((word) => word.toLowerCase() === "or");
|
|
187
|
+
if (trimmed.startsWith("(")) return { ok: true, kind: "feature", hasOr };
|
|
188
|
+
|
|
189
|
+
const leading = /^([A-Za-z][A-Za-z0-9-]*)/.exec(trimmed);
|
|
190
|
+
if (leading === null) return { ok: false, reason: "a media type or condition is required" };
|
|
191
|
+
const word = (leading[1] as string).toLowerCase();
|
|
192
|
+
const rest = trimmed.slice(leading[0].length);
|
|
193
|
+
|
|
194
|
+
if (word === "and" || word === "or") {
|
|
195
|
+
return { ok: false, reason: "a media type or condition is required" };
|
|
196
|
+
}
|
|
197
|
+
if (word === "not" || word === "only") {
|
|
198
|
+
if (rest.trim() === "") return { ok: false, reason: "a media type or condition is required" };
|
|
199
|
+
if (word === "only" && rest.trimStart().startsWith("(")) {
|
|
200
|
+
return { ok: false, reason: "`only` requires a media type" };
|
|
201
|
+
}
|
|
202
|
+
return { ok: true, kind: "modifier", hasOr };
|
|
203
|
+
}
|
|
204
|
+
if (MEDIA_TYPES.has(word)) return { ok: true, kind: "type", hasOr };
|
|
205
|
+
return { ok: false, reason: `\`${word}\` is not a supported media type` };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
interface ResolvedPart {
|
|
209
|
+
readonly alias: string;
|
|
210
|
+
readonly text: string;
|
|
211
|
+
readonly kind: "feature" | "type" | "modifier";
|
|
212
|
+
readonly hasOr: boolean;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function fail(message: string, span: Span): ConditionResolution {
|
|
216
|
+
return { ok: false, diagnostic: error("CHIKX3001", message, span) };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Resolve an ordered alias list into one normalized media query.
|
|
221
|
+
*
|
|
222
|
+
* The list usually combines inherited conditional-block aliases with an inline
|
|
223
|
+
* variant's aliases. Duplicate alias names are normalized (first appearance
|
|
224
|
+
* wins, §4.3); unknown aliases and definitions that cannot form a valid single
|
|
225
|
+
* expression are rejected with CHIKX3001 and no condition, so the caller omits
|
|
226
|
+
* the affected declaration or block. Combination is logical AND only: a media
|
|
227
|
+
* type is emitted first, `not`/`only` queries and queries containing `or`
|
|
228
|
+
* cannot be combined, and two media types are incompatible.
|
|
229
|
+
*
|
|
230
|
+
* `wholeSpan` covers the authored modifier chain for combination diagnostics;
|
|
231
|
+
* per-alias diagnostics use the exact {@link ConditionAlias.span}.
|
|
232
|
+
*/
|
|
233
|
+
export function resolveMediaCondition(
|
|
234
|
+
entries: readonly ConditionAlias[],
|
|
235
|
+
table: MediaAliasTable,
|
|
236
|
+
wholeSpan: Span,
|
|
237
|
+
): ConditionResolution {
|
|
238
|
+
const seen = new Set<string>();
|
|
239
|
+
const aliases: ConditionAlias[] = [];
|
|
240
|
+
const parts: ResolvedPart[] = [];
|
|
241
|
+
|
|
242
|
+
for (const entry of entries) {
|
|
243
|
+
if (seen.has(entry.alias)) continue;
|
|
244
|
+
seen.add(entry.alias);
|
|
245
|
+
const definition = table.definitions.get(entry.alias);
|
|
246
|
+
if (definition === undefined) {
|
|
247
|
+
return fail(
|
|
248
|
+
`Unknown media alias "@${entry.alias}". Define it in @media-types or in the project "media" configuration.`,
|
|
249
|
+
entry.span,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
const text = definition.trim();
|
|
253
|
+
const classified = classifyExpression(text);
|
|
254
|
+
if (!classified.ok) {
|
|
255
|
+
return fail(
|
|
256
|
+
`Media alias "@${entry.alias}" cannot form a valid single media query: ${classified.reason}.`,
|
|
257
|
+
entry.span,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
aliases.push(entry);
|
|
261
|
+
parts.push({ alias: entry.alias, text, kind: classified.kind, hasOr: classified.hasOr });
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (parts.length === 0) {
|
|
265
|
+
return fail("A media condition requires at least one alias.", wholeSpan);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (parts.length === 1) {
|
|
269
|
+
const only = parts[0] as ResolvedPart;
|
|
270
|
+
return {
|
|
271
|
+
ok: true,
|
|
272
|
+
condition: mediaConditionIr(only.text, [only.alias]),
|
|
273
|
+
aliases,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const combined = parts.map((part) => `@${part.alias}`).join("+");
|
|
278
|
+
if (parts.some((part) => part.kind === "modifier")) {
|
|
279
|
+
return fail(
|
|
280
|
+
`Media aliases "${combined}" cannot be combined: a \`not\`/\`only\` query cannot be joined with \`and\`.`,
|
|
281
|
+
wholeSpan,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
if (parts.some((part) => part.hasOr)) {
|
|
285
|
+
return fail(
|
|
286
|
+
`Media aliases "${combined}" cannot be combined: a query containing \`or\` cannot be joined with \`and\`.`,
|
|
287
|
+
wholeSpan,
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
const types = parts.filter((part) => part.kind === "type");
|
|
291
|
+
if (types.length > 1) {
|
|
292
|
+
const names = types.map((part) => `${part.alias} (${part.text})`).join(", ");
|
|
293
|
+
return fail(
|
|
294
|
+
`Media aliases "${combined}" combine incompatible media types (${names}) and cannot form valid CSS.`,
|
|
295
|
+
wholeSpan,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// A media type must lead a media query; reordering components of a logical AND
|
|
300
|
+
// is a query normalization, not a declaration reorder.
|
|
301
|
+
const ordered =
|
|
302
|
+
types.length === 1
|
|
303
|
+
? [types[0] as ResolvedPart, ...parts.filter((part) => part !== types[0])]
|
|
304
|
+
: parts;
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
ok: true,
|
|
308
|
+
condition: mediaConditionIr(
|
|
309
|
+
ordered.map((part) => part.text).join(" and "),
|
|
310
|
+
parts.map((part) => part.alias),
|
|
311
|
+
),
|
|
312
|
+
aliases,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opt-in merge grouping (ADR-0024; SPECIFICATION.md §5.3).
|
|
3
|
+
*
|
|
4
|
+
* `safe` grouping only joins *adjacent* identical runs. `merge` keeps that pass
|
|
5
|
+
* and then, for conditional runs only, moves a declaration run into a later run
|
|
6
|
+
* with the **same selector chain, condition string and native at-rule context**
|
|
7
|
+
* when every declaration of every run strictly between them belongs to a
|
|
8
|
+
* property family disjoint from every declaration of the moved run.
|
|
9
|
+
*
|
|
10
|
+
* Why this cannot change a cascade winner:
|
|
11
|
+
*
|
|
12
|
+
* - A winner is decided per property. If the two families are disjoint, no
|
|
13
|
+
* crossed declaration can compete for the moved declaration's property.
|
|
14
|
+
* - The moved run lands *before* the destination run's own declarations, so the
|
|
15
|
+
* relative order of the source and destination is preserved; selector
|
|
16
|
+
* specificity, condition overlap, `@layer` order and `!important` are
|
|
17
|
+
* unchanged by moving a declaration past a different property.
|
|
18
|
+
* - Native blocks are barriers: children of one block are merged among
|
|
19
|
+
* themselves, never across the block boundary, so native context identity is
|
|
20
|
+
* preserved.
|
|
21
|
+
*
|
|
22
|
+
* Wildcards (`all`, or a value containing `var(`) are never moved and nothing
|
|
23
|
+
* crosses them (see `property-families.ts`). A missed merge is acceptable; a
|
|
24
|
+
* changed cascade is not.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { nativeBlockIr, ruleIr, stylesheetIr } from "../ir/factories.js";
|
|
28
|
+
import type {
|
|
29
|
+
DeclarationIr,
|
|
30
|
+
NativeBlockChild,
|
|
31
|
+
NativeBlockIr,
|
|
32
|
+
RuleIr,
|
|
33
|
+
StylesheetIr,
|
|
34
|
+
} from "../ir/types.js";
|
|
35
|
+
import { propertyFamily } from "./property-families.js";
|
|
36
|
+
import { sameAncestors, sameContext } from "./spans.js";
|
|
37
|
+
|
|
38
|
+
/** One run's family set, or `null` when it contains a wildcard declaration. */
|
|
39
|
+
function runFamilies(declarations: readonly DeclarationIr[]): ReadonlySet<string> | null {
|
|
40
|
+
const families = new Set<string>();
|
|
41
|
+
for (const declaration of declarations) {
|
|
42
|
+
const family = propertyFamily(declaration.property, declaration.value);
|
|
43
|
+
if (family === null) return null;
|
|
44
|
+
families.add(family);
|
|
45
|
+
}
|
|
46
|
+
return families;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Whether two family sets share any property family. */
|
|
50
|
+
function intersects(left: ReadonlySet<string>, right: ReadonlySet<string>): boolean {
|
|
51
|
+
for (const family of left) {
|
|
52
|
+
if (right.has(family)) return true;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Whether a run may participate in a merge as a destination. */
|
|
58
|
+
function isConditionalRun(rule: RuleIr): boolean {
|
|
59
|
+
return rule.condition !== null && rule.condition.query !== "";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Identical selector chain, condition string and native context. */
|
|
63
|
+
function sameMergeRun(source: RuleIr, destination: RuleIr): boolean {
|
|
64
|
+
return (
|
|
65
|
+
source.selector === destination.selector &&
|
|
66
|
+
sameAncestors(source.ancestors, destination.ancestors) &&
|
|
67
|
+
sameContext(source.context, destination.context) &&
|
|
68
|
+
source.condition !== null &&
|
|
69
|
+
destination.condition !== null &&
|
|
70
|
+
source.condition.query === destination.condition.query
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Move the source declarations into the destination block, source first. */
|
|
75
|
+
function mergeInto(source: RuleIr, destination: RuleIr): RuleIr {
|
|
76
|
+
return ruleIr({
|
|
77
|
+
selector: destination.selector,
|
|
78
|
+
ancestors: destination.ancestors,
|
|
79
|
+
declarations: [...source.declarations, ...destination.declarations],
|
|
80
|
+
condition: destination.condition,
|
|
81
|
+
context: destination.context,
|
|
82
|
+
span: destination.span,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Merge non-adjacent conditional runs in one sibling list to a fixpoint.
|
|
88
|
+
*
|
|
89
|
+
* Greedy and deterministic: the lowest source index wins, then the lowest valid
|
|
90
|
+
* destination index. Every merge removes one node, so the loop terminates.
|
|
91
|
+
*/
|
|
92
|
+
function mergeSiblingRuns(nodes: readonly NativeBlockChild[]): readonly NativeBlockChild[] {
|
|
93
|
+
const result = [...nodes];
|
|
94
|
+
let merged = true;
|
|
95
|
+
while (merged) {
|
|
96
|
+
merged = false;
|
|
97
|
+
for (let sourceIndex = 0; sourceIndex < result.length && !merged; sourceIndex += 1) {
|
|
98
|
+
const source = result[sourceIndex];
|
|
99
|
+
if (source === undefined || !("declarations" in source) || !isConditionalRun(source)) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const sourceFamilies = runFamilies(source.declarations);
|
|
103
|
+
if (sourceFamilies === null) continue;
|
|
104
|
+
|
|
105
|
+
for (
|
|
106
|
+
let destinationIndex = sourceIndex + 1;
|
|
107
|
+
destinationIndex < result.length;
|
|
108
|
+
destinationIndex += 1
|
|
109
|
+
) {
|
|
110
|
+
const candidate = result[destinationIndex];
|
|
111
|
+
if (candidate === undefined) break;
|
|
112
|
+
if (!("declarations" in candidate)) {
|
|
113
|
+
// A native block or descriptor declaration is a hard barrier: no
|
|
114
|
+
// later sibling can be reached without changing native context.
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
if (
|
|
118
|
+
isConditionalRun(candidate) &&
|
|
119
|
+
sameMergeRun(source, candidate) &&
|
|
120
|
+
runFamilies(candidate.declarations) !== null
|
|
121
|
+
) {
|
|
122
|
+
result[destinationIndex] = mergeInto(source, candidate);
|
|
123
|
+
result.splice(sourceIndex, 1);
|
|
124
|
+
merged = true;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
// The candidate is an intervening run. A wildcard or a shared family
|
|
128
|
+
// blocks this source (and every later destination).
|
|
129
|
+
const crossed = runFamilies(candidate.declarations);
|
|
130
|
+
if (crossed === null || intersects(crossed, sourceFamilies)) break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Recurse into native blocks, then merge each sibling list independently. */
|
|
138
|
+
function mergeChildren(children: readonly NativeBlockChild[]): readonly NativeBlockChild[] {
|
|
139
|
+
const rebuilt: NativeBlockChild[] = children.map((child) => {
|
|
140
|
+
if (!("children" in child)) return child;
|
|
141
|
+
if (child.statement === true) return child;
|
|
142
|
+
return nativeBlockIr({
|
|
143
|
+
name: child.name,
|
|
144
|
+
prelude: child.prelude,
|
|
145
|
+
children: mergeChildren(child.children),
|
|
146
|
+
span: child.span,
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
return mergeSiblingRuns(rebuilt);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Apply merge grouping to an already safe-grouped stylesheet.
|
|
154
|
+
*
|
|
155
|
+
* The public `groupRuns(ir, "merge")` entry point runs the safe adjacent pass
|
|
156
|
+
* first, so `merge` is always a superset of `safe`. This function only adds the
|
|
157
|
+
* cross-run pass and never merges unconditional runs.
|
|
158
|
+
*/
|
|
159
|
+
export function mergeRuns(ir: StylesheetIr): StylesheetIr {
|
|
160
|
+
// Top-level stylesheets hold only rules and native blocks; the child helper's
|
|
161
|
+
// wider `NativeBlockChild` type exists for descriptor at-rules.
|
|
162
|
+
return stylesheetIr(mergeChildren(ir.rules) as readonly (RuleIr | NativeBlockIr)[]);
|
|
163
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Property-family classification for cascade-safe media-run merging (ADR-0024).
|
|
3
|
+
*
|
|
4
|
+
* The merge optimizer may move a conditional declaration run past intervening
|
|
5
|
+
* declarations only when no crossed declaration can compete for the same
|
|
6
|
+
* computed property. CSS cascade competition is per property: a winner for
|
|
7
|
+
* `color` never changes the winner for `padding`. A conservative *family*
|
|
8
|
+
* therefore groups every property that can write the same underlying value:
|
|
9
|
+
*
|
|
10
|
+
* - strip one vendor prefix (`-webkit-`, `-moz-`, `-ms-`, `-o-`);
|
|
11
|
+
* - take the segment before the first `-` (`background-color` → `background`,
|
|
12
|
+
* `border-left-color` → `border`, `grid-template-columns` → `grid`,
|
|
13
|
+
* `margin-block` → `margin`);
|
|
14
|
+
* - keep a custom property's full name (`--brand` is its own family and is
|
|
15
|
+
* case-sensitive).
|
|
16
|
+
*
|
|
17
|
+
* Standard shorthands share their longhands' first segment (`background`,
|
|
18
|
+
* `border`, `font`, `grid`, `margin`, `mask`, `outline`, `transition`, …), so
|
|
19
|
+
* the prefix rule already blocks shorthand/longhand competition. The frozen
|
|
20
|
+
* {@link SHORTHAND_FAMILIES} table covers the standard properties whose
|
|
21
|
+
* longhands do **not** share that segment (`inset` vs `top`, `place-*` vs
|
|
22
|
+
* `align-*`/`justify-*`, `columns` vs `column-count`, `gap` vs `row-gap`,
|
|
23
|
+
* `white-space` vs `text-wrap`, `font` vs `line-height`, logical `inline-size`
|
|
24
|
+
* vs `width`, and the legacy aliases) plus the legacy vendor spellings of the
|
|
25
|
+
* same values. Collapsing those groups is deliberately conservative: `width`
|
|
26
|
+
* and `height` never compete, but both use the synthetic `size` family.
|
|
27
|
+
*
|
|
28
|
+
* Two declaration classes are **wildcards** and are never mergeable and never
|
|
29
|
+
* crossed:
|
|
30
|
+
*
|
|
31
|
+
* - the `all` property, which resets every property; and
|
|
32
|
+
* - any declaration whose value contains `var(`, because a custom property set
|
|
33
|
+
* by a crossed declaration could change the substituted value.
|
|
34
|
+
*
|
|
35
|
+
* Unknown *cross-prefix* shorthands are the documented limit of this
|
|
36
|
+
* approximation (ADR-0024): a missed merge is acceptable, a wrong cascade is
|
|
37
|
+
* not. Extend the frozen table when a new such property enters the language;
|
|
38
|
+
* do not add speculative heuristics here.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/** Vendor prefixes the family rule strips, in the ADR-0024 order. */
|
|
42
|
+
const VENDOR_PREFIX = /^-(?:webkit|moz|ms|o)-/;
|
|
43
|
+
|
|
44
|
+
/** A `var()` substitution makes a declaration depend on crossed declarations. */
|
|
45
|
+
const VAR_REFERENCE = /var\s*\(/i;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Frozen equivalence families for shorthands, logical properties and legacy
|
|
49
|
+
* aliases whose members do not share the first textual segment.
|
|
50
|
+
*
|
|
51
|
+
* Values are the family names `propertyFamily` compares. A key is the
|
|
52
|
+
* unprefixed, lowercased property name. Entries intentionally over-block
|
|
53
|
+
* (for example `width`/`height` share `size`); they never under-block a known
|
|
54
|
+
* shorthand relationship.
|
|
55
|
+
*/
|
|
56
|
+
const SHORTHAND_FAMILIES: Readonly<Record<string, string>> = Object.freeze({
|
|
57
|
+
// `inset` and the physical offsets it expands to.
|
|
58
|
+
top: "inset",
|
|
59
|
+
right: "inset",
|
|
60
|
+
bottom: "inset",
|
|
61
|
+
left: "inset",
|
|
62
|
+
|
|
63
|
+
// Logical sizing competes with physical `width`/`height`; over-block both.
|
|
64
|
+
width: "size",
|
|
65
|
+
height: "size",
|
|
66
|
+
"inline-size": "size",
|
|
67
|
+
"block-size": "size",
|
|
68
|
+
"logical-width": "size",
|
|
69
|
+
"logical-height": "size",
|
|
70
|
+
"min-logical-width": "min",
|
|
71
|
+
"max-logical-width": "max",
|
|
72
|
+
"min-logical-height": "min",
|
|
73
|
+
"max-logical-height": "max",
|
|
74
|
+
|
|
75
|
+
// `place-*` shorthands expand into the `align-*` and `justify-*` longhands.
|
|
76
|
+
"align-content": "align",
|
|
77
|
+
"align-items": "align",
|
|
78
|
+
"align-self": "align",
|
|
79
|
+
"justify-content": "align",
|
|
80
|
+
"justify-items": "align",
|
|
81
|
+
"justify-self": "align",
|
|
82
|
+
"place-content": "align",
|
|
83
|
+
"place-items": "align",
|
|
84
|
+
"place-self": "align",
|
|
85
|
+
"flex-align": "align",
|
|
86
|
+
"flex-item-align": "align",
|
|
87
|
+
"flex-line-pack": "align",
|
|
88
|
+
"flex-pack": "align",
|
|
89
|
+
"box-align": "align",
|
|
90
|
+
"box-pack": "align",
|
|
91
|
+
|
|
92
|
+
// `gap` expands into row/column gaps; legacy grid-gap spellings follow.
|
|
93
|
+
gap: "gap",
|
|
94
|
+
"row-gap": "gap",
|
|
95
|
+
"column-gap": "gap",
|
|
96
|
+
"grid-gap": "gap",
|
|
97
|
+
"grid-row-gap": "gap",
|
|
98
|
+
"grid-column-gap": "gap",
|
|
99
|
+
|
|
100
|
+
// `columns` expands into `column-width` and `column-count`.
|
|
101
|
+
columns: "columns",
|
|
102
|
+
"column-width": "columns",
|
|
103
|
+
"column-count": "columns",
|
|
104
|
+
|
|
105
|
+
// `line-clamp` expands into these properties.
|
|
106
|
+
"line-clamp": "line",
|
|
107
|
+
"max-lines": "line",
|
|
108
|
+
"block-ellipsis": "line",
|
|
109
|
+
continue: "line",
|
|
110
|
+
|
|
111
|
+
// `white-space` also expands into the `text-wrap-*` longhands.
|
|
112
|
+
"white-space": "white",
|
|
113
|
+
"white-space-collapse": "white",
|
|
114
|
+
"white-space-trim": "white",
|
|
115
|
+
"text-wrap": "white",
|
|
116
|
+
"text-wrap-mode": "white",
|
|
117
|
+
"text-wrap-style": "white",
|
|
118
|
+
|
|
119
|
+
// The `font` shorthand resets `line-height`.
|
|
120
|
+
"line-height": "font",
|
|
121
|
+
"line-height-step": "font",
|
|
122
|
+
|
|
123
|
+
// Page-break aliases and the fragmentation properties they set.
|
|
124
|
+
"break-before": "break",
|
|
125
|
+
"break-after": "break",
|
|
126
|
+
"break-inside": "break",
|
|
127
|
+
"page-break-before": "break",
|
|
128
|
+
"page-break-after": "break",
|
|
129
|
+
"page-break-inside": "break",
|
|
130
|
+
"column-break-before": "break",
|
|
131
|
+
"column-break-after": "break",
|
|
132
|
+
"column-break-inside": "break",
|
|
133
|
+
|
|
134
|
+
// `word-wrap` is the legacy name of `overflow-wrap`.
|
|
135
|
+
"word-wrap": "overflow-wrap",
|
|
136
|
+
"overflow-wrap": "overflow-wrap",
|
|
137
|
+
|
|
138
|
+
// `color-adjust` is the legacy name of `print-color-adjust`.
|
|
139
|
+
"color-adjust": "print-color-adjust",
|
|
140
|
+
"print-color-adjust": "print-color-adjust",
|
|
141
|
+
|
|
142
|
+
// The legacy `inset-area` name aliases `position-area`.
|
|
143
|
+
"inset-area": "position",
|
|
144
|
+
|
|
145
|
+
// The legacy `motion` family aliases the `offset` properties.
|
|
146
|
+
motion: "offset",
|
|
147
|
+
"motion-path": "offset",
|
|
148
|
+
"motion-offset": "offset",
|
|
149
|
+
"motion-rotation": "offset",
|
|
150
|
+
|
|
151
|
+
// Legacy `order` and flexbox aliases.
|
|
152
|
+
"flex-order": "order",
|
|
153
|
+
"box-ordinal-group": "order",
|
|
154
|
+
"box-direction": "flex",
|
|
155
|
+
"box-flex": "flex",
|
|
156
|
+
"box-lines": "flex",
|
|
157
|
+
"box-orient": "flex",
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Family of one declaration, or `null` when the declaration is a wildcard.
|
|
162
|
+
*
|
|
163
|
+
* @param property Authored property name, custom properties included.
|
|
164
|
+
* @param value Authored value slice; `!important` does not affect the result.
|
|
165
|
+
* @returns The conservative family key, or `null` for `all`/`var()` barriers.
|
|
166
|
+
*/
|
|
167
|
+
export function propertyFamily(property: string, value: string): string | null {
|
|
168
|
+
if (VAR_REFERENCE.test(value)) return null;
|
|
169
|
+
|
|
170
|
+
if (property.startsWith("--")) {
|
|
171
|
+
// Custom property names are case-sensitive and never share a family.
|
|
172
|
+
return property;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const lower = property.trim().toLowerCase();
|
|
176
|
+
if (lower === "" || lower === "all") return null;
|
|
177
|
+
|
|
178
|
+
const unprefixed = lower.replace(VENDOR_PREFIX, "");
|
|
179
|
+
if (unprefixed.startsWith("-")) {
|
|
180
|
+
// Unknown vendor or malformed property: be safe and treat it as a barrier.
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const override = SHORTHAND_FAMILIES[unprefixed];
|
|
185
|
+
if (override !== undefined) return override;
|
|
186
|
+
|
|
187
|
+
const dash = unprefixed.indexOf("-");
|
|
188
|
+
return dash < 0 ? unprefixed : unprefixed.slice(0, dash);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Whether one declaration is a wildcard barrier (`all` or a `var()` value). */
|
|
192
|
+
export function isWildcardDeclaration(property: string, value: string): boolean {
|
|
193
|
+
return propertyFamily(property, value) === null;
|
|
194
|
+
}
|