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