Skip to content

Instantly share code, notes, and snippets.

@rtorr
Created July 31, 2013 16:32
Show Gist options
  • Save rtorr/6123658 to your computer and use it in GitHub Desktop.
Save rtorr/6123658 to your computer and use it in GitHub Desktop.
css to json
/**
* Shamelessly ripped from https://github.com/aramkocharyan/CSSJSON/blob/master/cssjson.js
* just to convert CSS to JSON for testing
*/
var CSSJSON = new function () {
var base = this;
base.init = function () {
String.prototype.trim = function () {return this.replace(/^\s+|\s+$/g, '');};
String.prototype.repeat = function (n) {return new Array(1 + n).join(this);};
};
base.init();
var lineAttrX = /([^\:]+):([^\;]*);/;
var altX = /(\/\*[\s\S]*?\*\/)|([^\s\;\{\}][^\;\{\}]*(?=\{))|(\})|([^\;\{\}]+\;(?!\s*\*\/))/gmi;
var capComment = 1;
var capSelector = 2;
var capEnd = 3;
var capAttr = 4;
var isEmpty = function (x) {return typeof x == 'undefined' || x.length == 0 || x == null;};
base.toJSON = function (cssString, args) {
var node = {children: {},attributes: {}};
var match = null;
var count = 0;
if (typeof args == 'undefined') {var args = {ordered: false,comments: false,stripComments: false,split: false};}
while ((match = altX.exec(cssString)) != null) {
if (!isEmpty(match[capComment]) && args.comments) {
var add = match[capComment].trim();
node[count++] = add;
} else if (!isEmpty(match[capSelector])) {
var name = match[capSelector].trim();
var newNode = base.toJSON(cssString, args);
} else if (!isEmpty(match[capEnd])) {
return node;
} else if (!isEmpty(match[capAttr])) {
var line = match[capAttr].trim();
var attr = lineAttrX.exec(line);
if (attr) {
var name = attr[1].trim();
var value = attr[2].trim();
if (args.ordered) {var obj = {};obj['name'] = name;obj['value'] = value;obj['type'] = 'attr';node[count++] = obj;}
else {if (name in node.attributes) {var currVal = node.attributes[name];
if (!(currVal instanceof Array)) {node.attributes[name] = [currVal];}
node.attributes[name].push(value);
} else {node.attributes[name] = value;}
}
} else {node[count++] = line;}
}
}
return node;
};
};
//Assume jquery
var $getJsonFromElement = function(element){
return CSSJSON.toJSON($(element).attr('style'));
};
/**
* CSS-JSON Converter for JavaScript
* Converts CSS to JSON and back.
* Version 2.1
*
* Released under the MIT license.
*
* Copyright (c) 2013 Aram Kocharyan, http://aramk.com/
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.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment