-
-
Save ezakto/e6728325a2376244d564 to your computer and use it in GitHub Desktop.
CSS-like plain objects to CSS strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
a, // CSS-like plain object | |
b, // Result string | |
c, // Selector key | |
d // Property key | |
){ | |
b=''; // Initialize result | |
for(c in a){ // Loop through the rules | |
b+=c+'{'; // Append selector and open rule's property listing | |
for(d in a[c]) // Loop through properties | |
b+=d+':'+a[c][d]+';'; // Append property:value; | |
b+='}' // Close rule | |
} | |
return b // There you go | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c,d){b='';for(c in a){b+=c+'{';for(d in a[c])b+=d+':'+a[c][d]+';';b+='}'}return b} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2015 Nicolás Arias <nicolas@nicolasarias.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "JSON2CSS", | |
"description": "Convert CSS-like plain objects into CSS strings", | |
"keywords": [ | |
"css", | |
"object" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>.selector{line-height:16px;color:red;}#selector{height:300px;}</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var css = { | |
".selector": { | |
"line-height": "16px", | |
"color": "red" | |
}, | |
"#selector": { | |
"height": "300px" | |
} | |
} | |
var myFunction = function(a,b,c,d){b='';for(c in a){b+=c+'{';for(d in a[c])b+=d+':'+a[c][d]+';';b+='}'}return b} | |
document.getElementById( "ret" ).innerHTML = myFunction(css) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about using recursion and abusing the fact that strings have a (deprecated) big method?