Created
February 24, 2013 04:11
-
-
Save forestbelton/5022559 to your computer and use it in GitHub Desktop.
Prepends class / id selectors with "ns_" if data-namespace="ns" is given on the <link> tag.
This file contains hidden or 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() { | |
var nsify = function(ns, selChunk) { | |
sels = selChunk.replace(/ /g, '').split(','); | |
for(var i = 0; i < sels.length; ++i) | |
if(sels[i][0].match(/[.#]/)) | |
sels[i] = sels[i][0] + ns + '-' + sels[i].substring(1); | |
return sels.join(','); | |
}; | |
var cssText = ''; | |
for(var i = 0; i < document.styleSheets.length; ++i) { | |
var sheet = document.styleSheets[i]; | |
if(!sheet.ownerNode.dataset) | |
continue; | |
if(!sheet.ownerNode.dataset.namespace) | |
continue; | |
var ns = sheet.ownerNode.dataset.namespace; | |
sheet.disabled = true; | |
for(var j = 0; j < sheet.cssRules.length; ++j) { | |
var rule = sheet.cssRules[j]; | |
var newsel = nsify(ns, rule.selectorText); | |
cssText += rule.cssText.replace(/^(.*?){/, newsel + ' {') + '\n\n'; | |
} | |
} | |
var style = document.createElement('style'); | |
style.type = 'text/css'; | |
if(style.styleSheet) | |
style.styleSheet.cssText = cssText; | |
else | |
style.appendChild(document.createTextNode(cssText)); | |
document.getElementsByTagName('head')[0].appendChild(style); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment