UNPKG

1.13 kBJavaScriptView Raw
1/**
2 * Compares two specificity vectors, returning the winning one.
3 *
4 * @param {Array} vector a
5 * @param {Array} vector b
6 * @return {Array}
7 * @api public
8 */
9
10function compareSpecificity(a, b) {
11 let i;
12
13 for (i = 0; i < 4; i++) {
14 if (a[i] === b[i]) {
15 continue;
16 }
17 if (a[i] > b[i]) {
18 return a;
19 }
20 return b;
21 }
22
23 return b;
24}
25
26/**
27 * CSS property constructor.
28 *
29 * @param {String} property
30 * @param {String} value
31 * @param {Selector} selector the property originates from
32 * @api public
33 */
34
35module.exports = (prop, value, selector) => {
36 let o = {};
37
38 /**
39 * Compares with another Property based on Selector#specificity.
40 *
41 * @api public
42 */
43
44 const compare = property => {
45 const a = selector.specificity();
46 const b = property.selector.specificity();
47 const winner = compareSpecificity(a, b);
48
49 if (winner === a && a !== b) {
50 return o;
51 }
52 return property;
53 };
54
55 o = {
56 prop,
57 value,
58 selector,
59 compare
60 };
61
62 return o;
63};