5
5
## Introduction
6
6
7
7
The ParserLib CSS parser is a CSS3 SAX-inspired parser written in JavaScript.
8
- By default, the parser only deals with standard CSS syntax and doesn't do validation (checking of property names and values).
8
+ It handles standard CSS syntax as well as validation (checking of
9
+ property names and values) although it is not guaranteed to thoroughly
10
+ validate all possible CSS properties.
9
11
10
12
## Adding to your project
11
13
12
- The CSS parser is intended for use primarily in command line JavaScript environments.
13
- The files you should use are in the ` build ` directory. Copy the files to an appropriate location for your usage.
14
+ The CSS parser is built for a number of different JavaScript
15
+ environments. The most recently released version of the parser
16
+ can be found in the ` dist ` directory when you check out the
17
+ repository; run ` npm run build ` to regenerate them from the
18
+ latest sources.
14
19
15
20
### Node.js
16
21
17
- To use the CSS parser in a Node.js script, include it at the beginning:
22
+ You can use the CSS parser in a ` Node.js ` script via the standard
23
+ ` npm ` package manager as the ` parserlib ` package (` npm install parserlib ` ):
24
+
25
+ ``` js
26
+ var parserlib = require (" parserlib" );
27
+
28
+ var parser = new parserlib.css.Parser ();
29
+ ```
30
+
31
+ Alternatively, you can copy a single file version of the parser from
32
+ ` dist/node-parserlib.js ` to your own project, and use it as follows:
18
33
``` js
19
34
var parserlib = require (" ./node-parserlib" );
20
35
```
21
36
22
37
### Rhino
23
38
24
- To use the CSS parser in a Rhino script, include it at the beginning:
39
+ To use the CSS parser in a Rhino script, copy the file
40
+ ` dist/parserlib.js ` to your project and then include it at the beginning:
25
41
``` js
26
42
load (" parserlib.js" );
27
43
```
@@ -37,7 +53,9 @@ Or include it as its component parts, the ParserLib core and the CSS parser:
37
53
<script src =" parserlib-core.js" ></script >
38
54
<script src =" parserlib-css.js" ></script >
39
55
```
40
- Note that parsing large JavaScript files may cause the browser to become unresponsive.
56
+ Note that parsing large JavaScript files may cause the browser to
57
+ become unresponsive. All three of these files are located in the
58
+ ` dist ` directory.
41
59
42
60
## Basic usage
43
61
@@ -360,11 +378,12 @@ parser.addListener("endrule", function(event) {
360
378
361
379
The ` property ` event fires whenever a CSS property (` name:value ` ) is encountered,
362
380
which may be inside of a rule, a media block, a page block, etc. The ` event ` object
363
- has three additional properties: ` property ` , which is the name of the property as a
381
+ has four additional properties: ` property ` , which is the name of the property as a
364
382
` parserlib.css.PropertyName ` object, ` value ` , which is an instance of
365
383
` parserlib.css.PropertyValue ` (both types inherit from ` parserlib.util.SyntaxUnit ` ),
366
- and ` important ` , which is a Boolean value indicating if the property is flagged
367
- with ` !important ` . Example:
384
+ ` important ` , which is a Boolean value indicating if the property is flagged
385
+ with ` !important ` , and ` invalid ` which is a Boolean value indicating
386
+ whether the property value failed validation. Example:
368
387
369
388
``` js
370
389
parser .addListener (" property" , function (event ) {
@@ -417,4 +436,5 @@ a:hover, foo ... bar {
417
436
418
437
## Running Tests
419
438
420
- With the Apache Ant build tool installed, you can run the tests via ` ant test ` from the repository's root.
439
+ You can run the tests via ` npm test ` from the repository's root. You
440
+ may need to run ` npm install ` first to install the necessary dependencies.
0 commit comments