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