postcss 0.3.1 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of postcss might be problematic. Click here for more details.

package/ChangeLog.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## 0.3.5
2
+ * Allow to use `Root` or `Result` as first argument in `process()`.
3
+ * Save parsed AST to `Result#root`.
4
+
5
+ ## 0.3.4
6
+ * Better space symbol detect to read UTF-8 BOM correctly.
7
+
8
+ ## 0.3.3
9
+ * Remove source map hacks by using new Mozilla’s `source-map` (by Simon Lydell).
10
+
11
+ ## 0.3.2
12
+ * Add URI encoding support for inline source maps.
13
+
1
14
  ## 0.3.1
2
15
  * Fix relative paths from previous source map.
3
16
  * Safer space split in `Rule#selectors` (by Simon Lydell).
package/README.md CHANGED
@@ -26,10 +26,19 @@ Sponsored by [Evil Martians](http://evilmartians.com/).
26
26
  * [Autoprefixer] adds vendor prefixes by Can I Use data.
27
27
  * [grunt-pixrem], `rem` unit polyfill.
28
28
  * [CSS MQPacker] joins same media queries.
29
-
30
- [Autoprefixer]: https://github.com/ai/autoprefixer
31
- [grunt-pixrem]: https://github.com/robwierzbowski/grunt-pixrem
32
- [CSS MQPacker]: https://github.com/hail2u/node-css-mqpacker
29
+ * [RTLCSS] mirrors styles for right-to-left locales.
30
+ * [CSSWring] and [grunt-csswring] CSS minifier with full source map support.
31
+ * [Grunt-webpcss] to duplicate images in CSS to WebP for supported browsers.
32
+ * [Pleeease] is a pack of various postprocessors.
33
+
34
+ [grunt-csswring]: https://github.com/princed/grunt-csswring
35
+ [Grunt-webpcss]: https://github.com/lexich/grunt-webpcss
36
+ [Autoprefixer]: https://github.com/ai/autoprefixer
37
+ [grunt-pixrem]: https://github.com/robwierzbowski/grunt-pixrem
38
+ [CSS MQPacker]: https://github.com/hail2u/node-css-mqpacker
39
+ [Pleeease]: http://pleeease.io/
40
+ [CSSWring]: https://github.com/hail2u/node-csswring
41
+ [RTLCSS]: https://github.com/MohammadYounes/rtlcss
33
42
 
34
43
  ## Quick Example
35
44
 
@@ -183,6 +192,39 @@ Because of this background difference, PostCSS:
183
192
 
184
193
  ## Usage
185
194
 
195
+ You can parse CSS by `postcss.parse()` method, which returns CSS AST:
196
+
197
+ ```js
198
+ var postcss = require('postcss');
199
+
200
+ var css = postcss.parse('a { color: black }');
201
+ ```
202
+
203
+ Then you can change this AST. Use `css.list` to get childs.
204
+ Properties `rule.selector`, `decl.prop`, `decl.value`, `atrule.name`
205
+ and `atrule.params` contain data.
206
+
207
+ Don’t use underscore properties (like `_selector`, `_params` and `_value`),
208
+ because they are only for comments save magic
209
+ (See [Raw Properties](#Raw Properties) below). Use getters and setters instead
210
+ (like `selector`, `selectors`, `params` and `value`).
211
+
212
+ ```js
213
+ css.list[0].value = 'white';
214
+ ```
215
+
216
+ After changes you can get new CSS and modification’s source map:
217
+
218
+ ```js
219
+ var result = css.toResult(options);
220
+
221
+ result.css //=> 'a { color: white }'
222
+ result.map //=> '{"version":3, … }'
223
+ ```
224
+
225
+ Methods `postcss.parse()` and `CSS#toResult()` are low level API, for most cases
226
+ it will be better to create processors with simplier API and chaining.
227
+
186
228
  ### Processor
187
229
 
188
230
  The function `postcss(fn)` creates a processor from your function:
@@ -247,6 +289,14 @@ processor.process(wrong, { from: 'main.css' });
247
289
  //=> Can't parse CSS: Unclosed block at line 1:1 in main.css
248
290
  ```
249
291
 
292
+ You can also use result from previous postprocessor or already parsed `Root`
293
+ as argument in next one:
294
+
295
+ ```js
296
+ result = processor1.process(css)
297
+ processor2.process(result)
298
+ ```
299
+
250
300
  ### Multiple Inputs
251
301
 
252
302
  The function `postcss()` generates processor only for one input.
@@ -566,7 +616,7 @@ Because CSS have nested structure, PostCSS also contains recursive iterator
566
616
 
567
617
  ```js
568
618
  root.eachInside(function (node, i) {
569
- console.log(node.type ' inside ' + parent.type);
619
+ console.log(node.type + ' inside ' + node.parent.type);
570
620
  });
571
621
  ```
572
622
 
@@ -590,6 +640,8 @@ root.eachComment(function (comment, i) {
590
640
  })
591
641
  ```
592
642
 
643
+ You can break iteration by `return false`.
644
+
593
645
  ### Root Node
594
646
 
595
647
  `Root` node contains entire CSS tree. Its children can be only `Comment`,
@@ -1,9 +1,9 @@
1
1
  (function() {
2
- var MapGenerator, Result, base64, fs, lazy, mozilla, path;
2
+ var MapGenerator, Result, base64js, fs, lazy, mozilla, path;
3
3
 
4
- mozilla = require('source-map');
4
+ base64js = require('base64-js');
5
5
 
6
- base64 = require('base64-js');
6
+ mozilla = require('source-map');
7
7
 
8
8
  Result = require('./result');
9
9
 
@@ -47,24 +47,12 @@
47
47
  });
48
48
 
49
49
  lazy(MapGenerator, 'prevMap', function() {
50
- var byte, bytes, file, map, start, text;
50
+ var file, map;
51
51
  if (this.opts.map && typeof this.opts.map !== 'boolean') {
52
52
  return this.opts.map;
53
53
  }
54
54
  if (this.isPrevInline()) {
55
- start = '# sourceMappingURL=data:application/json;base64,';
56
- text = this.prevAnnotation().text;
57
- text = text.slice(start.length);
58
- bytes = base64.toByteArray(text);
59
- return ((function() {
60
- var _i, _len, _results;
61
- _results = [];
62
- for (_i = 0, _len = bytes.length; _i < _len; _i++) {
63
- byte = bytes[_i];
64
- _results.push(String.fromCharCode(byte));
65
- }
66
- return _results;
67
- })()).join('');
55
+ return this.encodeInline(this.prevAnnotation().text);
68
56
  } else if (this.opts.from) {
69
57
  map = this.opts.from + '.map';
70
58
  if (this.prevAnnotation()) {
@@ -92,29 +80,42 @@
92
80
  }
93
81
  });
94
82
 
83
+ MapGenerator.prototype.encodeInline = function(text) {
84
+ var base64, byte, bytes, uri;
85
+ uri = '# sourceMappingURL=data:application/json,';
86
+ base64 = '# sourceMappingURL=data:application/json;base64,';
87
+ if (this.startWith(text, uri)) {
88
+ return decodeURIComponent(text.slice(uri.length));
89
+ } else if (this.startWith(text, base64)) {
90
+ text = text.slice(base64.length);
91
+ bytes = base64js.toByteArray(text);
92
+ return ((function() {
93
+ var _i, _len, _results;
94
+ _results = [];
95
+ for (_i = 0, _len = bytes.length; _i < _len; _i++) {
96
+ byte = bytes[_i];
97
+ _results.push(String.fromCharCode(byte));
98
+ }
99
+ return _results;
100
+ })()).join('');
101
+ } else {
102
+ throw new Error('Unknown source map encoding');
103
+ }
104
+ };
105
+
95
106
  MapGenerator.prototype.clearAnnotation = function() {
96
107
  var _ref;
97
108
  return (_ref = this.prevAnnotation()) != null ? _ref.removeSelf() : void 0;
98
109
  };
99
110
 
100
111
  MapGenerator.prototype.applyPrevMap = function() {
101
- var from, prev, source;
112
+ var from, prev;
102
113
  if (this.prevMap()) {
103
114
  prev = this.prevMap();
104
115
  prev = typeof prev === 'string' ? JSON.parse(prev) : prev instanceof mozilla.SourceMapConsumer ? mozilla.SourceMapGenerator.fromSourceMap(prev).toJSON() : typeof prev === 'object' && prev.toJSON ? prev.toJSON() : prev;
105
- from = path.dirname(this.opts.from);
106
- prev.sources = (function() {
107
- var _i, _len, _ref, _results;
108
- _ref = prev.sources;
109
- _results = [];
110
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
111
- source = _ref[_i];
112
- _results.push(this.relative(path.resolve(from, source)));
113
- }
114
- return _results;
115
- }).call(this);
116
116
  prev = new mozilla.SourceMapConsumer(prev);
117
- return this.map.applySourceMap(prev, this.relative(this.opts.from));
117
+ from = this.relative(this.opts.from);
118
+ return this.map.applySourceMap(prev, from, path.dirname(from));
118
119
  }
119
120
  };
120
121
 
@@ -135,7 +136,7 @@
135
136
  _results.push(char.charCodeAt(0));
136
137
  }
137
138
  return _results;
138
- }).call(this), "data:application/json;base64," + base64.fromByteArray(bytes)) : this.outputFile() + '.map';
139
+ }).call(this), "data:application/json;base64," + base64js.fromByteArray(bytes)) : this.outputFile() + '.map';
139
140
  return this.css += "\n/*# sourceMappingURL=" + content + " */";
140
141
  };
141
142
 
@@ -152,9 +153,9 @@
152
153
  this.applyPrevMap();
153
154
  this.addAnnotation();
154
155
  if (this.isInline()) {
155
- return new Result(this.css);
156
+ return new Result(this.root, this.css);
156
157
  } else {
157
- return new Result(this.css, this.map.toString());
158
+ return new Result(this.root, this.css, this.map.toString());
158
159
  }
159
160
  };
160
161
 
@@ -228,7 +229,7 @@
228
229
  if (this.isMap()) {
229
230
  return this.generateMap();
230
231
  } else {
231
- return new Result(this.root.toString());
232
+ return new Result(this.root);
232
233
  }
233
234
  };
234
235
 
package/lib/parse.js CHANGED
@@ -1,7 +1,7 @@
1
1
  (function() {
2
- var AtRule, Comment, Declaration, Parser, Raw, Root, Rule, SyntexError;
2
+ var AtRule, Comment, Declaration, Parser, Raw, Root, Rule, SyntaxError;
3
3
 
4
- SyntexError = require('./syntax-error');
4
+ SyntaxError = require('./syntax-error');
5
5
 
6
6
  Declaration = require('./declaration');
7
7
 
@@ -316,7 +316,7 @@
316
316
  column: this.column
317
317
  };
318
318
  }
319
- throw new SyntexError(message, this.source, position, this.opts.from);
319
+ throw new SyntaxError(message, this.source, position, this.opts.from);
320
320
  };
321
321
 
322
322
  Parser.prototype.move = function() {
@@ -344,7 +344,7 @@
344
344
  };
345
345
 
346
346
  Parser.prototype.space = function() {
347
- return this.letter === ' ' || this.letter === "\t" || this.letter === "\n" || this.letter === "\f" || this.letter === "\r";
347
+ return this.letter.match(/\s/);
348
348
  };
349
349
 
350
350
  Parser.prototype.init = function(node) {
package/lib/postcss.js CHANGED
@@ -1,5 +1,5 @@
1
1
  (function() {
2
- var AtRule, Comment, Declaration, PostCSS, Root, Rule, postcss,
2
+ var AtRule, Comment, Declaration, PostCSS, Result, Root, Rule, postcss,
3
3
  __slice = [].slice;
4
4
 
5
5
  Declaration = require('./declaration');
@@ -8,6 +8,8 @@
8
8
 
9
9
  AtRule = require('./at-rule');
10
10
 
11
+ Result = require('./result');
12
+
11
13
  Rule = require('./rule');
12
14
 
13
15
  Root = require('./root');
@@ -27,7 +29,7 @@
27
29
  if (opts == null) {
28
30
  opts = {};
29
31
  }
30
- parsed = postcss.parse(css, opts);
32
+ parsed = css instanceof Root ? css : css instanceof Result ? parsed = css.root : postcss.parse(css, opts);
31
33
  _ref = this.processors;
32
34
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
33
35
  processor = _ref[_i];
package/lib/result.js CHANGED
@@ -2,8 +2,12 @@
2
2
  var Result;
3
3
 
4
4
  Result = (function() {
5
- function Result(css, map) {
5
+ function Result(root, css, map) {
6
+ this.root = root;
6
7
  this.css = css;
8
+ if (this.css == null) {
9
+ this.css = this.root.toString();
10
+ }
7
11
  if (map) {
8
12
  this.map = map;
9
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss",
3
- "version": "0.3.1",
3
+ "version": "0.3.5",
4
4
  "description": "Framework for CSS postprocessors",
5
5
  "keywords": ["css", "parser", "postproccessor"],
6
6
  "author": "Andrey Sitnik <andrey@sitnik.ru>",
@@ -10,16 +10,16 @@
10
10
  "url": "https://github.com/ai/postcss.git"
11
11
  },
12
12
  "dependencies": {
13
- "source-map": "~0.1.32",
14
- "base64-js": "0.0.6"
13
+ "source-map": "~0.1.33",
14
+ "base64-js": "~0.0.6"
15
15
  },
16
16
  "devDependencies": {
17
17
  "coffee-script": "1.7.1",
18
- "fs-extra": "0.8.1",
18
+ "fs-extra": "0.9.1",
19
19
  "gonzales": "1.0.7",
20
- "rework": "0.20.2",
21
- "should": "3.1.2",
22
- "mocha": "1.17.1",
20
+ "rework": "0.20.3",
21
+ "should": "4.0.0",
22
+ "mocha": "1.20.0",
23
23
  "cssom": "0.3.0"
24
24
  },
25
25
  "main": "lib/postcss",