Last active
April 14, 2019 02:28
-
-
Save tomhodgins/9eb73cd436892dbb6f35e7870dad4e98 to your computer and use it in GitHub Desktop.
a library of functions for ripping around the CSSOM, finding, operating on, or working with the CSS stylesheets and rules you find
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
var csstools = (function() { | |
function parse(string) { | |
var iframe = document.createElement('iframe') | |
document.head.appendChild(iframe) | |
var style = iframe.contentDocument.createElement('style') | |
style.textContent = string | |
iframe.contentDocument.head.appendChild(style) | |
var stylesheet = iframe.contentDocument.styleSheets[0] | |
document.head.removeChild(iframe) | |
return stylesheet | |
} | |
function process(stylesheet, callback) { | |
function readRules(list) { | |
Array.prototype.slice.call(list.cssRules).forEach(function(rule) { | |
callback(rule) | |
if (rule.cssRules !== undefined) { | |
readRules(rule) | |
} | |
}) | |
} | |
if ( | |
[stylesheet, stylesheet.cssRules, callback].every(function(thing) { | |
return thing !== undefined | |
}) | |
&& typeof callback === 'function' | |
) { | |
readRules(stylesheet) | |
return stylesheet | |
} | |
} | |
function stringify(object) { | |
function rule(rule) { | |
return rule.cssText || '' | |
} | |
return Array.isArray(object) | |
? object.map(stringify).join('\n') | |
: (object.cssRules | |
? Array.prototype.slice.call(object.cssRules) | |
.map(rule) | |
.join('\n') | |
: rule(object) | |
) | |
} | |
function all() { | |
return Array.prototype.slice.call(document.styleSheets).map(function(stylesheet) { | |
try { | |
stylesheet.cssRules | |
} | |
catch(error) { | |
return null | |
} | |
return stylesheet | |
}).filter(Boolean) | |
} | |
function filter(stylesheet, test) { | |
var output = {cssRules: []} | |
process( | |
stylesheet, | |
function(rule) { | |
if (test(rule)) { | |
output.cssRules.push(rule) | |
} | |
} | |
) | |
return output | |
} | |
function selector(string) { | |
return all().map(function(stylesheet) { | |
return filter( | |
stylesheet, | |
function(rule) { | |
return rule.selectorText && rule.selectorText.indexOf(string) !== -1 | |
} | |
) | |
}) | |
} | |
function property(string) { | |
return all().map(function(stylesheet) { | |
return filter( | |
stylesheet, | |
function(rule) { | |
return rule.style && Array.prototype.slice.call(rule.style).some(function(property) { | |
return property.indexOf(string) !== -1 | |
}) | |
} | |
) | |
}) | |
} | |
function value(string) { | |
return all().map(function(stylesheet) { | |
return filter( | |
stylesheet, | |
function(rule) { | |
return rule.style && Array.prototype.slice.call(rule.style).some(function(prop) { | |
return rule.style.getPropertyValue(prop).indexOf(string) !== -1 | |
}) | |
} | |
) | |
}) | |
} | |
function query(string) { | |
return all().map(function(stylesheet) { | |
return filter( | |
stylesheet, | |
function(rule) { | |
return rule.media && rule.media.mediaText.indexOf(string) !== -1 | |
} | |
) | |
}) | |
} | |
function remove(object) { | |
Array.isArray(object) | |
? object.forEach(remove) | |
: (object.cssRules | |
? Array.prototype.slice.call(object.cssRules).map(remove) | |
: object.parentRule | |
? object.parentRule.deleteRule( | |
Array.prototype.slice.call(object.parentRule.cssRules).indexOf(object) | |
) | |
: object.parentStyleSheet.deleteRule( | |
Array.prototype.slice.call(object.parentStyleSheet.cssRules).indexOf(object) | |
) | |
) | |
} | |
return { | |
parse: parse, | |
process: process, | |
stringify: stringify, | |
all: all, | |
filter: filter, | |
selector: selector, | |
property: property, | |
value: value, | |
query: query, | |
remove: remove | |
} | |
})() |
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
var csstools=function(){function g(a,b){function c(a){Array.prototype.slice.call(a.cssRules).forEach(function(a){b(a);void 0!==a.cssRules&&c(a)})}if([a,a.cssRules,b].every(function(a){return void 0!==a})&&"function"===typeof b)return c(a),a}function h(a){function b(a){return a.cssText||""}return Array.isArray(a)?a.map(h).join("\n"):a.cssRules?Array.prototype.slice.call(a.cssRules).map(b).join("\n"):b(a)}function d(){return Array.prototype.slice.call(document.styleSheets).map(function(a){try{a.cssRules}catch(b){return null}return a}).filter(Boolean)}function e(a,b){var c={cssRules:[]};g(a,function(a){b(a)&&c.cssRules.push(a)});return c}function f(a){Array.isArray(a)?a.forEach(f):a.cssRules?Array.prototype.slice.call(a.cssRules).map(f):a.parentRule?a.parentRule.deleteRule(Array.prototype.slice.call(a.parentRule.cssRules).indexOf(a)):a.parentStyleSheet.deleteRule(Array.prototype.slice.call(a.parentStyleSheet.cssRules).indexOf(a))}return{parse:function(a){var b=document.createElement("iframe");document.head.appendChild(b);var c=b.contentDocument.createElement("style");c.textContent=a;b.contentDocument.head.appendChild(c);a=b.contentDocument.styleSheets[0];document.head.removeChild(b);return a},process:g,stringify:h,all:d,filter:e,selector:function(a){return d().map(function(b){return e(b,function(b){return b.selectorText&&-1!==b.selectorText.indexOf(a)})})},property:function(a){return d().map(function(b){return e(b,function(b){return b.style&&Array.prototype.slice.call(b.style).some(function(b){return-1!==b.indexOf(a)})})})},value:function(a){return d().map(function(b){return e(b,function(b){return b.style&&Array.prototype.slice.call(b.style).some(function(c){return-1!==b.style.getPropertyValue(c).indexOf(a)})})})},query:function(a){return d().map(function(b){return e(b,function(b){return b.media&&-1!==b.media.mediaText.indexOf(a)})})},remove:f}}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment