Skip to content

Commit 5f848b7

Browse files
JamesKyburzJames Halliday
authored andcommitted
Added getComputedStyle polyfill to test - showing insert-css fails in ie8
1 parent f24d993 commit 5f848b7

1 file changed

Lines changed: 83 additions & 9 deletions

File tree

test/insert.js

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,107 @@ var insertCss = require('../');
44
var css = 'body { background-color: purple; color: yellow; }';
55

66
test(function (t) {
7+
iePolyfil();
78
t.plan(6);
8-
if (typeof window.getComputedStyle !== 'function') {
9-
t.fail('cannot getComputedStyle()');
10-
}
119

1210
var before = colors();
1311
t.ok(before.bg === 'rgba(0,0,0,0)' || before.bg === 'transparent');
14-
t.equal(before.fg, 'rgb(0,0,0)');
12+
t.ok(before.fg === 'rgb(0,0,0)' || before.fg === '#000000');
1513

1614
insertCss(css);
1715

1816
var after = colors();
19-
t.equal(after.bg, 'rgb(128,0,128)');
20-
t.equal(after.fg, 'rgb(255,255,0)');
17+
t.ok(after.bg === 'rgb(128,0,128)' || after.bg === 'purple');
18+
t.ok(after.fg === 'rgb(255,255,0)' || after.fg === 'yellow');
2119

22-
var resetStyle = 'body { background-color: transparent; color: black; }';
20+
var resetStyle = 'body { background-color: transparent; color: #000000; }';
2321
insertCss(resetStyle);
2422

2523
var reset = colors();
2624
t.ok(reset.bg === 'rgba(0,0,0,0)' || reset.bg === 'transparent');
27-
t.ok(reset.fg === 'rgb(0,0,0)');
25+
t.ok(reset.fg === 'rgb(0,0,0)' || reset.fg === '#000000');
2826
});
2927

3028
function colors () {
31-
var style = window.getComputedStyle(document.body);
29+
var body = document.getElementsByTagName('body')[0];
30+
var style = window.getComputedStyle(body);
3231
return {
3332
bg: style.backgroundColor.replace(/\s+/g, ''),
3433
fg: style.color.replace(/\s+/g, '')
3534
};
3635
}
36+
37+
function iePolyfil() {
38+
// getComputedStyle https://raw.github.com/jonathantneal/Polyfills-for-IE8/master/getComputedStyle.js
39+
!('getComputedStyle' in this) && (this.getComputedStyle = (function () {
40+
function getPixelSize(element, style, property, fontSize) {
41+
var
42+
sizeWithSuffix = style[property],
43+
size = parseFloat(sizeWithSuffix),
44+
suffix = sizeWithSuffix.split(/\d/)[0],
45+
rootSize;
46+
47+
fontSize = fontSize != null ? fontSize : /%|em/.test(suffix) && element.parentElement ? getPixelSize(element.parentElement, element.parentElement.currentStyle, 'fontSize', null) : 16;
48+
rootSize = property == 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;
49+
50+
return (suffix == 'em') ? size * fontSize : (suffix == 'in') ? size * 96 : (suffix == 'pt') ? size * 96 / 72 : (suffix == '%') ? size / 100 * rootSize : size;
51+
}
52+
53+
function setShortStyleProperty(style, property) {
54+
var
55+
borderSuffix = property == 'border' ? 'Width' : '',
56+
t = property + 'Top' + borderSuffix,
57+
r = property + 'Right' + borderSuffix,
58+
b = property + 'Bottom' + borderSuffix,
59+
l = property + 'Left' + borderSuffix;
60+
61+
style[property] = (style[t] == style[r] == style[b] == style[l] ? [style[t]]
62+
: style[t] == style[b] && style[l] == style[r] ? [style[t], style[r]]
63+
: style[l] == style[r] ? [style[t], style[r], style[b]]
64+
: [style[t], style[r], style[b], style[l]]).join(' ');
65+
}
66+
67+
function CSSStyleDeclaration(element) {
68+
var
69+
currentStyle = element.currentStyle,
70+
style = this,
71+
fontSize = getPixelSize(element, currentStyle, 'fontSize', null);
72+
73+
for (property in currentStyle) {
74+
if (/width|height|margin.|padding.|border.+W/.test(property) && style[property] !== 'auto') {
75+
style[property] = getPixelSize(element, currentStyle, property, fontSize) + 'px';
76+
} else if (property === 'styleFloat') {
77+
style['float'] = currentStyle[property];
78+
} else {
79+
style[property] = currentStyle[property];
80+
}
81+
}
82+
83+
setShortStyleProperty(style, 'margin');
84+
setShortStyleProperty(style, 'padding');
85+
setShortStyleProperty(style, 'border');
86+
87+
style.fontSize = fontSize + 'px';
88+
89+
return style;
90+
}
91+
92+
CSSStyleDeclaration.prototype = {
93+
constructor: CSSStyleDeclaration,
94+
getPropertyPriority: function () {},
95+
getPropertyValue: function ( prop ) {
96+
return this[prop] || '';
97+
},
98+
item: function () {},
99+
removeProperty: function () {},
100+
setProperty: function () {},
101+
getPropertyCSSValue: function () {}
102+
};
103+
104+
function getComputedStyle(element) {
105+
return new CSSStyleDeclaration(element);
106+
}
107+
108+
return getComputedStyle;
109+
})(this));
110+
}

0 commit comments

Comments
 (0)