source: trip-planner-front/node_modules/postcss-selector-parser/dist/util/unesc.js@ 6a3a178

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

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = unesc;
5
6// Many thanks for this post which made this migration much easier.
7// https://mathiasbynens.be/notes/css-escapes
8
9/**
10 *
11 * @param {string} str
12 * @returns {[string, number]|undefined}
13 */
14function gobbleHex(str) {
15 var lower = str.toLowerCase();
16 var hex = '';
17 var spaceTerminated = false;
18
19 for (var i = 0; i < 6 && lower[i] !== undefined; i++) {
20 var code = lower.charCodeAt(i); // check to see if we are dealing with a valid hex char [a-f|0-9]
21
22 var valid = code >= 97 && code <= 102 || code >= 48 && code <= 57; // https://drafts.csswg.org/css-syntax/#consume-escaped-code-point
23
24 spaceTerminated = code === 32;
25
26 if (!valid) {
27 break;
28 }
29
30 hex += lower[i];
31 }
32
33 if (hex.length === 0) {
34 return undefined;
35 }
36
37 var codePoint = parseInt(hex, 16);
38 var isSurrogate = codePoint >= 0xD800 && codePoint <= 0xDFFF; // Add special case for
39 // "If this number is zero, or is for a surrogate, or is greater than the maximum allowed code point"
40 // https://drafts.csswg.org/css-syntax/#maximum-allowed-code-point
41
42 if (isSurrogate || codePoint === 0x0000 || codePoint > 0x10FFFF) {
43 return ["\uFFFD", hex.length + (spaceTerminated ? 1 : 0)];
44 }
45
46 return [String.fromCodePoint(codePoint), hex.length + (spaceTerminated ? 1 : 0)];
47}
48
49var CONTAINS_ESCAPE = /\\/;
50
51function unesc(str) {
52 var needToProcess = CONTAINS_ESCAPE.test(str);
53
54 if (!needToProcess) {
55 return str;
56 }
57
58 var ret = "";
59
60 for (var i = 0; i < str.length; i++) {
61 if (str[i] === "\\") {
62 var gobbled = gobbleHex(str.slice(i + 1, i + 7));
63
64 if (gobbled !== undefined) {
65 ret += gobbled[0];
66 i += gobbled[1];
67 continue;
68 } // Retain a pair of \\ if double escaped `\\\\`
69 // https://github.com/postcss/postcss-selector-parser/commit/268c9a7656fb53f543dc620aa5b73a30ec3ff20e
70
71
72 if (str[i + 1] === "\\") {
73 ret += "\\";
74 i++;
75 continue;
76 } // if \\ is at the end of the string retain it
77 // https://github.com/postcss/postcss-selector-parser/commit/01a6b346e3612ce1ab20219acc26abdc259ccefb
78
79
80 if (str.length === i + 1) {
81 ret += str[i];
82 }
83
84 continue;
85 }
86
87 ret += str[i];
88 }
89
90 return ret;
91}
92
93module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.