CSSJSON icon indicating copy to clipboard operation
CSSJSON copied to clipboard

Issue converting from JSON (ordered) to CSS

Open svarlitskiy opened this issue 8 years ago • 2 comments

Basically I can convert the css to JSON ordered but not back to css. Here are the fixes I would recommend to allow for that to happen correctly:

In CSSJSON.js -> base.toJSON function

replace

} else if (!isEmpty(match[capEnd])) {
    // Node has finished
    return node;
}

with

} else if (!isEmpty(match[capEnd])) {
    // Node has finished
    if (args.ordered) node.length=count;
    return node;
}

replace

return node;

with

if (args.ordered) node.length=count;
return node;

In CSSJSON.js -> base.toCSS function

replace

return cssString;

with

if ( typeof node.length === "number" ) {
    for (var j = 0; j < node.length; j++) {
        cssString += base.toCSS(node[j], depth);
    }
}
else if ( typeof node.value === "object" ) {
    cssString += strNode(node.name, node.value, depth);
}
else if ( typeof node.value != undefined ) {
    cssString += strAttr(node.name, node.value, depth);
}
return cssString;

In CSSJSON.js -> strNode function

replace

var strNode = function (name, value, depth) {
    var cssString = '\t'.repeat(depth) + name + ' {\n';
    cssString += base.toCSS(value, depth + 1);
    cssString += '\t'.repeat(depth) + '}\n';
    return cssString;
};

with

var strNode = function (name, value, depth) {
    var cssIndent = '\t'.repeat(depth), 
        cssString = cssIndent + name.split(/\s*,\s*/gi).join( ',\n' + cssIndent ) + ' {\n';

    cssString += base.toCSS(value, depth + 1);
    cssString += cssIndent + '}\n';
    return cssString;
};

svarlitskiy avatar Jul 11 '17 01:07 svarlitskiy

@svarlitskiy Hi, can you make a PR please ?

Kompwu avatar Jul 29 '18 18:07 Kompwu

@svarlitskiy I tried but it doesn't work!

dozsolti avatar Feb 12 '20 16:02 dozsolti