This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from Frontenda/specificity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspecificity.js
100 lines (95 loc) · 2.74 KB
/
specificity.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Generated by CoffeeScript 1.4.0
(function() {
var _matchesSelector, _specificity;
$.specificity = function(selector, options) {
var element, highestSpecificity, sel, selectors, spec, specifities, _i, _len;
if (options == null) {
options = {};
}
if (options.element) {
element = options.element;
if (element instanceof jQuery) {
element = element[0];
}
selectors = selector.split(',');
specifities = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = selectors.length; _i < _len; _i++) {
sel = selectors[_i];
if (_matchesSelector(element, sel)) {
_results.push(_specificity(sel));
}
}
return _results;
})();
highestSpecificity = 0;
for (_i = 0, _len = specifities.length; _i < _len; _i++) {
spec = specifities[_i];
if (spec > highestSpecificity) {
highestSpecificity = spec;
}
}
return highestSpecificity;
} else {
return _specificity(selector);
}
};
_specificity = function(selector) {
var m, matches, p, result, sel, selectors, specificity, tmp, _i, _j, _len, _len1;
specificity = [0, 0, 0, 0, 0];
tmp = 0;
selectors = selector.split(/\s*,\s*/);
for (_i = 0, _len = selectors.length; _i < _len; _i++) {
sel = selectors[_i];
matches = sel.match(/#/g);
if (matches) {
specificity[1] += matches.length;
}
matches = sel.match(/\./g);
if (matches) {
specificity[2] += matches.length;
}
matches = sel.match(/\[.+\]/g);
if (matches) {
specificity[2] += matches.length;
}
matches = sel.match(/:(?:first-letter|first-line|before|after|:selection)/g);
if (matches) {
tmp = matches.length;
specificity[3] += tmp;
}
matches = sel.match(/:/g);
if (matches) {
specificity[2] += matches.length - tmp;
}
tmp = 0;
matches = sel.match(/[+>~]/g);
if (matches) {
specificity[4] += matches.length;
}
sel = sel.replace(/\(.*?\)/g, "").replace(/\[.*?\]/g, "").replace(/[.#:][^ +>~]*/ig, "");
matches = sel.match(/[^ +>~]+/g);
if (matches) {
specificity[3] += matches.length;
}
}
m = 10000;
result = 0;
for (_j = 0, _len1 = specificity.length; _j < _len1; _j++) {
p = specificity[_j];
result += p * m;
m /= 10;
}
return result;
};
_matchesSelector = function(element, selector) {
var fn;
fn = element.matchesSelector || element.mozMatchesSelector || element.webkitMatchesSelector;
try {
return fn.call(element, selector);
} catch (e) {
}
return null;
};
}).call(this);