diff --git a/.gitignore b/.gitignore index 2e7b3f5..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ node_modules -test.css -test.js diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 4a3c398..0000000 --- a/.npmignore +++ /dev/null @@ -1,6 +0,0 @@ -support -test -examples -*.sock -test.css -test.js diff --git a/History.md b/History.md index c6f8cb5..dbc401c 100644 --- a/History.md +++ b/History.md @@ -1,15 +1,75 @@ +2.0.0 / 2014-06-18 +================== + + * update 'css' to 2.0.0 + * depend on the 'css' package, which now contains the stringifier + +1.4.1 / 2013-12-09 +================== + + * add missing files to component.json + +1.4.0 / 2013-XX-XX +================== + + * add source map generation + +1.3.2 / 2013-10-18 +================== + + * fix whitespace and indentation in the Compressed compiler. + * add @namespace support + * add .stylesheet(node) + +1.3.1 / 2013-06-02 +================== + + * fix output of rules with no declarations for Identity compiler + * fix defaulting of options + +1.3.0 / 2013-05-28 +================== + + * add ignoring of empty rulesets. Closes #7 + * add separate compilers + * add @supports support + * add @page compilation support + * fix comment output. Closes #16 + * fix trailing ; with comments within rules + * fix comment indentation + +1.2.0 / 2013-05-21 +================== + + * add @document compilation. Closes #82 + +1.1.0 / 2013-03-19 +================== + + * add omission of comments when compressed + * add comment support + +1.0.5 / 2013-03-15 +================== + + * fix indentation of multiple selectors in @media. Closes #11 + +1.0.4 / 2012-11-15 +================== + + * fix indentation -1.0.3 / 2012-09-04 +1.0.3 / 2012-09-04 ================== * add __@charset__ support [rstacruz] -1.0.2 / 2012-09-01 +1.0.2 / 2012-09-01 ================== * add component support -1.0.1 / 2012-07-26 +1.0.1 / 2012-07-26 ================== * add "selectors" array support diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0239d9c --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +(The MIT License) + +Copyright (c) 2012 TJ Holowaychuk + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile deleted file mode 100644 index 4e9c8d3..0000000 --- a/Makefile +++ /dev/null @@ -1,7 +0,0 @@ - -test: - @./node_modules/.bin/mocha \ - --require should \ - --reporter spec - -.PHONY: test \ No newline at end of file diff --git a/Readme.md b/Readme.md index 745ae92..954623d 100644 --- a/Readme.md +++ b/Readme.md @@ -1,33 +1,15 @@ - # css-stringify - CSS compiler using the AST provided by [css-parse](https://github.com/visionmedia/node-css-parse). - -## Performance - - Formats 15,000 lines of CSS (2mb) in 23ms on my macbook air. + JavaScript CSS stringifier for Node.js (exports the `stringify` method of [css](https://github.com/reworkcss/css)) -## License +## Installation -(The MIT License) + $ npm install css-stringify -Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca> +## Usage -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -'Software'), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: + Please see the [css](https://github.com/reworkcss/css) module documentation. -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. +## License -THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file + MIT diff --git a/component.json b/component.json deleted file mode 100644 index 939e778..0000000 --- a/component.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "css-stringify", - "repo": "visionmedia/node-css-stringify", - "version": "1.0.2", - "description": "CSS compiler", - "keywords": ["css", "stringify", "stylesheet"], - "scripts": ["index.js"] -} \ No newline at end of file diff --git a/examples/dialog.css b/examples/dialog.css deleted file mode 100644 index df74183..0000000 --- a/examples/dialog.css +++ /dev/null @@ -1,92 +0,0 @@ -#dialog { - position: fixed; - left: 50%; - top: 150px; - max-width: 600px; - min-width: 250px; - border: 1px solid #eee; - background: white; - z-index: 1000; -} - -#dialog .content { - padding: 15px 20px; -} - -#dialog h1 { - margin: 0 0 5px 0; - font-size: 16px; - font-weight: normal; -} - -#dialog p { - margin: 0; - padding: 0; - font-size: .9em; -} - -#dialog.modal { - box-shadow: 0 1px 8px 0 black; -} - -/* close */ - -#dialog .close { - position: absolute; - top: 3px; - right: 10px; - text-decoration: none; - color: #888; - font-size: 16px; - font-weight: bold; - display: none; -} - -#dialog.closable .close { - display: block; -} - -#dialog .close:hover { - color: black; -} - -#dialog .close:active { - margin-top: 1px; -} - -/* slide */ - -#dialog.slide { - -webkit-transition: opacity 300ms, top 300ms; - -moz-transition: opacity 300ms, top 300ms; -} - -#dialog.slide.hide { - opacity: 0; - top: -500px; -} - -/* fade */ - -#dialog.fade { - -webkit-transition: opacity 300ms; - -moz-transition: opacity 300ms; -} - -#dialog.fade.hide { - opacity: 0; -} - -/* scale */ - -#dialog.scale { - -webkit-transition: -webkit-transform 300ms; - -moz-transition: -moz-transform 300ms; - -webkit-transform: scale(1); - -moz-transform: scale(1); -} - -#dialog.scale.hide { - -webkit-transform: scale(0); - -moz-transform: scale(0); -} \ No newline at end of file diff --git a/examples/dialog.js b/examples/dialog.js deleted file mode 100644 index 69cb6fe..0000000 --- a/examples/dialog.js +++ /dev/null @@ -1,12 +0,0 @@ - -/** - * Module dependencies. - */ - -var parse = require('css-parse') - , stringify = require('..') - , fs = require('fs') - , read = fs.readFileSync - , css = read('examples/dialog.css', 'utf8'); - -console.log(stringify(parse(css), { compress: true })); diff --git a/examples/keyframes.css b/examples/keyframes.css deleted file mode 100644 index a50d14a..0000000 --- a/examples/keyframes.css +++ /dev/null @@ -1,10 +0,0 @@ -@keyframes fade { - from { - opacity: 0; - opacity: 1 - } - - to { - opacity: 1 - } -} \ No newline at end of file diff --git a/examples/keyframes.js b/examples/keyframes.js deleted file mode 100644 index 7454ff9..0000000 --- a/examples/keyframes.js +++ /dev/null @@ -1,12 +0,0 @@ - -/** - * Module dependencies. - */ - -var parse = require('css-parse') - , stringify = require('..') - , fs = require('fs') - , read = fs.readFileSync - , css = read('examples/keyframes.css', 'utf8'); - -console.log(stringify(parse(css), { compress: true })); diff --git a/examples/media.css b/examples/media.css deleted file mode 100644 index a9e6128..0000000 --- a/examples/media.css +++ /dev/null @@ -1,23 +0,0 @@ -@media screen, projection { - html { - background: #fffef0; - color: #300; - } - - body { - max-width: 35em; - margin: 0 auto; - } -} - -@media print { - html { - background: #fff; - color: #000; - } - - body { - padding: 1in; - border: 0.5pt solid #666; - } -} \ No newline at end of file diff --git a/examples/media.js b/examples/media.js deleted file mode 100644 index ec5bcf5..0000000 --- a/examples/media.js +++ /dev/null @@ -1,12 +0,0 @@ - -/** - * Module dependencies. - */ - -var parse = require('css-parse') - , stringify = require('..') - , fs = require('fs') - , read = fs.readFileSync - , css = read('examples/media.css', 'utf8'); - -console.log(stringify(parse(css), { compress: false })); diff --git a/index.js b/index.js index 0cbcda9..b4c1752 100644 --- a/index.js +++ b/index.js @@ -1,180 +1 @@ - -/** - * Stringfy the given AST `node`. - * - * @param {Object} node - * @param {Object} options - * @return {String} - * @api public - */ - -module.exports = function(node, options){ - return new Compiler(options).compile(node); -}; - -/** - * Initialize a new `Compiler`. - */ - -function Compiler(options) { - options = options || {}; - this.compress = options.compress; - this.indentation = options.indent; -} - -/** - * Compile `node`. - */ - -Compiler.prototype.compile = function(node){ - return node.stylesheet.rules.map(this.visit, this) - .join(this.compress ? '' : '\n\n'); -}; - -/** - * Visit `node`. - */ - -Compiler.prototype.visit = function(node){ - if (node.charset) return this.charset(node); - if (node.keyframes) return this.keyframes(node); - if (node.media) return this.media(node); - if (node.import) return this.import(node); - return this.rule(node); -}; - -/** - * Visit import node. - */ - -Compiler.prototype.import = function(node){ - return '@import ' + node.import + ';'; -}; - -/** - * Visit media node. - */ - -Compiler.prototype.media = function(node){ - if (this.compress) { - return '@media ' - + node.media - + '{' - + node.rules.map(this.visit, this).join('') - + '}'; - } - - return '@media ' - + node.media - + ' {\n' - + this.indent(1) - + node.rules.map(this.visit, this).join('\n\n') - + this.indent(-1) - + '\n}'; -}; - -/** - * Visit charset node. - */ - -Compiler.prototype.charset = function(node){ - if (this.compress) { - return '@charset ' + node.charset + ';'; - } - - return '@charset ' + node.charset + ';\n'; -}; - -/** - * Visit keyframes node. - */ - -Compiler.prototype.keyframes = function(node){ - if (this.compress) { - return '@' - + (node.vendor || '') - + 'keyframes ' - + node.name - + '{' - + node.keyframes.map(this.keyframe, this).join('') - + '}'; - } - - return '@' - + (node.vendor || '') - + 'keyframes ' - + node.name - + ' {\n' - + this.indent(1) - + node.keyframes.map(this.keyframe, this).join('\n') - + this.indent(-1) - + '}'; -}; - -/** - * Visit keyframe node. - */ - -Compiler.prototype.keyframe = function(node){ - if (this.compress) { - return node.values.join(',') - + '{' - + node.declarations.map(this.declaration, this).join(';') - + '}'; - } - - return this.indent() - + node.values.join(', ') - + ' {\n' - + this.indent(1) - + node.declarations.map(this.declaration, this).join(';\n') - + this.indent(-1) - + '\n' + this.indent() + '}\n'; -}; - -/** - * Visit rule node. - */ - -Compiler.prototype.rule = function(node){ - if (this.compress) { - return node.selectors.join(',') - + '{' - + node.declarations.map(this.declaration, this).join(';') - + '}'; - } - - return this.indent() + node.selectors.join(',\n') - + ' {\n' - + this.indent(1) - + node.declarations.map(this.declaration, this).join(';\n') - + this.indent(-1) - + '\n' + this.indent() + '}'; -}; - -/** - * Visit declaration node. - */ - -Compiler.prototype.declaration = function(node){ - if (this.compress) { - return node.property + ':' + node.value; - } - - return this.indent() + node.property + ': ' + node.value; -}; - -/** - * Increase, decrease or return current indentation. - */ -Compiler.prototype.indent = function(level) { - this.level = this.level || 1; - - if (level !== undefined) { - this.level += level; - - return ''; - } - - return Array(this.level).join(this.indentation || ' '); -}; +module.exports = require('css').stringify; diff --git a/package.json b/package.json index 8c7c23b..3f55a24 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,23 @@ { "name": "css-stringify", - "version": "1.0.3", + "version": "2.0.0", "description": "CSS compiler", - "keywords": ["css", "stringify", "stylesheet"], + "main": "index", + "files": [ + "index.js" + ], + "dependencies": { + "css": "^2.0.0" + }, "author": "TJ Holowaychuk ", - "devDependencies": { - "mocha": "*", - "should": "*", - "css-parse": "1.0.3" + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/reworkcss/css-stringify.git" }, - "main": "index" -} \ No newline at end of file + "keywords": [ + "css", + "stringify", + "stylesheet" + ] +} diff --git a/test/cases/at_charset.css b/test/cases/at_charset.css deleted file mode 100644 index e63c05c..0000000 --- a/test/cases/at_charset.css +++ /dev/null @@ -1 +0,0 @@ -@charset "utf-8"; diff --git a/test/cases/import.css b/test/cases/import.css deleted file mode 100644 index ebeed7b..0000000 --- a/test/cases/import.css +++ /dev/null @@ -1 +0,0 @@ -@import 'foo.css'; \ No newline at end of file diff --git a/test/cases/keyframes.css b/test/cases/keyframes.css deleted file mode 100644 index a50d14a..0000000 --- a/test/cases/keyframes.css +++ /dev/null @@ -1,10 +0,0 @@ -@keyframes fade { - from { - opacity: 0; - opacity: 1 - } - - to { - opacity: 1 - } -} \ No newline at end of file diff --git a/test/cases/media.css b/test/cases/media.css deleted file mode 100644 index 69ef305..0000000 --- a/test/cases/media.css +++ /dev/null @@ -1,23 +0,0 @@ -@media screen, projection { - html { - background: #fffef0; - color: #300 - } - - body { - max-width: 35em; - margin: 0 auto - } -} - -@media print { - html { - background: #fff; - color: #000 - } - - body { - padding: 1in; - border: 0.5pt solid #666 - } -} \ No newline at end of file diff --git a/test/cases/rules.css b/test/cases/rules.css deleted file mode 100644 index 96f2504..0000000 --- a/test/cases/rules.css +++ /dev/null @@ -1,9 +0,0 @@ -tobi { - name: 'tobi'; - age: 2 -} - -loki { - name: 'loki'; - age: 1 -} \ No newline at end of file diff --git a/test/cases/selectors.css b/test/cases/selectors.css deleted file mode 100644 index 5920f9e..0000000 --- a/test/cases/selectors.css +++ /dev/null @@ -1,5 +0,0 @@ -foo, -bar, -baz { - color: 'black' -} \ No newline at end of file diff --git a/test/css-stringify.js b/test/css-stringify.js deleted file mode 100644 index 30e6a0b..0000000 --- a/test/css-stringify.js +++ /dev/null @@ -1,24 +0,0 @@ - -/** - * Module dependencies. - */ - -var stringify = require('..') - , parse = require('css-parse') - , fs = require('fs') - , path = require('path') - , read = fs.readFileSync - , readdir = fs.readdirSync; - -describe('stringify(obj)', function(){ - readdir('test/cases').forEach(function(file){ - var compress = ~file.indexOf('compress'); - file = path.basename(file, '.css'); - it('should stringify ' + file, function(){ - var css = read(path.join('test', 'cases', file + '.css'), 'utf8'); - if (compress) file = file.replace('.compress', ''); - var ret = stringify(parse(css), { compress: compress }); - ret.should.equal(css); - }); - }); -}); \ No newline at end of file