1 | /**
|
2 | * Module dependencies.
|
3 | */
|
4 |
|
5 | const specificity = require('specificity');
|
6 |
|
7 | /**
|
8 | * Returns specificity based on selector text and tokens.
|
9 | *
|
10 | * @param {String} selector
|
11 | * @param {Array} tokens
|
12 | * @api private.
|
13 | */
|
14 |
|
15 | function getSpecificity(text) {
|
16 | const spec = specificity.calculate(text);
|
17 |
|
18 | return spec[0].specificity.split(',');
|
19 | }
|
20 |
|
21 | /**
|
22 | * CSS selector constructor.
|
23 | *
|
24 | * @param {String} selector text
|
25 | * @param {Array} optionally, precalculated specificity
|
26 | * @api public
|
27 | */
|
28 |
|
29 | module.exports = (text, spec) => {
|
30 | let _spec = spec;
|
31 |
|
32 | const /**
|
33 | * Lazy specificity getter
|
34 | *
|
35 | * @api public
|
36 | */
|
37 |
|
38 | _specificity = () => {
|
39 | if (!spec) {
|
40 | _spec = getSpecificity(text);
|
41 | }
|
42 | return _spec;
|
43 | };
|
44 |
|
45 | return {
|
46 | spec: _spec,
|
47 |
|
48 | specificity: _specificity
|
49 | };
|
50 | };
|