-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (39 loc) · 1.32 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
'use strict';
var isUnitlessNumber = require('./lib/CSSProperty').isUnitlessNumber;
var hyphenateStyleName = require('./lib/hyphenateStyleName');
var isArray = Array.isArray;
var keys = Object.keys;
var counter = 1;
// Follows syntax at https://developer.mozilla.org/en-US/docs/Web/CSS/content,
// including multiple space separated values.
var unquotedContentValueRegex = /^(normal|none|(\b(url\([^)]*\)|chapter_counter|attr\([^)]*\)|(no-)?(open|close)-quote|inherit)((\b\s*)|$|\s+))+)$/;
function buildRule(key, value) {
if (!isUnitlessNumber[key] && typeof value === 'number') {
value = '' + value + 'px';
}
else if (key === 'content' && !unquotedContentValueRegex.test(value)) {
value = "'" + value.replace(/'/g, "\\'") + "'";
}
return hyphenateStyleName(key) + ': ' + value + '; ';
}
function styleToCssString(rules) {
var result = ''
if (!rules || keys(rules).length === 0) {
return result;
}
var styleKeys = keys(rules);
for (var j = 0, l = styleKeys.length; j < l; j++) {
var styleKey = styleKeys[j];
var value = rules[styleKey];
if (isArray(value)) {
for (var i = 0, len = value.length; i < len; i++) {
result += buildRule(styleKey, value[i]);
}
}
else {
result += buildRule(styleKey, value);
}
}
return result;
}
module.exports = styleToCssString;