cssparser
Advanced tools
Comparing version 0.9.1 to 0.9.2
@@ -41,4 +41,9 @@ #!/usr/bin/env node | ||
}) | ||
.option('beautify-delimiter', { | ||
abbr: 'b', | ||
flag: true, | ||
help: 'Beautify delimiters such as comma and whitespaces for simple & deep type.' | ||
}) | ||
.option('version', { | ||
abbr: 'V', | ||
abbr: 'v', | ||
flag: true, | ||
@@ -52,6 +57,13 @@ help: 'print version and exit', | ||
function toJSON(raw, type, indent) { | ||
function toJSON(raw, type, indent, beautifyDelimiter) { | ||
var parser = new cssparser.Parser(); | ||
var ast = parser.parse(raw) | ||
if (beautifyDelimiter) { | ||
ast.setOptions({ | ||
commaDelimiter: ', ', | ||
whitespaceDelimiter: ' ' | ||
}) | ||
} | ||
return JSON.stringify(ast.toJSON(type), null, indent); | ||
@@ -71,5 +83,6 @@ } | ||
var indent = opts.indent; | ||
var beautifyDelimiter = opts['beautify-delimiter']; | ||
try { | ||
var json = toJSON(raw, type, indent); | ||
var json = toJSON(raw, type, indent, beautifyDelimiter); | ||
} catch (e) { | ||
@@ -76,0 +89,0 @@ var output = e.message |
@@ -16,3 +16,3 @@ { | ||
], | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"preferGlobal": true, | ||
@@ -39,11 +39,16 @@ "repository": { | ||
"devDependencies": { | ||
"babel-cli": "^6.23.0", | ||
"babel-preset-env": "^1.1.10", | ||
"babel-cli": "latest", | ||
"babel-plugin-transform-class-properties": "latest", | ||
"babel-preset-env": "latest", | ||
"gulp": "^3.9.1", | ||
"gulp-file-include": "^1.0.0", | ||
"jison": ">= 0.4.4", | ||
"jison-lex": "^0.3.4" | ||
"jison-lex": "^0.3.4", | ||
"uglify-js": "^2.8.13" | ||
}, | ||
"scripts": { | ||
"es6": "gulp merge-js && babel dist/js -d dist/babel", | ||
"clean": "rm -fr dist", | ||
"merge-js": "gulp merge-js", | ||
"babel": "babel dist/js -d dist/babel", | ||
"es6": "npm run merge-js && npm run babel", | ||
"generate": "gulp merge-jison && jison dist/jison/cssparser.y dist/jison/css.l -o lib/cssparser.js", | ||
@@ -55,4 +60,5 @@ "build": "npm run es6 && npm run generate", | ||
"test-all": "npm run test-simple && npm run test-deep && npm run test-atomic", | ||
"copy-web": "cp ./lib/cssparser.js ./demo/", | ||
"release": "npm run build && npm run copy-web", | ||
"copy-web": "cp ./lib/cssparser.min.js ./demo/", | ||
"uglify": "mkdir -p dist/js && uglifyjs lib/cssparser.js -m -c -o lib/cssparser.min.js", | ||
"release": "npm run clean && npm run build && npm run uglify && npm run copy-web", | ||
"test": "npm run release && npm run test-all", | ||
@@ -59,0 +65,0 @@ "inspect": "node --inspect --debug-brk lib/cli.js test/test.css -c -t " |
@@ -11,3 +11,3 @@ ![Travis Build Status - Master](https://img.shields.io/travis/cwdoh/cssparser.js/master.svg) | ||
##Description | ||
## Description | ||
@@ -17,7 +17,7 @@ * License: MIT license - [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) | ||
##Demo | ||
## Demo | ||
* [http://cwdoh.github.io/cssparser.js/demo/CSS_stringify.html](//cwdoh.github.io/cssparser.js/demo/CSS_stringify.html) | ||
##Dependency | ||
## Dependency | ||
@@ -31,5 +31,5 @@ Just want to use cssparser.js? Nothing needed. | ||
##Usage | ||
## Usage | ||
###from Command-line | ||
### from Command-line | ||
@@ -57,3 +57,3 @@ | ||
###from CommonJS Module | ||
### from CommonJS Module | ||
@@ -76,5 +76,5 @@ You can generate javascript object from your javascript module. | ||
##Generating parser from source | ||
## Generating parser from source | ||
###Getting jison & source | ||
### Getting jison & source | ||
@@ -86,3 +86,3 @@ ```bash | ||
###Generating parser from source | ||
### Generating parser from source | ||
@@ -93,3 +93,3 @@ ```bash | ||
##JSON Structure | ||
## JSON Structure | ||
@@ -105,3 +105,3 @@ There are 3 types of JSON format. | ||
##Example | ||
## Example | ||
@@ -114,3 +114,3 @@ Example is tested with rulesets of [http://css3please.com](http://css3please.com) | ||
###Input | ||
### Input | ||
@@ -133,5 +133,5 @@ ```css | ||
###JSON Output | ||
### JSON Output | ||
####Type 'simple' | ||
#### Type 'simple' | ||
@@ -259,3 +259,3 @@ ```javascript | ||
####Type 'atomic' | ||
#### Type 'atomic' | ||
@@ -412,4 +412,10 @@ ```javascript | ||
##Change log | ||
## Change log | ||
* 0.9.2 - March 17th, 2017 | ||
* Now supports beautify delimiter option for simple & deep type. | ||
* Showing version will be run lower-case 'v' instead 'V'. | ||
* Fixed missing keyframe name and added type & level descriptions for simple type. | ||
* Fixed EOF error case. | ||
* Added '-b' option for beautify delimiters. | ||
* 0.9.1 - March 8th, 2017 | ||
@@ -428,10 +434,1 @@ * Added 'rule' type on the css style node when simple mode. | ||
* Initial release of cssparser.js. | ||
##To do list | ||
* Parsing & generating options like simple JSON expression or more detailed. | ||
* Error recovery for input css. | ||
* Utilities | ||
* Minify & optimize css. | ||
* Auto-generate Cross-browsing stylesheets. | ||
@@ -35,3 +35,3 @@ class AtRule extends CSSObject { | ||
toSimpleJSON() { | ||
return mixin(super.toSimpleJSON(), { | ||
return joinValues(super.toSimpleJSON(), { | ||
mediaQuery: toSimple(this.get('nextExpression')) | ||
@@ -48,3 +48,3 @@ }) | ||
toSimpleJSON() { | ||
return mixin(super.toSimpleJSON(), { | ||
return joinValues(super.toSimpleJSON(), { | ||
prefix: toSimple(this.get('prefix')) | ||
@@ -66,3 +66,3 @@ }) | ||
toSimpleJSON() { | ||
return mixin(super.toSimpleJSON(), { | ||
return joinValues(super.toSimpleJSON(), { | ||
nestedRules: toSimple(this.get('nestedRules')) | ||
@@ -83,2 +83,3 @@ }) | ||
type: '@' + toSimple(this.get('rule')), | ||
name: toSimple(this.get('name')), | ||
keyframes: toSimple(this.get('value')) | ||
@@ -102,3 +103,3 @@ } | ||
toSimple(this.get('value')).map((o) => { | ||
mixin(json, o) | ||
joinValues(json, o) | ||
}) | ||
@@ -105,0 +106,0 @@ |
@@ -52,3 +52,3 @@ class QualifiedRule extends CSSObject { | ||
toSimple(this.get('value')).map((o) => { | ||
mixin(json, o) | ||
joinValues(json, o) | ||
}) | ||
@@ -55,0 +55,0 @@ |
@@ -6,2 +6,20 @@ class CSSObject { | ||
static _options = { | ||
commaDelimiter: ',', | ||
whitespaceDelimiter: ' ' | ||
} | ||
setOptions(customOptions) { | ||
CSSObject._options = mixin(this.options, customOptions) | ||
} | ||
get options() { | ||
return CSSObject._options | ||
} | ||
set options(customOptions) { | ||
console.warn('For beautify AST output, `setOptions()` method would be recommended instead of assigning directly.') | ||
this.setOptions(customOptions) | ||
} | ||
getType(type) { | ||
@@ -92,2 +110,24 @@ return 'OBJECT' | ||
toAtomicJSON() { | ||
var json = super.toAtomicJSON() | ||
json.level = 'atomic' | ||
return json | ||
} | ||
toDeepJSON() { | ||
var json = super.toDeepJSON() | ||
json.level = 'deep' | ||
return json | ||
} | ||
toSimpleJSON() { | ||
return { | ||
type: 'stylesheet', | ||
level: 'simple', | ||
value: toSimple(this.get('value', [])) | ||
} | ||
} | ||
static create() { | ||
@@ -94,0 +134,0 @@ return new StyleSheet() |
@@ -21,3 +21,11 @@ const concat = function(l, r) { | ||
const mixin = (target, source) => { | ||
const join = function(o, delimiter) { | ||
if (isArray(o)) { | ||
return o.join(delimiter) | ||
} | ||
return o | ||
} | ||
const joinValues = (target, source) => { | ||
for (var prop in source) { | ||
@@ -34,2 +42,16 @@ if (prop in target) { | ||
const mixin = (target, source) => { | ||
var result = {} | ||
for (var prop in target) { | ||
result[prop] = target[prop] | ||
} | ||
for (var prop in source) { | ||
result[prop] = source[prop] | ||
} | ||
return result | ||
} | ||
const isArray = (o) => Object.prototype.toString.call(o) === '[object Array]' | ||
@@ -36,0 +58,0 @@ |
@@ -130,3 +130,3 @@ class PrimitiveVal extends CSSObject { | ||
+ '(' | ||
+ toSimple(this.get('parameters')) | ||
+ join(toSimple(this.get('parameters')), this.options.commaDelimiter) | ||
+ ')' | ||
@@ -155,3 +155,3 @@ } | ||
toSimpleJSON() { | ||
return toSimple(this.get('value', [])).join(' ') | ||
return toSimple(this.get('value', [])).join(this.options.whitespaceDelimiter) | ||
} | ||
@@ -158,0 +158,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1016123
44
8
7306
4
422
5