-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (66 loc) · 2.44 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(function(factory, global){
if(typeof define === 'function' && define.amd){
define(factory);
}
else if(typeof module === 'object' && typeof exports === 'object'){
module.exports = factory();
}
else{
global.getMatchedCSSRules = factory();
}
})((function(win){
function matchMedia(mediaRule){
return win.matchMedia(mediaRule.media.mediaText).matches;
}
function matchesSelector(el, selector) {
var matchesSelector = el.matchesSelector || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
if (matchesSelector) {
try {
return matchesSelector.call(el, selector);
} catch (e) {
return false;
}
}else{
var matches = el.ownerDocument.querySelectorAll(selector),
len = matches.length;
while(len && len--){
if(matches[len] === el){
return true;
}
}
}
return false;
}
function getMatchedCSSRules(el){
var matchedRules = [],
sheets = el.ownerDocument.styleSheets,
slen = sheets.length, rlen, rules, mrules, mrlen, rule, mediaMatched;
if (el.nodeType === 1) {
while(slen && slen--){
rules = sheets[slen].cssRules || sheets[slen].rules;
rlen = rules.length;
while(rlen && rlen--){
rule = rules[rlen];
if(rule instanceof CSSStyleRule && matchesSelector(el, rule.selectorText)){
matchedRules.push(rule);
}else if(rule instanceof CSSMediaRule){
if(matchMedia(rule)){
mrules = rule.cssRules || rule.rules;
mrlen = mrules.length;
while(mrlen && mrlen--){
rule = mrules[mrlen];
if(rule instanceof CSSStyleRule && matchesSelector(el, rule.selectorText)){
matchedRules.push(rule);
}
}
}
}
}
}
}
return matchedRules;
}
return function(){
return win.getMatchedCSSRules ? win.getMatchedCSSRules : getMatchedCSSRules;
};
})(window), this);