@@ -67,6 +67,72 @@ because they're named exactly the same as the corresponding section of the Synta
67
67
* ` parseAListOfComponentValues() `
68
68
* ` parseACommaSeparatedListOfComponentValues() `
69
69
70
+ Canonicalizing Against A Grammar
71
+ --------------------------------
72
+
73
+ By default, the parser can only do so much;
74
+ it knows how to interpret the top-level rules in a stylesheet,
75
+ but not how to interpret the contents of anything below that.
76
+ This means that anything nested within a top-level block is left as a bare token stream,
77
+ requiring you to call the correct parsing function on it.
78
+
79
+ The ` canonicalize() ` function takes a parsing result and a grammar
80
+ and transforms the result accordingly,
81
+ rendering the result into an easier-to-digest form.
82
+
83
+ A grammar is an object with one of the following four forms:
84
+
85
+ 1 . ```
86
+ {"stylesheet":true}
87
+ ```
88
+
89
+ 2. ```
90
+ {
91
+ "qualified": <grammar>,
92
+ "@foo": <grammar>,
93
+ "unknown": <function>
94
+ }
95
+ ```
96
+
97
+ 3. ```
98
+ {
99
+ "declarations": true,
100
+ "@foo": <grammar>
101
+ "unknown": <function>
102
+ }
103
+ ```
104
+
105
+ 4. `null`
106
+
107
+ A `stylesheet` block contains nothing else;
108
+ it just means that this rule uses the top-level grammar for its contents.
109
+ This is true, for example, of the `@media` rule.
110
+
111
+ A `qualified` block means that the rule's contents are qualified rules (style rules) and at-rules.
112
+ The "qualified" key must have another grammar as its value (often `{declarations:true}`).
113
+ Any at-rules that are valid in this context must be listed,
114
+ also with a grammar for their contents.
115
+ Optionally, the "unknown" key can be provided with a function value;
116
+ this will be called with any unknown at-rules (ones not listed in the grammar)/
117
+ If it returns a truthy value, it's inserted into the structure with everything else;
118
+ if falsey, the rule is put into the "errors" entry of the resulting block for later processing or ignoring.
119
+
120
+ A `declarations` block means that the rule's contents are declarations and at-rules.
121
+ Currently, the "declarations" key only accepts the value `true`;
122
+ eventually it'll allow you to specify what declarations are valid.
123
+ Similar to `qualified` blocks,
124
+ you must list what at-rules are allowed,
125
+ and can provide an "unknown" function.
126
+
127
+ A `null` just means that the block has no contents.
128
+ This is used for at-rules that are statements,
129
+ ended with a semicolon rather than a block,
130
+ like `@import`.
131
+
132
+ A `CSSGrammar` object is provided with a default grammar for CSS.
133
+ If you call `canonicalize()` without a grammar,
134
+ this is used automatically.
135
+
70
136
Node Integration
71
137
----------------
72
138
0 commit comments