123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- var calculate = function(input) {
- var selectors,
- selector,
- i,
- len,
- results = [];
-
- selectors = input.split(',');
- for (i = 0, len = selectors.length; i < len; i += 1) {
- selector = selectors[i];
- if (selector.length > 0) {
- results.push(calculateSingle(selector));
- }
- }
- return results;
- };
- var calculateSingle = function(input) {
- var selector = input,
- findMatch,
- typeCount = {
- 'a': 0,
- 'b': 0,
- 'c': 0
- },
- parts = [],
-
- attributeRegex = /(\[[^\]]+\])/g,
- idRegex = /(#[^\#\s\+>~\.\[:\)]+)/g,
- classRegex = /(\.[^\s\+>~\.\[:\)]+)/g,
- pseudoElementRegex = /(::[^\s\+>~\.\[:]+|:first-line|:first-letter|:before|:after)/gi,
-
-
-
- pseudoClassWithBracketsRegex = /(:(?!not|global|local)[\w-]+\([^\)]*\))/gi,
-
- pseudoClassRegex = /(:(?!not|global|local)[^\s\+>~\.\[:]+)/g,
- elementRegex = /([^\s\+>~\.\[:]+)/g;
-
-
- findMatch = function(regex, type) {
- var matches, i, len, match, index, length;
- if (regex.test(selector)) {
- matches = selector.match(regex);
- for (i = 0, len = matches.length; i < len; i += 1) {
- typeCount[type] += 1;
- match = matches[i];
- index = selector.indexOf(match);
- length = match.length;
- parts.push({
- selector: input.substr(index, length),
- type: type,
- index: index,
- length: length
- });
-
- selector = selector.replace(match, Array(length + 1).join(' '));
- }
- }
- };
-
-
- (function() {
- var replaceWithPlainText = function(regex) {
- var matches, i, len, match;
- if (regex.test(selector)) {
- matches = selector.match(regex);
- for (i = 0, len = matches.length; i < len; i += 1) {
- match = matches[i];
- selector = selector.replace(match, Array(match.length + 1).join('A'));
- }
- }
- },
-
- escapeHexadecimalRegex = /\\[0-9A-Fa-f]{6}\s?/g,
-
- escapeHexadecimalRegex2 = /\\[0-9A-Fa-f]{1,5}\s/g,
-
- escapeSpecialCharacter = /\\./g;
- replaceWithPlainText(escapeHexadecimalRegex);
- replaceWithPlainText(escapeHexadecimalRegex2);
- replaceWithPlainText(escapeSpecialCharacter);
- }());
-
- (function() {
- var regex = /{[^]*/gm,
- matches, i, len, match;
- if (regex.test(selector)) {
- matches = selector.match(regex);
- for (i = 0, len = matches.length; i < len; i += 1) {
- match = matches[i];
- selector = selector.replace(match, Array(match.length + 1).join(' '));
- }
- }
- }());
-
- findMatch(attributeRegex, 'b');
-
- findMatch(idRegex, 'a');
-
- findMatch(classRegex, 'b');
-
- findMatch(pseudoElementRegex, 'c');
-
- findMatch(pseudoClassWithBracketsRegex, 'b');
- findMatch(pseudoClassRegex, 'b');
-
- selector = selector.replace(/[\*\s\+>~]/g, ' ');
-
-
- selector = selector.replace(/[#\.]/g, ' ');
-
-
- selector = selector.replace(/:not/g, ' ');
- selector = selector.replace(/:local/g, ' ');
- selector = selector.replace(/:global/g, ' ');
- selector = selector.replace(/[\(\)]/g, ' ');
-
- findMatch(elementRegex, 'c');
-
-
- parts.sort(function(a, b) {
- return a.index - b.index;
- });
- return {
- selector: input,
- specificity: '0,' + typeCount.a.toString() + ',' + typeCount.b.toString() + ',' + typeCount.c.toString(),
- specificityArray: [0, typeCount.a, typeCount.b, typeCount.c],
- parts: parts
- };
- };
- var compare = function(a, b) {
- var aSpecificity,
- bSpecificity,
- i;
- if (typeof a ==='string') {
- if (a.indexOf(',') !== -1) {
- throw 'Invalid CSS selector';
- } else {
- aSpecificity = calculateSingle(a)['specificityArray'];
- }
- } else if (Array.isArray(a)) {
- if (a.filter(function(e) { return (typeof e === 'number'); }).length !== 4) {
- throw 'Invalid specificity array';
- } else {
- aSpecificity = a;
- }
- } else {
- throw 'Invalid CSS selector or specificity array';
- }
- if (typeof b ==='string') {
- if (b.indexOf(',') !== -1) {
- throw 'Invalid CSS selector';
- } else {
- bSpecificity = calculateSingle(b)['specificityArray'];
- }
- } else if (Array.isArray(b)) {
- if (b.filter(function(e) { return (typeof e === 'number'); }).length !== 4) {
- throw 'Invalid specificity array';
- } else {
- bSpecificity = b;
- }
- } else {
- throw 'Invalid CSS selector or specificity array';
- }
- for (i = 0; i < 4; i += 1) {
- if (aSpecificity[i] < bSpecificity[i]) {
- return -1;
- } else if (aSpecificity[i] > bSpecificity[i]) {
- return 1;
- }
- }
- return 0;
- };
- export {
- calculate,
- compare
- };
|