1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = parseNestedPropRoot;
/**
* Attempts to parse a selector as if it"s a root for a group of nested props
* E.g.: `margin: {`, `font: 10px/1.1 Arial {` ("{" excluded)
*/
function parseNestedPropRoot(propString) {
var modesEntered = [{
mode: "normal",
character: null,
isCalculationEnabled: true
}];
var result = {};
var lastModeIndex = 0;
var propName = "";
for (var i = 0; i < propString.length; i++) {
var character = propString[i];
var prevCharacter = propString[i - 1]; // If entering/exiting a string
if (character === '"' || character === "'") {
if (modesEntered[lastModeIndex].isCalculationEnabled === true) {
modesEntered.push({
mode: "string",
isCalculationEnabled: false,
character: character
});
lastModeIndex++;
} else if (modesEntered[lastModeIndex].mode === "string" && modesEntered[lastModeIndex].character === character && prevCharacter !== "\\") {
modesEntered.pop();
lastModeIndex--;
}
} // If entering/exiting interpolation
if (character === "{") {
modesEntered.push({
mode: "interpolation",
isCalculationEnabled: true
});
lastModeIndex++;
} else if (character === "}") {
modesEntered.pop();
lastModeIndex--;
} // Check for : outside fn call, string or interpolation. It must be at the
// end of a string or have a whitespace between it and following value
if (modesEntered[lastModeIndex].mode === "normal" && character === ":" && prevCharacter !== "\\") {
var propValueStr = propString.substring(i + 1);
if (propValueStr.length) {
var propValue = {
before: /^(\s*)/.exec(propValueStr)[1],
value: propValueStr.trim()
}; // It's a declaration if 1) there is a whitespace after :, or
// 2) the value is a number with/without a unit (starts with a number
// or a dot), or
// 3) the value is a variable (starts with $), or
// 4) the value a string, surprisingly
if (propValue.before === "" && !/^[0-9.$'"]/.test(propValue.value)) {
return null;
} // +1 for the colon
propValue.sourceIndex = propValue.before.length + i + 1;
result.propValue = propValue;
}
result.propName = {
after: /(\s*)$/.exec(propName)[1],
value: propName.trim()
};
return result;
}
propName += character;
}
return null;
}
|