css-selector-parser.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright (c) Felix Böhm
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. *
  9. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS,
  12. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13. */
  14. // Modified by Gildas Lormeau
  15. /* global CSS */
  16. this.cssWhat = this.cssWhat || (() => {
  17. "use strict";
  18. /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */
  19. (function (root) {
  20. if (root.CSS && root.CSS.escape) {
  21. return root.CSS.escape;
  22. }
  23. // https://drafts.csswg.org/cssom/#serialize-an-identifier
  24. var cssEscape = function (value) {
  25. if (arguments.length == 0) {
  26. throw new TypeError("`CSS.escape` requires an argument.");
  27. }
  28. var string = String(value);
  29. var length = string.length;
  30. var index = -1;
  31. var codeUnit;
  32. var result = "";
  33. var firstCodeUnit = string.charCodeAt(0);
  34. while (++index < length) {
  35. codeUnit = string.charCodeAt(index);
  36. // Note: there’s no need to special-case astral symbols, surrogate
  37. // pairs, or lone surrogates.
  38. // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
  39. // (U+FFFD).
  40. if (codeUnit == 0x0000) {
  41. result += "\uFFFD";
  42. continue;
  43. }
  44. if (
  45. // If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
  46. // U+007F, […]
  47. (codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
  48. // If the character is the first character and is in the range [0-9]
  49. // (U+0030 to U+0039), […]
  50. (index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
  51. // If the character is the second character and is in the range [0-9]
  52. // (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
  53. (
  54. index == 1 &&
  55. codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
  56. firstCodeUnit == 0x002D
  57. )
  58. ) {
  59. // https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
  60. result += "\\" + codeUnit.toString(16) + " ";
  61. continue;
  62. }
  63. if (
  64. // If the character is the first character and is a `-` (U+002D), and
  65. // there is no second character, […]
  66. index == 0 &&
  67. length == 1 &&
  68. codeUnit == 0x002D
  69. ) {
  70. result += "\\" + string.charAt(index);
  71. continue;
  72. }
  73. // If the character is not handled by one of the above rules and is
  74. // greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
  75. // is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
  76. // U+005A), or [a-z] (U+0061 to U+007A), […]
  77. if (
  78. codeUnit >= 0x0080 ||
  79. codeUnit == 0x002D ||
  80. codeUnit == 0x005F ||
  81. codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
  82. codeUnit >= 0x0041 && codeUnit <= 0x005A ||
  83. codeUnit >= 0x0061 && codeUnit <= 0x007A
  84. ) {
  85. // the character itself
  86. result += string.charAt(index);
  87. continue;
  88. }
  89. // Otherwise, the escaped character.
  90. // https://drafts.csswg.org/cssom/#escape-a-character
  91. result += "\\" + string.charAt(index);
  92. }
  93. return result;
  94. };
  95. if (!root.CSS) {
  96. root.CSS = {};
  97. }
  98. root.CSS.escape = cssEscape;
  99. return cssEscape;
  100. })(this);
  101. const re_name = /^(?:\\.|[\w\-\u00c0-\uFFFF])+/,
  102. re_escape = /\\([\da-f]{1,6}\s?|(\s)|.)/ig,
  103. //modified version of https://github.com/jquery/sizzle/blob/master/src/sizzle.js#L87
  104. re_attr = /^\s*((?:\\.|[\w\u00c0-\uFFFF-])+)\s*(?:(\S?)=\s*(?:(['"])([^]*?)\3|(#?(?:\\.|[\w\u00c0-\uFFFF-])*)|)|)\s*(i)?\]/;
  105. const actionTypes = {
  106. __proto__: null,
  107. "undefined": "exists",
  108. "": "equals",
  109. "~": "element",
  110. "^": "start",
  111. "$": "end",
  112. "*": "any",
  113. "!": "not",
  114. "|": "hyphen"
  115. };
  116. const simpleSelectors = {
  117. __proto__: null,
  118. ">": "child",
  119. "~": "sibling",
  120. "+": "adjacent"
  121. };
  122. const attribSelectors = {
  123. __proto__: null,
  124. "#": ["id", "equals"],
  125. ".": ["class", "element"]
  126. };
  127. //pseudos, whose data-property is parsed as well
  128. const unpackPseudos = {
  129. __proto__: null,
  130. "has": true,
  131. "not": true,
  132. "matches": true
  133. };
  134. const stripQuotesFromPseudos = {
  135. __proto__: null,
  136. "contains": true,
  137. "icontains": true
  138. };
  139. const quotes = {
  140. __proto__: null,
  141. "\"": true,
  142. "'": true
  143. };
  144. const pseudoElements = [
  145. "after", "before", "cue", "first-letter", "first-line", "selection", "slotted"
  146. ];
  147. const stringify = (() => {
  148. const actionTypes = {
  149. "equals": "",
  150. "element": "~",
  151. "start": "^",
  152. "end": "$",
  153. "any": "*",
  154. "not": "!",
  155. "hyphen": "|"
  156. };
  157. const simpleSelectors = {
  158. __proto__: null,
  159. child: " > ",
  160. sibling: " ~ ",
  161. adjacent: " + ",
  162. descendant: " ",
  163. universal: "*"
  164. };
  165. function stringify(token) {
  166. let value = "";
  167. token.forEach(token => value += stringifySubselector(token) + ",");
  168. return value.substring(0, value.length - 1);
  169. }
  170. function stringifySubselector(token) {
  171. let value = "";
  172. token.forEach(token => value += stringifyToken(token));
  173. return value;
  174. }
  175. function stringifyToken(token) {
  176. if (token.type in simpleSelectors) return simpleSelectors[token.type];
  177. if (token.type == "tag") return escapeName(token.name);
  178. if (token.type == "attribute") {
  179. if (token.action == "exists") return "[" + escapeName(token.name) + "]";
  180. if (token.expandedSelector && token.name == "id" && token.action == "equals" && !token.ignoreCase) return "#" + escapeName(token.value);
  181. if (token.expandedSelector && token.name == "class" && token.action == "element" && !token.ignoreCase) return "." + escapeName(token.value);
  182. return "[" +
  183. escapeName(token.name) + actionTypes[token.action] + "=\"" +
  184. escapeName(token.value) + "\"" + (token.ignoreCase ? "i" : "") + "]";
  185. }
  186. if (token.type == "pseudo") {
  187. if (token.data == null) return ":" + escapeName(token.name);
  188. if (typeof token.data == "string") return ":" + escapeName(token.name) + "(" + token.data + ")";
  189. return ":" + escapeName(token.name) + "(" + stringify(token.data) + ")";
  190. }
  191. if (token.type == "pseudo-element") {
  192. return "::" + escapeName(token.name);
  193. }
  194. }
  195. function escapeName(str) {
  196. return CSS.escape(str);
  197. }
  198. return stringify;
  199. })();
  200. return {
  201. parse,
  202. stringify
  203. };
  204. // unescape function taken from https://github.com/jquery/sizzle/blob/master/src/sizzle.js#L139
  205. function funescape(_, escaped, escapedWhitespace) {
  206. const high = "0x" + escaped - 0x10000;
  207. // NaN means non-codepoint
  208. // Support: Firefox
  209. // Workaround erroneous numeric interpretation of +"0x"
  210. return high != high || escapedWhitespace ?
  211. escaped :
  212. // BMP codepoint
  213. high < 0 ?
  214. String.fromCharCode(high + 0x10000) :
  215. // Supplemental Plane codepoint (surrogate pair)
  216. String.fromCharCode(high >> 10 | 0xD800, high & 0x3FF | 0xDC00);
  217. }
  218. function unescapeCSS(str) {
  219. return str.replace(re_escape, funescape);
  220. }
  221. function isWhitespace(c) {
  222. return c == " " || c == "\n" || c == "\t" || c == "\f" || c == "\r";
  223. }
  224. function parse(selector, options) {
  225. const subselects = [];
  226. selector = parseSelector(subselects, selector + "", options);
  227. if (selector != "") {
  228. throw new SyntaxError("Unmatched selector: " + selector);
  229. }
  230. return subselects;
  231. }
  232. function parseSelector(subselects, selector, options) {
  233. let tokens = [], sawWS = false, data, firstChar, name, quot;
  234. stripWhitespace(0);
  235. while (selector != "") {
  236. firstChar = selector.charAt(0);
  237. if (isWhitespace(firstChar)) {
  238. sawWS = true;
  239. stripWhitespace(1);
  240. } else if (firstChar in simpleSelectors) {
  241. tokens.push({ type: simpleSelectors[firstChar] });
  242. sawWS = false;
  243. stripWhitespace(1);
  244. } else if (firstChar == ",") {
  245. if (tokens.length == 0) {
  246. throw new SyntaxError("empty sub-selector");
  247. }
  248. subselects.push(tokens);
  249. tokens = [];
  250. sawWS = false;
  251. stripWhitespace(1);
  252. } else {
  253. if (sawWS) {
  254. if (tokens.length > 0) {
  255. tokens.push({ type: "descendant" });
  256. }
  257. sawWS = false;
  258. }
  259. if (firstChar == "*") {
  260. selector = selector.substr(1);
  261. tokens.push({ type: "universal" });
  262. } else if (firstChar in attribSelectors) {
  263. selector = selector.substr(1);
  264. tokens.push({
  265. expandedSelector: true,
  266. type: "attribute",
  267. name: attribSelectors[firstChar][0],
  268. action: attribSelectors[firstChar][1],
  269. value: getName(),
  270. ignoreCase: false
  271. });
  272. } else if (firstChar == "[") {
  273. selector = selector.substr(1);
  274. data = selector.match(re_attr);
  275. if (!data) {
  276. throw new SyntaxError("Malformed attribute selector: " + selector);
  277. }
  278. selector = selector.substr(data[0].length);
  279. name = unescapeCSS(data[1]);
  280. if (
  281. !options || (
  282. "lowerCaseAttributeNames" in options ?
  283. options.lowerCaseAttributeNames :
  284. !options.xmlMode
  285. )
  286. ) {
  287. name = name.toLowerCase();
  288. }
  289. tokens.push({
  290. type: "attribute",
  291. name: name,
  292. action: actionTypes[data[2]],
  293. value: unescapeCSS(data[4] || data[5] || ""),
  294. ignoreCase: !!data[6]
  295. });
  296. } else if (firstChar == ":") {
  297. if (selector.charAt(1) == ":") {
  298. selector = selector.substr(2);
  299. tokens.push({ type: "pseudo-element", name: getName().toLowerCase() });
  300. continue;
  301. }
  302. selector = selector.substr(1);
  303. name = getName().toLowerCase();
  304. data = null;
  305. if (selector.charAt(0) == "(") {
  306. if (name in unpackPseudos) {
  307. quot = selector.charAt(1);
  308. const quoted = quot in quotes;
  309. selector = selector.substr(quoted + 1);
  310. data = [];
  311. selector = parseSelector(data, selector, options);
  312. if (quoted) {
  313. if (selector.charAt(0) != quot) {
  314. throw new SyntaxError("unmatched quotes in :" + name);
  315. } else {
  316. selector = selector.substr(1);
  317. }
  318. }
  319. if (selector.charAt(0) != ")") {
  320. throw new SyntaxError("missing closing parenthesis in :" + name + " " + selector);
  321. }
  322. selector = selector.substr(1);
  323. } else {
  324. let pos = 1, counter = 1;
  325. for (; counter > 0 && pos < selector.length; pos++) {
  326. if (selector.charAt(pos) == "(" && !isEscaped(pos)) counter++;
  327. else if (selector.charAt(pos) == ")" && !isEscaped(pos)) counter--;
  328. }
  329. if (counter) {
  330. throw new SyntaxError("parenthesis not matched");
  331. }
  332. data = selector.substr(1, pos - 2);
  333. selector = selector.substr(pos);
  334. if (name in stripQuotesFromPseudos) {
  335. quot = data.charAt(0);
  336. if (quot == data.slice(-1) && quot in quotes) {
  337. data = data.slice(1, -1);
  338. }
  339. data = unescapeCSS(data);
  340. }
  341. }
  342. }
  343. tokens.push({ type: pseudoElements.indexOf(name) == -1 ? "pseudo" : "pseudo-element", name: name, data: data });
  344. } else if (re_name.test(selector)) {
  345. name = getName();
  346. if (!options || ("lowerCaseTags" in options ? options.lowerCaseTags : !options.xmlMode)) {
  347. name = name.toLowerCase();
  348. }
  349. tokens.push({ type: "tag", name: name });
  350. } else {
  351. if (tokens.length && tokens[tokens.length - 1].type == "descendant") {
  352. tokens.pop();
  353. }
  354. addToken(subselects, tokens);
  355. return selector;
  356. }
  357. }
  358. }
  359. addToken(subselects, tokens);
  360. return selector;
  361. function getName() {
  362. const sub = selector.match(re_name)[0];
  363. selector = selector.substr(sub.length);
  364. return unescapeCSS(sub);
  365. }
  366. function stripWhitespace(start) {
  367. while (isWhitespace(selector.charAt(start))) start++;
  368. selector = selector.substr(start);
  369. }
  370. function isEscaped(pos) {
  371. let slashCount = 0;
  372. while (selector.charAt(--pos) == "\\") slashCount++;
  373. return (slashCount & 1) == 1;
  374. }
  375. }
  376. function addToken(subselects, tokens) {
  377. if (subselects.length > 0 && tokens.length == 0) {
  378. throw new SyntaxError("empty sub-selector");
  379. }
  380. subselects.push(tokens);
  381. }
  382. })();