Skip to content

Commit 6ea6269

Browse files
committed
Update README.md
[ci skip]
1 parent 3fc7ea6 commit 6ea6269

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ The constructor accepts an options object that specifies additional features the
5151
* `strict` - set to true to disable error recovery and stop on the first syntax error. Default is false.
5252

5353
Here's an example with some options set:
54+
5455
```js
5556
var parser = new parserlib.css.Parser({ starHack: true, underscoreHack: true });
5657
```
58+
5759
You can then parse a string of CSS code by passing into the `parse()` method:
60+
5861
```js
5962
parser.parse(someCSSText);
6063
```
64+
6165
The `parse()` method throws an error if a non-recoverable syntax error occurs, otherwise it finishes silently. This method does not return a value nor does it build up an abstract syntax tree (AST) for you, it simply parses the CSS text and fires events at important moments along the parse.
6266

6367
Note: The `parseStyleSheet()` method is provided for compatibility with SAC-based APIs but does the exact same thing as `parse()`.
@@ -80,9 +84,11 @@ The `parserlib.css.MediaQuery` type represents all parts of a media query. Each
8084
* `features` - an array of `parserlib.css.MediaFeature` objects
8185

8286
For example, consider the following media query:
87+
8388
```css
8489
only screen and (max-device-width: 768px) and (orientation:portrait)
8590
```
91+
8692
A corresponding object would have the following values:
8793

8894
* `modifier` = "only"
@@ -93,9 +99,9 @@ A corresponding object would have the following values:
9399

94100
The `parserlib.css.PropertyName` type represents a property name. Each instance has the following properties:
95101

96-
* `hack` - if star or underscore hacks are allowed, either "*" or "_" if present (`null` if not present or hacks are not allowed)
102+
* `hack` - if star or underscore hacks are allowed, either `*` or `_` if present (`null` if not present or hacks are not allowed)
97103

98-
When star hacks are allowed, the `text` property becomes the actual property name, so `*width` has `hack` equal to "*" and `text` equal to "width". If no hacks are allowed, then `*width` causes a syntax error while `_width` has `hack` equal to `null` and `text` equal to "_width".
104+
When star hacks are allowed, the `text` property becomes the actual property name, so `*width` has `hack` equal to `*` and `text` equal to "width". If no hacks are allowed, then `*width` causes a syntax error while `_width` has `hack` equal to `null` and `text` equal to "_width".
99105

100106
The `parserlib.css.PropertyValue` type represents a property value. Since property values in CSS are complex, this type of object wraps the various parts into a single interface. Each instance has the following properties:
101107

@@ -108,17 +114,21 @@ The `parserlib.css.PropertyValuePart` type represents an individual part of a pr
108114
* `type` - the type of value part ("unknown", "dimension", "percentage", "integer", "number", "color", "uri", "string", "identifier" or "operator")
109115

110116
A part is considered any atomic piece of a property value not including white space. Consider the following:
117+
111118
```css
112119
font: 1em/1.5em "Times New Roman", Times, serif;
113120
```
114-
The `PropertyName` is "font" and the `PropertyValue' represents everything after the colon. The parts are "1em" (dimension), "/" (operator), "1.5em" (dimension), "\"Times New Roman\"" (string), "," (operator), "Times" (identifier), "," (operator), and "serif" (identifier).
121+
122+
The `PropertyName` is "font" and the `PropertyValue` represents everything after the colon. The parts are "1em" (dimension), "/" (operator), "1.5em" (dimension), "Times New Roman" (string), "," (operator), "Times" (identifier), "," (operator), and "serif" (identifier).
115123

116124
### Selectors
117125

118126
The `parserlib.css.Selector` type represents a single selector. Each instance has a `parts` property, which is an array of `parserlib.css.SelectorPart` objects, which represent atomic parts of the selector, and `parserlib.css.Combinator` objects, which represent combinators in the selector. Consider the following selector:
127+
119128
```css
120129
li.selected > a:hover
121130
```
131+
122132
This selector has three parts: `li.selected`, `>`, and `a:hover`. The first part is a `SelectorPart`, the second is a `Combinator`, and the third is a `SelectorPart`. Each `SelectorPart` is made up of an optional element name followed by an ID, class, attribute condition, pseudo class, and/or pseudo element.
123133

124134
Each instance of `parserlib.css.SelectorPart` has an `elementName` property, which represents the element name as a `parserlib.css.SelectorSubPart` object or `null` if there isn't one, and a `modifiers` property, which is an array of `parserlib.css.SelectorSubPart` objects. Each `SelectorSubPart` object represents the smallest individual piece of a selector and has a `type` property indicating the type of subpart, "elementName", "class", "attribute", "pseudo", "id", "not". If the `type` is "not", then the `args` property contains an array of `SelectorPart` arguments that were passed to `not()`.
@@ -143,6 +153,7 @@ You should assign your event handlers before calling the `parse()` method.
143153
### `startstylesheet` and `endstylesheet` events
144154

145155
The `startstylesheet` event fires just before parsing of the CSS text begins and the `endstylesheet` event fires just after all of the CSS text has been parsed. There is no additional information provided for these events. Example:
156+
146157
```js
147158
parser.addListener("startstylesheet", function(){
148159
console.log("Starting to parse style sheet");
@@ -152,33 +163,41 @@ parser.addListener("endstylesheet", function(){
152163
console.log("Finished parsing style sheet");
153164
});
154165
```
166+
155167
### `charset` event
156168

157169
The `charset` event fires when the `@charset` directive is found in a style sheet. Since `@charset` is required to appear first in a style sheet, any other occurances cause a syntax error. The `charset` event provides an `event` object with a property called `charset`, which contains the name of the character set for the style sheet. Example:
170+
158171
```js
159172
parser.addListener("charset", function(event){
160173
console.log("Character set is " + event.charset);
161174
});
162175
```
176+
163177
### `namespace` event
164178

165179
The `namespace` event fires when the `@namespace` directive is found in a style sheet. The `namespace` event provides an `event` object with two properties: `prefix`, which is the namespace prefix, and `uri`, which is the namespace URI. Example:
180+
166181
```js
167182
parser.addListener("namespace", function(event){
168183
console.log("Namespace with prefix=" + event.prefix + " and URI=" + event.uri);
169184
});
170185
```
186+
171187
### `import` event
172188

173189
The `import` event fires when the `@import` directive is found in a style sheet. The `import` event provides an `event` object with two properties: `uri`, which is the URI to import, and `media`, which is an array of media queries for which this URI applies. The `media` array contains zero or more `parserlib.css.MediaQuery` objects. Example:
190+
174191
```js
175192
parser.addListener("import", function(event){
176193
console.log("Importing " + event.uri + " for media types [" + event.media + "]");
177194
});
178195
```
196+
179197
### `startfontface` and `endfontface` events
180198

181199
The `startfontface` event fires when `@font-face` is encountered and the `endfontface` event fires just after the closing right brace (`}`) is encountered after `@font-face`. There is no additional information available on the `event` object. Example:
200+
182201
```js
183202
parser.addListener("startfontface", function(event){
184203
console.log("Starting font face");
@@ -188,9 +207,11 @@ parser.addListener("endfontface", function(event){
188207
console.log("Ending font face");
189208
});
190209
```
210+
191211
### `startpage` and `endpage` events
192212

193213
The `startpage` event fires when `@page` is encountered and the `endpage` event fires just after the closing right brace (`}`) is encountered after `@page`. The `event` object has two properties: `id`, which is the page ID, and `pseudo`, which is the page pseudo class. Example:
214+
194215
```js
195216
parser.addListener("startpage", function(event){
196217
console.log("Starting page with ID=" + event.id + " and pseudo=" + event.pseudo);
@@ -200,48 +221,53 @@ parser.addListener("endpage", function(event){
200221
console.log("Ending page with ID=" + event.id + " and pseudo=" + event.pseudo);
201222
});
202223
```
224+
203225
### `startpagemargin` and `endpagemargin` events
204226

205227
The `startpagemargin` event fires when a page margin directive (such as `@top-left`) is encountered and the `endfontface` event fires just after the closing right brace (`}`) is encountered after the page margin. The `event` object has a `margin` property, which contains the actual page margin encountered. Example:
228+
206229
```js
207230
parser.addListener("startpagemargin", function(event){
208231
console.log("Starting page margin " + event.margin);
209232
});
210233

211-
212234
parser.addListener("endpagemargin", function(event){
213235
console.log("Ending page margin " + event.margin);
214236
});
215237
```
238+
216239
### `startmedia` and `endmedia` events
217240

218241
The `startmedia` event fires when `@media` is encountered and the `endmedia` event fires just after the closing right brace (`}`) is encountered after `@media`. The `event` object has one property, `media`, which is an array of `parserlib.css.MediaQuery` objects. Example:
242+
219243
```js
220244
parser.addListener("startpagemargin", function(event){
221245
console.log("Starting page margin " + event.margin);
222246
});
223247

224-
225248
parser.addListener("endpagemargin", function(event){
226249
console.log("Ending page margin " + event.margin);
227250
});
228251
```
252+
229253
### `startkeyframes` and `endkeyframes` events
230254

231255
The `startkeyframes` event fires when `@keyframes` (or any vendor prefixed version) is encountered and the `endkeyframes` event fires just after the closing right brace (`}`) is encountered after `@keyframes`. The `event` object has one property, `name`, which is the name of the animation. Example:
256+
232257
```js
233258
parser.addListener("startkeyframes", function(event){
234259
console.log("Starting animation definition " + event.name);
235260
});
236261

237-
238262
parser.addListener("endkeyframes", function(event){
239263
console.log("Ending animation definition " + event.name);
240264
});
241265
```
266+
242267
### `startrule` and `endrule` events
243268

244269
The `startrule` event fires just after all selectors on a rule have been parsed and the `endrule` event fires just after the closing right brace (`}`) is encountered for the rule. The `event` object has one additional property, `selectors`, which is an array of `parserlib.css.Selector` objects. Example:
270+
245271
```js
246272
parser.addListener("startrule", function(event){
247273
console.log("Starting rule with " + event.selectors.length + " selector(s)");
@@ -271,17 +297,21 @@ parser.addListener("endrule", function(event){
271297
console.log("Ending rule with selectors [" + event.selectors + "]");
272298
});
273299
```
300+
274301
### `property` event
275302

276303
The `property` event fires whenever a CSS property (`name:value`) is encountered, which may be inside of a rule, a media block, a page block, etc. The `event` object has three additional properties: `property`, which is the name of the property as a `parserlib.css.PropertyName` object, `value`, which is an instance of `parserlib.css.PropertyValue`(both types inherit from `parserlib.util.SyntaxUnit`), and `important`, which is a Boolean value indicating if the property is flagged with `!important`. Example:
304+
277305
```js
278306
parser.addListener("property", function(event){
279307
console.log("Property '" + event.property + "' has a value of '" + event.value + "' and " + (event.important ? "is" : "isn't") + " important. (" + event.property.line + "," + event.property.col + ")");
280308
});
281309
```
310+
282311
### `error` event
283312

284313
The `error` event fires whenever a recoverable error occurs during parsing. When in strict mode, this event does not fire. The `event` object contains three additional properties: `message`, which is the error message, `line`, which is the line on which the error occurred, and `col`, which is the column on that line in which the error occurred. Example:
314+
285315
```js
286316
parser.addListener("error", function(event){
287317
console.log("Parse error: " + event.message + " (" + event.line + "," + event.col + ")", "error");
@@ -293,21 +323,25 @@ parser.addListener("error", function(event){
293323
The CSS parser's goal is to be on-par with error recovery of CSS parsers in browsers. To that end, the following error recovery mechanisms are in place:
294324

295325
* **Properties** - a syntactically incorrect property definition will be skipped over completely. For instance, the second property below is dropped:
326+
296327
```css
297328
a:hover {
298329
color: red;
299330
font:: Helvetica; /*dropped!*/
300331
text-decoration: underline;
301332
}
302333
```
334+
303335
* **Selectors** - if there's a syntax error in *any* selector, the entire rule is skipped over. For instance, the following rule is completely skipped:
336+
304337
```css
305338
a:hover, foo ... bar {
306339
color: red;
307340
font: Helvetica;
308341
text-decoration: underline;
309342
}
310343
```
344+
311345
* **@ Rules** - there are certain @ rules that are only valid in certain contexts. The parser will skip over `@charset`, `@namespace`, and `@import` if they're found anywhere other than the beginning of the input.
312346

313347
* **Unknown @ Rules** - any @ rules that isn't recognized is automatically skipped, meaning the entire block after it is not parsed.

0 commit comments

Comments
 (0)