Comparing version
18
index.js
@@ -1,12 +0,18 @@ | ||
var inserted = []; | ||
var inserted = {}; | ||
module.exports = function (css) { | ||
if (inserted.indexOf(css) >= 0) return; | ||
inserted.push(css); | ||
if (inserted[css]) return; | ||
inserted[css] = true; | ||
var elem = document.createElement('style'); | ||
var text = document.createTextNode(css); | ||
elem.appendChild(text); | ||
elem.setAttribute('type', 'text/css'); | ||
if ('textContent' in elem) { | ||
elem.textContent = css; | ||
} else { | ||
elem.styleSheet.cssText = css; | ||
} | ||
document.head.appendChild(elem); | ||
var head = document.getElementsByTagName('head')[0]; | ||
head.appendChild(elem); | ||
}; |
{ | ||
"name": "insert-css", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "insert a string of css into the <head>", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tape": "~2.3.0" | ||
"tape": "~2.4.0", | ||
"computed-style": "~0.1.3" | ||
}, | ||
@@ -11,0 +12,0 @@ "repository": { |
var test = require('tape'); | ||
var insertCss = require('../'); | ||
var getStyle = require('computed-style'); | ||
@@ -8,9 +9,6 @@ var css = 'body { background-color: purple; color: yellow; }'; | ||
t.plan(6); | ||
if (typeof window.getComputedStyle !== 'function') { | ||
t.fail('cannot getComputedStyle()'); | ||
} | ||
var before = colors(); | ||
t.ok(before.bg === 'rgba(0,0,0,0)' || before.bg === 'transparent'); | ||
t.equal(before.fg, 'rgb(0,0,0)'); | ||
t.ok(before.fg === 'rgb(0,0,0)' || before.fg === '#000000'); | ||
@@ -20,6 +18,6 @@ insertCss(css); | ||
var after = colors(); | ||
t.equal(after.bg, 'rgb(128,0,128)'); | ||
t.equal(after.fg, 'rgb(255,255,0)'); | ||
t.ok(after.bg === 'rgb(128,0,128)' || after.bg === 'purple'); | ||
t.ok(after.fg === 'rgb(255,255,0)' || after.fg === 'yellow'); | ||
var resetStyle = 'body { background-color: transparent; color: black; }'; | ||
var resetStyle = 'body { background-color: transparent; color: #000000; }'; | ||
insertCss(resetStyle); | ||
@@ -29,11 +27,11 @@ | ||
t.ok(reset.bg === 'rgba(0,0,0,0)' || reset.bg === 'transparent'); | ||
t.ok(reset.fg === 'rgb(0,0,0)'); | ||
t.ok(reset.fg === 'rgb(0,0,0)' || reset.fg === '#000000'); | ||
}); | ||
function colors () { | ||
var style = window.getComputedStyle(document.body); | ||
var body = document.getElementsByTagName('body')[0]; | ||
return { | ||
bg: style.backgroundColor.replace(/\s+/g, ''), | ||
fg: style.color.replace(/\s+/g, '') | ||
bg: getStyle(body, 'backgroundColor').replace(/\s+/g, ''), | ||
fg: getStyle(body, 'color').replace(/\s+/g, '') | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
6033
3.87%74
4.23%2
100%