-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (38 loc) · 974 Bytes
/
index.js
File metadata and controls
46 lines (38 loc) · 974 Bytes
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
/**
* Expose `style`.
*/
module.exports = style;
/**
* Return the style for `prop` using the given `selector`.
*
* @param {String} selector
* @param {String} prop
* @return {String}
* @api public
*/
function style(selector, prop) {
var cache = style.cache = style.cache || {}
, cid = selector + ':' + prop;
if (cache[cid]) return cache[cid];
var parts = selector.split(/ +/)
, len = parts.length
, parent = document.createElement('div')
, root = parent
, child
, part;
for (var i = 0; i < len; ++i) {
part = parts[i];
child = document.createElement('div');
parent.appendChild(child);
parent = child;
if ('#' == part[0]) {
child.setAttribute('id', part.substr(1));
} else if ('.' == part[0]) {
child.setAttribute('class', part.substr(1));
}
}
document.body.appendChild(root);
var ret = getComputedStyle(child)[prop];
document.body.removeChild(root);
return cache[cid] = ret;
}