CjsSS (Cascading Javascript Style Sheet) is LESS SASSy and more Stylush than the competition ;)
cjsss.min.js(Node.js optional for server-side preprocessing)
objects
camelCaseto
dash-caseof Javascript
objectkeys converted auto-magicially
evaluated Javascript fully sandboxed by default
SCRIPTreferences within the CSS (e.g.
<script src='https://github.com/gka/chroma.js'></script>).
Variables within
styleattributes
SCRIPTtag(s) in the
HEADof your HTML document below any
LINK/
STYLEtags you want to process:
<script type="text/javascript" src="cjsss-polyfill.js"></script> <!-- IE6-7 support --> <script type="text/javascript" src="cjsss.min.js"></script>
cjsssattribute to the
LINKand/or
STYLEtags you want to process:
<link rel="stylesheet" type="text/css" href="css/site.css" cjsss> <style type="text/css" cjsss>...</style>
optionsused by CjsSS.js (if any) using JSON-style object definitions (both keys and values are "double quoted"):
<style type="text/css" cjsss='{ "crlf": "\n" }'>...</style> <link rel="stylesheet" type="text/css" href="css/site.css" cjsss='{ "crlf": "\n" }'>
LINKand/or
STYLEtags to use CjsSS.js like this:
/*<script> var mainColor = 'blue'; </script>*/ DIV { color: /*mainColor*/; }
data-cjsssin place of
cjsssif you want to be HTML5 compliant.
autorunand
exposethe
window.cjsssobject (NOTE:
exposehappens auto-magicially when you disable
autorun) like so:
When CjsSS.js is<script> window.cjsss = { options: { selector: '', //# disables autorun expose: true //# exposes `window.cjsss` } }; </script> <script type="text/javascript" src="cjsss.min.js"></script> <!-- include CjsSS.js -->
exposed, the following properties are made available under
window.cjsss:
dataexposes the internal variables utilized by the
services:
cachestores the associative array that holds (amoung other things) the original
innerHTMLof the CjsSS.js processed
LINKand
STYLEtags, keyed by DOM reference.
injectstores the associative array that holds the values added via
cjsss.inject, keyed by the provided
sVarName.
servicesstores the unmodified default implementations of the internal services used by CjsSS.js.
inject(sVarName, variant)function injects the provided
variantinto the evaluated Javascript code via the provided
sVarName. This functionality allows you to expose any Javascript functionality you wish to the evaluated Javascript code. This call is always forwarded to the implementation present in
services.injectso there is no need to reset this property.
options* stores the default values as described in the options section.
process(vElements, oOptions)function processes the referenced
vElementswith preference to the provided
oOptions.
vElementscan be of type
string(as a CSS selector) or as an array-like collection of DOM element references (such as a NodeList, jQuery object or an Array).
oOptionsis an
objectdescribing the desired options. This call is always forwarded to the implementation present in
services.processso there is no need to reset this property.
services* stores the default implementations of the internal services used by CjsSS.js. Please view source code within
cjsss.jsfor further details.
versionstores the version number of CjsSS.js that is being run.
exposebeing run are preserved in addition to any other properties not described above (e.g.
window.cjsss.customwould be preserved).
injectCustom Functionality
autorun,
cjsss.injectcan be used to expose any Javascript variable or value to your CjsSS.js evaluated Javascript code:
The code above exposes a function called "invertColor" to all CjsSS.js evaluated Javascript code like this:<script> //# .inject a function named "invertColor" cjsss.inject("invertColor", function(sHex) { var r, g, b, sReturnVal; if (sHex.length === 7 && sHex.indexOf('#') === 0) { r = "0" + (255 - parseInt(sHex.substr(1, 2), 16)).toString(16); g = "0" + (255 - parseInt(sHex.substr(3, 2), 16)).toString(16); b = "0" + (255 - parseInt(sHex.substr(5, 2), 16)).toString(16); sReturnVal = "#" + r.substr(-2) + g.substr(-2) + b.substr(-2); } return sReturnVal; }); //# .inject an object named "userConfig" var defaultColor = { color: "green" }; cjsss.inject("userConfig", defaultColor); //# .inject an object named "span" var spanCss = { backgroundColor: "red" subObject: defaultColor }; cjsss.inject("span", spanCss); //# .inject a string named "textTransform" cjsss.inject("textTransform", "capitalize"); //# Process the LINK / STYLE tags with a "cjsss" attribute and no oOptions cjsss.process("[cjsss]", {}); </script>
/*<script> var sMainColor = "#cccccc"; </script>*/ DIV { color: /*invertColor(sMainColor)*/; //# invertColor returns '#333333' text-transform: /*textTransform*/; //# outputs 'capitalize' } P { /*mixin(userConfig)*/ //# outputs 'color: "green";' } /*mixin(span, true)*/ //# outputs 'span { background-color: "red"; color: "green"; }'
cjsss.servicesFactoryor
cjsss.services.
cjsss.servicesexposes all of the internal functionality of CjsSS.js, allowing every piece of functionality within CjsSS.js to be overridden. Implementing a
cjsss.servicesFactoryunder
window.cjsssprior to including the CjsSS.js
SCRIPTtag(s) allows you to override any
cjsss.servicesfunctionality prior to CjsSS.js loading, or you can replace any of the functions under
cjsss.servicesafter CjsSS.js has been exposed via
window.cjsss. For example, if you wanted to override the
exposefunctionality to make CjsSS.js available in a variable other than
window.cjsss, you could do the following:
<script> var newVar = null; window.cjsss = { options: { expose: true //# exposes CjsSS.js under `newVar` }, servicesFactory: function ($cjsss) { return { expose: function() { newVar = $cjsss; } }; } }; </script> <script type="text/javascript" src="cjsss.min.js"></script>
autorunbehavior of CjsSS.js then you will want to include the CjsSS.js
SCRIPTreference after all
cjsssattributed
LINK/
STYLEtags.
LINKtag CSS files are read via AJAX, the limitations of
XMLHttpRequestcan restrict where you serve your CSS files from. If your CSS files exist on the same server, this is a non-issue.
LINKtags are removed from the DOM, their CSS file contents are modified and placed within a new
STYLEtag that is appended into the
HEAD. This process can break some relative URLs within your CSS files (e.g.
background-image: url("../img/a.png");). It is therefore recommended that you use absolute URLs within your CSS files (e.g.
background-image: url("/img/a.png");) in order to avoid any such errors.
SCRIPTs are all evaluated in series at initilization prior to CSS modification, so the following will not work as expected:
At initilization/*<script> var mainColor = 'blue'; </script>*/ DIV { color: /*mainColor*/; //# expecting 'blue' but gets 'green' } /*<script> mainColor = 'green'; </script>*/ SPAN { color: /*mainColor*/; //# expecting 'green' }
mainColorwill be set to "blue" then "green" prior to any CSS modification, so the
colorof both DIV and SPAN will be set to "green".
document.querySelectorAlland
JSON.parsefunctionality has been polyfilled via
cjsss.polyfill.jsor with jQuery (which is auto-magicially imported) included prior to CjsSS.js. The required "advanced" JavaScript functionality is:
cjsss.polyfill.jsor jQuery's
$()
cjsss.polyfill.jsor jQuery's
$.parseJSON()
LINKtags are removed from the DOM, their CSS file contents are modified and placed within a new
STYLEtag that is appended into the
HEAD. This is done so that the original unprocessed CSS referenced by the
LINKtag does not apply to the webpage.
exposeCjsSS.js, the
window.cjsssobject will be added under the global scope.
scopeof either
global,
localor
this, CjsSS.js-embedded Javascript has the ability to modify the global scope.
scopeof
global, the
window.mixinfunction will be added under the global scope (though it will not overwrite an existing
window.mixinif one exists).
LINKand
STYLEtags is modified based on the embedded Javascript.
Oh, you mean Cee & Jay? They are the mascots for CjsSS.js!
CjsSS (cee-JAY-hiss or Cascading Javascript Style Sheet) is the union of two related web technologies joined seemlessly (unnaturally?) into one solution. Thanks to the akwardness of the acronym (ending in three S's) it seemed reminisent of a snake's hiss... "CSS and Javascript sss-ing"... "CJ hiss"... Hey! I need a two-headed snake!
CjsSS.js is a Vanilla-Javascript toolkit made by Nick Campbell, © 2014.
CjsSS.js is released under the MIT License. See the license.txt file in the source code for copy permission.
Nick is a web nerd who likes to tinker in his spare time; CjsSS.js is a product of that tinkering. Development is not supported by advertising nor the marketing of your email addresses. If you find this code useful, please consider tossing a few dollars into my tip jar. If you are a company that finds this code useful, please feel free to toss more than a few dollars into my tip jar ;)