UNPKG

j2c

Version:

A tiny CSS in JS solution.

251 lines (232 loc) 7.35 kB
// // var style = document.getElementsByTagName('head')[0].appendChild(document.createElement('style')) // // var sheet = style.sheet // // var own = ({}).hasOwnProperty // // var insert, remove, nodeCache = {}, ruleCount = {} // // if (options && options.fastDOM && ('insertRule' in style.sheet)) { // // insert = function(rule) { // // nodeCount // // nodeCache[rule] = sheet.insertRule(rule) // // } // // remove = function(rule) { // // sheet.deleteRule(nodeCache[rule]) // // for (var r in nodeCache) if (nodeCache[r] > nodeCache[rule]) nodeCache[r]-- // // delete nodeCache[rule] // // } // // } else { // // insert = function(rule) { // // nodeCache[rule] = document.createTextNode(rule) // // style.appendChild(nodeCache[rule]) // // } // // remove = function(rule, nodeCache, isLast) { // // sheet.deleteRule(nodeCache[rule]) // // delete nodeCache[rule] // // } // // } // // export default function(options) { // // var index = {} // // var elements = {} // // function Sheet() { // // this.buffers = [] // // this.sheets = null // // this.insertCount = 0 // // } // // Sheet.prototype = { // // inject: function() { // // if (this.insertCount++ > 0) return // // if (this.rules == null) this.rules = this.buffers.map(function(buf){return buf.join('')}) // // this.rules.forEach(function(rule) { // // if (!++ruleCount[rule]) { // // ruleCount[rule] = 1 // // insert(rule) // // } // // }) // // }, // // remove: function() { // // if (--this.insertCount === 0) { // // this.rules.forEach(function(rule) { // // if (--ruleCount[rule] === 0) { // // delete ruleCount[rule] // // remove(rule) // // } // // }) // // } // // this.insertCount = Math.max(this.insertCount, 0) // // }, // // toString: function() { // // return this.buffer.join('') // // }, // // valueOf: function() { // // return this.buffer.join('') // // } // // } // // return function(j2c) { // // var buf, depth, errors, sheet // // return {$sink: function() { // // return { // // init: function() { // // sheet = new Sheet() // // buf = [] // // sheet.buffers.push(buf) // // level = 0 // // errors = [] // // }, // // done: function() { // // if (level > 0) errors.push('unclosed block') // // if (errors.length > 0) throw new Error(buildErrorMessage()) // // }, // // err: function(msg) { // // errors.push(msg) // // } // // } // // }} // // } // // } // // var head = document.getElementsByTagName('head')[0] // // export default function(options) { // // return function domPlugin(j2c) { // // var sheet = j2c.sheet // // j2c.sheet = function(tree) { // // var sheet = sheet.call(j2c, tree) // // if (sheet in index) { // // index[sheet]++ // // } else { // // index[sheet] = 1 // // var style = document.createElement('style') // // style.type = 'text/css' // might not even be needed // // if (stlye.styleSheet) { // // style.styleSheet.cssText = sheet; // // } else { // // style.appendChild(document.createTextNode(sheet)); // // } // // elements[sheet] = style // // head.appendChild(style) // // } // // return sheet // // } // // j2c.remove: function(sheet) { // // if (!( sheet in index )) return // // index[sheet]-- // // if (index[sheet] === 0) { // // var style = elements[sheet] // // style.parentNode.removeChild(style) // // delete elements[child] // // } // // } // // } // // } // var style = document.getElementsByTagName('head')[0].appendChild(document.createElement('style')) // var sheet = style.sheet // var own = ({}).hasOwnProperty // var insert, remove, nodeCache = {}, ruleCount = {} // if (options && options.fastDOM && ('insertRule' in style.sheet)) { // insert = function(rule) { // nodeCount // nodeCache[rule] = sheet.insertRule(rule) // } // remove = function(rule) { // sheet.deleteRule(nodeCache[rule]) // for (var r in nodeCache) if (nodeCache[r] > nodeCache[rule]) nodeCache[r]-- // delete nodeCache[rule] // } // } else { // insert = function(rule) { // nodeCache[rule] = document.createTextNode(rule) // style.appendChild(nodeCache[rule]) // } // remove = function(rule, nodeCache, isLast) { // sheet.deleteRule(nodeCache[rule]) // delete nodeCache[rule] // } // } // export default function(options) { // var index = {} // var elements = {} // function Sheet() { // this.buffers = [] // this.sheets = null // this.insertCount = 0 // } // Sheet.prototype = { // inject: function() { // if (this.insertCount++ > 0) return // if (this.rules == null) this.rules = this.buffers.map(function(buf){return buf.join('')}) // this.rules.forEach(function(rule) { // if (!++ruleCount[rule]) { // ruleCount[rule] = 1 // insert(rule) // } // }) // }, // remove: function() { // if (--this.insertCount === 0) { // this.rules.forEach(function(rule) { // if (--ruleCount[rule] === 0) { // delete ruleCount[rule] // remove(rule) // } // }) // } // this.insertCount = Math.max(this.insertCount, 0) // }, // toString: function() { // return this.buffer.join('') // }, // valueOf: function() { // return this.buffer.join('') // } // } // return function(j2c) { // var buf, depth, errors, sheet // return {$sink: function() { // return { // init: function() { // sheet = new Sheet() // buf = [] // sheet.buffers.push(buf) // level = 0 // errors = [] // }, // done: function() { // if (level > 0) errors.push('unclosed block') // if (errors.length > 0) throw new Error(buildErrorMessage()) // }, // err: function(msg) { // errors.push(msg) // } // } // }} // } // } // var head = document.getElementsByTagName('head')[0] // export default function(options) { // return function domPlugin(j2c) { // var sheet = j2c.sheet // j2c.sheet = function(tree) { // var sheet = sheet.call(j2c, tree) // if (sheet in index) { // index[sheet]++ // } else { // index[sheet] = 1 // var style = document.createElement('style') // style.type = 'text/css' // might not even be needed // if (stlye.styleSheet) { // style.styleSheet.cssText = sheet; // } else { // style.appendChild(document.createTextNode(sheet)); // } // elements[sheet] = style // head.appendChild(style) // } // return sheet // } // j2c.remove: function(sheet) { // if (!( sheet in index )) return // index[sheet]-- // if (index[sheet] === 0) { // var style = elements[sheet] // style.parentNode.removeChild(style) // delete elements[child] // } // } // } // }