specificity
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "specificity", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Calculate the specificity of a CSS selector", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -64,3 +64,3 @@ # Specificity Calculator | ||
Specificity Calculator also exposes a `compare` function. This function accepts two CSS selectors, `a` and `b`. | ||
Specificity Calculator also exposes a `compare` function. This function accepts two CSS selectors or specificity arrays, `a` and `b`. | ||
@@ -72,5 +72,7 @@ * It returns `-1` if `a` has a lower specificity than `b` | ||
```js | ||
SPECIFICITY.compare('div', '.active'); // -1 | ||
SPECIFICITY.compare('#main', 'div'); // 1 | ||
SPECIFICITY.compare('span', 'div'); // 0 | ||
SPECIFICITY.compare('div', '.active'); // -1 | ||
SPECIFICITY.compare('#main', 'div'); // 1 | ||
SPECIFICITY.compare('span', 'div'); // 0 | ||
SPECIFICITY.compare('span', [0,0,0,1]); // 0 | ||
SPECIFICITY.compare('#main > div', [0,1,0,1]); // 0 | ||
``` | ||
@@ -77,0 +79,0 @@ |
@@ -168,2 +168,3 @@ var SPECIFICITY = (function() { | ||
* Compares two CSS selectors for specificity | ||
* Alternatively you can replace one of the CSS selectors with a specificity array | ||
* | ||
@@ -179,13 +180,34 @@ * - it returns -1 if a has a lower specificity than b | ||
if (a.indexOf(',') !== -1) { | ||
throw a + ' is not a valid CSS selector'; | ||
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 (b.indexOf(',') !== -1) { | ||
throw b + ' is not a valid CSS selector'; | ||
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'; | ||
} | ||
aSpecificity = calculateSingle(a)['specificityArray']; | ||
bSpecificity = calculateSingle(b)['specificityArray']; | ||
for (i = 0; i < 4; i += 1) { | ||
@@ -192,0 +214,0 @@ if (aSpecificity[i] < bSpecificity[i]) { |
@@ -78,2 +78,4 @@ var specificity = require('../'), | ||
{ a: '#main p .active', b: '#main span :focus', expected: 0 }, | ||
{ a: [0, 1, 1, 1], b: '#main span :focus', expected: 0 }, | ||
{ a: '#main p .active', b: [0, 1, 1, 1], expected: 0 }, | ||
{ a: ':focus', b: 'span a', expected: 1 }, | ||
@@ -80,0 +82,0 @@ { a: '#main', b: 'span a:hover', expected: 1 }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16450
318
91