-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathPropertyValuePart.js
More file actions
247 lines (221 loc) · 8.78 KB
/
PropertyValuePart.js
File metadata and controls
247 lines (221 loc) · 8.78 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"use strict";
module.exports = PropertyValuePart;
var SyntaxUnit = require("../util/SyntaxUnit");
var Colors = require("./Colors");
var Parser = require("./Parser");
var Tokens = require("./Tokens");
/**
* Represents a single part of a CSS property value, meaning that it represents
* just one part of the data between ":" and ";".
* @param {String} text The text representation of the unit.
* @param {int} line The line of text on which the unit resides.
* @param {int} col The column of text on which the unit resides.
* @namespace parserlib.css
* @class PropertyValuePart
* @extends parserlib.util.SyntaxUnit
* @constructor
*/
function PropertyValuePart(text, line, col, optionalHint) {
var hint = optionalHint || {};
SyntaxUnit.call(this, text, line, col, Parser.PROPERTY_VALUE_PART_TYPE);
/**
* Indicates the type of value unit.
* @type String
* @property type
*/
this.type = "unknown";
// figure out what type of data it is
var temp;
// it is a measurement?
if (/^([+-]?[\d.]+)([a-z]+)$/i.test(text)) { // dimension
this.type = "dimension";
this.value = Number(RegExp.$1);
this.units = RegExp.$2;
// try to narrow down
switch (this.units.toLowerCase()) {
case "em":
case "rem":
case "ex":
case "px":
case "cm":
case "mm":
case "in":
case "pt":
case "pc":
case "ch":
case "vh":
case "vw":
case "vmax":
case "vmin":
this.type = "length";
break;
case "fr":
this.type = "grid";
break;
case "deg":
case "rad":
case "grad":
case "turn":
this.type = "angle";
break;
case "ms":
case "s":
this.type = "time";
break;
case "hz":
case "khz":
this.type = "frequency";
break;
case "dpi":
case "dpcm":
this.type = "resolution";
break;
//default
}
} else if (/^([+-]?[\d.]+)%$/i.test(text)) { // percentage
this.type = "percentage";
this.value = Number(RegExp.$1);
} else if (/^([+-]?\d+)$/i.test(text)) { // integer
this.type = "integer";
this.value = Number(RegExp.$1);
} else if (/^([+-]?[\d.]+)$/i.test(text)) { // number
this.type = "number";
this.value = Number(RegExp.$1);
} else if (/^#([a-f0-9]{3,6})/i.test(text)) { // hexcolor
this.type = "color";
temp = RegExp.$1;
if (temp.length === 3) {
this.red = parseInt(temp.charAt(0) + temp.charAt(0), 16);
this.green = parseInt(temp.charAt(1) + temp.charAt(1), 16);
this.blue = parseInt(temp.charAt(2) + temp.charAt(2), 16);
} else {
this.red = parseInt(temp.substring(0, 2), 16);
this.green = parseInt(temp.substring(2, 4), 16);
this.blue = parseInt(temp.substring(4, 6), 16);
}
} else if (/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/i.test(text)) { // rgb() color with absolute numbers
this.type = "color";
this.red = Number(RegExp.$1);
this.green = Number(RegExp.$2);
this.blue = Number(RegExp.$3);
} else if (/^rgb\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)/i.test(text)) { // rgb() color with percentages
this.type = "color";
this.red = Number(RegExp.$1) * 255 / 100;
this.green = Number(RegExp.$2) * 255 / 100;
this.blue = Number(RegExp.$3) * 255 / 100;
} else if (/^rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([\d.]+)\s*\)/i.test(text)) { // rgba() color with absolute numbers
this.type = "color";
this.red = Number(RegExp.$1);
this.green = Number(RegExp.$2);
this.blue = Number(RegExp.$3);
this.alpha = Number(RegExp.$4);
} else if (/^rgba\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*,\s*([\d.]+)\s*\)/i.test(text)) { // rgba() color with percentages
this.type = "color";
this.red = Number(RegExp.$1) * 255 / 100;
this.green = Number(RegExp.$2) * 255 / 100;
this.blue = Number(RegExp.$3) * 255 / 100;
this.alpha = Number(RegExp.$4);
} else if (/^hsl\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)/i.test(text)) { // hsl()
this.type = "color";
this.hue = Number(RegExp.$1);
this.saturation = Number(RegExp.$2) / 100;
this.lightness = Number(RegExp.$3) / 100;
} else if (/^hsla\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*,\s*([\d.]+)\s*\)/i.test(text)) { // hsla() color with percentages
this.type = "color";
this.hue = Number(RegExp.$1);
this.saturation = Number(RegExp.$2) / 100;
this.lightness = Number(RegExp.$3) / 100;
this.alpha = Number(RegExp.$4);
} else if (/^url\(("([^\\"]|\\.)*")\)/i.test(text)) { // URI
// generated by TokenStream.readURI, so always double-quoted.
this.type = "uri";
this.uri = PropertyValuePart.parseString(RegExp.$1);
} else if (/^([^(]+)\(/i.test(text)) {
this.type = "function";
this.name = RegExp.$1;
this.value = text;
} else if (/^"([^\n\r\f\\"]|\\\r\n|\\[^\r0-9a-f]|\\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?)*"/i.test(text)) { // double-quoted string
this.type = "string";
this.value = PropertyValuePart.parseString(text);
} else if (/^'([^\n\r\f\\']|\\\r\n|\\[^\r0-9a-f]|\\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?)*'/i.test(text)) { // single-quoted string
this.type = "string";
this.value = PropertyValuePart.parseString(text);
} else if (Colors[text.toLowerCase()]) { // named color
this.type = "color";
temp = Colors[text.toLowerCase()].substring(1);
this.red = parseInt(temp.substring(0, 2), 16);
this.green = parseInt(temp.substring(2, 4), 16);
this.blue = parseInt(temp.substring(4, 6), 16);
} else if (/^[,/]$/.test(text)) {
this.type = "operator";
this.value = text;
} else if (/^-?[a-z_\u00A0-\uFFFF][a-z0-9\-_\u00A0-\uFFFF]*$/i.test(text)) {
this.type = "identifier";
this.value = text;
}
// There can be ambiguity with escape sequences in identifiers, as
// well as with "color" parts which are also "identifiers", so record
// an explicit hint when the token generating this PropertyValuePart
// was an identifier.
this.wasIdent = Boolean(hint.ident);
}
PropertyValuePart.prototype = new SyntaxUnit();
PropertyValuePart.prototype.constructor = PropertyValuePart;
/**
* Helper method to parse a CSS string.
*/
PropertyValuePart.parseString = function(str) {
str = str.slice(1, -1); // Strip surrounding single/double quotes
var replacer = function(match, esc) {
if (/^(\n|\r\n|\r|\f)$/.test(esc)) {
return "";
}
var m = /^[0-9a-f]{1,6}/i.exec(esc);
if (m) {
var codePoint = parseInt(m[0], 16);
if (String.fromCodePoint) {
return String.fromCodePoint(codePoint);
} else {
// XXX No support for surrogates on old JavaScript engines.
return String.fromCharCode(codePoint);
}
}
return esc;
};
return str.replace(/\\(\r\n|[^\r0-9a-f]|[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?)/ig,
replacer);
};
/**
* Helper method to serialize a CSS string.
*/
PropertyValuePart.serializeString = function(value) {
var replacer = function(match) {
if (match === "\"") {
return "\\" + match;
}
var cp = match.codePointAt ? match.codePointAt(0) :
// We only escape non-surrogate chars, so using charCodeAt
// is harmless here.
match.charCodeAt(0);
return "\\" + cp.toString(16) + " ";
};
return "\"" + value.replace(/["\r\n\f]/g, replacer) + "\"";
};
/**
* Create a new syntax unit based solely on the given token.
* Convenience method for creating a new syntax unit when
* it represents a single token instead of multiple.
* @param {Object} token The token object to represent.
* @return {parserlib.css.PropertyValuePart} The object representing the token.
* @static
* @method fromToken
*/
PropertyValuePart.fromToken = function(token) {
var part = new PropertyValuePart(token.value, token.startLine, token.startCol, {
// Tokens can have escaped characters that would fool the type
// identification in the PropertyValuePart constructor, so pass
// in a hint if this was an identifier.
ident: token.type === Tokens.IDENT
});
return part;
};