source: trip-planner-front/node_modules/postcss-selector-parser/dist/selectors/attribute.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 15.6 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.unescapeValue = unescapeValue;
5exports["default"] = void 0;
6
7var _cssesc = _interopRequireDefault(require("cssesc"));
8
9var _unesc = _interopRequireDefault(require("../util/unesc"));
10
11var _namespace = _interopRequireDefault(require("./namespace"));
12
13var _types = require("./types");
14
15var _CSSESC_QUOTE_OPTIONS;
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
18
19function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
20
21function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
22
23function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
24
25function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
26
27var deprecate = require("util-deprecate");
28
29var WRAPPED_IN_QUOTES = /^('|")([^]*)\1$/;
30var warnOfDeprecatedValueAssignment = deprecate(function () {}, "Assigning an attribute a value containing characters that might need to be escaped is deprecated. " + "Call attribute.setValue() instead.");
31var warnOfDeprecatedQuotedAssignment = deprecate(function () {}, "Assigning attr.quoted is deprecated and has no effect. Assign to attr.quoteMark instead.");
32var warnOfDeprecatedConstructor = deprecate(function () {}, "Constructing an Attribute selector with a value without specifying quoteMark is deprecated. Note: The value should be unescaped now.");
33
34function unescapeValue(value) {
35 var deprecatedUsage = false;
36 var quoteMark = null;
37 var unescaped = value;
38 var m = unescaped.match(WRAPPED_IN_QUOTES);
39
40 if (m) {
41 quoteMark = m[1];
42 unescaped = m[2];
43 }
44
45 unescaped = (0, _unesc["default"])(unescaped);
46
47 if (unescaped !== value) {
48 deprecatedUsage = true;
49 }
50
51 return {
52 deprecatedUsage: deprecatedUsage,
53 unescaped: unescaped,
54 quoteMark: quoteMark
55 };
56}
57
58function handleDeprecatedContructorOpts(opts) {
59 if (opts.quoteMark !== undefined) {
60 return opts;
61 }
62
63 if (opts.value === undefined) {
64 return opts;
65 }
66
67 warnOfDeprecatedConstructor();
68
69 var _unescapeValue = unescapeValue(opts.value),
70 quoteMark = _unescapeValue.quoteMark,
71 unescaped = _unescapeValue.unescaped;
72
73 if (!opts.raws) {
74 opts.raws = {};
75 }
76
77 if (opts.raws.value === undefined) {
78 opts.raws.value = opts.value;
79 }
80
81 opts.value = unescaped;
82 opts.quoteMark = quoteMark;
83 return opts;
84}
85
86var Attribute = /*#__PURE__*/function (_Namespace) {
87 _inheritsLoose(Attribute, _Namespace);
88
89 function Attribute(opts) {
90 var _this;
91
92 if (opts === void 0) {
93 opts = {};
94 }
95
96 _this = _Namespace.call(this, handleDeprecatedContructorOpts(opts)) || this;
97 _this.type = _types.ATTRIBUTE;
98 _this.raws = _this.raws || {};
99 Object.defineProperty(_this.raws, 'unquoted', {
100 get: deprecate(function () {
101 return _this.value;
102 }, "attr.raws.unquoted is deprecated. Call attr.value instead."),
103 set: deprecate(function () {
104 return _this.value;
105 }, "Setting attr.raws.unquoted is deprecated and has no effect. attr.value is unescaped by default now.")
106 });
107 _this._constructed = true;
108 return _this;
109 }
110 /**
111 * Returns the Attribute's value quoted such that it would be legal to use
112 * in the value of a css file. The original value's quotation setting
113 * used for stringification is left unchanged. See `setValue(value, options)`
114 * if you want to control the quote settings of a new value for the attribute.
115 *
116 * You can also change the quotation used for the current value by setting quoteMark.
117 *
118 * Options:
119 * * quoteMark {'"' | "'" | null} - Use this value to quote the value. If this
120 * option is not set, the original value for quoteMark will be used. If
121 * indeterminate, a double quote is used. The legal values are:
122 * * `null` - the value will be unquoted and characters will be escaped as necessary.
123 * * `'` - the value will be quoted with a single quote and single quotes are escaped.
124 * * `"` - the value will be quoted with a double quote and double quotes are escaped.
125 * * preferCurrentQuoteMark {boolean} - if true, prefer the source quote mark
126 * over the quoteMark option value.
127 * * smart {boolean} - if true, will select a quote mark based on the value
128 * and the other options specified here. See the `smartQuoteMark()`
129 * method.
130 **/
131
132
133 var _proto = Attribute.prototype;
134
135 _proto.getQuotedValue = function getQuotedValue(options) {
136 if (options === void 0) {
137 options = {};
138 }
139
140 var quoteMark = this._determineQuoteMark(options);
141
142 var cssescopts = CSSESC_QUOTE_OPTIONS[quoteMark];
143 var escaped = (0, _cssesc["default"])(this._value, cssescopts);
144 return escaped;
145 };
146
147 _proto._determineQuoteMark = function _determineQuoteMark(options) {
148 return options.smart ? this.smartQuoteMark(options) : this.preferredQuoteMark(options);
149 }
150 /**
151 * Set the unescaped value with the specified quotation options. The value
152 * provided must not include any wrapping quote marks -- those quotes will
153 * be interpreted as part of the value and escaped accordingly.
154 */
155 ;
156
157 _proto.setValue = function setValue(value, options) {
158 if (options === void 0) {
159 options = {};
160 }
161
162 this._value = value;
163 this._quoteMark = this._determineQuoteMark(options);
164
165 this._syncRawValue();
166 }
167 /**
168 * Intelligently select a quoteMark value based on the value's contents. If
169 * the value is a legal CSS ident, it will not be quoted. Otherwise a quote
170 * mark will be picked that minimizes the number of escapes.
171 *
172 * If there's no clear winner, the quote mark from these options is used,
173 * then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
174 * true). If the quoteMark is unspecified, a double quote is used.
175 *
176 * @param options This takes the quoteMark and preferCurrentQuoteMark options
177 * from the quoteValue method.
178 */
179 ;
180
181 _proto.smartQuoteMark = function smartQuoteMark(options) {
182 var v = this.value;
183 var numSingleQuotes = v.replace(/[^']/g, '').length;
184 var numDoubleQuotes = v.replace(/[^"]/g, '').length;
185
186 if (numSingleQuotes + numDoubleQuotes === 0) {
187 var escaped = (0, _cssesc["default"])(v, {
188 isIdentifier: true
189 });
190
191 if (escaped === v) {
192 return Attribute.NO_QUOTE;
193 } else {
194 var pref = this.preferredQuoteMark(options);
195
196 if (pref === Attribute.NO_QUOTE) {
197 // pick a quote mark that isn't none and see if it's smaller
198 var quote = this.quoteMark || options.quoteMark || Attribute.DOUBLE_QUOTE;
199 var opts = CSSESC_QUOTE_OPTIONS[quote];
200 var quoteValue = (0, _cssesc["default"])(v, opts);
201
202 if (quoteValue.length < escaped.length) {
203 return quote;
204 }
205 }
206
207 return pref;
208 }
209 } else if (numDoubleQuotes === numSingleQuotes) {
210 return this.preferredQuoteMark(options);
211 } else if (numDoubleQuotes < numSingleQuotes) {
212 return Attribute.DOUBLE_QUOTE;
213 } else {
214 return Attribute.SINGLE_QUOTE;
215 }
216 }
217 /**
218 * Selects the preferred quote mark based on the options and the current quote mark value.
219 * If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
220 * instead.
221 */
222 ;
223
224 _proto.preferredQuoteMark = function preferredQuoteMark(options) {
225 var quoteMark = options.preferCurrentQuoteMark ? this.quoteMark : options.quoteMark;
226
227 if (quoteMark === undefined) {
228 quoteMark = options.preferCurrentQuoteMark ? options.quoteMark : this.quoteMark;
229 }
230
231 if (quoteMark === undefined) {
232 quoteMark = Attribute.DOUBLE_QUOTE;
233 }
234
235 return quoteMark;
236 };
237
238 _proto._syncRawValue = function _syncRawValue() {
239 var rawValue = (0, _cssesc["default"])(this._value, CSSESC_QUOTE_OPTIONS[this.quoteMark]);
240
241 if (rawValue === this._value) {
242 if (this.raws) {
243 delete this.raws.value;
244 }
245 } else {
246 this.raws.value = rawValue;
247 }
248 };
249
250 _proto._handleEscapes = function _handleEscapes(prop, value) {
251 if (this._constructed) {
252 var escaped = (0, _cssesc["default"])(value, {
253 isIdentifier: true
254 });
255
256 if (escaped !== value) {
257 this.raws[prop] = escaped;
258 } else {
259 delete this.raws[prop];
260 }
261 }
262 };
263
264 _proto._spacesFor = function _spacesFor(name) {
265 var attrSpaces = {
266 before: '',
267 after: ''
268 };
269 var spaces = this.spaces[name] || {};
270 var rawSpaces = this.raws.spaces && this.raws.spaces[name] || {};
271 return Object.assign(attrSpaces, spaces, rawSpaces);
272 };
273
274 _proto._stringFor = function _stringFor(name, spaceName, concat) {
275 if (spaceName === void 0) {
276 spaceName = name;
277 }
278
279 if (concat === void 0) {
280 concat = defaultAttrConcat;
281 }
282
283 var attrSpaces = this._spacesFor(spaceName);
284
285 return concat(this.stringifyProperty(name), attrSpaces);
286 }
287 /**
288 * returns the offset of the attribute part specified relative to the
289 * start of the node of the output string.
290 *
291 * * "ns" - alias for "namespace"
292 * * "namespace" - the namespace if it exists.
293 * * "attribute" - the attribute name
294 * * "attributeNS" - the start of the attribute or its namespace
295 * * "operator" - the match operator of the attribute
296 * * "value" - The value (string or identifier)
297 * * "insensitive" - the case insensitivity flag;
298 * @param part One of the possible values inside an attribute.
299 * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
300 */
301 ;
302
303 _proto.offsetOf = function offsetOf(name) {
304 var count = 1;
305
306 var attributeSpaces = this._spacesFor("attribute");
307
308 count += attributeSpaces.before.length;
309
310 if (name === "namespace" || name === "ns") {
311 return this.namespace ? count : -1;
312 }
313
314 if (name === "attributeNS") {
315 return count;
316 }
317
318 count += this.namespaceString.length;
319
320 if (this.namespace) {
321 count += 1;
322 }
323
324 if (name === "attribute") {
325 return count;
326 }
327
328 count += this.stringifyProperty("attribute").length;
329 count += attributeSpaces.after.length;
330
331 var operatorSpaces = this._spacesFor("operator");
332
333 count += operatorSpaces.before.length;
334 var operator = this.stringifyProperty("operator");
335
336 if (name === "operator") {
337 return operator ? count : -1;
338 }
339
340 count += operator.length;
341 count += operatorSpaces.after.length;
342
343 var valueSpaces = this._spacesFor("value");
344
345 count += valueSpaces.before.length;
346 var value = this.stringifyProperty("value");
347
348 if (name === "value") {
349 return value ? count : -1;
350 }
351
352 count += value.length;
353 count += valueSpaces.after.length;
354
355 var insensitiveSpaces = this._spacesFor("insensitive");
356
357 count += insensitiveSpaces.before.length;
358
359 if (name === "insensitive") {
360 return this.insensitive ? count : -1;
361 }
362
363 return -1;
364 };
365
366 _proto.toString = function toString() {
367 var _this2 = this;
368
369 var selector = [this.rawSpaceBefore, '['];
370 selector.push(this._stringFor('qualifiedAttribute', 'attribute'));
371
372 if (this.operator && (this.value || this.value === '')) {
373 selector.push(this._stringFor('operator'));
374 selector.push(this._stringFor('value'));
375 selector.push(this._stringFor('insensitiveFlag', 'insensitive', function (attrValue, attrSpaces) {
376 if (attrValue.length > 0 && !_this2.quoted && attrSpaces.before.length === 0 && !(_this2.spaces.value && _this2.spaces.value.after)) {
377 attrSpaces.before = " ";
378 }
379
380 return defaultAttrConcat(attrValue, attrSpaces);
381 }));
382 }
383
384 selector.push(']');
385 selector.push(this.rawSpaceAfter);
386 return selector.join('');
387 };
388
389 _createClass(Attribute, [{
390 key: "quoted",
391 get: function get() {
392 var qm = this.quoteMark;
393 return qm === "'" || qm === '"';
394 },
395 set: function set(value) {
396 warnOfDeprecatedQuotedAssignment();
397 }
398 /**
399 * returns a single (`'`) or double (`"`) quote character if the value is quoted.
400 * returns `null` if the value is not quoted.
401 * returns `undefined` if the quotation state is unknown (this can happen when
402 * the attribute is constructed without specifying a quote mark.)
403 */
404
405 }, {
406 key: "quoteMark",
407 get: function get() {
408 return this._quoteMark;
409 }
410 /**
411 * Set the quote mark to be used by this attribute's value.
412 * If the quote mark changes, the raw (escaped) value at `attr.raws.value` of the attribute
413 * value is updated accordingly.
414 *
415 * @param {"'" | '"' | null} quoteMark The quote mark or `null` if the value should be unquoted.
416 */
417 ,
418 set: function set(quoteMark) {
419 if (!this._constructed) {
420 this._quoteMark = quoteMark;
421 return;
422 }
423
424 if (this._quoteMark !== quoteMark) {
425 this._quoteMark = quoteMark;
426
427 this._syncRawValue();
428 }
429 }
430 }, {
431 key: "qualifiedAttribute",
432 get: function get() {
433 return this.qualifiedName(this.raws.attribute || this.attribute);
434 }
435 }, {
436 key: "insensitiveFlag",
437 get: function get() {
438 return this.insensitive ? 'i' : '';
439 }
440 }, {
441 key: "value",
442 get: function get() {
443 return this._value;
444 }
445 /**
446 * Before 3.0, the value had to be set to an escaped value including any wrapped
447 * quote marks. In 3.0, the semantics of `Attribute.value` changed so that the value
448 * is unescaped during parsing and any quote marks are removed.
449 *
450 * Because the ambiguity of this semantic change, if you set `attr.value = newValue`,
451 * a deprecation warning is raised when the new value contains any characters that would
452 * require escaping (including if it contains wrapped quotes).
453 *
454 * Instead, you should call `attr.setValue(newValue, opts)` and pass options that describe
455 * how the new value is quoted.
456 */
457 ,
458 set: function set(v) {
459 if (this._constructed) {
460 var _unescapeValue2 = unescapeValue(v),
461 deprecatedUsage = _unescapeValue2.deprecatedUsage,
462 unescaped = _unescapeValue2.unescaped,
463 quoteMark = _unescapeValue2.quoteMark;
464
465 if (deprecatedUsage) {
466 warnOfDeprecatedValueAssignment();
467 }
468
469 if (unescaped === this._value && quoteMark === this._quoteMark) {
470 return;
471 }
472
473 this._value = unescaped;
474 this._quoteMark = quoteMark;
475
476 this._syncRawValue();
477 } else {
478 this._value = v;
479 }
480 }
481 }, {
482 key: "attribute",
483 get: function get() {
484 return this._attribute;
485 },
486 set: function set(name) {
487 this._handleEscapes("attribute", name);
488
489 this._attribute = name;
490 }
491 }]);
492
493 return Attribute;
494}(_namespace["default"]);
495
496exports["default"] = Attribute;
497Attribute.NO_QUOTE = null;
498Attribute.SINGLE_QUOTE = "'";
499Attribute.DOUBLE_QUOTE = '"';
500var CSSESC_QUOTE_OPTIONS = (_CSSESC_QUOTE_OPTIONS = {
501 "'": {
502 quotes: 'single',
503 wrap: true
504 },
505 '"': {
506 quotes: 'double',
507 wrap: true
508 }
509}, _CSSESC_QUOTE_OPTIONS[null] = {
510 isIdentifier: true
511}, _CSSESC_QUOTE_OPTIONS);
512
513function defaultAttrConcat(attrValue, attrSpaces) {
514 return "" + attrSpaces.before + attrValue + attrSpaces.after;
515}
Note: See TracBrowser for help on using the repository browser.