1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const rangeOperators = ['>=', '<=', '>', '<', '='];
|
6 | const styleSearch = require('style-search');
|
7 |
|
8 | function getRangeContextOperators(node) {
|
9 | const operators = [];
|
10 |
|
11 | styleSearch({ source: node.value, target: rangeOperators }, (match) => {
|
12 | const before = node[match.startIndex - 1];
|
13 |
|
14 | if (before === '>' || before === '<') {
|
15 | return;
|
16 | }
|
17 |
|
18 | operators.push(match.target);
|
19 | });
|
20 |
|
21 |
|
22 |
|
23 | return operators.sort((a, b) => b.length - a.length);
|
24 | }
|
25 |
|
26 | function getRangeContextName(parsedNode) {
|
27 |
|
28 |
|
29 | if (parsedNode.length === 3) {
|
30 | return parsedNode[1];
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 | return parsedNode.find((value) => value.match(/^(?!--)\D+/) || value.match(/^(--).+/));
|
36 | }
|
37 |
|
38 | module.exports = function (node) {
|
39 | const nodeValue = node.value;
|
40 |
|
41 | const operators = getRangeContextOperators(node);
|
42 |
|
43 |
|
44 | const parsedMedia = nodeValue.replace(/[()\s]/g, '').split(new RegExp(operators.join('|')));
|
45 |
|
46 | const name = getRangeContextName(parsedMedia);
|
47 | const nameObj = {
|
48 | value: name,
|
49 | sourceIndex: node.sourceIndex + nodeValue.indexOf(name),
|
50 | };
|
51 |
|
52 | const values = parsedMedia
|
53 | .filter((parsedValue) => parsedValue !== name)
|
54 | .map((value) => {
|
55 | return {
|
56 | value,
|
57 | sourceIndex: node.sourceIndex + nodeValue.indexOf(value),
|
58 | };
|
59 | });
|
60 |
|
61 | return {
|
62 | name: nameObj,
|
63 | values,
|
64 | };
|
65 | };
|