patternlibrary/styleguide/kss-assets/kss.js

54 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-06-21 01:38:13 +00:00
(function() {
var KssStateGenerator;
KssStateGenerator = (function() {
var pseudo_selectors;
pseudo_selectors = ['hover', 'enabled', 'disabled', 'active', 'visited', 'focus', 'target', 'checked', 'empty', 'first-of-type', 'last-of-type', 'first-child', 'last-child'];
function KssStateGenerator() {
var idx, idxs, pseudos, replaceRule, rule, stylesheet, _i, _len, _len2, _ref, _ref2;
pseudos = new RegExp("(\\:" + (pseudo_selectors.join('|\\:')) + ")", "g");
try {
_ref = document.styleSheets;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
stylesheet = _ref[_i];
if (stylesheet.href && stylesheet.href.indexOf(document.domain) >= 0) {
idxs = [];
_ref2 = stylesheet.cssRules;
for (idx = 0, _len2 = _ref2.length; idx < _len2; idx++) {
rule = _ref2[idx];
if ((rule.type === CSSRule.STYLE_RULE) && pseudos.test(rule.selectorText)) {
replaceRule = function(matched, stuff) {
return matched.replace(/\:/g, '.pseudo-class-');
};
this.insertRule(rule.cssText.replace(pseudos, replaceRule));
}
pseudos.lastIndex = 0;
}
}
}
} catch (_error) {}
}
KssStateGenerator.prototype.insertRule = function(rule) {
var headEl, styleEl;
headEl = document.getElementsByTagName('head')[0];
styleEl = document.createElement('style');
styleEl.type = 'text/css';
if (styleEl.styleSheet) {
styleEl.styleSheet.cssText = rule;
} else {
styleEl.appendChild(document.createTextNode(rule));
}
return headEl.appendChild(styleEl);
};
return KssStateGenerator;
})();
new KssStateGenerator;
}).call(this);