1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports["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 | */
|
---|
14 | function 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 |
|
---|
49 | var CONTAINS_ESCAPE = /\\/;
|
---|
50 |
|
---|
51 | function 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 |
|
---|
93 | module.exports = exports.default; |
---|