-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
35 lines (28 loc) · 910 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module.exports = function createStyleSheet(blocks) {
var style = document.createElement('style');
var text = Object.keys(blocks).map(function (selector) {
return processRuleSet(selector, blocks[selector]);
}).join('\n');
style.setAttribute('type', 'text/css');
style.appendChild(document.createTextNode(text));
return style;
}
function processRuleSet(selector, block) {
return selector + ' {\n' + processDeclarationBlock(block) + '\n}';
}
function processDeclarationBlock(block) {
return Object.keys(block).map(function (prop) {
return processDeclaration(prop, block[prop]);
}).join('\n');
}
function processDeclaration(prop, value) {
if (!isNaN(value) && value != 0) {
value = value + 'px';
}
return hyphenate(prop) + ': ' + value + ';';
}
function hyphenate(prop) {
return prop.replace(/[A-Z]/g, function (match) {
return '-' + match.toLowerCase();
});
}