Socket
Socket
Sign inDemoInstall

cssparser

Package Overview
Dependencies
8
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.2 to 0.9.0

.babelrc

163

lib/cli.js
#!/usr/bin/env node
(function () {
var colors = require('colors');
var cssparser = require('./cssparser.js');
var nomnom = require('nomnom');
var fs = require('fs');
var path = require('path');
var version = require('../package.json').version;
var opts = require("nomnom")
.script('cssparser')
.option('file', {
flag: true,
position: 0,
help: 'CSS document file'
})
.option('outfile', {
abbr: 'o',
metavar: 'FILE',
help: 'Filename or base name of the generated JSON'
})
.option('indent', {
abbr: 'i',
default: 0,
help: 'indentation(string or number)'
})
.option('type', {
abbr: 't',
default: 'simple',
choices: [ 'simple', 'deep', 'atomic' ],
metavar: 'TYPE',
help: 'The type of JSON to generate (simple, deep, atomic)'
})
.option('console', {
abbr: 'c',
flag: true,
help: 'Display JSON to console only. this option will ignore output-file options.'
})
.option('version', {
abbr: 'V',
flag: true,
help: 'print version and exit',
callback: function() {
return version;
}
})
.parse();
function toJSON( raw, type, indent ) {
var parser = new cssparser.Parser();
return JSON.stringify( parser.parse( raw ), null, indent );
}
if (opts.file) {
try {
var raw = fs.readFileSync(path.normalize(opts.file), 'utf8');
}
catch (e) {
console.error( e.toString().red );
return;
}
(function() {
var colors = require('colors');
var cssparser = require('./cssparser.js');
var nomnom = require('nomnom');
var fs = require('fs');
var path = require('path');
var name = path.basename((opts.outfile||opts.file)).replace(/\..*$/g,'');
var type = opts.type;
var indent = opts.indent;
var version = require('../package.json').version;
try {
var json = toJSON(raw, type, indent);
}
catch (e) {
console.error( e.toString().red );
return;
}
var opts = require("nomnom")
.script('cssparser')
.option('file', {
flag: true,
position: 0,
help: 'CSS document file'
})
.option('outfile', {
abbr: 'o',
metavar: 'FILE',
help: 'Filename or base name of the generated JSON'
})
.option('indent', {
abbr: 'i',
default: 4,
help: 'indentation(string or number)'
})
.option('type', {
abbr: 't',
default: 'simple',
choices: ['simple', 'deep', 'atomic'],
metavar: 'TYPE',
help: 'The type of JSON to generate (simple, deep, atomic)'
})
.option('console', {
abbr: 'c',
flag: true,
help: 'Display JSON to console only. this option will ignore output-file options.'
})
.option('version', {
abbr: 'V',
flag: true,
help: 'print version and exit',
callback: function() {
return version;
}
})
.parse();
if ( opts.console ) {
console.log( json );
}
else {
fs.writeFileSync(opts.outfile||(name + '.json'), json );
}
function toJSON(raw, type, indent) {
var parser = new cssparser.Parser();
var ast = parser.parse(raw)
return JSON.stringify(ast.toJSON(type), null, indent);
}
if (opts.file) {
try {
var raw = fs.readFileSync(path.normalize(opts.file), 'utf8');
} catch (e) {
console.error(e.toString().red + '\n' + e.stack.red);
return;
}
})();
var name = path.basename((opts.outfile || opts.file)).replace(/\..*$/g, '');
var type = opts.type;
var indent = opts.indent;
try {
var json = toJSON(raw, type, indent);
} catch (e) {
var output = e.message
if ('stack' in e) {
output += '\n' + e.stack
}
if ('hash' in e) {
output += '\n' + JSON.stringify(e[e.hash], null, '\t')
}
console.error(output.red);
return;
}
if (opts.console) {
console.log(json);
} else {
fs.writeFileSync(opts.outfile || (name + '.json'), json);
}
}
})();
{
"name": "cssparser",
"author": {
"name":"Chang W. Doh",
"email": "changwook.doh@gmail.com",
"site": "http://moonchild.pe.kr"
},
"description": "parsing CSS document and transform to JSON format",
"keywords": [
"css",
"parser",
"json",
"validation",
"transform"
],
"version": "0.2.2",
"preferGlobal": true,
"repository": {
"type": "git",
"url": "git://github.com/cwdoh/cssparser.js.git"
},
"bugs": {
"url": "http://github.com/cwdoh/cssparser.js/issues"
},
"main": "lib/cssparser.js",
"bin": {
"cssparser": "lib/cli.js"
},
"engines": {
"node": ">= 0.6"
},
"dependencies": {
"nomnom": ">= 1.5.x",
"colors": ">= 0.6.0"
},
"devDependencies": {
"grunt": "~0.4.x",
"jison": ">= 0.4.4",
"grunt-contrib-concat": "~0.1.2",
"grunt-contrib-uglify": "~0.1.2",
"grunt-contrib-copy": "~0.4.0",
"grunt-contrib-yuidoc": "~0.4.0",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-coffee": "~0.6.0"
},
"homepage": "http://cwdoh.github.io/cssparser.js/",
"optionalDependencies": {},
"license": {
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
}
"name": "cssparser",
"author": {
"name": "Chang W. Doh",
"email": "changwook.doh@gmail.com",
"site": "http://www.cwdoh.com"
},
"description": "parsing CSS document and transforming to JSON format",
"keywords": [
"css",
"parser",
"json",
"validation",
"transform"
],
"version": "0.9.0",
"preferGlobal": true,
"repository": {
"type": "git",
"url": "git://github.com/cwdoh/cssparser.js.git"
},
"bugs": {
"url": "http://github.com/cwdoh/cssparser.js/issues"
},
"main": "lib/cli.js",
"bin": {
"cssparser": "lib/cli.js"
},
"engines": {
"node": ">= 0.6"
},
"dependencies": {
"colors": "^1.1.2",
"lodash": "^4.17.4",
"nomnom": ">= 1.5.x"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-preset-env": "^1.1.10",
"gulp": "^3.9.1",
"gulp-file-include": "^1.0.0",
"jison": ">= 0.4.4",
"jison-lex": "^0.3.4"
},
"scripts": {
"es6": "gulp merge-js && babel dist/js -d dist/babel",
"generate": "gulp merge-jison && jison dist/jison/cssparser.y dist/jison/css.l -o lib/cssparser.js",
"build": "npm run es6 && npm run generate",
"test-simple": "node lib/cli.js test/test.css -c -t simple",
"test-deep": "node lib/cli.js test/test.css -c -t deep",
"test-atomic": "node lib/cli.js test/test.css -c -t atomic",
"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",
"test": "npm run release && npm run test-all",
"inspect": "node --inspect --debug-brk lib/cli.js test/test.css -c -t "
},
"homepage": "https://cwdoh.github.io/cssparser.js/",
"optionalDependencies": {},
"license": {
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
}

@@ -0,1 +1,6 @@

![Travis Build Status - Master](https://img.shields.io/travis/cwdoh/cssparser.js/master.svg)
![Travis Build Status - Develop](https://img.shields.io/travis/cwdoh/cssparser.js/develop.svg)
![npm downloads in the last month](https://img.shields.io/npm/dm/cssparser.svg)
![npm total downloads](https://img.shields.io/npm/dt/cssparser.svg)
cssparser.js

@@ -56,6 +61,9 @@ ======

// parse & getting json
var json = parser.parse( cssText );
// parse
var ast = parser.parse(raw)
// getting json
var json = ast.toJSON(type)
##Generating parser from source

@@ -65,12 +73,8 @@

$ npm install jison -g
$ git clone https://github.com/cwdoh/cssparser.js.git
$ npm install
###Generating from source
###Generating parser from source
$ grunt
or
$ jison ./src/cssparser.y ./src/css.l
$ npm run build

@@ -89,225 +93,310 @@

###Type 'simple'
##Example
Example is tested with rulesets of [http://css3please.com](http://css3please.com)
stylesheet_object =
+ charset [Object]
cssparser example/test.css --console -i 4
+ imports [Array]
+ [Object(Import)]
+ type : "import" : [DOMString]// URI or string
+ mediaquries [DOMString] : // if query exist
###Input
+ namespaces [Array]
+ namespace [DOMString] : // URI or string
+ prefix [DOMString] : // if prefix exist
```css
@charset 'utf-8';
@import url("fineprint.css") print;
@media screen {
* {
position: absolute;
}
}
+ rulelist [Array]
+ [Object(Media)]
+ type [DOMString] : "media"
+ mediaqueries [DOMString] : // query string
+ children [Array] : // nested rulelist
+ rulelist // …
.footer {
position: fixed;
bottom: 0;
width: 1rem;
}
```
+ [Object(FontFace)]
+ type [DOMString] : "fontface"
+ declarations [Object] : // declarations
###JSON Output
+ [Object(Page)]
+ type [DOMString] : "page"
+ id [DOMString] : // identifier
+ pseudo [DOMString] : // pseudo string
+ declarations [Object] : // declarations
####Type 'simple'
+ [Object(Style)]
+ type [DOMString] : "style"
+ selector [DOMString] : // selector string
+ declarations [Object] : // declarations
```javascript
[
{
"type": "@charset",
"value": "'utf-8'"
},
{
"type": "@import",
"value": "url(\"fineprint.css\")",
"mediaQuery": [
"print"
]
},
{
"type": "@media",
"value": [
"screen"
],
"nestedRules": [
{
"selectors": [
"*"
],
"declarations": {
"position": "absolute"
}
}
]
},
{
"selectors": [
".footer"
],
"declarations": {
"position": "fixed",
"bottom": 0,
"width": "1rem"
}
}
]
```
+ [Object(Keyframes)]
+ type [DOMString] : "keyframes"
+ id [DOMString] : // identifier
+ prefix [DOMString] : // vendor prefix e.g. -moz-, -webkit-, -o-, …
+ keyframes [Array]
+ [Object(keyframe)]
+ type [DOMString] : "keyframe"
+ offset [DOMString] : // offset string
+ declarations [Object] : // declarations
####Type 'deep'
###Type 'deep'
```javascript
{
"type": "STYLESHEET",
"value": [
{
"type": "AT_RULE",
"rule": "charset",
"value": "'utf-8'"
},
{
"type": "AT_RULE",
"rule": "import",
"value": "url(\"fineprint.css\")",
"nextExpression": [
"print"
]
},
{
"type": "AT_RULE",
"rule": "media",
"value": [
"screen"
],
"nestedRules": [
{
"type": "QUALIFIED_RULE",
"value": {
"type": "DECLARATION_LIST",
"value": [
{
"type": "DECLARATION",
"property": "position",
"value": "absolute"
}
]
},
"selectors": [
"*"
]
}
]
},
{
"type": "QUALIFIED_RULE",
"value": {
"type": "DECLARATION_LIST",
"value": [
{
"type": "DECLARATION",
"property": "position",
"value": "fixed"
},
{
"type": "DECLARATION",
"property": "bottom",
"value": 0
},
{
"type": "DECLARATION",
"property": "width",
"value": "1rem"
}
]
},
"selectors": [
".footer"
]
}
]
}
```
Not yet.
####Type 'atomic'
###Type 'atomic'
```javascript
{
"type": "STYLESHEET",
"value": [
{
"type": "AT_RULE",
"rule": {
"type": "ID",
"value": "charset",
"prefix": "@"
},
"value": {
"type": "STRING",
"value": "'utf-8'"
}
},
{
"type": "AT_RULE",
"rule": {
"type": "ID",
"value": "import",
"prefix": "@"
},
"value": {
"type": "URL",
"name": {
"type": "ID",
"value": "url"
},
"value": "\"fineprint.css\""
},
"nextExpression": {
"type": "MEDIA_QUERY_LIST",
"value": [
{
"type": "MEDIA_QUERY",
"mediaType": {
"type": "ID",
"value": "print"
}
}
]
}
},
{
"type": "AT_RULE",
"rule": {
"type": "ID",
"value": "media",
"prefix": "@"
},
"value": {
"type": "MEDIA_QUERY_LIST",
"value": [
{
"type": "MEDIA_QUERY",
"mediaType": {
"type": "ID",
"value": "screen"
}
}
]
},
"nestedRules": [
{
"type": "QUALIFIED_RULE",
"value": {
"type": "DECLARATION_LIST",
"value": [
{
"type": "DECLARATION",
"property": {
"type": "ID",
"value": "position"
},
"value": {
"type": "ID",
"value": "absolute"
}
}
]
},
"selectors": {
"type": "SELECTOR_LIST",
"value": [
{
"type": "UNIVERSAL_SELECTOR",
"value": "*"
}
]
}
}
]
},
{
"type": "QUALIFIED_RULE",
"value": {
"type": "DECLARATION_LIST",
"value": [
{
"type": "DECLARATION",
"property": {
"type": "ID",
"value": "position"
},
"value": {
"type": "ID",
"value": "fixed"
}
},
{
"type": "DECLARATION",
"property": {
"type": "ID",
"value": "bottom"
},
"value": {
"type": "NUMBER",
"value": 0
}
},
{
"type": "DECLARATION",
"property": {
"type": "ID",
"value": "width"
},
"value": {
"type": "DIMENSION",
"value": 1,
"unit": "rem"
}
}
]
},
"selectors": {
"type": "SELECTOR_LIST",
"value": [
{
"type": "CLASS_SELECTOR",
"value": ".footer"
}
]
}
}
]
}
```
Not yet.
##Example
Example is tested with rulesets of [http://css3please.com](http://css3please.com)
cssparser example/test.css --console -i 4
###Input
@charset 'utf-8';
@import 'custom.css';
@import url("fineprint.css");
@import url("fineprint.css") print;
@import url("bluish.css") projection, tv;
@import "common.css" screen, projection;
@import url('landscape.css') screen and (orientation:landscape);
@namespace "http://www.w3c.org";
@namespace svg "http://www.w3c.org/svg";
@media screen {
* {
position: absolute;
}
.box_shadow {
-webkit-box-shadow: 0px 0px 4px 0px #ffffff; /* Android 2.3+, iOS 4.0.2-4.2, Safari 3-4 */
box-shadow: 0px 0px 4px 0px #ffffff; /* Chrome 6+, Firefox 4+, IE 9+, iOS 5+, Opera 10.50+ */
}
}
@-webkit-keyframes myanim {
0% { opacity: 0.0; }
50% { opacity: 0.5; }
100% { opacity: 1.0; }
}
.matrix {
-webkit-transform: matrix(1.186,-0.069,0.102,1.036,16.595,73.291);
-moz-transform: matrix(1.186,-0.069,0.102,1.036,16.595px,73.291px);
-ms-transform: matrix(1.186,-0.069,0.102,1.036,16.595,73.291);
-o-transform: matrix(1.186,-0.069,0.102,1.036,16.595,73.291);
transform: matrix(1.186,-0.069,0.102,1.036,16.595,73.291);
}
@font-face {
font-family: 'WebFont';
src: url('myfont.woff') format('woff'), /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
url('myfont.ttf') format('truetype'); /* Chrome 4+, Firefox 3.5, Opera 10+, Safari 3—5 */
}
###JSON Output
{
"charset": "'utf-8'",
"imports": [
{
"import": "'custom.css'"
},
{
"import": "url(\"fineprint.css\")"
},
{
"import": "url(\"fineprint.css\")",
"mediaqueries": "print"
},
{
"import": "url(\"bluish.css\")",
"mediaqueries": "projection, tv"
},
{
"import": "\"common.css\"",
"mediaqueries": "screen, projection"
},
{
"import": "url('landscape.css')",
"mediaqueries": "screen and (orientation:landscape)"
}
],
"namespaces": [
{
"namespace": "\"http://www.w3c.org\""
},
{
"namespace": "\"http://www.w3c.org/svg\"",
"prefix": "svg"
}
],
"rulelist": [
{
"type": "media",
"mediaqueries": "screen",
"children": [
{
"type": "style",
"selector": "*",
"declarations": {
"position": "absolute"
}
},
{
"type": "style",
"selector": ".box_shadow",
"declarations": {
"-webkit-box-shadow": "0px 0px 4px 0px #ffffff",
"box-shadow": "0px 0px 4px 0px #ffffff"
}
}
]
},
{
"type": "keyframes",
"id": "myanim",
"keyframes": [
{
"offset": "0%",
"declarations": {
"opacity": "0.0"
}
},
{
"offset": "50%",
"declarations": {
"opacity": "0.5"
}
},
{
"offset": "100%",
"declarations": {
"opacity": "1.0"
}
}
],
"prefix": "-webkit-"
},
{
"type": "style",
"selector": ".matrix",
"declarations": {
"-webkit-transform": "matrix(1.186,-0.069,0.102,1.036,16.595,73.291)",
"-moz-transform": "matrix(1.186,-0.069,0.102,1.036,16.595px,73.291px)",
"-ms-transform": "matrix(1.186,-0.069,0.102,1.036,16.595,73.291)",
"-o-transform": "matrix(1.186,-0.069,0.102,1.036,16.595,73.291)",
"transform": "matrix(1.186,-0.069,0.102,1.036,16.595,73.291)"
}
},
{
"type": "fontface",
"declarations": {
"font-family": "'WebFont'",
"src": "url('myfont.woff') format('woff'),url('myfont.ttf') format('truetype')"
}
}
]
}
##Change log
* 0.9.0-alpha - March 5th, 2016
* Fully rewrited parser.
* Supports three modes such as simple, deep, atomic.
* Also, simple mode produced different results instead of the format of previous version.
* 0.2.2 - July 27th, 2013
* Add ratio type expression with '/'. thanks to Mohsen Heydari.
* 0.2.1 - May 21st, 2013
* Update grunt, dependencies, cli options & output message.
* Add 'keyframe' type at child node of keyframes.
* 0.2.0 - May 20th, 2013
* Initial release of cssparser.js.
* 0.2.1 - May 21st, 2013
* Update grunt, dependencies, cli options & output message
* Add 'keyframe' type at child node of keyframes
* 0.2.2 - July 27th, 2013
* Add ratio type expression with '/'. thanks to Mohsen Heydari.

@@ -314,0 +403,0 @@ ##To do list

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc