-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathOverview.bs
158 lines (131 loc) · 4.89 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<pre class='metadata'>
Title: CSS Parser API
Shortname: css-parser-api
Level: 1
Status: UD
Group: HOUDINI
URL: https://drafts.css-houdini.org/css-parser-api/
Editor: Tab Atkins-Bittner
Abstract: An API exposing the CSS parser more directly,
Abstract: for parsing arbitrary CSS-like languages into a mildly typed representation.
</pre>
Introduction {#intro}
=====================
Common data-interchange / parsing formats are very valuable
for reducing the learning curve of new languages,
as users get to lean on their existing knowledge of the format when authoring
and only have to newly learn the specifics of the language.
This is why generic parsing formats like XML or JSON have become so popular.
The CSS language could benefit from this same treatment;
a number of languages and tools rely on CSS-like syntax to express themselves,
but they usually rely on ad-hoc parsing
(often regex-based)
which can be relatively fragile,
and might break with CSS practices in interesting syntax corner cases.
Similarly, CSS syntax is increasingly used in places like attribute values
(such as the <{img/sizes}> attribute,
or most of the SVG presentation attributes),
and custom elements wanting to do the same thing
similarly have to rely on ad-hoc parsing right now.
To help with these sorts of cases,
this spec exposes the [[!css-syntax-3]] parsing algorithms,
and represents their results in a mildly-typed representation,
simpler and more abstract than what [[css-typed-om-1]] does for CSS properties.
Parsing API {#parsing-api}
==========================
<pre class=idl>
partial interface CSS {
sequence<CSSParserRule> parseStylesheet(DOMString css, optional CSSParserOptions options);
sequence<CSSParserRule> parseRuleList(DOMString css, optional CSSParserOptions options);
CSSParserRule parseRule(DOMString css, optional CSSParserOptions options);
sequence<(CSSParserDeclaration or CSSParserRule)> parseDeclarationList(DOMString css, optional CSSParserOptions options);
CSSParserDeclaration parseDeclaration(DOMString css, optional CSSParserOptions options);
CSSParserValue parseValue(DOMString css);
sequence<CSSParserValue> parseValueList(DOMString css);
sequence<sequence<CSSParserValue>> parseCommaValueList(DOMString css);
};
dictionary CSSParserOptions {
object atRules;
/* dict of at-rule name => at-rule type
(contains decls or contains qualified rules) */
};
</pre>
Issue: {{parseCommaValueList()}} is in Syntax, and thus here,
because it's actually a very common operation.
It's trivial to do yourself
(just call {{parseValueList()}} and then split into an array on top-level commas),
but comma-separated lists are so common
that it was worthwhile to improve spec ergonomics
by providing a shortcut for that functionality.
Is it worth it to provide this to JS as well?
Issue: Do we handle comments?
Currently I don't;
Syntax by default just drops comments,
but allows an impl to preserve information about them if they want.
Maybe add an option to preserve comments?
If so, they can appear *anywhere*,
in any API that returns a sequence.
Issue: What do we do if an unknown at-rule
(not appearing in the {{atRules}} option)
shows up in the results?
Default to decls or rules?
Or treat it more simply as just a token sequence?
Parser Values {#parser-values}
==============================
<pre class=idl>
interface CSSParserRule {
};
interface CSSParserAtRule : CSSParserRule {
attribute DOMString name;
attribute FrozenArray<CSSParserValue> prelude;
attribute FrozenArray<CSSParserRule>? body;
/* nullable to handle at-statements */
};
interface CSSParserQualifiedRule : CSSParserRule {
attribute FrozenArray<CSSParserValue> prelude;
attribute FrozenArray<CSSParserRule> body;
};
interface CSSParserDeclaration : CSSParserRule {
attribute DOMString name;
attribute FrozenArray<CSSParserValue> body;
};
interface CSSParserValue {
};
interface CSSParserBlock : CSSParserValue {
attribute DOMString name; /* "[]", "{}", or "()" */
attribute FrozenArray<CSSParserValue> body;
};
interface CSSParserFunction : CSSParserValue {
attribute DOMString name;
attribute FrozenArray<FrozenArray<CSSParserValue>> args;
};
interface CSSParserIdent : CSSParserValue {
attribute DOMString value;
};
interface CSSParserNumber : CSSParserValue {
attribute double value;
};
interface CSSParserPercentage : CSSParserValue {
attribute double value;
};
interface CSSParserDimension : CSSParserValue {
attribute double value;
attribute DOMString type;
};
interface CSSParserAtKeyword : CSSParserValue {
attribute DOMString value;
};
interface CSSParserHash : CSSParserValue {
attribute DOMString value;
/* expose an "is ident" boolean? */
};
interface CSSParserString : CSSParserValue {
attribute DOMString value;
};
interface CSSParserChar : CSSParserValue {
attribute DOMString value;
/* for all delims, whitespace, and the
weird Selectors-based tokens
(split up into the individual chars) */
};
</pre>