Skip to content

Commit 5bbf49a

Browse files
committed
Supports 3 modes
- Implemented atomic, deep, simple modes - Updated README.md to roughly describe new formats
1 parent 0114f3f commit 5bbf49a

11 files changed

+1140
-885
lines changed

README.md

+299-214
Large diffs are not rendered by default.

lib/cssparser.js

+486-400
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"validation",
1414
"transform"
1515
],
16-
"version": "0.2.2",
16+
"version": "0.9.0",
1717
"preferGlobal": true,
1818
"repository": {
1919
"type": "git",
@@ -31,35 +31,29 @@
3131
},
3232
"dependencies": {
3333
"colors": "^1.1.2",
34-
"jison-lex": "^0.3.4",
3534
"lodash": "^4.17.4",
3635
"nomnom": ">= 1.5.x"
3736
},
3837
"devDependencies": {
3938
"babel-cli": "^6.23.0",
4039
"babel-preset-env": "^1.1.10",
41-
"grunt": "~0.4.x",
42-
"grunt-contrib-clean": "~0.4.0",
43-
"grunt-contrib-coffee": "~0.6.0",
44-
"grunt-contrib-concat": "~0.1.2",
45-
"grunt-contrib-copy": "~0.4.0",
46-
"grunt-contrib-uglify": "~0.1.2",
47-
"grunt-contrib-yuidoc": "~0.4.0",
4840
"gulp": "^3.9.1",
4941
"gulp-file-include": "^1.0.0",
50-
"jison": ">= 0.4.4"
42+
"jison": ">= 0.4.4",
43+
"jison-lex": "^0.3.4"
5144
},
5245
"scripts": {
5346
"es6": "gulp merge-js && babel dist/js -d dist/babel",
5447
"generate": "gulp merge-jison && jison dist/jison/cssparser.y dist/jison/css.l -o lib/cssparser.js",
5548
"build": "npm run es6 && npm run generate",
5649
"test-simple": "node lib/cli.js test/test.css -c -t simple",
5750
"test-deep": "node lib/cli.js test/test.css -c -t deep",
58-
"test-atomic": "node lib/cli.js test/test.css -c -t deep",
51+
"test-atomic": "node lib/cli.js test/test.css -c -t atomic",
5952
"test-all": "npm run test-simple && npm run test-deep && npm run test-atomic",
6053
"copy-web": "cp ./lib/cssparser.js ./demo/",
6154
"release": "npm run build && npm run copy-web",
62-
"test": "npm run release && npm run test-all"
55+
"test": "npm run release && npm run test-all",
56+
"inspect": "node --inspect --debug-brk lib/cli.js test/test.css -c -t "
6357
},
6458
"homepage": "https://cwdoh.github.io/cssparser.js/",
6559
"optionalDependencies": {},

src/nodes/at_rule.js

+34-63
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ class AtRule extends CSSObject {
33
return 'AT_RULE'
44
}
55

6-
toJSON() {
6+
toSimpleJSON() {
77
return {
8-
type: this.getType(),
9-
rule: toJSON(this.get('rule', null)),
10-
value: toJSON(this.get('value', null))
8+
type: '@' + toSimple(this.get('rule')),
9+
value: toSimple(this.get('value'))
1110
}
1211
}
1312

@@ -16,7 +15,9 @@ class AtRule extends CSSObject {
1615
var result = rule.match(regexp)
1716

1817
if (result) {
19-
this.set('rule', IdentVal.create(result[1]))
18+
var identVal = IdentVal.create(result[1])
19+
identVal.set('prefix', '@')
20+
this.set('rule', identVal)
2021
}
2122

2223
return this
@@ -30,26 +31,23 @@ class AtCharset extends AtRule {
3031
}
3132

3233
class AtImport extends AtRule {
33-
toJSON() {
34-
var json = super.toJSON()
35-
json.nextExpression = toJSON(this.get('nextExpression', null))
36-
37-
return json
34+
toSimpleJSON() {
35+
return mixin(super.toSimpleJSON(), {
36+
mediaQuery: toSimple(this.get('nextExpression'))
37+
})
3838
}
39-
39+
4040
static create(rule) {
4141
return new AtImport().setRule(rule)
4242
}
4343
}
4444

4545
class AtNamespace extends AtRule {
46-
toJSON() {
47-
var json = super.toJSON()
48-
json.prefix = toJSON(this.get('prefix', null))
49-
50-
return json
46+
toSimpleJSON() {
47+
return mixin(super.toSimpleJSON(), {
48+
prefix: toSimple(this.get('prefix'))
49+
})
5150
}
52-
5351
static create(rule) {
5452
return new AtNamespace().setRule(rule)
5553
}
@@ -62,11 +60,10 @@ class AtFontface extends AtRule {
6260
}
6361

6462
class AtNestedRule extends AtRule {
65-
toJSON() {
66-
var json = super.toJSON()
67-
json.nestedRules = toJSON(this.get('nestedRules', null))
68-
69-
return json
63+
toSimpleJSON() {
64+
return mixin(super.toSimpleJSON(), {
65+
nestedRules: toSimple(this.get('nestedRules'))
66+
})
7067
}
7168
}
7269

@@ -77,12 +74,11 @@ class AtMedia extends AtNestedRule {
7774
}
7875

7976
class AtKeyframes extends AtRule {
80-
toJSON() {
81-
var json = super.toJSON()
82-
83-
json.name = toJSON(this.get('name'))
84-
85-
return json
77+
toSimpleJSON() {
78+
return {
79+
type: '@' + toSimple(this.get('rule')),
80+
keyframes: toSimple(this.get('value'))
81+
}
8682
}
8783

8884
static create(rule) {
@@ -96,21 +92,13 @@ class AtKeyframesBlockList extends CSSObject {
9692
return 'KEYFRAME_BLOCK_LIST'
9793
}
9894

99-
add(block) {
100-
if (!this.value) {
101-
this.value = []
102-
}
103-
104-
this.value.push(block)
95+
toSimpleJSON() {
96+
var json = {}
97+
toSimple(this.get('value')).map((o) => {
98+
mixin(json, o)
99+
})
105100

106-
return this
107-
}
108-
109-
toJSON() {
110-
return {
111-
type: this.getType(),
112-
value: this.get('value', []).map((o) => toJSON(o))
113-
}
101+
return json
114102
}
115103

116104
static create() {
@@ -123,10 +111,10 @@ class AtKeyframesBlock extends CSSObject {
123111
return 'KEYFRAME_BLOCK'
124112
}
125113

126-
toJSON() {
127-
var json = super.toJSON()
128-
json.selector = toJSON(this.get('selector', null))
129-
114+
toSimpleJSON() {
115+
var json = {}
116+
json[toSimple(this.get('selector'))] = toSimple(this.get('value'))
117+
130118
return json
131119
}
132120

@@ -137,14 +125,6 @@ class AtKeyframesBlock extends CSSObject {
137125
}
138126

139127
class AtSupport extends AtNestedRule {
140-
toJSON() {
141-
var json = super.toJSON()
142-
json.property = toJSON(this.get('property', null))
143-
json.operator = toJSON(this.get('operator', null))
144-
145-
return json
146-
}
147-
148128
static create(rule) {
149129
return new AtSupport().setRule(rule)
150130
}
@@ -155,15 +135,6 @@ class AtSupportExpression extends CSSObject {
155135
return 'SUPPORT_EXPRESSION'
156136
}
157137

158-
toJSON() {
159-
var json = super.toJSON()
160-
json.property = toJSON(this.get('property', null))
161-
json.operator = toJSON(this.get('operator', null))
162-
json.nextExpression = toJSON(this.get('nextExpression', null))
163-
164-
return json
165-
}
166-
167138
static create(selector) {
168139
return new AtSupportExpression()
169140
}

src/nodes/declaration.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ class QualifiedRule extends CSSObject {
33
return 'QUALIFIED_RULE'
44
}
55

6-
toJSON() {
6+
toSimpleJSON() {
77
return {
8-
type: this.getType(),
9-
selectors: toJSON(this.get('selectors')),
10-
value: toJSON(this.get('value'))
8+
selectors: toSimple(this.get('selectors')),
9+
declarations: toSimple(this.get('value'))
1110
}
1211
}
1312

@@ -22,14 +21,11 @@ class Declaration extends CSSObject {
2221
return 'DECLARATION'
2322
}
2423

25-
toJSON() {
26-
return {
27-
type: this.getType(),
28-
property: toJSON(this.get('property')),
29-
value: toJSON(this.get('value')),
30-
important: this.get('important', false),
31-
ieOnlyHack: toJSON(this.get('ieOnlyHack', false))
32-
}
24+
toSimpleJSON() {
25+
var json = {}
26+
json[toSimple(this.get('property'))] = toSimple(this.get('value'))
27+
28+
return json
3329
}
3430

3531
static create(property, value) {
@@ -44,11 +40,14 @@ class DeclarationList extends CSSObject {
4440
return 'DECLARATION_LIST'
4541
}
4642

47-
toJSON() {
48-
return {
49-
type: this.getType(),
50-
value: this.get('value').map((o) => toJSON(o))
51-
}
43+
toSimpleJSON() {
44+
var json = {}
45+
46+
toSimple(this.get('value')).map((o) => {
47+
mixin(json, o)
48+
})
49+
50+
return json
5251
}
5352

5453
static create(value) {

src/nodes/mediaQuery.js

+40-27
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,12 @@ class MediaQueryList extends CSSObject {
33
return 'MEDIA_QUERY_LIST'
44
}
55

6-
add(mediaQuery) {
7-
if (!this.value) {
8-
this.value = []
9-
}
10-
11-
if (mediaQuery) {
12-
this.value.push(mediaQuery)
13-
}
14-
15-
return this
6+
toDeepJSON() {
7+
return this.toSimpleJSON()
168
}
179

18-
toJSON() {
19-
return {
20-
type: this.getType(),
21-
value: this.get('value').map((o) => toJSON(o))
22-
}
10+
toSimpleJSON() {
11+
return toSimple(this.get('value'))
2312
}
2413

2514
static create() {
@@ -32,15 +21,26 @@ class MediaQuery extends CSSObject {
3221
return 'MEDIA_QUERY'
3322
}
3423

35-
toJSON() {
36-
return {
37-
type: this.getType(),
38-
mediaType: toJSON(this.get('mediaType', null)),
39-
prefix: toJSON(this.get('prefix', null)),
40-
nextExpression: toJSON(this.get('nextExpression', null))
24+
toDeepJSON() {
25+
return this.toSimpleJSON()
26+
}
27+
28+
toSimpleJSON() {
29+
var json = toSimple(this.get('mediaType'))
30+
31+
var prefix = this.get('prefix')
32+
if (prefix) {
33+
json = toSimple(prefix) + ' ' + json
34+
}
35+
var nextExpression = this.get('nextExpression')
36+
if (nextExpression) {
37+
json += ' ' + toSimple(nextExpression)
4138
}
39+
40+
return json
4241
}
4342

43+
4444
static create() {
4545
return new MediaQuery()
4646
}
@@ -51,13 +51,26 @@ class MediaQueryExpression extends CSSObject {
5151
return 'MEDIA_QUERY_EXPRESSION'
5252
}
5353

54-
toJSON() {
55-
return {
56-
type: this.getType(),
57-
feature: toJSON(this.get('mediaFeature', null)),
58-
value: toJSON(this.get('value', null)),
59-
nextExpression: toJSON(this.get('nextExpression', null))
54+
toDeepJSON() {
55+
return this.toSimpleJSON()
56+
}
57+
58+
toSimpleJSON() {
59+
var expression = '(' + toSimple(this.get('mediaFeature'))
60+
61+
var value = toSimple(this.get('value'))
62+
if (value) {
63+
expression += ': ' + value
6064
}
65+
66+
expression += ')'
67+
68+
var nextExpression = this.get('nextExpression')
69+
if (nextExpression) {
70+
expression += ' ' + toSimple(nextExpression)
71+
}
72+
73+
return expression
6174
}
6275

6376
static create(feature, value) {

0 commit comments

Comments
 (0)