11/* eslint-disable */
22// polyfill element.matches
3- if ( ! Element . prototype . matches ) {
3+ if ( typeof Element !== 'undefined' && ! Element . prototype . matches ) {
44 Element . prototype . matches =
55 Element . prototype . matchesSelector ||
66 Element . prototype . mozMatchesSelector ||
77 Element . prototype . msMatchesSelector ||
88 Element . prototype . oMatchesSelector ||
99 Element . prototype . webkitMatchesSelector ||
10- function ( s ) {
10+ function ( s ) {
1111 var matches = ( this . document || this . ownerDocument ) . querySelectorAll ( s ) ,
1212 i = matches . length ;
1313 while ( -- i >= 0 && matches . item ( i ) !== this ) { }
@@ -16,42 +16,43 @@ if (!Element.prototype.matches) {
1616}
1717
1818// polyfill window.getMatchedCSSRules() in FireFox 6+
19- if ( typeof window . getMatchedCSSRules !== 'function' ) {
19+ if ( typeof window !== 'undefined' && typeof window . getMatchedCSSRules !== 'function' ) {
2020 var ELEMENT_RE = / [ \w - ] + / g,
2121 ID_RE = / # [ \w - ] + / g,
2222 CLASS_RE = / \. [ \w - ] + / g,
2323 ATTR_RE = / \[ [ ^ \] ] + \] / g,
2424 // :not() pseudo-class does not add to specificity, but its content does as if it was outside it
2525 PSEUDO_CLASSES_RE = / \: (? ! n o t ) [ \w - ] + ( \( .* \) ) ? / g,
2626 PSEUDO_ELEMENTS_RE = / \: \: ? ( a f t e r | b e f o r e | f i r s t - l e t t e r | f i r s t - l i n e | s e l e c t i o n ) / g;
27+
2728 // convert an array-like object to array
28- function toArray ( list ) {
29+ function toArray ( list ) {
2930 return [ ] . slice . call ( list ) ;
3031 }
3132
3233 // handles extraction of `cssRules` as an `Array` from a stylesheet or something that behaves the same
33- function getSheetRules ( stylesheet ) {
34+ function getSheetRules ( stylesheet ) {
3435 var sheet_media = stylesheet . media && stylesheet . media . mediaText ;
3536 // if this sheet is disabled skip it
36- if ( stylesheet . disabled ) return [ ] ;
37+ if ( stylesheet . disabled ) return [ ] ;
3738 // if this sheet's media is specified and doesn't match the viewport then skip it
38- if ( sheet_media && sheet_media . length && ! window . matchMedia ( sheet_media ) . matches ) return [ ] ;
39+ if ( sheet_media && sheet_media . length && ! window . matchMedia ( sheet_media ) . matches ) return [ ] ;
3940 // get the style rules of this sheet
4041 return toArray ( stylesheet . cssRules ) ;
4142 }
4243
43- function _find ( string , re ) {
44+ function _find ( string , re ) {
4445 var matches = string . match ( re ) ;
4546 return re ? re . length : 0 ;
4647 }
4748
4849 // calculates the specificity of a given `selector`
49- function calculateScore ( selector ) {
50- var score = [ 0 , 0 , 0 ] ,
50+ function calculateScore ( selector ) {
51+ var score = [ 0 , 0 , 0 ] ,
5152 parts = selector . split ( ' ' ) ,
5253 part , match ;
5354 //TODO: clean the ':not' part since the last ELEMENT_RE will pick it up
54- while ( part = parts . shift ( ) , typeof part == 'string' ) {
55+ while ( part = parts . shift ( ) , typeof part == 'string' ) {
5556 // find all pseudo-elements
5657 match = _find ( part , PSEUDO_ELEMENTS_RE ) ;
5758 score [ 2 ] = match ;
@@ -84,21 +85,21 @@ if ( typeof window.getMatchedCSSRules !== 'function' ) {
8485 }
8586
8687 // returns the heights possible specificity score an element can get from a give rule's selectorText
87- function getSpecificityScore ( element , selector_text ) {
88+ function getSpecificityScore ( element , selector_text ) {
8889 var selectors = selector_text . split ( ',' ) ,
8990 selector , score , result = 0 ;
90- while ( selector = selectors . shift ( ) ) {
91- if ( element . matches ( selector ) ) {
91+ while ( selector = selectors . shift ( ) ) {
92+ if ( element . matches ( selector ) ) {
9293 score = calculateScore ( selector ) ;
9394 result = score > result ? score : result ;
9495 }
9596 }
9697 return result ;
9798 }
9899
99- function sortBySpecificity ( element , rules ) {
100+ function sortBySpecificity ( element , rules ) {
100101 // comparing function that sorts CSSStyleRules according to specificity of their `selectorText`
101- function compareSpecificity ( a , b ) {
102+ function compareSpecificity ( a , b ) {
102103 return getSpecificityScore ( element , b . selectorText ) - getSpecificityScore ( element , a . selectorText ) ;
103104 }
104105
@@ -116,28 +117,28 @@ if ( typeof window.getMatchedCSSRules !== 'function' ) {
116117
117118 // assuming the browser hands us stylesheets in order of appearance
118119 // we iterate them from the beginning to follow proper cascade order
119- while ( sheet = style_sheets . shift ( ) ) {
120+ while ( sheet = style_sheets . shift ( ) ) {
120121 // get the style rules of this sheet
121122 rules = getSheetRules ( sheet ) ;
122123 // loop the rules in order of appearance
123- while ( rule = rules . shift ( ) ) {
124+ while ( rule = rules . shift ( ) ) {
124125 // if this is an @import rule
125- if ( rule . styleSheet ) {
126+ if ( rule . styleSheet ) {
126127 // insert the imported stylesheet's rules at the beginning of this stylesheet's rules
127128 rules = getSheetRules ( rule . styleSheet ) . concat ( rules ) ;
128129 // and skip this rule
129130 continue ;
130131 }
131132 // if there's no stylesheet attribute BUT there IS a media attribute it's a media rule
132- else if ( rule . media ) {
133+ else if ( rule . media ) {
133134 // insert the contained rules of this media rule to the beginning of this stylesheet's rules
134135 rules = getSheetRules ( rule ) . concat ( rules ) ;
135136 // and skip it
136137 continue
137138 }
138139 //TODO: for now only polyfilling Gecko
139140 // check if this element matches this rule's selector
140- if ( element . matches ( rule . selectorText ) ) {
141+ if ( element . matches ( rule . selectorText ) ) {
141142 // push the rule to the results set
142143 result . push ( rule ) ;
143144 }
0 commit comments