'use strict'; require('./object-keys'); require('./array-map'); var camel = /([a-z])([A-Z])/g; var hyphens = '$1-$2'; var contexts = {}; function parseStyles (styles) { if (typeof styles === 'string') { return styles; } if (Object.prototype.toString.call(styles) !== '[object Object]') { return ''; } return Object.keys(styles).map(function (key) { var prop = key.replace(camel, hyphens).toLowerCase(); return prop + ':' + styles[key]; }).join(';'); } function context (name) { if (contexts[name]) { return contexts[name]; } var cache; var rules; var remove; function getStylesheet () { if (cache) { return cache; } var style = document.createElement('style'); document.body.appendChild(style); style.setAttribute('data-context', name); cache = document.styleSheets[document.styleSheets.length - 1]; rules = cache.cssRules ? 'cssRules' : 'rules'; remove = cache.removeRule ? 'removeRule' : 'deleteRule'; return cache; } function add (selector, styles) { var css = parseStyles(styles); var sheet = getStylesheet(); var len = sheet[rules].length; if (sheet.insertRule) { sheet.insertRule(selector + '{' + css + '}', len); } else if (sheet.addRule) { sheet.addRule(selector, css, len); } } function remove (selector) { var sheet = getStylesheet(); var length = sheet[rules].length; var i; for (i = length - 1; i >= 0; i--) { if (sheet[rules][i].selectorText === selector) { sheet[remove](i); } } } function clear () { var sheet = getStylesheet(); while (sheet[rules].length) { sheet[remove](0); } } add.clear = clear; add.remove = remove; contexts[name] = add; return contexts[name]; } var ctx = context('default'); ctx.context = context; module.exports = ctx;