You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4545 lines
113 KiB

4 years ago
  1. function _typeof(obj) {
  2. "@babel/helpers - typeof";
  3. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  4. _typeof = function (obj) {
  5. return typeof obj;
  6. };
  7. } else {
  8. _typeof = function (obj) {
  9. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  10. };
  11. }
  12. return _typeof(obj);
  13. }
  14. function _slicedToArray(arr, i) {
  15. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
  16. }
  17. function _toConsumableArray(arr) {
  18. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
  19. }
  20. function _arrayWithoutHoles(arr) {
  21. if (Array.isArray(arr)) return _arrayLikeToArray(arr);
  22. }
  23. function _arrayWithHoles(arr) {
  24. if (Array.isArray(arr)) return arr;
  25. }
  26. function _iterableToArray(iter) {
  27. if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
  28. }
  29. function _iterableToArrayLimit(arr, i) {
  30. if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
  31. var _arr = [];
  32. var _n = true;
  33. var _d = false;
  34. var _e = undefined;
  35. try {
  36. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  37. _arr.push(_s.value);
  38. if (i && _arr.length === i) break;
  39. }
  40. } catch (err) {
  41. _d = true;
  42. _e = err;
  43. } finally {
  44. try {
  45. if (!_n && _i["return"] != null) _i["return"]();
  46. } finally {
  47. if (_d) throw _e;
  48. }
  49. }
  50. return _arr;
  51. }
  52. function _unsupportedIterableToArray(o, minLen) {
  53. if (!o) return;
  54. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  55. var n = Object.prototype.toString.call(o).slice(8, -1);
  56. if (n === "Object" && o.constructor) n = o.constructor.name;
  57. if (n === "Map" || n === "Set") return Array.from(n);
  58. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  59. }
  60. function _arrayLikeToArray(arr, len) {
  61. if (len == null || len > arr.length) len = arr.length;
  62. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  63. return arr2;
  64. }
  65. function _nonIterableSpread() {
  66. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  67. }
  68. function _nonIterableRest() {
  69. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  70. }
  71. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  72. function createCommonjsModule(fn, module) {
  73. return module = { exports: {} }, fn(module, module.exports), module.exports;
  74. }
  75. var estraverse = createCommonjsModule(function (module, exports) {
  76. /*
  77. Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
  78. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  79. Redistribution and use in source and binary forms, with or without
  80. modification, are permitted provided that the following conditions are met:
  81. * Redistributions of source code must retain the above copyright
  82. notice, this list of conditions and the following disclaimer.
  83. * Redistributions in binary form must reproduce the above copyright
  84. notice, this list of conditions and the following disclaimer in the
  85. documentation and/or other materials provided with the distribution.
  86. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  87. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  88. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  89. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  90. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  91. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  92. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  93. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  94. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  95. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  96. */
  97. /*jslint vars:false, bitwise:true*/
  98. /*jshint indent:4*/
  99. /*global exports:true*/
  100. (function clone(exports) {
  101. var Syntax, VisitorOption, VisitorKeys, BREAK, SKIP, REMOVE;
  102. function deepCopy(obj) {
  103. var ret = {},
  104. key,
  105. val;
  106. for (key in obj) {
  107. if (obj.hasOwnProperty(key)) {
  108. val = obj[key];
  109. if (typeof val === 'object' && val !== null) {
  110. ret[key] = deepCopy(val);
  111. } else {
  112. ret[key] = val;
  113. }
  114. }
  115. }
  116. return ret;
  117. } // based on LLVM libc++ upper_bound / lower_bound
  118. // MIT License
  119. function upperBound(array, func) {
  120. var diff, len, i, current;
  121. len = array.length;
  122. i = 0;
  123. while (len) {
  124. diff = len >>> 1;
  125. current = i + diff;
  126. if (func(array[current])) {
  127. len = diff;
  128. } else {
  129. i = current + 1;
  130. len -= diff + 1;
  131. }
  132. }
  133. return i;
  134. }
  135. Syntax = {
  136. AssignmentExpression: 'AssignmentExpression',
  137. AssignmentPattern: 'AssignmentPattern',
  138. ArrayExpression: 'ArrayExpression',
  139. ArrayPattern: 'ArrayPattern',
  140. ArrowFunctionExpression: 'ArrowFunctionExpression',
  141. AwaitExpression: 'AwaitExpression',
  142. // CAUTION: It's deferred to ES7.
  143. BlockStatement: 'BlockStatement',
  144. BinaryExpression: 'BinaryExpression',
  145. BreakStatement: 'BreakStatement',
  146. CallExpression: 'CallExpression',
  147. CatchClause: 'CatchClause',
  148. ClassBody: 'ClassBody',
  149. ClassDeclaration: 'ClassDeclaration',
  150. ClassExpression: 'ClassExpression',
  151. ComprehensionBlock: 'ComprehensionBlock',
  152. // CAUTION: It's deferred to ES7.
  153. ComprehensionExpression: 'ComprehensionExpression',
  154. // CAUTION: It's deferred to ES7.
  155. ConditionalExpression: 'ConditionalExpression',
  156. ContinueStatement: 'ContinueStatement',
  157. DebuggerStatement: 'DebuggerStatement',
  158. DirectiveStatement: 'DirectiveStatement',
  159. DoWhileStatement: 'DoWhileStatement',
  160. EmptyStatement: 'EmptyStatement',
  161. ExportAllDeclaration: 'ExportAllDeclaration',
  162. ExportDefaultDeclaration: 'ExportDefaultDeclaration',
  163. ExportNamedDeclaration: 'ExportNamedDeclaration',
  164. ExportSpecifier: 'ExportSpecifier',
  165. ExpressionStatement: 'ExpressionStatement',
  166. ForStatement: 'ForStatement',
  167. ForInStatement: 'ForInStatement',
  168. ForOfStatement: 'ForOfStatement',
  169. FunctionDeclaration: 'FunctionDeclaration',
  170. FunctionExpression: 'FunctionExpression',
  171. GeneratorExpression: 'GeneratorExpression',
  172. // CAUTION: It's deferred to ES7.
  173. Identifier: 'Identifier',
  174. IfStatement: 'IfStatement',
  175. ImportExpression: 'ImportExpression',
  176. ImportDeclaration: 'ImportDeclaration',
  177. ImportDefaultSpecifier: 'ImportDefaultSpecifier',
  178. ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
  179. ImportSpecifier: 'ImportSpecifier',
  180. Literal: 'Literal',
  181. LabeledStatement: 'LabeledStatement',
  182. LogicalExpression: 'LogicalExpression',
  183. MemberExpression: 'MemberExpression',
  184. MetaProperty: 'MetaProperty',
  185. MethodDefinition: 'MethodDefinition',
  186. ModuleSpecifier: 'ModuleSpecifier',
  187. NewExpression: 'NewExpression',
  188. ObjectExpression: 'ObjectExpression',
  189. ObjectPattern: 'ObjectPattern',
  190. Program: 'Program',
  191. Property: 'Property',
  192. RestElement: 'RestElement',
  193. ReturnStatement: 'ReturnStatement',
  194. SequenceExpression: 'SequenceExpression',
  195. SpreadElement: 'SpreadElement',
  196. Super: 'Super',
  197. SwitchStatement: 'SwitchStatement',
  198. SwitchCase: 'SwitchCase',
  199. TaggedTemplateExpression: 'TaggedTemplateExpression',
  200. TemplateElement: 'TemplateElement',
  201. TemplateLiteral: 'TemplateLiteral',
  202. ThisExpression: 'ThisExpression',
  203. ThrowStatement: 'ThrowStatement',
  204. TryStatement: 'TryStatement',
  205. UnaryExpression: 'UnaryExpression',
  206. UpdateExpression: 'UpdateExpression',
  207. VariableDeclaration: 'VariableDeclaration',
  208. VariableDeclarator: 'VariableDeclarator',
  209. WhileStatement: 'WhileStatement',
  210. WithStatement: 'WithStatement',
  211. YieldExpression: 'YieldExpression'
  212. };
  213. VisitorKeys = {
  214. AssignmentExpression: ['left', 'right'],
  215. AssignmentPattern: ['left', 'right'],
  216. ArrayExpression: ['elements'],
  217. ArrayPattern: ['elements'],
  218. ArrowFunctionExpression: ['params', 'body'],
  219. AwaitExpression: ['argument'],
  220. // CAUTION: It's deferred to ES7.
  221. BlockStatement: ['body'],
  222. BinaryExpression: ['left', 'right'],
  223. BreakStatement: ['label'],
  224. CallExpression: ['callee', 'arguments'],
  225. CatchClause: ['param', 'body'],
  226. ClassBody: ['body'],
  227. ClassDeclaration: ['id', 'superClass', 'body'],
  228. ClassExpression: ['id', 'superClass', 'body'],
  229. ComprehensionBlock: ['left', 'right'],
  230. // CAUTION: It's deferred to ES7.
  231. ComprehensionExpression: ['blocks', 'filter', 'body'],
  232. // CAUTION: It's deferred to ES7.
  233. ConditionalExpression: ['test', 'consequent', 'alternate'],
  234. ContinueStatement: ['label'],
  235. DebuggerStatement: [],
  236. DirectiveStatement: [],
  237. DoWhileStatement: ['body', 'test'],
  238. EmptyStatement: [],
  239. ExportAllDeclaration: ['source'],
  240. ExportDefaultDeclaration: ['declaration'],
  241. ExportNamedDeclaration: ['declaration', 'specifiers', 'source'],
  242. ExportSpecifier: ['exported', 'local'],
  243. ExpressionStatement: ['expression'],
  244. ForStatement: ['init', 'test', 'update', 'body'],
  245. ForInStatement: ['left', 'right', 'body'],
  246. ForOfStatement: ['left', 'right', 'body'],
  247. FunctionDeclaration: ['id', 'params', 'body'],
  248. FunctionExpression: ['id', 'params', 'body'],
  249. GeneratorExpression: ['blocks', 'filter', 'body'],
  250. // CAUTION: It's deferred to ES7.
  251. Identifier: [],
  252. IfStatement: ['test', 'consequent', 'alternate'],
  253. ImportExpression: ['source'],
  254. ImportDeclaration: ['specifiers', 'source'],
  255. ImportDefaultSpecifier: ['local'],
  256. ImportNamespaceSpecifier: ['local'],
  257. ImportSpecifier: ['imported', 'local'],
  258. Literal: [],
  259. LabeledStatement: ['label', 'body'],
  260. LogicalExpression: ['left', 'right'],
  261. MemberExpression: ['object', 'property'],
  262. MetaProperty: ['meta', 'property'],
  263. MethodDefinition: ['key', 'value'],
  264. ModuleSpecifier: [],
  265. NewExpression: ['callee', 'arguments'],
  266. ObjectExpression: ['properties'],
  267. ObjectPattern: ['properties'],
  268. Program: ['body'],
  269. Property: ['key', 'value'],
  270. RestElement: ['argument'],
  271. ReturnStatement: ['argument'],
  272. SequenceExpression: ['expressions'],
  273. SpreadElement: ['argument'],
  274. Super: [],
  275. SwitchStatement: ['discriminant', 'cases'],
  276. SwitchCase: ['test', 'consequent'],
  277. TaggedTemplateExpression: ['tag', 'quasi'],
  278. TemplateElement: [],
  279. TemplateLiteral: ['quasis', 'expressions'],
  280. ThisExpression: [],
  281. ThrowStatement: ['argument'],
  282. TryStatement: ['block', 'handler', 'finalizer'],
  283. UnaryExpression: ['argument'],
  284. UpdateExpression: ['argument'],
  285. VariableDeclaration: ['declarations'],
  286. VariableDeclarator: ['id', 'init'],
  287. WhileStatement: ['test', 'body'],
  288. WithStatement: ['object', 'body'],
  289. YieldExpression: ['argument']
  290. }; // unique id
  291. BREAK = {};
  292. SKIP = {};
  293. REMOVE = {};
  294. VisitorOption = {
  295. Break: BREAK,
  296. Skip: SKIP,
  297. Remove: REMOVE
  298. };
  299. function Reference(parent, key) {
  300. this.parent = parent;
  301. this.key = key;
  302. }
  303. Reference.prototype.replace = function replace(node) {
  304. this.parent[this.key] = node;
  305. };
  306. Reference.prototype.remove = function remove() {
  307. if (Array.isArray(this.parent)) {
  308. this.parent.splice(this.key, 1);
  309. return true;
  310. } else {
  311. this.replace(null);
  312. return false;
  313. }
  314. };
  315. function Element(node, path, wrap, ref) {
  316. this.node = node;
  317. this.path = path;
  318. this.wrap = wrap;
  319. this.ref = ref;
  320. }
  321. function Controller() {} // API:
  322. // return property path array from root to current node
  323. Controller.prototype.path = function path() {
  324. var i, iz, j, jz, result, element;
  325. function addToPath(result, path) {
  326. if (Array.isArray(path)) {
  327. for (j = 0, jz = path.length; j < jz; ++j) {
  328. result.push(path[j]);
  329. }
  330. } else {
  331. result.push(path);
  332. }
  333. } // root node
  334. if (!this.__current.path) {
  335. return null;
  336. } // first node is sentinel, second node is root element
  337. result = [];
  338. for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {
  339. element = this.__leavelist[i];
  340. addToPath(result, element.path);
  341. }
  342. addToPath(result, this.__current.path);
  343. return result;
  344. }; // API:
  345. // return type of current node
  346. Controller.prototype.type = function () {
  347. var node = this.current();
  348. return node.type || this.__current.wrap;
  349. }; // API:
  350. // return array of parent elements
  351. Controller.prototype.parents = function parents() {
  352. var i, iz, result; // first node is sentinel
  353. result = [];
  354. for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {
  355. result.push(this.__leavelist[i].node);
  356. }
  357. return result;
  358. }; // API:
  359. // return current node
  360. Controller.prototype.current = function current() {
  361. return this.__current.node;
  362. };
  363. Controller.prototype.__execute = function __execute(callback, element) {
  364. var previous, result;
  365. result = undefined;
  366. previous = this.__current;
  367. this.__current = element;
  368. this.__state = null;
  369. if (callback) {
  370. result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);
  371. }
  372. this.__current = previous;
  373. return result;
  374. }; // API:
  375. // notify control skip / break
  376. Controller.prototype.notify = function notify(flag) {
  377. this.__state = flag;
  378. }; // API:
  379. // skip child nodes of current node
  380. Controller.prototype.skip = function () {
  381. this.notify(SKIP);
  382. }; // API:
  383. // break traversals
  384. Controller.prototype['break'] = function () {
  385. this.notify(BREAK);
  386. }; // API:
  387. // remove node
  388. Controller.prototype.remove = function () {
  389. this.notify(REMOVE);
  390. };
  391. Controller.prototype.__initialize = function (root, visitor) {
  392. this.visitor = visitor;
  393. this.root = root;
  394. this.__worklist = [];
  395. this.__leavelist = [];
  396. this.__current = null;
  397. this.__state = null;
  398. this.__fallback = null;
  399. if (visitor.fallback === 'iteration') {
  400. this.__fallback = Object.keys;
  401. } else if (typeof visitor.fallback === 'function') {
  402. this.__fallback = visitor.fallback;
  403. }
  404. this.__keys = VisitorKeys;
  405. if (visitor.keys) {
  406. this.__keys = Object.assign(Object.create(this.__keys), visitor.keys);
  407. }
  408. };
  409. function isNode(node) {
  410. if (node == null) {
  411. return false;
  412. }
  413. return typeof node === 'object' && typeof node.type === 'string';
  414. }
  415. function isProperty(nodeType, key) {
  416. return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key;
  417. }
  418. function candidateExistsInLeaveList(leavelist, candidate) {
  419. for (var i = leavelist.length - 1; i >= 0; --i) {
  420. if (leavelist[i].node === candidate) {
  421. return true;
  422. }
  423. }
  424. return false;
  425. }
  426. Controller.prototype.traverse = function traverse(root, visitor) {
  427. var worklist, leavelist, element, node, nodeType, ret, key, current, current2, candidates, candidate, sentinel;
  428. this.__initialize(root, visitor);
  429. sentinel = {}; // reference
  430. worklist = this.__worklist;
  431. leavelist = this.__leavelist; // initialize
  432. worklist.push(new Element(root, null, null, null));
  433. leavelist.push(new Element(null, null, null, null));
  434. while (worklist.length) {
  435. element = worklist.pop();
  436. if (element === sentinel) {
  437. element = leavelist.pop();
  438. ret = this.__execute(visitor.leave, element);
  439. if (this.__state === BREAK || ret === BREAK) {
  440. return;
  441. }
  442. continue;
  443. }
  444. if (element.node) {
  445. ret = this.__execute(visitor.enter, element);
  446. if (this.__state === BREAK || ret === BREAK) {
  447. return;
  448. }
  449. worklist.push(sentinel);
  450. leavelist.push(element);
  451. if (this.__state === SKIP || ret === SKIP) {
  452. continue;
  453. }
  454. node = element.node;
  455. nodeType = node.type || element.wrap;
  456. candidates = this.__keys[nodeType];
  457. if (!candidates) {
  458. if (this.__fallback) {
  459. candidates = this.__fallback(node);
  460. } else {
  461. throw new Error('Unknown node type ' + nodeType + '.');
  462. }
  463. }
  464. current = candidates.length;
  465. while ((current -= 1) >= 0) {
  466. key = candidates[current];
  467. candidate = node[key];
  468. if (!candidate) {
  469. continue;
  470. }
  471. if (Array.isArray(candidate)) {
  472. current2 = candidate.length;
  473. while ((current2 -= 1) >= 0) {
  474. if (!candidate[current2]) {
  475. continue;
  476. }
  477. if (candidateExistsInLeaveList(leavelist, candidate[current2])) {
  478. continue;
  479. }
  480. if (isProperty(nodeType, candidates[current])) {
  481. element = new Element(candidate[current2], [key, current2], 'Property', null);
  482. } else if (isNode(candidate[current2])) {
  483. element = new Element(candidate[current2], [key, current2], null, null);
  484. } else {
  485. continue;
  486. }
  487. worklist.push(element);
  488. }
  489. } else if (isNode(candidate)) {
  490. if (candidateExistsInLeaveList(leavelist, candidate)) {
  491. continue;
  492. }
  493. worklist.push(new Element(candidate, key, null, null));
  494. }
  495. }
  496. }
  497. }
  498. };
  499. Controller.prototype.replace = function replace(root, visitor) {
  500. var worklist, leavelist, node, nodeType, target, element, current, current2, candidates, candidate, sentinel, outer, key;
  501. function removeElem(element) {
  502. var i, key, nextElem, parent;
  503. if (element.ref.remove()) {
  504. // When the reference is an element of an array.
  505. key = element.ref.key;
  506. parent = element.ref.parent; // If removed from array, then decrease following items' keys.
  507. i = worklist.length;
  508. while (i--) {
  509. nextElem = worklist[i];
  510. if (nextElem.ref && nextElem.ref.parent === parent) {
  511. if (nextElem.ref.key < key) {
  512. break;
  513. }
  514. --nextElem.ref.key;
  515. }
  516. }
  517. }
  518. }
  519. this.__initialize(root, visitor);
  520. sentinel = {}; // reference
  521. worklist = this.__worklist;
  522. leavelist = this.__leavelist; // initialize
  523. outer = {
  524. root: root
  525. };
  526. element = new Element(root, null, null, new Reference(outer, 'root'));
  527. worklist.push(element);
  528. leavelist.push(element);
  529. while (worklist.length) {
  530. element = worklist.pop();
  531. if (element === sentinel) {
  532. element = leavelist.pop();
  533. target = this.__execute(visitor.leave, element); // node may be replaced with null,
  534. // so distinguish between undefined and null in this place
  535. if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
  536. // replace
  537. element.ref.replace(target);
  538. }
  539. if (this.__state === REMOVE || target === REMOVE) {
  540. removeElem(element);
  541. }
  542. if (this.__state === BREAK || target === BREAK) {
  543. return outer.root;
  544. }
  545. continue;
  546. }
  547. target = this.__execute(visitor.enter, element); // node may be replaced with null,
  548. // so distinguish between undefined and null in this place
  549. if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
  550. // replace
  551. element.ref.replace(target);
  552. element.node = target;
  553. }
  554. if (this.__state === REMOVE || target === REMOVE) {
  555. removeElem(element);
  556. element.node = null;
  557. }
  558. if (this.__state === BREAK || target === BREAK) {
  559. return outer.root;
  560. } // node may be null
  561. node = element.node;
  562. if (!node) {
  563. continue;
  564. }
  565. worklist.push(sentinel);
  566. leavelist.push(element);
  567. if (this.__state === SKIP || target === SKIP) {
  568. continue;
  569. }
  570. nodeType = node.type || element.wrap;
  571. candidates = this.__keys[nodeType];
  572. if (!candidates) {
  573. if (this.__fallback) {
  574. candidates = this.__fallback(node);
  575. } else {
  576. throw new Error('Unknown node type ' + nodeType + '.');
  577. }
  578. }
  579. current = candidates.length;
  580. while ((current -= 1) >= 0) {
  581. key = candidates[current];
  582. candidate = node[key];
  583. if (!candidate) {
  584. continue;
  585. }
  586. if (Array.isArray(candidate)) {
  587. current2 = candidate.length;
  588. while ((current2 -= 1) >= 0) {
  589. if (!candidate[current2]) {
  590. continue;
  591. }
  592. if (isProperty(nodeType, candidates[current])) {
  593. element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));
  594. } else if (isNode(candidate[current2])) {
  595. element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));
  596. } else {
  597. continue;
  598. }
  599. worklist.push(element);
  600. }
  601. } else if (isNode(candidate)) {
  602. worklist.push(new Element(candidate, key, null, new Reference(node, key)));
  603. }
  604. }
  605. }
  606. return outer.root;
  607. };
  608. function traverse(root, visitor) {
  609. var controller = new Controller();
  610. return controller.traverse(root, visitor);
  611. }
  612. function replace(root, visitor) {
  613. var controller = new Controller();
  614. return controller.replace(root, visitor);
  615. }
  616. function extendCommentRange(comment, tokens) {
  617. var target;
  618. target = upperBound(tokens, function search(token) {
  619. return token.range[0] > comment.range[0];
  620. });
  621. comment.extendedRange = [comment.range[0], comment.range[1]];
  622. if (target !== tokens.length) {
  623. comment.extendedRange[1] = tokens[target].range[0];
  624. }
  625. target -= 1;
  626. if (target >= 0) {
  627. comment.extendedRange[0] = tokens[target].range[1];
  628. }
  629. return comment;
  630. }
  631. function attachComments(tree, providedComments, tokens) {
  632. // At first, we should calculate extended comment ranges.
  633. var comments = [],
  634. comment,
  635. len,
  636. i,
  637. cursor;
  638. if (!tree.range) {
  639. throw new Error('attachComments needs range information');
  640. } // tokens array is empty, we attach comments to tree as 'leadingComments'
  641. if (!tokens.length) {
  642. if (providedComments.length) {
  643. for (i = 0, len = providedComments.length; i < len; i += 1) {
  644. comment = deepCopy(providedComments[i]);
  645. comment.extendedRange = [0, tree.range[0]];
  646. comments.push(comment);
  647. }
  648. tree.leadingComments = comments;
  649. }
  650. return tree;
  651. }
  652. for (i = 0, len = providedComments.length; i < len; i += 1) {
  653. comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
  654. } // This is based on John Freeman's implementation.
  655. cursor = 0;
  656. traverse(tree, {
  657. enter: function (node) {
  658. var comment;
  659. while (cursor < comments.length) {
  660. comment = comments[cursor];
  661. if (comment.extendedRange[1] > node.range[0]) {
  662. break;
  663. }
  664. if (comment.extendedRange[1] === node.range[0]) {
  665. if (!node.leadingComments) {
  666. node.leadingComments = [];
  667. }
  668. node.leadingComments.push(comment);
  669. comments.splice(cursor, 1);
  670. } else {
  671. cursor += 1;
  672. }
  673. } // already out of owned node
  674. if (cursor === comments.length) {
  675. return VisitorOption.Break;
  676. }
  677. if (comments[cursor].extendedRange[0] > node.range[1]) {
  678. return VisitorOption.Skip;
  679. }
  680. }
  681. });
  682. cursor = 0;
  683. traverse(tree, {
  684. leave: function (node) {
  685. var comment;
  686. while (cursor < comments.length) {
  687. comment = comments[cursor];
  688. if (node.range[1] < comment.extendedRange[0]) {
  689. break;
  690. }
  691. if (node.range[1] === comment.extendedRange[0]) {
  692. if (!node.trailingComments) {
  693. node.trailingComments = [];
  694. }
  695. node.trailingComments.push(comment);
  696. comments.splice(cursor, 1);
  697. } else {
  698. cursor += 1;
  699. }
  700. } // already out of owned node
  701. if (cursor === comments.length) {
  702. return VisitorOption.Break;
  703. }
  704. if (comments[cursor].extendedRange[0] > node.range[1]) {
  705. return VisitorOption.Skip;
  706. }
  707. }
  708. });
  709. return tree;
  710. }
  711. exports.Syntax = Syntax;
  712. exports.traverse = traverse;
  713. exports.replace = replace;
  714. exports.attachComments = attachComments;
  715. exports.VisitorKeys = VisitorKeys;
  716. exports.VisitorOption = VisitorOption;
  717. exports.Controller = Controller;
  718. exports.cloneEnvironment = function () {
  719. return clone({});
  720. };
  721. return exports;
  722. })(exports);
  723. /* vim: set sw=4 ts=4 et tw=80 : */
  724. });
  725. var parser = createCommonjsModule(function (module) {
  726. /*
  727. * Generated by PEG.js 0.10.0.
  728. *
  729. * http://pegjs.org/
  730. */
  731. (function (root, factory) {
  732. if ( module.exports) {
  733. module.exports = factory();
  734. }
  735. })(commonjsGlobal, function () {
  736. function peg$subclass(child, parent) {
  737. function ctor() {
  738. this.constructor = child;
  739. }
  740. ctor.prototype = parent.prototype;
  741. child.prototype = new ctor();
  742. }
  743. function peg$SyntaxError(message, expected, found, location) {
  744. this.message = message;
  745. this.expected = expected;
  746. this.found = found;
  747. this.location = location;
  748. this.name = "SyntaxError";
  749. if (typeof Error.captureStackTrace === "function") {
  750. Error.captureStackTrace(this, peg$SyntaxError);
  751. }
  752. }
  753. peg$subclass(peg$SyntaxError, Error);
  754. peg$SyntaxError.buildMessage = function (expected, found) {
  755. var DESCRIBE_EXPECTATION_FNS = {
  756. literal: function literal(expectation) {
  757. return "\"" + literalEscape(expectation.text) + "\"";
  758. },
  759. "class": function _class(expectation) {
  760. var escapedParts = "",
  761. i;
  762. for (i = 0; i < expectation.parts.length; i++) {
  763. escapedParts += expectation.parts[i] instanceof Array ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) : classEscape(expectation.parts[i]);
  764. }
  765. return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
  766. },
  767. any: function any(expectation) {
  768. return "any character";
  769. },
  770. end: function end(expectation) {
  771. return "end of input";
  772. },
  773. other: function other(expectation) {
  774. return expectation.description;
  775. }
  776. };
  777. function hex(ch) {
  778. return ch.charCodeAt(0).toString(16).toUpperCase();
  779. }
  780. function literalEscape(s) {
  781. return s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\0/g, '\\0').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/[\x00-\x0F]/g, function (ch) {
  782. return '\\x0' + hex(ch);
  783. }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
  784. return '\\x' + hex(ch);
  785. });
  786. }
  787. function classEscape(s) {
  788. return s.replace(/\\/g, '\\\\').replace(/\]/g, '\\]').replace(/\^/g, '\\^').replace(/-/g, '\\-').replace(/\0/g, '\\0').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/[\x00-\x0F]/g, function (ch) {
  789. return '\\x0' + hex(ch);
  790. }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
  791. return '\\x' + hex(ch);
  792. });
  793. }
  794. function describeExpectation(expectation) {
  795. return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
  796. }
  797. function describeExpected(expected) {
  798. var descriptions = new Array(expected.length),
  799. i,
  800. j;
  801. for (i = 0; i < expected.length; i++) {
  802. descriptions[i] = describeExpectation(expected[i]);
  803. }
  804. descriptions.sort();
  805. if (descriptions.length > 0) {
  806. for (i = 1, j = 1; i < descriptions.length; i++) {
  807. if (descriptions[i - 1] !== descriptions[i]) {
  808. descriptions[j] = descriptions[i];
  809. j++;
  810. }
  811. }
  812. descriptions.length = j;
  813. }
  814. switch (descriptions.length) {
  815. case 1:
  816. return descriptions[0];
  817. case 2:
  818. return descriptions[0] + " or " + descriptions[1];
  819. default:
  820. return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1];
  821. }
  822. }
  823. function describeFound(found) {
  824. return found ? "\"" + literalEscape(found) + "\"" : "end of input";
  825. }
  826. return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
  827. };
  828. function peg$parse(input, options) {
  829. options = options !== void 0 ? options : {};
  830. var peg$FAILED = {},
  831. peg$startRuleFunctions = {
  832. start: peg$parsestart
  833. },
  834. peg$startRuleFunction = peg$parsestart,
  835. peg$c0 = function peg$c0(ss) {
  836. return ss.length === 1 ? ss[0] : {
  837. type: 'matches',
  838. selectors: ss
  839. };
  840. },
  841. peg$c1 = function peg$c1() {
  842. return void 0;
  843. },
  844. peg$c2 = " ",
  845. peg$c3 = peg$literalExpectation(" ", false),
  846. peg$c4 = /^[^ [\],():#!=><~+.]/,
  847. peg$c5 = peg$classExpectation([" ", "[", "]", ",", "(", ")", ":", "#", "!", "=", ">", "<", "~", "+", "."], true, false),
  848. peg$c6 = function peg$c6(i) {
  849. return i.join('');
  850. },
  851. peg$c7 = ">",
  852. peg$c8 = peg$literalExpectation(">", false),
  853. peg$c9 = function peg$c9() {
  854. return 'child';
  855. },
  856. peg$c10 = "~",
  857. peg$c11 = peg$literalExpectation("~", false),
  858. peg$c12 = function peg$c12() {
  859. return 'sibling';
  860. },
  861. peg$c13 = "+",
  862. peg$c14 = peg$literalExpectation("+", false),
  863. peg$c15 = function peg$c15() {
  864. return 'adjacent';
  865. },
  866. peg$c16 = function peg$c16() {
  867. return 'descendant';
  868. },
  869. peg$c17 = ",",
  870. peg$c18 = peg$literalExpectation(",", false),
  871. peg$c19 = function peg$c19(s, ss) {
  872. return [s].concat(ss.map(function (s) {
  873. return s[3];
  874. }));
  875. },
  876. peg$c20 = function peg$c20(a, ops) {
  877. return ops.reduce(function (memo, rhs) {
  878. return {
  879. type: rhs[0],
  880. left: memo,
  881. right: rhs[1]
  882. };
  883. }, a);
  884. },
  885. peg$c21 = "!",
  886. peg$c22 = peg$literalExpectation("!", false),
  887. peg$c23 = function peg$c23(subject, as) {
  888. var b = as.length === 1 ? as[0] : {
  889. type: 'compound',
  890. selectors: as
  891. };
  892. if (subject) b.subject = true;
  893. return b;
  894. },
  895. peg$c24 = "*",
  896. peg$c25 = peg$literalExpectation("*", false),
  897. peg$c26 = function peg$c26(a) {
  898. return {
  899. type: 'wildcard',
  900. value: a
  901. };
  902. },
  903. peg$c27 = "#",
  904. peg$c28 = peg$literalExpectation("#", false),
  905. peg$c29 = function peg$c29(i) {
  906. return {
  907. type: 'identifier',
  908. value: i
  909. };
  910. },
  911. peg$c30 = "[",
  912. peg$c31 = peg$literalExpectation("[", false),
  913. peg$c32 = "]",
  914. peg$c33 = peg$literalExpectation("]", false),
  915. peg$c34 = function peg$c34(v) {
  916. return v;
  917. },
  918. peg$c35 = /^[><!]/,
  919. peg$c36 = peg$classExpectation([">", "<", "!"], false, false),
  920. peg$c37 = "=",
  921. peg$c38 = peg$literalExpectation("=", false),
  922. peg$c39 = function peg$c39(a) {
  923. return (a || '') + '=';
  924. },
  925. peg$c40 = /^[><]/,
  926. peg$c41 = peg$classExpectation([">", "<"], false, false),
  927. peg$c42 = ".",
  928. peg$c43 = peg$literalExpectation(".", false),
  929. peg$c44 = function peg$c44(name, op, value) {
  930. return {
  931. type: 'attribute',
  932. name: name,
  933. operator: op,
  934. value: value
  935. };
  936. },
  937. peg$c45 = function peg$c45(name) {
  938. return {
  939. type: 'attribute',
  940. name: name
  941. };
  942. },
  943. peg$c46 = "\"",
  944. peg$c47 = peg$literalExpectation("\"", false),
  945. peg$c48 = /^[^\\"]/,
  946. peg$c49 = peg$classExpectation(["\\", "\""], true, false),
  947. peg$c50 = "\\",
  948. peg$c51 = peg$literalExpectation("\\", false),
  949. peg$c52 = peg$anyExpectation(),
  950. peg$c53 = function peg$c53(a, b) {
  951. return a + b;
  952. },
  953. peg$c54 = function peg$c54(d) {
  954. return {
  955. type: 'literal',
  956. value: strUnescape(d.join(''))
  957. };
  958. },
  959. peg$c55 = "'",
  960. peg$c56 = peg$literalExpectation("'", false),
  961. peg$c57 = /^[^\\']/,
  962. peg$c58 = peg$classExpectation(["\\", "'"], true, false),
  963. peg$c59 = /^[0-9]/,
  964. peg$c60 = peg$classExpectation([["0", "9"]], false, false),
  965. peg$c61 = function peg$c61(a, b) {
  966. // Can use `a.flat().join('')` once supported
  967. var leadingDecimals = a ? [].concat.apply([], a).join('') : '';
  968. return {
  969. type: 'literal',
  970. value: parseFloat(leadingDecimals + b.join(''))
  971. };
  972. },
  973. peg$c62 = function peg$c62(i) {
  974. return {
  975. type: 'literal',
  976. value: i
  977. };
  978. },
  979. peg$c63 = "type(",
  980. peg$c64 = peg$literalExpectation("type(", false),
  981. peg$c65 = /^[^ )]/,
  982. peg$c66 = peg$classExpectation([" ", ")"], true, false),
  983. peg$c67 = ")",
  984. peg$c68 = peg$literalExpectation(")", false),
  985. peg$c69 = function peg$c69(t) {
  986. return {
  987. type: 'type',
  988. value: t.join('')
  989. };
  990. },
  991. peg$c70 = /^[imsu]/,
  992. peg$c71 = peg$classExpectation(["i", "m", "s", "u"], false, false),
  993. peg$c72 = "/",
  994. peg$c73 = peg$literalExpectation("/", false),
  995. peg$c74 = /^[^\/]/,
  996. peg$c75 = peg$classExpectation(["/"], true, false),
  997. peg$c76 = function peg$c76(d, flgs) {
  998. return {
  999. type: 'regexp',
  1000. value: new RegExp(d.join(''), flgs ? flgs.join('') : '')
  1001. };
  1002. },
  1003. peg$c77 = function peg$c77(i, is) {
  1004. return {
  1005. type: 'field',
  1006. name: is.reduce(function (memo, p) {
  1007. return memo + p[0] + p[1];
  1008. }, i)
  1009. };
  1010. },
  1011. peg$c78 = ":not(",
  1012. peg$c79 = peg$literalExpectation(":not(", false),
  1013. peg$c80 = function peg$c80(ss) {
  1014. return {
  1015. type: 'not',
  1016. selectors: ss
  1017. };
  1018. },
  1019. peg$c81 = ":matches(",
  1020. peg$c82 = peg$literalExpectation(":matches(", false),
  1021. peg$c83 = function peg$c83(ss) {
  1022. return {
  1023. type: 'matches',
  1024. selectors: ss
  1025. };
  1026. },
  1027. peg$c84 = ":has(",
  1028. peg$c85 = peg$literalExpectation(":has(", false),
  1029. peg$c86 = function peg$c86(ss) {
  1030. return {
  1031. type: 'has',
  1032. selectors: ss
  1033. };
  1034. },
  1035. peg$c87 = ":first-child",
  1036. peg$c88 = peg$literalExpectation(":first-child", false),
  1037. peg$c89 = function peg$c89() {
  1038. return nth(1);
  1039. },
  1040. peg$c90 = ":last-child",
  1041. peg$c91 = peg$literalExpectation(":last-child", false),
  1042. peg$c92 = function peg$c92() {
  1043. return nthLast(1);
  1044. },
  1045. peg$c93 = ":nth-child(",
  1046. peg$c94 = peg$literalExpectation(":nth-child(", false),
  1047. peg$c95 = function peg$c95(n) {
  1048. return nth(parseInt(n.join(''), 10));
  1049. },
  1050. peg$c96 = ":nth-last-child(",
  1051. peg$c97 = peg$literalExpectation(":nth-last-child(", false),
  1052. peg$c98 = function peg$c98(n) {
  1053. return nthLast(parseInt(n.join(''), 10));
  1054. },
  1055. peg$c99 = ":",
  1056. peg$c100 = peg$literalExpectation(":", false),
  1057. peg$c101 = "statement",
  1058. peg$c102 = peg$literalExpectation("statement", true),
  1059. peg$c103 = "expression",
  1060. peg$c104 = peg$literalExpectation("expression", true),
  1061. peg$c105 = "declaration",
  1062. peg$c106 = peg$literalExpectation("declaration", true),
  1063. peg$c107 = "function",
  1064. peg$c108 = peg$literalExpectation("function", true),
  1065. peg$c109 = "pattern",
  1066. peg$c110 = peg$literalExpectation("pattern", true),
  1067. peg$c111 = function peg$c111(c) {
  1068. return {
  1069. type: 'class',
  1070. name: c
  1071. };
  1072. },
  1073. peg$currPos = 0,
  1074. peg$posDetailsCache = [{
  1075. line: 1,
  1076. column: 1
  1077. }],
  1078. peg$maxFailPos = 0,
  1079. peg$maxFailExpected = [],
  1080. peg$resultsCache = {},
  1081. peg$result;
  1082. if ("startRule" in options) {
  1083. if (!(options.startRule in peg$startRuleFunctions)) {
  1084. throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
  1085. }
  1086. peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
  1087. }
  1088. function peg$literalExpectation(text, ignoreCase) {
  1089. return {
  1090. type: "literal",
  1091. text: text,
  1092. ignoreCase: ignoreCase
  1093. };
  1094. }
  1095. function peg$classExpectation(parts, inverted, ignoreCase) {
  1096. return {
  1097. type: "class",
  1098. parts: parts,
  1099. inverted: inverted,
  1100. ignoreCase: ignoreCase
  1101. };
  1102. }
  1103. function peg$anyExpectation() {
  1104. return {
  1105. type: "any"
  1106. };
  1107. }
  1108. function peg$endExpectation() {
  1109. return {
  1110. type: "end"
  1111. };
  1112. }
  1113. function peg$computePosDetails(pos) {
  1114. var details = peg$posDetailsCache[pos],
  1115. p;
  1116. if (details) {
  1117. return details;
  1118. } else {
  1119. p = pos - 1;
  1120. while (!peg$posDetailsCache[p]) {
  1121. p--;
  1122. }
  1123. details = peg$posDetailsCache[p];
  1124. details = {
  1125. line: details.line,
  1126. column: details.column
  1127. };
  1128. while (p < pos) {
  1129. if (input.charCodeAt(p) === 10) {
  1130. details.line++;
  1131. details.column = 1;
  1132. } else {
  1133. details.column++;
  1134. }
  1135. p++;
  1136. }
  1137. peg$posDetailsCache[pos] = details;
  1138. return details;
  1139. }
  1140. }
  1141. function peg$computeLocation(startPos, endPos) {
  1142. var startPosDetails = peg$computePosDetails(startPos),
  1143. endPosDetails = peg$computePosDetails(endPos);
  1144. return {
  1145. start: {
  1146. offset: startPos,
  1147. line: startPosDetails.line,
  1148. column: startPosDetails.column
  1149. },
  1150. end: {
  1151. offset: endPos,
  1152. line: endPosDetails.line,
  1153. column: endPosDetails.column
  1154. }
  1155. };
  1156. }
  1157. function peg$fail(expected) {
  1158. if (peg$currPos < peg$maxFailPos) {
  1159. return;
  1160. }
  1161. if (peg$currPos > peg$maxFailPos) {
  1162. peg$maxFailPos = peg$currPos;
  1163. peg$maxFailExpected = [];
  1164. }
  1165. peg$maxFailExpected.push(expected);
  1166. }
  1167. function peg$buildStructuredError(expected, found, location) {
  1168. return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected, found), expected, found, location);
  1169. }
  1170. function peg$parsestart() {
  1171. var s0, s1, s2, s3;
  1172. var key = peg$currPos * 30 + 0,
  1173. cached = peg$resultsCache[key];
  1174. if (cached) {
  1175. peg$currPos = cached.nextPos;
  1176. return cached.result;
  1177. }
  1178. s0 = peg$currPos;
  1179. s1 = peg$parse_();
  1180. if (s1 !== peg$FAILED) {
  1181. s2 = peg$parseselectors();
  1182. if (s2 !== peg$FAILED) {
  1183. s3 = peg$parse_();
  1184. if (s3 !== peg$FAILED) {
  1185. s1 = peg$c0(s2);
  1186. s0 = s1;
  1187. } else {
  1188. peg$currPos = s0;
  1189. s0 = peg$FAILED;
  1190. }
  1191. } else {
  1192. peg$currPos = s0;
  1193. s0 = peg$FAILED;
  1194. }
  1195. } else {
  1196. peg$currPos = s0;
  1197. s0 = peg$FAILED;
  1198. }
  1199. if (s0 === peg$FAILED) {
  1200. s0 = peg$currPos;
  1201. s1 = peg$parse_();
  1202. if (s1 !== peg$FAILED) {
  1203. s1 = peg$c1();
  1204. }
  1205. s0 = s1;
  1206. }
  1207. peg$resultsCache[key] = {
  1208. nextPos: peg$currPos,
  1209. result: s0
  1210. };
  1211. return s0;
  1212. }
  1213. function peg$parse_() {
  1214. var s0, s1;
  1215. var key = peg$currPos * 30 + 1,
  1216. cached = peg$resultsCache[key];
  1217. if (cached) {
  1218. peg$currPos = cached.nextPos;
  1219. return cached.result;
  1220. }
  1221. s0 = [];
  1222. if (input.charCodeAt(peg$currPos) === 32) {
  1223. s1 = peg$c2;
  1224. peg$currPos++;
  1225. } else {
  1226. s1 = peg$FAILED;
  1227. {
  1228. peg$fail(peg$c3);
  1229. }
  1230. }
  1231. while (s1 !== peg$FAILED) {
  1232. s0.push(s1);
  1233. if (input.charCodeAt(peg$currPos) === 32) {
  1234. s1 = peg$c2;
  1235. peg$currPos++;
  1236. } else {
  1237. s1 = peg$FAILED;
  1238. {
  1239. peg$fail(peg$c3);
  1240. }
  1241. }
  1242. }
  1243. peg$resultsCache[key] = {
  1244. nextPos: peg$currPos,
  1245. result: s0
  1246. };
  1247. return s0;
  1248. }
  1249. function peg$parseidentifierName() {
  1250. var s0, s1, s2;
  1251. var key = peg$currPos * 30 + 2,
  1252. cached = peg$resultsCache[key];
  1253. if (cached) {
  1254. peg$currPos = cached.nextPos;
  1255. return cached.result;
  1256. }
  1257. s0 = peg$currPos;
  1258. s1 = [];
  1259. if (peg$c4.test(input.charAt(peg$currPos))) {
  1260. s2 = input.charAt(peg$currPos);
  1261. peg$currPos++;
  1262. } else {
  1263. s2 = peg$FAILED;
  1264. {
  1265. peg$fail(peg$c5);
  1266. }
  1267. }
  1268. if (s2 !== peg$FAILED) {
  1269. while (s2 !== peg$FAILED) {
  1270. s1.push(s2);
  1271. if (peg$c4.test(input.charAt(peg$currPos))) {
  1272. s2 = input.charAt(peg$currPos);
  1273. peg$currPos++;
  1274. } else {
  1275. s2 = peg$FAILED;
  1276. {
  1277. peg$fail(peg$c5);
  1278. }
  1279. }
  1280. }
  1281. } else {
  1282. s1 = peg$FAILED;
  1283. }
  1284. if (s1 !== peg$FAILED) {
  1285. s1 = peg$c6(s1);
  1286. }
  1287. s0 = s1;
  1288. peg$resultsCache[key] = {
  1289. nextPos: peg$currPos,
  1290. result: s0
  1291. };
  1292. return s0;
  1293. }
  1294. function peg$parsebinaryOp() {
  1295. var s0, s1, s2, s3;
  1296. var key = peg$currPos * 30 + 3,
  1297. cached = peg$resultsCache[key];
  1298. if (cached) {
  1299. peg$currPos = cached.nextPos;
  1300. return cached.result;
  1301. }
  1302. s0 = peg$currPos;
  1303. s1 = peg$parse_();
  1304. if (s1 !== peg$FAILED) {
  1305. if (input.charCodeAt(peg$currPos) === 62) {
  1306. s2 = peg$c7;
  1307. peg$currPos++;
  1308. } else {
  1309. s2 = peg$FAILED;
  1310. {
  1311. peg$fail(peg$c8);
  1312. }
  1313. }
  1314. if (s2 !== peg$FAILED) {
  1315. s3 = peg$parse_();
  1316. if (s3 !== peg$FAILED) {
  1317. s1 = peg$c9();
  1318. s0 = s1;
  1319. } else {
  1320. peg$currPos = s0;
  1321. s0 = peg$FAILED;
  1322. }
  1323. } else {
  1324. peg$currPos = s0;
  1325. s0 = peg$FAILED;
  1326. }
  1327. } else {
  1328. peg$currPos = s0;
  1329. s0 = peg$FAILED;
  1330. }
  1331. if (s0 === peg$FAILED) {
  1332. s0 = peg$currPos;
  1333. s1 = peg$parse_();
  1334. if (s1 !== peg$FAILED) {
  1335. if (input.charCodeAt(peg$currPos) === 126) {
  1336. s2 = peg$c10;
  1337. peg$currPos++;
  1338. } else {
  1339. s2 = peg$FAILED;
  1340. {
  1341. peg$fail(peg$c11);
  1342. }
  1343. }
  1344. if (s2 !== peg$FAILED) {
  1345. s3 = peg$parse_();
  1346. if (s3 !== peg$FAILED) {
  1347. s1 = peg$c12();
  1348. s0 = s1;
  1349. } else {
  1350. peg$currPos = s0;
  1351. s0 = peg$FAILED;
  1352. }
  1353. } else {
  1354. peg$currPos = s0;
  1355. s0 = peg$FAILED;
  1356. }
  1357. } else {
  1358. peg$currPos = s0;
  1359. s0 = peg$FAILED;
  1360. }
  1361. if (s0 === peg$FAILED) {
  1362. s0 = peg$currPos;
  1363. s1 = peg$parse_();
  1364. if (s1 !== peg$FAILED) {
  1365. if (input.charCodeAt(peg$currPos) === 43) {
  1366. s2 = peg$c13;
  1367. peg$currPos++;
  1368. } else {
  1369. s2 = peg$FAILED;
  1370. {
  1371. peg$fail(peg$c14);
  1372. }
  1373. }
  1374. if (s2 !== peg$FAILED) {
  1375. s3 = peg$parse_();
  1376. if (s3 !== peg$FAILED) {
  1377. s1 = peg$c15();
  1378. s0 = s1;
  1379. } else {
  1380. peg$currPos = s0;
  1381. s0 = peg$FAILED;
  1382. }
  1383. } else {
  1384. peg$currPos = s0;
  1385. s0 = peg$FAILED;
  1386. }
  1387. } else {
  1388. peg$currPos = s0;
  1389. s0 = peg$FAILED;
  1390. }
  1391. if (s0 === peg$FAILED) {
  1392. s0 = peg$currPos;
  1393. if (input.charCodeAt(peg$currPos) === 32) {
  1394. s1 = peg$c2;
  1395. peg$currPos++;
  1396. } else {
  1397. s1 = peg$FAILED;
  1398. {
  1399. peg$fail(peg$c3);
  1400. }
  1401. }
  1402. if (s1 !== peg$FAILED) {
  1403. s2 = peg$parse_();
  1404. if (s2 !== peg$FAILED) {
  1405. s1 = peg$c16();
  1406. s0 = s1;
  1407. } else {
  1408. peg$currPos = s0;
  1409. s0 = peg$FAILED;
  1410. }
  1411. } else {
  1412. peg$currPos = s0;
  1413. s0 = peg$FAILED;
  1414. }
  1415. }
  1416. }
  1417. }
  1418. peg$resultsCache[key] = {
  1419. nextPos: peg$currPos,
  1420. result: s0
  1421. };
  1422. return s0;
  1423. }
  1424. function peg$parseselectors() {
  1425. var s0, s1, s2, s3, s4, s5, s6, s7;
  1426. var key = peg$currPos * 30 + 4,
  1427. cached = peg$resultsCache[key];
  1428. if (cached) {
  1429. peg$currPos = cached.nextPos;
  1430. return cached.result;
  1431. }
  1432. s0 = peg$currPos;
  1433. s1 = peg$parseselector();
  1434. if (s1 !== peg$FAILED) {
  1435. s2 = [];
  1436. s3 = peg$currPos;
  1437. s4 = peg$parse_();
  1438. if (s4 !== peg$FAILED) {
  1439. if (input.charCodeAt(peg$currPos) === 44) {
  1440. s5 = peg$c17;
  1441. peg$currPos++;
  1442. } else {
  1443. s5 = peg$FAILED;
  1444. {
  1445. peg$fail(peg$c18);
  1446. }
  1447. }
  1448. if (s5 !== peg$FAILED) {
  1449. s6 = peg$parse_();
  1450. if (s6 !== peg$FAILED) {
  1451. s7 = peg$parseselector();
  1452. if (s7 !== peg$FAILED) {
  1453. s4 = [s4, s5, s6, s7];
  1454. s3 = s4;
  1455. } else {
  1456. peg$currPos = s3;
  1457. s3 = peg$FAILED;
  1458. }
  1459. } else {
  1460. peg$currPos = s3;
  1461. s3 = peg$FAILED;
  1462. }
  1463. } else {
  1464. peg$currPos = s3;
  1465. s3 = peg$FAILED;
  1466. }
  1467. } else {
  1468. peg$currPos = s3;
  1469. s3 = peg$FAILED;
  1470. }
  1471. while (s3 !== peg$FAILED) {
  1472. s2.push(s3);
  1473. s3 = peg$currPos;
  1474. s4 = peg$parse_();
  1475. if (s4 !== peg$FAILED) {
  1476. if (input.charCodeAt(peg$currPos) === 44) {
  1477. s5 = peg$c17;
  1478. peg$currPos++;
  1479. } else {
  1480. s5 = peg$FAILED;
  1481. {
  1482. peg$fail(peg$c18);
  1483. }
  1484. }
  1485. if (s5 !== peg$FAILED) {
  1486. s6 = peg$parse_();
  1487. if (s6 !== peg$FAILED) {
  1488. s7 = peg$parseselector();
  1489. if (s7 !== peg$FAILED) {
  1490. s4 = [s4, s5, s6, s7];
  1491. s3 = s4;
  1492. } else {
  1493. peg$currPos = s3;
  1494. s3 = peg$FAILED;
  1495. }
  1496. } else {
  1497. peg$currPos = s3;
  1498. s3 = peg$FAILED;
  1499. }
  1500. } else {
  1501. peg$currPos = s3;
  1502. s3 = peg$FAILED;
  1503. }
  1504. } else {
  1505. peg$currPos = s3;
  1506. s3 = peg$FAILED;
  1507. }
  1508. }
  1509. if (s2 !== peg$FAILED) {
  1510. s1 = peg$c19(s1, s2);
  1511. s0 = s1;
  1512. } else {
  1513. peg$currPos = s0;
  1514. s0 = peg$FAILED;
  1515. }
  1516. } else {
  1517. peg$currPos = s0;
  1518. s0 = peg$FAILED;
  1519. }
  1520. peg$resultsCache[key] = {
  1521. nextPos: peg$currPos,
  1522. result: s0
  1523. };
  1524. return s0;
  1525. }
  1526. function peg$parseselector() {
  1527. var s0, s1, s2, s3, s4, s5;
  1528. var key = peg$currPos * 30 + 5,
  1529. cached = peg$resultsCache[key];
  1530. if (cached) {
  1531. peg$currPos = cached.nextPos;
  1532. return cached.result;
  1533. }
  1534. s0 = peg$currPos;
  1535. s1 = peg$parsesequence();
  1536. if (s1 !== peg$FAILED) {
  1537. s2 = [];
  1538. s3 = peg$currPos;
  1539. s4 = peg$parsebinaryOp();
  1540. if (s4 !== peg$FAILED) {
  1541. s5 = peg$parsesequence();
  1542. if (s5 !== peg$FAILED) {
  1543. s4 = [s4, s5];
  1544. s3 = s4;
  1545. } else {
  1546. peg$currPos = s3;
  1547. s3 = peg$FAILED;
  1548. }
  1549. } else {
  1550. peg$currPos = s3;
  1551. s3 = peg$FAILED;
  1552. }
  1553. while (s3 !== peg$FAILED) {
  1554. s2.push(s3);
  1555. s3 = peg$currPos;
  1556. s4 = peg$parsebinaryOp();
  1557. if (s4 !== peg$FAILED) {
  1558. s5 = peg$parsesequence();
  1559. if (s5 !== peg$FAILED) {
  1560. s4 = [s4, s5];
  1561. s3 = s4;
  1562. } else {
  1563. peg$currPos = s3;
  1564. s3 = peg$FAILED;
  1565. }
  1566. } else {
  1567. peg$currPos = s3;
  1568. s3 = peg$FAILED;
  1569. }
  1570. }
  1571. if (s2 !== peg$FAILED) {
  1572. s1 = peg$c20(s1, s2);
  1573. s0 = s1;
  1574. } else {
  1575. peg$currPos = s0;
  1576. s0 = peg$FAILED;
  1577. }
  1578. } else {
  1579. peg$currPos = s0;
  1580. s0 = peg$FAILED;
  1581. }
  1582. peg$resultsCache[key] = {
  1583. nextPos: peg$currPos,
  1584. result: s0
  1585. };
  1586. return s0;
  1587. }
  1588. function peg$parsesequence() {
  1589. var s0, s1, s2, s3;
  1590. var key = peg$currPos * 30 + 6,
  1591. cached = peg$resultsCache[key];
  1592. if (cached) {
  1593. peg$currPos = cached.nextPos;
  1594. return cached.result;
  1595. }
  1596. s0 = peg$currPos;
  1597. if (input.charCodeAt(peg$currPos) === 33) {
  1598. s1 = peg$c21;
  1599. peg$currPos++;
  1600. } else {
  1601. s1 = peg$FAILED;
  1602. {
  1603. peg$fail(peg$c22);
  1604. }
  1605. }
  1606. if (s1 === peg$FAILED) {
  1607. s1 = null;
  1608. }
  1609. if (s1 !== peg$FAILED) {
  1610. s2 = [];
  1611. s3 = peg$parseatom();
  1612. if (s3 !== peg$FAILED) {
  1613. while (s3 !== peg$FAILED) {
  1614. s2.push(s3);
  1615. s3 = peg$parseatom();
  1616. }
  1617. } else {
  1618. s2 = peg$FAILED;
  1619. }
  1620. if (s2 !== peg$FAILED) {
  1621. s1 = peg$c23(s1, s2);
  1622. s0 = s1;
  1623. } else {
  1624. peg$currPos = s0;
  1625. s0 = peg$FAILED;
  1626. }
  1627. } else {
  1628. peg$currPos = s0;
  1629. s0 = peg$FAILED;
  1630. }
  1631. peg$resultsCache[key] = {
  1632. nextPos: peg$currPos,
  1633. result: s0
  1634. };
  1635. return s0;
  1636. }
  1637. function peg$parseatom() {
  1638. var s0;
  1639. var key = peg$currPos * 30 + 7,
  1640. cached = peg$resultsCache[key];
  1641. if (cached) {
  1642. peg$currPos = cached.nextPos;
  1643. return cached.result;
  1644. }
  1645. s0 = peg$parsewildcard();
  1646. if (s0 === peg$FAILED) {
  1647. s0 = peg$parseidentifier();
  1648. if (s0 === peg$FAILED) {
  1649. s0 = peg$parseattr();
  1650. if (s0 === peg$FAILED) {
  1651. s0 = peg$parsefield();
  1652. if (s0 === peg$FAILED) {
  1653. s0 = peg$parsenegation();
  1654. if (s0 === peg$FAILED) {
  1655. s0 = peg$parsematches();
  1656. if (s0 === peg$FAILED) {
  1657. s0 = peg$parsehas();
  1658. if (s0 === peg$FAILED) {
  1659. s0 = peg$parsefirstChild();
  1660. if (s0 === peg$FAILED) {
  1661. s0 = peg$parselastChild();
  1662. if (s0 === peg$FAILED) {
  1663. s0 = peg$parsenthChild();
  1664. if (s0 === peg$FAILED) {
  1665. s0 = peg$parsenthLastChild();
  1666. if (s0 === peg$FAILED) {
  1667. s0 = peg$parseclass();
  1668. }
  1669. }
  1670. }
  1671. }
  1672. }
  1673. }
  1674. }
  1675. }
  1676. }
  1677. }
  1678. }
  1679. peg$resultsCache[key] = {
  1680. nextPos: peg$currPos,
  1681. result: s0
  1682. };
  1683. return s0;
  1684. }
  1685. function peg$parsewildcard() {
  1686. var s0, s1;
  1687. var key = peg$currPos * 30 + 8,
  1688. cached = peg$resultsCache[key];
  1689. if (cached) {
  1690. peg$currPos = cached.nextPos;
  1691. return cached.result;
  1692. }
  1693. s0 = peg$currPos;
  1694. if (input.charCodeAt(peg$currPos) === 42) {
  1695. s1 = peg$c24;
  1696. peg$currPos++;
  1697. } else {
  1698. s1 = peg$FAILED;
  1699. {
  1700. peg$fail(peg$c25);
  1701. }
  1702. }
  1703. if (s1 !== peg$FAILED) {
  1704. s1 = peg$c26(s1);
  1705. }
  1706. s0 = s1;
  1707. peg$resultsCache[key] = {
  1708. nextPos: peg$currPos,
  1709. result: s0
  1710. };
  1711. return s0;
  1712. }
  1713. function peg$parseidentifier() {
  1714. var s0, s1, s2;
  1715. var key = peg$currPos * 30 + 9,
  1716. cached = peg$resultsCache[key];
  1717. if (cached) {
  1718. peg$currPos = cached.nextPos;
  1719. return cached.result;
  1720. }
  1721. s0 = peg$currPos;
  1722. if (input.charCodeAt(peg$currPos) === 35) {
  1723. s1 = peg$c27;
  1724. peg$currPos++;
  1725. } else {
  1726. s1 = peg$FAILED;
  1727. {
  1728. peg$fail(peg$c28);
  1729. }
  1730. }
  1731. if (s1 === peg$FAILED) {
  1732. s1 = null;
  1733. }
  1734. if (s1 !== peg$FAILED) {
  1735. s2 = peg$parseidentifierName();
  1736. if (s2 !== peg$FAILED) {
  1737. s1 = peg$c29(s2);
  1738. s0 = s1;
  1739. } else {
  1740. peg$currPos = s0;
  1741. s0 = peg$FAILED;
  1742. }
  1743. } else {
  1744. peg$currPos = s0;
  1745. s0 = peg$FAILED;
  1746. }
  1747. peg$resultsCache[key] = {
  1748. nextPos: peg$currPos,
  1749. result: s0
  1750. };
  1751. return s0;
  1752. }
  1753. function peg$parseattr() {
  1754. var s0, s1, s2, s3, s4, s5;
  1755. var key = peg$currPos * 30 + 10,
  1756. cached = peg$resultsCache[key];
  1757. if (cached) {
  1758. peg$currPos = cached.nextPos;
  1759. return cached.result;
  1760. }
  1761. s0 = peg$currPos;
  1762. if (input.charCodeAt(peg$currPos) === 91) {
  1763. s1 = peg$c30;
  1764. peg$currPos++;
  1765. } else {
  1766. s1 = peg$FAILED;
  1767. {
  1768. peg$fail(peg$c31);
  1769. }
  1770. }
  1771. if (s1 !== peg$FAILED) {
  1772. s2 = peg$parse_();
  1773. if (s2 !== peg$FAILED) {
  1774. s3 = peg$parseattrValue();
  1775. if (s3 !== peg$FAILED) {
  1776. s4 = peg$parse_();
  1777. if (s4 !== peg$FAILED) {
  1778. if (input.charCodeAt(peg$currPos) === 93) {
  1779. s5 = peg$c32;
  1780. peg$currPos++;
  1781. } else {
  1782. s5 = peg$FAILED;
  1783. {
  1784. peg$fail(peg$c33);
  1785. }
  1786. }
  1787. if (s5 !== peg$FAILED) {
  1788. s1 = peg$c34(s3);
  1789. s0 = s1;
  1790. } else {
  1791. peg$currPos = s0;
  1792. s0 = peg$FAILED;
  1793. }
  1794. } else {
  1795. peg$currPos = s0;
  1796. s0 = peg$FAILED;
  1797. }
  1798. } else {
  1799. peg$currPos = s0;
  1800. s0 = peg$FAILED;
  1801. }
  1802. } else {
  1803. peg$currPos = s0;
  1804. s0 = peg$FAILED;
  1805. }
  1806. } else {
  1807. peg$currPos = s0;
  1808. s0 = peg$FAILED;
  1809. }
  1810. peg$resultsCache[key] = {
  1811. nextPos: peg$currPos,
  1812. result: s0
  1813. };
  1814. return s0;
  1815. }
  1816. function peg$parseattrOps() {
  1817. var s0, s1, s2;
  1818. var key = peg$currPos * 30 + 11,
  1819. cached = peg$resultsCache[key];
  1820. if (cached) {
  1821. peg$currPos = cached.nextPos;
  1822. return cached.result;
  1823. }
  1824. s0 = peg$currPos;
  1825. if (peg$c35.test(input.charAt(peg$currPos))) {
  1826. s1 = input.charAt(peg$currPos);
  1827. peg$currPos++;
  1828. } else {
  1829. s1 = peg$FAILED;
  1830. {
  1831. peg$fail(peg$c36);
  1832. }
  1833. }
  1834. if (s1 === peg$FAILED) {
  1835. s1 = null;
  1836. }
  1837. if (s1 !== peg$FAILED) {
  1838. if (input.charCodeAt(peg$currPos) === 61) {
  1839. s2 = peg$c37;
  1840. peg$currPos++;
  1841. } else {
  1842. s2 = peg$FAILED;
  1843. {
  1844. peg$fail(peg$c38);
  1845. }
  1846. }
  1847. if (s2 !== peg$FAILED) {
  1848. s1 = peg$c39(s1);
  1849. s0 = s1;
  1850. } else {
  1851. peg$currPos = s0;
  1852. s0 = peg$FAILED;
  1853. }
  1854. } else {
  1855. peg$currPos = s0;
  1856. s0 = peg$FAILED;
  1857. }
  1858. if (s0 === peg$FAILED) {
  1859. if (peg$c40.test(input.charAt(peg$currPos))) {
  1860. s0 = input.charAt(peg$currPos);
  1861. peg$currPos++;
  1862. } else {
  1863. s0 = peg$FAILED;
  1864. {
  1865. peg$fail(peg$c41);
  1866. }
  1867. }
  1868. }
  1869. peg$resultsCache[key] = {
  1870. nextPos: peg$currPos,
  1871. result: s0
  1872. };
  1873. return s0;
  1874. }
  1875. function peg$parseattrEqOps() {
  1876. var s0, s1, s2;
  1877. var key = peg$currPos * 30 + 12,
  1878. cached = peg$resultsCache[key];
  1879. if (cached) {
  1880. peg$currPos = cached.nextPos;
  1881. return cached.result;
  1882. }
  1883. s0 = peg$currPos;
  1884. if (input.charCodeAt(peg$currPos) === 33) {
  1885. s1 = peg$c21;
  1886. peg$currPos++;
  1887. } else {
  1888. s1 = peg$FAILED;
  1889. {
  1890. peg$fail(peg$c22);
  1891. }
  1892. }
  1893. if (s1 === peg$FAILED) {
  1894. s1 = null;
  1895. }
  1896. if (s1 !== peg$FAILED) {
  1897. if (input.charCodeAt(peg$currPos) === 61) {
  1898. s2 = peg$c37;
  1899. peg$currPos++;
  1900. } else {
  1901. s2 = peg$FAILED;
  1902. {
  1903. peg$fail(peg$c38);
  1904. }
  1905. }
  1906. if (s2 !== peg$FAILED) {
  1907. s1 = peg$c39(s1);
  1908. s0 = s1;
  1909. } else {
  1910. peg$currPos = s0;
  1911. s0 = peg$FAILED;
  1912. }
  1913. } else {
  1914. peg$currPos = s0;
  1915. s0 = peg$FAILED;
  1916. }
  1917. peg$resultsCache[key] = {
  1918. nextPos: peg$currPos,
  1919. result: s0
  1920. };
  1921. return s0;
  1922. }
  1923. function peg$parseattrName() {
  1924. var s0, s1, s2;
  1925. var key = peg$currPos * 30 + 13,
  1926. cached = peg$resultsCache[key];
  1927. if (cached) {
  1928. peg$currPos = cached.nextPos;
  1929. return cached.result;
  1930. }
  1931. s0 = peg$currPos;
  1932. s1 = [];
  1933. s2 = peg$parseidentifierName();
  1934. if (s2 === peg$FAILED) {
  1935. if (input.charCodeAt(peg$currPos) === 46) {
  1936. s2 = peg$c42;
  1937. peg$currPos++;
  1938. } else {
  1939. s2 = peg$FAILED;
  1940. {
  1941. peg$fail(peg$c43);
  1942. }
  1943. }
  1944. }
  1945. if (s2 !== peg$FAILED) {
  1946. while (s2 !== peg$FAILED) {
  1947. s1.push(s2);
  1948. s2 = peg$parseidentifierName();
  1949. if (s2 === peg$FAILED) {
  1950. if (input.charCodeAt(peg$currPos) === 46) {
  1951. s2 = peg$c42;
  1952. peg$currPos++;
  1953. } else {
  1954. s2 = peg$FAILED;
  1955. {
  1956. peg$fail(peg$c43);
  1957. }
  1958. }
  1959. }
  1960. }
  1961. } else {
  1962. s1 = peg$FAILED;
  1963. }
  1964. if (s1 !== peg$FAILED) {
  1965. s1 = peg$c6(s1);
  1966. }
  1967. s0 = s1;
  1968. peg$resultsCache[key] = {
  1969. nextPos: peg$currPos,
  1970. result: s0
  1971. };
  1972. return s0;
  1973. }
  1974. function peg$parseattrValue() {
  1975. var s0, s1, s2, s3, s4, s5;
  1976. var key = peg$currPos * 30 + 14,
  1977. cached = peg$resultsCache[key];
  1978. if (cached) {
  1979. peg$currPos = cached.nextPos;
  1980. return cached.result;
  1981. }
  1982. s0 = peg$currPos;
  1983. s1 = peg$parseattrName();
  1984. if (s1 !== peg$FAILED) {
  1985. s2 = peg$parse_();
  1986. if (s2 !== peg$FAILED) {
  1987. s3 = peg$parseattrEqOps();
  1988. if (s3 !== peg$FAILED) {
  1989. s4 = peg$parse_();
  1990. if (s4 !== peg$FAILED) {
  1991. s5 = peg$parsetype();
  1992. if (s5 === peg$FAILED) {
  1993. s5 = peg$parseregex();
  1994. }
  1995. if (s5 !== peg$FAILED) {
  1996. s1 = peg$c44(s1, s3, s5);
  1997. s0 = s1;
  1998. } else {
  1999. peg$currPos = s0;
  2000. s0 = peg$FAILED;
  2001. }
  2002. } else {
  2003. peg$currPos = s0;
  2004. s0 = peg$FAILED;
  2005. }
  2006. } else {
  2007. peg$currPos = s0;
  2008. s0 = peg$FAILED;
  2009. }
  2010. } else {
  2011. peg$currPos = s0;
  2012. s0 = peg$FAILED;
  2013. }
  2014. } else {
  2015. peg$currPos = s0;
  2016. s0 = peg$FAILED;
  2017. }
  2018. if (s0 === peg$FAILED) {
  2019. s0 = peg$currPos;
  2020. s1 = peg$parseattrName();
  2021. if (s1 !== peg$FAILED) {
  2022. s2 = peg$parse_();
  2023. if (s2 !== peg$FAILED) {
  2024. s3 = peg$parseattrOps();
  2025. if (s3 !== peg$FAILED) {
  2026. s4 = peg$parse_();
  2027. if (s4 !== peg$FAILED) {
  2028. s5 = peg$parsestring();
  2029. if (s5 === peg$FAILED) {
  2030. s5 = peg$parsenumber();
  2031. if (s5 === peg$FAILED) {
  2032. s5 = peg$parsepath();
  2033. }
  2034. }
  2035. if (s5 !== peg$FAILED) {
  2036. s1 = peg$c44(s1, s3, s5);
  2037. s0 = s1;
  2038. } else {
  2039. peg$currPos = s0;
  2040. s0 = peg$FAILED;
  2041. }
  2042. } else {
  2043. peg$currPos = s0;
  2044. s0 = peg$FAILED;
  2045. }
  2046. } else {
  2047. peg$currPos = s0;
  2048. s0 = peg$FAILED;
  2049. }
  2050. } else {
  2051. peg$currPos = s0;
  2052. s0 = peg$FAILED;
  2053. }
  2054. } else {
  2055. peg$currPos = s0;
  2056. s0 = peg$FAILED;
  2057. }
  2058. if (s0 === peg$FAILED) {
  2059. s0 = peg$currPos;
  2060. s1 = peg$parseattrName();
  2061. if (s1 !== peg$FAILED) {
  2062. s1 = peg$c45(s1);
  2063. }
  2064. s0 = s1;
  2065. }
  2066. }
  2067. peg$resultsCache[key] = {
  2068. nextPos: peg$currPos,
  2069. result: s0
  2070. };
  2071. return s0;
  2072. }
  2073. function peg$parsestring() {
  2074. var s0, s1, s2, s3, s4, s5;
  2075. var key = peg$currPos * 30 + 15,
  2076. cached = peg$resultsCache[key];
  2077. if (cached) {
  2078. peg$currPos = cached.nextPos;
  2079. return cached.result;
  2080. }
  2081. s0 = peg$currPos;
  2082. if (input.charCodeAt(peg$currPos) === 34) {
  2083. s1 = peg$c46;
  2084. peg$currPos++;
  2085. } else {
  2086. s1 = peg$FAILED;
  2087. {
  2088. peg$fail(peg$c47);
  2089. }
  2090. }
  2091. if (s1 !== peg$FAILED) {
  2092. s2 = [];
  2093. if (peg$c48.test(input.charAt(peg$currPos))) {
  2094. s3 = input.charAt(peg$currPos);
  2095. peg$currPos++;
  2096. } else {
  2097. s3 = peg$FAILED;
  2098. {
  2099. peg$fail(peg$c49);
  2100. }
  2101. }
  2102. if (s3 === peg$FAILED) {
  2103. s3 = peg$currPos;
  2104. if (input.charCodeAt(peg$currPos) === 92) {
  2105. s4 = peg$c50;
  2106. peg$currPos++;
  2107. } else {
  2108. s4 = peg$FAILED;
  2109. {
  2110. peg$fail(peg$c51);
  2111. }
  2112. }
  2113. if (s4 !== peg$FAILED) {
  2114. if (input.length > peg$currPos) {
  2115. s5 = input.charAt(peg$currPos);
  2116. peg$currPos++;
  2117. } else {
  2118. s5 = peg$FAILED;
  2119. {
  2120. peg$fail(peg$c52);
  2121. }
  2122. }
  2123. if (s5 !== peg$FAILED) {
  2124. s4 = peg$c53(s4, s5);
  2125. s3 = s4;
  2126. } else {
  2127. peg$currPos = s3;
  2128. s3 = peg$FAILED;
  2129. }
  2130. } else {
  2131. peg$currPos = s3;
  2132. s3 = peg$FAILED;
  2133. }
  2134. }
  2135. while (s3 !== peg$FAILED) {
  2136. s2.push(s3);
  2137. if (peg$c48.test(input.charAt(peg$currPos))) {
  2138. s3 = input.charAt(peg$currPos);
  2139. peg$currPos++;
  2140. } else {
  2141. s3 = peg$FAILED;
  2142. {
  2143. peg$fail(peg$c49);
  2144. }
  2145. }
  2146. if (s3 === peg$FAILED) {
  2147. s3 = peg$currPos;
  2148. if (input.charCodeAt(peg$currPos) === 92) {
  2149. s4 = peg$c50;
  2150. peg$currPos++;
  2151. } else {
  2152. s4 = peg$FAILED;
  2153. {
  2154. peg$fail(peg$c51);
  2155. }
  2156. }
  2157. if (s4 !== peg$FAILED) {
  2158. if (input.length > peg$currPos) {
  2159. s5 = input.charAt(peg$currPos);
  2160. peg$currPos++;
  2161. } else {
  2162. s5 = peg$FAILED;
  2163. {
  2164. peg$fail(peg$c52);
  2165. }
  2166. }
  2167. if (s5 !== peg$FAILED) {
  2168. s4 = peg$c53(s4, s5);
  2169. s3 = s4;
  2170. } else {
  2171. peg$currPos = s3;
  2172. s3 = peg$FAILED;
  2173. }
  2174. } else {
  2175. peg$currPos = s3;
  2176. s3 = peg$FAILED;
  2177. }
  2178. }
  2179. }
  2180. if (s2 !== peg$FAILED) {
  2181. if (input.charCodeAt(peg$currPos) === 34) {
  2182. s3 = peg$c46;
  2183. peg$currPos++;
  2184. } else {
  2185. s3 = peg$FAILED;
  2186. {
  2187. peg$fail(peg$c47);
  2188. }
  2189. }
  2190. if (s3 !== peg$FAILED) {
  2191. s1 = peg$c54(s2);
  2192. s0 = s1;
  2193. } else {
  2194. peg$currPos = s0;
  2195. s0 = peg$FAILED;
  2196. }
  2197. } else {
  2198. peg$currPos = s0;
  2199. s0 = peg$FAILED;
  2200. }
  2201. } else {
  2202. peg$currPos = s0;
  2203. s0 = peg$FAILED;
  2204. }
  2205. if (s0 === peg$FAILED) {
  2206. s0 = peg$currPos;
  2207. if (input.charCodeAt(peg$currPos) === 39) {
  2208. s1 = peg$c55;
  2209. peg$currPos++;
  2210. } else {
  2211. s1 = peg$FAILED;
  2212. {
  2213. peg$fail(peg$c56);
  2214. }
  2215. }
  2216. if (s1 !== peg$FAILED) {
  2217. s2 = [];
  2218. if (peg$c57.test(input.charAt(peg$currPos))) {
  2219. s3 = input.charAt(peg$currPos);
  2220. peg$currPos++;
  2221. } else {
  2222. s3 = peg$FAILED;
  2223. {
  2224. peg$fail(peg$c58);
  2225. }
  2226. }
  2227. if (s3 === peg$FAILED) {
  2228. s3 = peg$currPos;
  2229. if (input.charCodeAt(peg$currPos) === 92) {
  2230. s4 = peg$c50;
  2231. peg$currPos++;
  2232. } else {
  2233. s4 = peg$FAILED;
  2234. {
  2235. peg$fail(peg$c51);
  2236. }
  2237. }
  2238. if (s4 !== peg$FAILED) {
  2239. if (input.length > peg$currPos) {
  2240. s5 = input.charAt(peg$currPos);
  2241. peg$currPos++;
  2242. } else {
  2243. s5 = peg$FAILED;
  2244. {
  2245. peg$fail(peg$c52);
  2246. }
  2247. }
  2248. if (s5 !== peg$FAILED) {
  2249. s4 = peg$c53(s4, s5);
  2250. s3 = s4;
  2251. } else {
  2252. peg$currPos = s3;
  2253. s3 = peg$FAILED;
  2254. }
  2255. } else {
  2256. peg$currPos = s3;
  2257. s3 = peg$FAILED;
  2258. }
  2259. }
  2260. while (s3 !== peg$FAILED) {
  2261. s2.push(s3);
  2262. if (peg$c57.test(input.charAt(peg$currPos))) {
  2263. s3 = input.charAt(peg$currPos);
  2264. peg$currPos++;
  2265. } else {
  2266. s3 = peg$FAILED;
  2267. {
  2268. peg$fail(peg$c58);
  2269. }
  2270. }
  2271. if (s3 === peg$FAILED) {
  2272. s3 = peg$currPos;
  2273. if (input.charCodeAt(peg$currPos) === 92) {
  2274. s4 = peg$c50;
  2275. peg$currPos++;
  2276. } else {
  2277. s4 = peg$FAILED;
  2278. {
  2279. peg$fail(peg$c51);
  2280. }
  2281. }
  2282. if (s4 !== peg$FAILED) {
  2283. if (input.length > peg$currPos) {
  2284. s5 = input.charAt(peg$currPos);
  2285. peg$currPos++;
  2286. } else {
  2287. s5 = peg$FAILED;
  2288. {
  2289. peg$fail(peg$c52);
  2290. }
  2291. }
  2292. if (s5 !== peg$FAILED) {
  2293. s4 = peg$c53(s4, s5);
  2294. s3 = s4;
  2295. } else {
  2296. peg$currPos = s3;
  2297. s3 = peg$FAILED;
  2298. }
  2299. } else {
  2300. peg$currPos = s3;
  2301. s3 = peg$FAILED;
  2302. }
  2303. }
  2304. }
  2305. if (s2 !== peg$FAILED) {
  2306. if (input.charCodeAt(peg$currPos) === 39) {
  2307. s3 = peg$c55;
  2308. peg$currPos++;
  2309. } else {
  2310. s3 = peg$FAILED;
  2311. {
  2312. peg$fail(peg$c56);
  2313. }
  2314. }
  2315. if (s3 !== peg$FAILED) {
  2316. s1 = peg$c54(s2);
  2317. s0 = s1;
  2318. } else {
  2319. peg$currPos = s0;
  2320. s0 = peg$FAILED;
  2321. }
  2322. } else {
  2323. peg$currPos = s0;
  2324. s0 = peg$FAILED;
  2325. }
  2326. } else {
  2327. peg$currPos = s0;
  2328. s0 = peg$FAILED;
  2329. }
  2330. }
  2331. peg$resultsCache[key] = {
  2332. nextPos: peg$currPos,
  2333. result: s0
  2334. };
  2335. return s0;
  2336. }
  2337. function peg$parsenumber() {
  2338. var s0, s1, s2, s3;
  2339. var key = peg$currPos * 30 + 16,
  2340. cached = peg$resultsCache[key];
  2341. if (cached) {
  2342. peg$currPos = cached.nextPos;
  2343. return cached.result;
  2344. }
  2345. s0 = peg$currPos;
  2346. s1 = peg$currPos;
  2347. s2 = [];
  2348. if (peg$c59.test(input.charAt(peg$currPos))) {
  2349. s3 = input.charAt(peg$currPos);
  2350. peg$currPos++;
  2351. } else {
  2352. s3 = peg$FAILED;
  2353. {
  2354. peg$fail(peg$c60);
  2355. }
  2356. }
  2357. while (s3 !== peg$FAILED) {
  2358. s2.push(s3);
  2359. if (peg$c59.test(input.charAt(peg$currPos))) {
  2360. s3 = input.charAt(peg$currPos);
  2361. peg$currPos++;
  2362. } else {
  2363. s3 = peg$FAILED;
  2364. {
  2365. peg$fail(peg$c60);
  2366. }
  2367. }
  2368. }
  2369. if (s2 !== peg$FAILED) {
  2370. if (input.charCodeAt(peg$currPos) === 46) {
  2371. s3 = peg$c42;
  2372. peg$currPos++;
  2373. } else {
  2374. s3 = peg$FAILED;
  2375. {
  2376. peg$fail(peg$c43);
  2377. }
  2378. }
  2379. if (s3 !== peg$FAILED) {
  2380. s2 = [s2, s3];
  2381. s1 = s2;
  2382. } else {
  2383. peg$currPos = s1;
  2384. s1 = peg$FAILED;
  2385. }
  2386. } else {
  2387. peg$currPos = s1;
  2388. s1 = peg$FAILED;
  2389. }
  2390. if (s1 === peg$FAILED) {
  2391. s1 = null;
  2392. }
  2393. if (s1 !== peg$FAILED) {
  2394. s2 = [];
  2395. if (peg$c59.test(input.charAt(peg$currPos))) {
  2396. s3 = input.charAt(peg$currPos);
  2397. peg$currPos++;
  2398. } else {
  2399. s3 = peg$FAILED;
  2400. {
  2401. peg$fail(peg$c60);
  2402. }
  2403. }
  2404. if (s3 !== peg$FAILED) {
  2405. while (s3 !== peg$FAILED) {
  2406. s2.push(s3);
  2407. if (peg$c59.test(input.charAt(peg$currPos))) {
  2408. s3 = input.charAt(peg$currPos);
  2409. peg$currPos++;
  2410. } else {
  2411. s3 = peg$FAILED;
  2412. {
  2413. peg$fail(peg$c60);
  2414. }
  2415. }
  2416. }
  2417. } else {
  2418. s2 = peg$FAILED;
  2419. }
  2420. if (s2 !== peg$FAILED) {
  2421. s1 = peg$c61(s1, s2);
  2422. s0 = s1;
  2423. } else {
  2424. peg$currPos = s0;
  2425. s0 = peg$FAILED;
  2426. }
  2427. } else {
  2428. peg$currPos = s0;
  2429. s0 = peg$FAILED;
  2430. }
  2431. peg$resultsCache[key] = {
  2432. nextPos: peg$currPos,
  2433. result: s0
  2434. };
  2435. return s0;
  2436. }
  2437. function peg$parsepath() {
  2438. var s0, s1;
  2439. var key = peg$currPos * 30 + 17,
  2440. cached = peg$resultsCache[key];
  2441. if (cached) {
  2442. peg$currPos = cached.nextPos;
  2443. return cached.result;
  2444. }
  2445. s0 = peg$currPos;
  2446. s1 = peg$parseidentifierName();
  2447. if (s1 !== peg$FAILED) {
  2448. s1 = peg$c62(s1);
  2449. }
  2450. s0 = s1;
  2451. peg$resultsCache[key] = {
  2452. nextPos: peg$currPos,
  2453. result: s0
  2454. };
  2455. return s0;
  2456. }
  2457. function peg$parsetype() {
  2458. var s0, s1, s2, s3, s4, s5;
  2459. var key = peg$currPos * 30 + 18,
  2460. cached = peg$resultsCache[key];
  2461. if (cached) {
  2462. peg$currPos = cached.nextPos;
  2463. return cached.result;
  2464. }
  2465. s0 = peg$currPos;
  2466. if (input.substr(peg$currPos, 5) === peg$c63) {
  2467. s1 = peg$c63;
  2468. peg$currPos += 5;
  2469. } else {
  2470. s1 = peg$FAILED;
  2471. {
  2472. peg$fail(peg$c64);
  2473. }
  2474. }
  2475. if (s1 !== peg$FAILED) {
  2476. s2 = peg$parse_();
  2477. if (s2 !== peg$FAILED) {
  2478. s3 = [];
  2479. if (peg$c65.test(input.charAt(peg$currPos))) {
  2480. s4 = input.charAt(peg$currPos);
  2481. peg$currPos++;
  2482. } else {
  2483. s4 = peg$FAILED;
  2484. {
  2485. peg$fail(peg$c66);
  2486. }
  2487. }
  2488. if (s4 !== peg$FAILED) {
  2489. while (s4 !== peg$FAILED) {
  2490. s3.push(s4);
  2491. if (peg$c65.test(input.charAt(peg$currPos))) {
  2492. s4 = input.charAt(peg$currPos);
  2493. peg$currPos++;
  2494. } else {
  2495. s4 = peg$FAILED;
  2496. {
  2497. peg$fail(peg$c66);
  2498. }
  2499. }
  2500. }
  2501. } else {
  2502. s3 = peg$FAILED;
  2503. }
  2504. if (s3 !== peg$FAILED) {
  2505. s4 = peg$parse_();
  2506. if (s4 !== peg$FAILED) {
  2507. if (input.charCodeAt(peg$currPos) === 41) {
  2508. s5 = peg$c67;
  2509. peg$currPos++;
  2510. } else {
  2511. s5 = peg$FAILED;
  2512. {
  2513. peg$fail(peg$c68);
  2514. }
  2515. }
  2516. if (s5 !== peg$FAILED) {
  2517. s1 = peg$c69(s3);
  2518. s0 = s1;
  2519. } else {
  2520. peg$currPos = s0;
  2521. s0 = peg$FAILED;
  2522. }
  2523. } else {
  2524. peg$currPos = s0;
  2525. s0 = peg$FAILED;
  2526. }
  2527. } else {
  2528. peg$currPos = s0;
  2529. s0 = peg$FAILED;
  2530. }
  2531. } else {
  2532. peg$currPos = s0;
  2533. s0 = peg$FAILED;
  2534. }
  2535. } else {
  2536. peg$currPos = s0;
  2537. s0 = peg$FAILED;
  2538. }
  2539. peg$resultsCache[key] = {
  2540. nextPos: peg$currPos,
  2541. result: s0
  2542. };
  2543. return s0;
  2544. }
  2545. function peg$parseflags() {
  2546. var s0, s1;
  2547. var key = peg$currPos * 30 + 19,
  2548. cached = peg$resultsCache[key];
  2549. if (cached) {
  2550. peg$currPos = cached.nextPos;
  2551. return cached.result;
  2552. }
  2553. s0 = [];
  2554. if (peg$c70.test(input.charAt(peg$currPos))) {
  2555. s1 = input.charAt(peg$currPos);
  2556. peg$currPos++;
  2557. } else {
  2558. s1 = peg$FAILED;
  2559. {
  2560. peg$fail(peg$c71);
  2561. }
  2562. }
  2563. if (s1 !== peg$FAILED) {
  2564. while (s1 !== peg$FAILED) {
  2565. s0.push(s1);
  2566. if (peg$c70.test(input.charAt(peg$currPos))) {
  2567. s1 = input.charAt(peg$currPos);
  2568. peg$currPos++;
  2569. } else {
  2570. s1 = peg$FAILED;
  2571. {
  2572. peg$fail(peg$c71);
  2573. }
  2574. }
  2575. }
  2576. } else {
  2577. s0 = peg$FAILED;
  2578. }
  2579. peg$resultsCache[key] = {
  2580. nextPos: peg$currPos,
  2581. result: s0
  2582. };
  2583. return s0;
  2584. }
  2585. function peg$parseregex() {
  2586. var s0, s1, s2, s3, s4;
  2587. var key = peg$currPos * 30 + 20,
  2588. cached = peg$resultsCache[key];
  2589. if (cached) {
  2590. peg$currPos = cached.nextPos;
  2591. return cached.result;
  2592. }
  2593. s0 = peg$currPos;
  2594. if (input.charCodeAt(peg$currPos) === 47) {
  2595. s1 = peg$c72;
  2596. peg$currPos++;
  2597. } else {
  2598. s1 = peg$FAILED;
  2599. {
  2600. peg$fail(peg$c73);
  2601. }
  2602. }
  2603. if (s1 !== peg$FAILED) {
  2604. s2 = [];
  2605. if (peg$c74.test(input.charAt(peg$currPos))) {
  2606. s3 = input.charAt(peg$currPos);
  2607. peg$currPos++;
  2608. } else {
  2609. s3 = peg$FAILED;
  2610. {
  2611. peg$fail(peg$c75);
  2612. }
  2613. }
  2614. if (s3 !== peg$FAILED) {
  2615. while (s3 !== peg$FAILED) {
  2616. s2.push(s3);
  2617. if (peg$c74.test(input.charAt(peg$currPos))) {
  2618. s3 = input.charAt(peg$currPos);
  2619. peg$currPos++;
  2620. } else {
  2621. s3 = peg$FAILED;
  2622. {
  2623. peg$fail(peg$c75);
  2624. }
  2625. }
  2626. }
  2627. } else {
  2628. s2 = peg$FAILED;
  2629. }
  2630. if (s2 !== peg$FAILED) {
  2631. if (input.charCodeAt(peg$currPos) === 47) {
  2632. s3 = peg$c72;
  2633. peg$currPos++;
  2634. } else {
  2635. s3 = peg$FAILED;
  2636. {
  2637. peg$fail(peg$c73);
  2638. }
  2639. }
  2640. if (s3 !== peg$FAILED) {
  2641. s4 = peg$parseflags();
  2642. if (s4 === peg$FAILED) {
  2643. s4 = null;
  2644. }
  2645. if (s4 !== peg$FAILED) {
  2646. s1 = peg$c76(s2, s4);
  2647. s0 = s1;
  2648. } else {
  2649. peg$currPos = s0;
  2650. s0 = peg$FAILED;
  2651. }
  2652. } else {
  2653. peg$currPos = s0;
  2654. s0 = peg$FAILED;
  2655. }
  2656. } else {
  2657. peg$currPos = s0;
  2658. s0 = peg$FAILED;
  2659. }
  2660. } else {
  2661. peg$currPos = s0;
  2662. s0 = peg$FAILED;
  2663. }
  2664. peg$resultsCache[key] = {
  2665. nextPos: peg$currPos,
  2666. result: s0
  2667. };
  2668. return s0;
  2669. }
  2670. function peg$parsefield() {
  2671. var s0, s1, s2, s3, s4, s5, s6;
  2672. var key = peg$currPos * 30 + 21,
  2673. cached = peg$resultsCache[key];
  2674. if (cached) {
  2675. peg$currPos = cached.nextPos;
  2676. return cached.result;
  2677. }
  2678. s0 = peg$currPos;
  2679. if (input.charCodeAt(peg$currPos) === 46) {
  2680. s1 = peg$c42;
  2681. peg$currPos++;
  2682. } else {
  2683. s1 = peg$FAILED;
  2684. {
  2685. peg$fail(peg$c43);
  2686. }
  2687. }
  2688. if (s1 !== peg$FAILED) {
  2689. s2 = peg$parseidentifierName();
  2690. if (s2 !== peg$FAILED) {
  2691. s3 = [];
  2692. s4 = peg$currPos;
  2693. if (input.charCodeAt(peg$currPos) === 46) {
  2694. s5 = peg$c42;
  2695. peg$currPos++;
  2696. } else {
  2697. s5 = peg$FAILED;
  2698. {
  2699. peg$fail(peg$c43);
  2700. }
  2701. }
  2702. if (s5 !== peg$FAILED) {
  2703. s6 = peg$parseidentifierName();
  2704. if (s6 !== peg$FAILED) {
  2705. s5 = [s5, s6];
  2706. s4 = s5;
  2707. } else {
  2708. peg$currPos = s4;
  2709. s4 = peg$FAILED;
  2710. }
  2711. } else {
  2712. peg$currPos = s4;
  2713. s4 = peg$FAILED;
  2714. }
  2715. while (s4 !== peg$FAILED) {
  2716. s3.push(s4);
  2717. s4 = peg$currPos;
  2718. if (input.charCodeAt(peg$currPos) === 46) {
  2719. s5 = peg$c42;
  2720. peg$currPos++;
  2721. } else {
  2722. s5 = peg$FAILED;
  2723. {
  2724. peg$fail(peg$c43);
  2725. }
  2726. }
  2727. if (s5 !== peg$FAILED) {
  2728. s6 = peg$parseidentifierName();
  2729. if (s6 !== peg$FAILED) {
  2730. s5 = [s5, s6];
  2731. s4 = s5;
  2732. } else {
  2733. peg$currPos = s4;
  2734. s4 = peg$FAILED;
  2735. }
  2736. } else {
  2737. peg$currPos = s4;
  2738. s4 = peg$FAILED;
  2739. }
  2740. }
  2741. if (s3 !== peg$FAILED) {
  2742. s1 = peg$c77(s2, s3);
  2743. s0 = s1;
  2744. } else {
  2745. peg$currPos = s0;
  2746. s0 = peg$FAILED;
  2747. }
  2748. } else {
  2749. peg$currPos = s0;
  2750. s0 = peg$FAILED;
  2751. }
  2752. } else {
  2753. peg$currPos = s0;
  2754. s0 = peg$FAILED;
  2755. }
  2756. peg$resultsCache[key] = {
  2757. nextPos: peg$currPos,
  2758. result: s0
  2759. };
  2760. return s0;
  2761. }
  2762. function peg$parsenegation() {
  2763. var s0, s1, s2, s3, s4, s5;
  2764. var key = peg$currPos * 30 + 22,
  2765. cached = peg$resultsCache[key];
  2766. if (cached) {
  2767. peg$currPos = cached.nextPos;
  2768. return cached.result;
  2769. }
  2770. s0 = peg$currPos;
  2771. if (input.substr(peg$currPos, 5) === peg$c78) {
  2772. s1 = peg$c78;
  2773. peg$currPos += 5;
  2774. } else {
  2775. s1 = peg$FAILED;
  2776. {
  2777. peg$fail(peg$c79);
  2778. }
  2779. }
  2780. if (s1 !== peg$FAILED) {
  2781. s2 = peg$parse_();
  2782. if (s2 !== peg$FAILED) {
  2783. s3 = peg$parseselectors();
  2784. if (s3 !== peg$FAILED) {
  2785. s4 = peg$parse_();
  2786. if (s4 !== peg$FAILED) {
  2787. if (input.charCodeAt(peg$currPos) === 41) {
  2788. s5 = peg$c67;
  2789. peg$currPos++;
  2790. } else {
  2791. s5 = peg$FAILED;
  2792. {
  2793. peg$fail(peg$c68);
  2794. }
  2795. }
  2796. if (s5 !== peg$FAILED) {
  2797. s1 = peg$c80(s3);
  2798. s0 = s1;
  2799. } else {
  2800. peg$currPos = s0;
  2801. s0 = peg$FAILED;
  2802. }
  2803. } else {
  2804. peg$currPos = s0;
  2805. s0 = peg$FAILED;
  2806. }
  2807. } else {
  2808. peg$currPos = s0;
  2809. s0 = peg$FAILED;
  2810. }
  2811. } else {
  2812. peg$currPos = s0;
  2813. s0 = peg$FAILED;
  2814. }
  2815. } else {
  2816. peg$currPos = s0;
  2817. s0 = peg$FAILED;
  2818. }
  2819. peg$resultsCache[key] = {
  2820. nextPos: peg$currPos,
  2821. result: s0
  2822. };
  2823. return s0;
  2824. }
  2825. function peg$parsematches() {
  2826. var s0, s1, s2, s3, s4, s5;
  2827. var key = peg$currPos * 30 + 23,
  2828. cached = peg$resultsCache[key];
  2829. if (cached) {
  2830. peg$currPos = cached.nextPos;
  2831. return cached.result;
  2832. }
  2833. s0 = peg$currPos;
  2834. if (input.substr(peg$currPos, 9) === peg$c81) {
  2835. s1 = peg$c81;
  2836. peg$currPos += 9;
  2837. } else {
  2838. s1 = peg$FAILED;
  2839. {
  2840. peg$fail(peg$c82);
  2841. }
  2842. }
  2843. if (s1 !== peg$FAILED) {
  2844. s2 = peg$parse_();
  2845. if (s2 !== peg$FAILED) {
  2846. s3 = peg$parseselectors();
  2847. if (s3 !== peg$FAILED) {
  2848. s4 = peg$parse_();
  2849. if (s4 !== peg$FAILED) {
  2850. if (input.charCodeAt(peg$currPos) === 41) {
  2851. s5 = peg$c67;
  2852. peg$currPos++;
  2853. } else {
  2854. s5 = peg$FAILED;
  2855. {
  2856. peg$fail(peg$c68);
  2857. }
  2858. }
  2859. if (s5 !== peg$FAILED) {
  2860. s1 = peg$c83(s3);
  2861. s0 = s1;
  2862. } else {
  2863. peg$currPos = s0;
  2864. s0 = peg$FAILED;
  2865. }
  2866. } else {
  2867. peg$currPos = s0;
  2868. s0 = peg$FAILED;
  2869. }
  2870. } else {
  2871. peg$currPos = s0;
  2872. s0 = peg$FAILED;
  2873. }
  2874. } else {
  2875. peg$currPos = s0;
  2876. s0 = peg$FAILED;
  2877. }
  2878. } else {
  2879. peg$currPos = s0;
  2880. s0 = peg$FAILED;
  2881. }
  2882. peg$resultsCache[key] = {
  2883. nextPos: peg$currPos,
  2884. result: s0
  2885. };
  2886. return s0;
  2887. }
  2888. function peg$parsehas() {
  2889. var s0, s1, s2, s3, s4, s5;
  2890. var key = peg$currPos * 30 + 24,
  2891. cached = peg$resultsCache[key];
  2892. if (cached) {
  2893. peg$currPos = cached.nextPos;
  2894. return cached.result;
  2895. }
  2896. s0 = peg$currPos;
  2897. if (input.substr(peg$currPos, 5) === peg$c84) {
  2898. s1 = peg$c84;
  2899. peg$currPos += 5;
  2900. } else {
  2901. s1 = peg$FAILED;
  2902. {
  2903. peg$fail(peg$c85);
  2904. }
  2905. }
  2906. if (s1 !== peg$FAILED) {
  2907. s2 = peg$parse_();
  2908. if (s2 !== peg$FAILED) {
  2909. s3 = peg$parseselectors();
  2910. if (s3 !== peg$FAILED) {
  2911. s4 = peg$parse_();
  2912. if (s4 !== peg$FAILED) {
  2913. if (input.charCodeAt(peg$currPos) === 41) {
  2914. s5 = peg$c67;
  2915. peg$currPos++;
  2916. } else {
  2917. s5 = peg$FAILED;
  2918. {
  2919. peg$fail(peg$c68);
  2920. }
  2921. }
  2922. if (s5 !== peg$FAILED) {
  2923. s1 = peg$c86(s3);
  2924. s0 = s1;
  2925. } else {
  2926. peg$currPos = s0;
  2927. s0 = peg$FAILED;
  2928. }
  2929. } else {
  2930. peg$currPos = s0;
  2931. s0 = peg$FAILED;
  2932. }
  2933. } else {
  2934. peg$currPos = s0;
  2935. s0 = peg$FAILED;
  2936. }
  2937. } else {
  2938. peg$currPos = s0;
  2939. s0 = peg$FAILED;
  2940. }
  2941. } else {
  2942. peg$currPos = s0;
  2943. s0 = peg$FAILED;
  2944. }
  2945. peg$resultsCache[key] = {
  2946. nextPos: peg$currPos,
  2947. result: s0
  2948. };
  2949. return s0;
  2950. }
  2951. function peg$parsefirstChild() {
  2952. var s0, s1;
  2953. var key = peg$currPos * 30 + 25,
  2954. cached = peg$resultsCache[key];
  2955. if (cached) {
  2956. peg$currPos = cached.nextPos;
  2957. return cached.result;
  2958. }
  2959. s0 = peg$currPos;
  2960. if (input.substr(peg$currPos, 12) === peg$c87) {
  2961. s1 = peg$c87;
  2962. peg$currPos += 12;
  2963. } else {
  2964. s1 = peg$FAILED;
  2965. {
  2966. peg$fail(peg$c88);
  2967. }
  2968. }
  2969. if (s1 !== peg$FAILED) {
  2970. s1 = peg$c89();
  2971. }
  2972. s0 = s1;
  2973. peg$resultsCache[key] = {
  2974. nextPos: peg$currPos,
  2975. result: s0
  2976. };
  2977. return s0;
  2978. }
  2979. function peg$parselastChild() {
  2980. var s0, s1;
  2981. var key = peg$currPos * 30 + 26,
  2982. cached = peg$resultsCache[key];
  2983. if (cached) {
  2984. peg$currPos = cached.nextPos;
  2985. return cached.result;
  2986. }
  2987. s0 = peg$currPos;
  2988. if (input.substr(peg$currPos, 11) === peg$c90) {
  2989. s1 = peg$c90;
  2990. peg$currPos += 11;
  2991. } else {
  2992. s1 = peg$FAILED;
  2993. {
  2994. peg$fail(peg$c91);
  2995. }
  2996. }
  2997. if (s1 !== peg$FAILED) {
  2998. s1 = peg$c92();
  2999. }
  3000. s0 = s1;
  3001. peg$resultsCache[key] = {
  3002. nextPos: peg$currPos,
  3003. result: s0
  3004. };
  3005. return s0;
  3006. }
  3007. function peg$parsenthChild() {
  3008. var s0, s1, s2, s3, s4, s5;
  3009. var key = peg$currPos * 30 + 27,
  3010. cached = peg$resultsCache[key];
  3011. if (cached) {
  3012. peg$currPos = cached.nextPos;
  3013. return cached.result;
  3014. }
  3015. s0 = peg$currPos;
  3016. if (input.substr(peg$currPos, 11) === peg$c93) {
  3017. s1 = peg$c93;
  3018. peg$currPos += 11;
  3019. } else {
  3020. s1 = peg$FAILED;
  3021. {
  3022. peg$fail(peg$c94);
  3023. }
  3024. }
  3025. if (s1 !== peg$FAILED) {
  3026. s2 = peg$parse_();
  3027. if (s2 !== peg$FAILED) {
  3028. s3 = [];
  3029. if (peg$c59.test(input.charAt(peg$currPos))) {
  3030. s4 = input.charAt(peg$currPos);
  3031. peg$currPos++;
  3032. } else {
  3033. s4 = peg$FAILED;
  3034. {
  3035. peg$fail(peg$c60);
  3036. }
  3037. }
  3038. if (s4 !== peg$FAILED) {
  3039. while (s4 !== peg$FAILED) {
  3040. s3.push(s4);
  3041. if (peg$c59.test(input.charAt(peg$currPos))) {
  3042. s4 = input.charAt(peg$currPos);
  3043. peg$currPos++;
  3044. } else {
  3045. s4 = peg$FAILED;
  3046. {
  3047. peg$fail(peg$c60);
  3048. }
  3049. }
  3050. }
  3051. } else {
  3052. s3 = peg$FAILED;
  3053. }
  3054. if (s3 !== peg$FAILED) {
  3055. s4 = peg$parse_();
  3056. if (s4 !== peg$FAILED) {
  3057. if (input.charCodeAt(peg$currPos) === 41) {
  3058. s5 = peg$c67;
  3059. peg$currPos++;
  3060. } else {
  3061. s5 = peg$FAILED;
  3062. {
  3063. peg$fail(peg$c68);
  3064. }
  3065. }
  3066. if (s5 !== peg$FAILED) {
  3067. s1 = peg$c95(s3);
  3068. s0 = s1;
  3069. } else {
  3070. peg$currPos = s0;
  3071. s0 = peg$FAILED;
  3072. }
  3073. } else {
  3074. peg$currPos = s0;
  3075. s0 = peg$FAILED;
  3076. }
  3077. } else {
  3078. peg$currPos = s0;
  3079. s0 = peg$FAILED;
  3080. }
  3081. } else {
  3082. peg$currPos = s0;
  3083. s0 = peg$FAILED;
  3084. }
  3085. } else {
  3086. peg$currPos = s0;
  3087. s0 = peg$FAILED;
  3088. }
  3089. peg$resultsCache[key] = {
  3090. nextPos: peg$currPos,
  3091. result: s0
  3092. };
  3093. return s0;
  3094. }
  3095. function peg$parsenthLastChild() {
  3096. var s0, s1, s2, s3, s4, s5;
  3097. var key = peg$currPos * 30 + 28,
  3098. cached = peg$resultsCache[key];
  3099. if (cached) {
  3100. peg$currPos = cached.nextPos;
  3101. return cached.result;
  3102. }
  3103. s0 = peg$currPos;
  3104. if (input.substr(peg$currPos, 16) === peg$c96) {
  3105. s1 = peg$c96;
  3106. peg$currPos += 16;
  3107. } else {
  3108. s1 = peg$FAILED;
  3109. {
  3110. peg$fail(peg$c97);
  3111. }
  3112. }
  3113. if (s1 !== peg$FAILED) {
  3114. s2 = peg$parse_();
  3115. if (s2 !== peg$FAILED) {
  3116. s3 = [];
  3117. if (peg$c59.test(input.charAt(peg$currPos))) {
  3118. s4 = input.charAt(peg$currPos);
  3119. peg$currPos++;
  3120. } else {
  3121. s4 = peg$FAILED;
  3122. {
  3123. peg$fail(peg$c60);
  3124. }
  3125. }
  3126. if (s4 !== peg$FAILED) {
  3127. while (s4 !== peg$FAILED) {
  3128. s3.push(s4);
  3129. if (peg$c59.test(input.charAt(peg$currPos))) {
  3130. s4 = input.charAt(peg$currPos);
  3131. peg$currPos++;
  3132. } else {
  3133. s4 = peg$FAILED;
  3134. {
  3135. peg$fail(peg$c60);
  3136. }
  3137. }
  3138. }
  3139. } else {
  3140. s3 = peg$FAILED;
  3141. }
  3142. if (s3 !== peg$FAILED) {
  3143. s4 = peg$parse_();
  3144. if (s4 !== peg$FAILED) {
  3145. if (input.charCodeAt(peg$currPos) === 41) {
  3146. s5 = peg$c67;
  3147. peg$currPos++;
  3148. } else {
  3149. s5 = peg$FAILED;
  3150. {
  3151. peg$fail(peg$c68);
  3152. }
  3153. }
  3154. if (s5 !== peg$FAILED) {
  3155. s1 = peg$c98(s3);
  3156. s0 = s1;
  3157. } else {
  3158. peg$currPos = s0;
  3159. s0 = peg$FAILED;
  3160. }
  3161. } else {
  3162. peg$currPos = s0;
  3163. s0 = peg$FAILED;
  3164. }
  3165. } else {
  3166. peg$currPos = s0;
  3167. s0 = peg$FAILED;
  3168. }
  3169. } else {
  3170. peg$currPos = s0;
  3171. s0 = peg$FAILED;
  3172. }
  3173. } else {
  3174. peg$currPos = s0;
  3175. s0 = peg$FAILED;
  3176. }
  3177. peg$resultsCache[key] = {
  3178. nextPos: peg$currPos,
  3179. result: s0
  3180. };
  3181. return s0;
  3182. }
  3183. function peg$parseclass() {
  3184. var s0, s1, s2;
  3185. var key = peg$currPos * 30 + 29,
  3186. cached = peg$resultsCache[key];
  3187. if (cached) {
  3188. peg$currPos = cached.nextPos;
  3189. return cached.result;
  3190. }
  3191. s0 = peg$currPos;
  3192. if (input.charCodeAt(peg$currPos) === 58) {
  3193. s1 = peg$c99;
  3194. peg$currPos++;
  3195. } else {
  3196. s1 = peg$FAILED;
  3197. {
  3198. peg$fail(peg$c100);
  3199. }
  3200. }
  3201. if (s1 !== peg$FAILED) {
  3202. if (input.substr(peg$currPos, 9).toLowerCase() === peg$c101) {
  3203. s2 = input.substr(peg$currPos, 9);
  3204. peg$currPos += 9;
  3205. } else {
  3206. s2 = peg$FAILED;
  3207. {
  3208. peg$fail(peg$c102);
  3209. }
  3210. }
  3211. if (s2 === peg$FAILED) {
  3212. if (input.substr(peg$currPos, 10).toLowerCase() === peg$c103) {
  3213. s2 = input.substr(peg$currPos, 10);
  3214. peg$currPos += 10;
  3215. } else {
  3216. s2 = peg$FAILED;
  3217. {
  3218. peg$fail(peg$c104);
  3219. }
  3220. }
  3221. if (s2 === peg$FAILED) {
  3222. if (input.substr(peg$currPos, 11).toLowerCase() === peg$c105) {
  3223. s2 = input.substr(peg$currPos, 11);
  3224. peg$currPos += 11;
  3225. } else {
  3226. s2 = peg$FAILED;
  3227. {
  3228. peg$fail(peg$c106);
  3229. }
  3230. }
  3231. if (s2 === peg$FAILED) {
  3232. if (input.substr(peg$currPos, 8).toLowerCase() === peg$c107) {
  3233. s2 = input.substr(peg$currPos, 8);
  3234. peg$currPos += 8;
  3235. } else {
  3236. s2 = peg$FAILED;
  3237. {
  3238. peg$fail(peg$c108);
  3239. }
  3240. }
  3241. if (s2 === peg$FAILED) {
  3242. if (input.substr(peg$currPos, 7).toLowerCase() === peg$c109) {
  3243. s2 = input.substr(peg$currPos, 7);
  3244. peg$currPos += 7;
  3245. } else {
  3246. s2 = peg$FAILED;
  3247. {
  3248. peg$fail(peg$c110);
  3249. }
  3250. }
  3251. }
  3252. }
  3253. }
  3254. }
  3255. if (s2 !== peg$FAILED) {
  3256. s1 = peg$c111(s2);
  3257. s0 = s1;
  3258. } else {
  3259. peg$currPos = s0;
  3260. s0 = peg$FAILED;
  3261. }
  3262. } else {
  3263. peg$currPos = s0;
  3264. s0 = peg$FAILED;
  3265. }
  3266. peg$resultsCache[key] = {
  3267. nextPos: peg$currPos,
  3268. result: s0
  3269. };
  3270. return s0;
  3271. }
  3272. function nth(n) {
  3273. return {
  3274. type: 'nth-child',
  3275. index: {
  3276. type: 'literal',
  3277. value: n
  3278. }
  3279. };
  3280. }
  3281. function nthLast(n) {
  3282. return {
  3283. type: 'nth-last-child',
  3284. index: {
  3285. type: 'literal',
  3286. value: n
  3287. }
  3288. };
  3289. }
  3290. function strUnescape(s) {
  3291. return s.replace(/\\(.)/g, function (match, ch) {
  3292. switch (ch) {
  3293. case 'b':
  3294. return '\b';
  3295. case 'f':
  3296. return '\f';
  3297. case 'n':
  3298. return '\n';
  3299. case 'r':
  3300. return '\r';
  3301. case 't':
  3302. return '\t';
  3303. case 'v':
  3304. return '\v';
  3305. default:
  3306. return ch;
  3307. }
  3308. });
  3309. }
  3310. peg$result = peg$startRuleFunction();
  3311. if (peg$result !== peg$FAILED && peg$currPos === input.length) {
  3312. return peg$result;
  3313. } else {
  3314. if (peg$result !== peg$FAILED && peg$currPos < input.length) {
  3315. peg$fail(peg$endExpectation());
  3316. }
  3317. throw peg$buildStructuredError(peg$maxFailExpected, peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null, peg$maxFailPos < input.length ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1) : peg$computeLocation(peg$maxFailPos, peg$maxFailPos));
  3318. }
  3319. }
  3320. return {
  3321. SyntaxError: peg$SyntaxError,
  3322. parse: peg$parse
  3323. };
  3324. });
  3325. });
  3326. function _objectEntries(obj) {
  3327. var entries = [];
  3328. var keys = Object.keys(obj);
  3329. for (var k = 0; k < keys.length; k++) entries.push([keys[k], obj[keys[k]]]);
  3330. return entries;
  3331. }
  3332. /**
  3333. * @typedef {"LEFT_SIDE"|"RIGHT_SIDE"} Side
  3334. */
  3335. var LEFT_SIDE = 'LEFT_SIDE';
  3336. var RIGHT_SIDE = 'RIGHT_SIDE';
  3337. /**
  3338. * @external AST
  3339. * @see https://esprima.readthedocs.io/en/latest/syntax-tree-format.html
  3340. */
  3341. /**
  3342. * One of the rules of `grammar.pegjs`
  3343. * @typedef {PlainObject} SelectorAST
  3344. * @see grammar.pegjs
  3345. */
  3346. /**
  3347. * The `sequence` production of `grammar.pegjs`
  3348. * @typedef {PlainObject} SelectorSequenceAST
  3349. */
  3350. /**
  3351. * Get the value of a property which may be multiple levels down
  3352. * in the object.
  3353. * @param {?PlainObject} obj
  3354. * @param {string} key
  3355. * @returns {undefined|boolean|string|number|external:AST}
  3356. */
  3357. function getPath(obj, key) {
  3358. var keys = key.split('.');
  3359. for (var i = 0; i < keys.length; i++) {
  3360. if (obj == null) {
  3361. return obj;
  3362. }
  3363. obj = obj[keys[i]];
  3364. }
  3365. return obj;
  3366. }
  3367. /**
  3368. * Determine whether `node` can be reached by following `path`,
  3369. * starting at `ancestor`.
  3370. * @param {?external:AST} node
  3371. * @param {?external:AST} ancestor
  3372. * @param {string[]} path
  3373. * @returns {boolean}
  3374. */
  3375. function inPath(node, ancestor, path) {
  3376. if (path.length === 0) {
  3377. return node === ancestor;
  3378. }
  3379. if (ancestor == null) {
  3380. return false;
  3381. }
  3382. var field = ancestor[path[0]];
  3383. var remainingPath = path.slice(1);
  3384. if (Array.isArray(field)) {
  3385. for (var i = 0, l = field.length; i < l; ++i) {
  3386. if (inPath(node, field[i], remainingPath)) {
  3387. return true;
  3388. }
  3389. }
  3390. return false;
  3391. } else {
  3392. return inPath(node, field, remainingPath);
  3393. }
  3394. }
  3395. /**
  3396. * Given a `node` and its ancestors, determine if `node` is matched
  3397. * by `selector`.
  3398. * @param {?external:AST} node
  3399. * @param {?SelectorAST} selector
  3400. * @param {external:AST[]} [ancestry=[]]
  3401. * @throws {Error} Unknowns (operator, class name, selector type, or
  3402. * selector value type)
  3403. * @returns {boolean}
  3404. */
  3405. function matches(node, selector, ancestry) {
  3406. if (!selector) {
  3407. return true;
  3408. }
  3409. if (!node) {
  3410. return false;
  3411. }
  3412. if (!ancestry) {
  3413. ancestry = [];
  3414. }
  3415. switch (selector.type) {
  3416. case 'wildcard':
  3417. return true;
  3418. case 'identifier':
  3419. return selector.value.toLowerCase() === node.type.toLowerCase();
  3420. case 'field':
  3421. {
  3422. var path = selector.name.split('.');
  3423. var ancestor = ancestry[path.length - 1];
  3424. return inPath(node, ancestor, path);
  3425. }
  3426. case 'matches':
  3427. for (var i = 0, l = selector.selectors.length; i < l; ++i) {
  3428. if (matches(node, selector.selectors[i], ancestry)) {
  3429. return true;
  3430. }
  3431. }
  3432. return false;
  3433. case 'compound':
  3434. for (var _i = 0, _l = selector.selectors.length; _i < _l; ++_i) {
  3435. if (!matches(node, selector.selectors[_i], ancestry)) {
  3436. return false;
  3437. }
  3438. }
  3439. return true;
  3440. case 'not':
  3441. for (var _i2 = 0, _l2 = selector.selectors.length; _i2 < _l2; ++_i2) {
  3442. if (matches(node, selector.selectors[_i2], ancestry)) {
  3443. return false;
  3444. }
  3445. }
  3446. return true;
  3447. case 'has':
  3448. {
  3449. var _ret = function () {
  3450. var collector = [];
  3451. var _loop = function _loop(_i3, _l3) {
  3452. var a = [];
  3453. estraverse.traverse(node, {
  3454. enter: function enter(node, parent) {
  3455. if (parent != null) {
  3456. a.unshift(parent);
  3457. }
  3458. if (matches(node, selector.selectors[_i3], a)) {
  3459. collector.push(node);
  3460. }
  3461. },
  3462. leave: function leave() {
  3463. a.shift();
  3464. },
  3465. fallback: 'iteration'
  3466. });
  3467. };
  3468. for (var _i3 = 0, _l3 = selector.selectors.length; _i3 < _l3; ++_i3) {
  3469. _loop(_i3);
  3470. }
  3471. return {
  3472. v: collector.length !== 0
  3473. };
  3474. }();
  3475. if (_typeof(_ret) === "object") return _ret.v;
  3476. }
  3477. case 'child':
  3478. if (matches(node, selector.right, ancestry)) {
  3479. return matches(ancestry[0], selector.left, ancestry.slice(1));
  3480. }
  3481. return false;
  3482. case 'descendant':
  3483. if (matches(node, selector.right, ancestry)) {
  3484. for (var _i4 = 0, _l4 = ancestry.length; _i4 < _l4; ++_i4) {
  3485. if (matches(ancestry[_i4], selector.left, ancestry.slice(_i4 + 1))) {
  3486. return true;
  3487. }
  3488. }
  3489. }
  3490. return false;
  3491. case 'attribute':
  3492. {
  3493. var p = getPath(node, selector.name);
  3494. switch (selector.operator) {
  3495. case void 0:
  3496. return p != null;
  3497. case '=':
  3498. switch (selector.value.type) {
  3499. case 'regexp':
  3500. return typeof p === 'string' && selector.value.value.test(p);
  3501. case 'literal':
  3502. return "".concat(selector.value.value) === "".concat(p);
  3503. case 'type':
  3504. return selector.value.value === _typeof(p);
  3505. }
  3506. throw new Error("Unknown selector value type: ".concat(selector.value.type));
  3507. case '!=':
  3508. switch (selector.value.type) {
  3509. case 'regexp':
  3510. return !selector.value.value.test(p);
  3511. case 'literal':
  3512. return "".concat(selector.value.value) !== "".concat(p);
  3513. case 'type':
  3514. return selector.value.value !== _typeof(p);
  3515. }
  3516. throw new Error("Unknown selector value type: ".concat(selector.value.type));
  3517. case '<=':
  3518. return p <= selector.value.value;
  3519. case '<':
  3520. return p < selector.value.value;
  3521. case '>':
  3522. return p > selector.value.value;
  3523. case '>=':
  3524. return p >= selector.value.value;
  3525. }
  3526. throw new Error("Unknown operator: ".concat(selector.operator));
  3527. }
  3528. case 'sibling':
  3529. return matches(node, selector.right, ancestry) && sibling(node, selector.left, ancestry, LEFT_SIDE) || selector.left.subject && matches(node, selector.left, ancestry) && sibling(node, selector.right, ancestry, RIGHT_SIDE);
  3530. case 'adjacent':
  3531. return matches(node, selector.right, ancestry) && adjacent(node, selector.left, ancestry, LEFT_SIDE) || selector.right.subject && matches(node, selector.left, ancestry) && adjacent(node, selector.right, ancestry, RIGHT_SIDE);
  3532. case 'nth-child':
  3533. return matches(node, selector.right, ancestry) && nthChild(node, ancestry, function () {
  3534. return selector.index.value - 1;
  3535. });
  3536. case 'nth-last-child':
  3537. return matches(node, selector.right, ancestry) && nthChild(node, ancestry, function (length) {
  3538. return length - selector.index.value;
  3539. });
  3540. case 'class':
  3541. switch (selector.name.toLowerCase()) {
  3542. case 'statement':
  3543. if (node.type.slice(-9) === 'Statement') return true;
  3544. // fallthrough: interface Declaration <: Statement { }
  3545. case 'declaration':
  3546. return node.type.slice(-11) === 'Declaration';
  3547. case 'pattern':
  3548. if (node.type.slice(-7) === 'Pattern') return true;
  3549. // fallthrough: interface Expression <: Node, Pattern { }
  3550. case 'expression':
  3551. return node.type.slice(-10) === 'Expression' || node.type.slice(-7) === 'Literal' || node.type === 'Identifier' && (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty') || node.type === 'MetaProperty';
  3552. case 'function':
  3553. return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
  3554. }
  3555. throw new Error("Unknown class name: ".concat(selector.name));
  3556. }
  3557. throw new Error("Unknown selector type: ".concat(selector.type));
  3558. }
  3559. /**
  3560. * Determines if the given node has a sibling that matches the
  3561. * given selector.
  3562. * @param {external:AST} node
  3563. * @param {SelectorSequenceAST} selector
  3564. * @param {external:AST[]} ancestry
  3565. * @param {Side} side
  3566. * @returns {boolean}
  3567. */
  3568. function sibling(node, selector, ancestry, side) {
  3569. var _ancestry = _slicedToArray(ancestry, 1),
  3570. parent = _ancestry[0];
  3571. if (!parent) {
  3572. return false;
  3573. }
  3574. var keys = estraverse.VisitorKeys[parent.type];
  3575. for (var i = 0, l = keys.length; i < l; ++i) {
  3576. var listProp = parent[keys[i]];
  3577. if (Array.isArray(listProp)) {
  3578. var startIndex = listProp.indexOf(node);
  3579. if (startIndex < 0) {
  3580. continue;
  3581. }
  3582. var lowerBound = void 0,
  3583. upperBound = void 0;
  3584. if (side === LEFT_SIDE) {
  3585. lowerBound = 0;
  3586. upperBound = startIndex;
  3587. } else {
  3588. lowerBound = startIndex + 1;
  3589. upperBound = listProp.length;
  3590. }
  3591. for (var k = lowerBound; k < upperBound; ++k) {
  3592. if (matches(listProp[k], selector, ancestry)) {
  3593. return true;
  3594. }
  3595. }
  3596. }
  3597. }
  3598. return false;
  3599. }
  3600. /**
  3601. * Determines if the given node has an adjacent sibling that matches
  3602. * the given selector.
  3603. * @param {external:AST} node
  3604. * @param {SelectorSequenceAST} selector
  3605. * @param {external:AST[]} ancestry
  3606. * @param {Side} side
  3607. * @returns {boolean}
  3608. */
  3609. function adjacent(node, selector, ancestry, side) {
  3610. var _ancestry2 = _slicedToArray(ancestry, 1),
  3611. parent = _ancestry2[0];
  3612. if (!parent) {
  3613. return false;
  3614. }
  3615. var keys = estraverse.VisitorKeys[parent.type];
  3616. for (var i = 0, l = keys.length; i < l; ++i) {
  3617. var listProp = parent[keys[i]];
  3618. if (Array.isArray(listProp)) {
  3619. var idx = listProp.indexOf(node);
  3620. if (idx < 0) {
  3621. continue;
  3622. }
  3623. if (side === LEFT_SIDE && idx > 0 && matches(listProp[idx - 1], selector, ancestry)) {
  3624. return true;
  3625. }
  3626. if (side === RIGHT_SIDE && idx < listProp.length - 1 && matches(listProp[idx + 1], selector, ancestry)) {
  3627. return true;
  3628. }
  3629. }
  3630. }
  3631. return false;
  3632. }
  3633. /**
  3634. * @callback IndexFunction
  3635. * @param {Integer} len Containing list's length
  3636. * @returns {Integer}
  3637. */
  3638. /**
  3639. * Determines if the given node is the nth child, determined by
  3640. * `idxFn`, which is given the containing list's length.
  3641. * @param {external:AST} node
  3642. * @param {external:AST[]} ancestry
  3643. * @param {IndexFunction} idxFn
  3644. * @returns {boolean}
  3645. */
  3646. function nthChild(node, ancestry, idxFn) {
  3647. var _ancestry3 = _slicedToArray(ancestry, 1),
  3648. parent = _ancestry3[0];
  3649. if (!parent) {
  3650. return false;
  3651. }
  3652. var keys = estraverse.VisitorKeys[parent.type];
  3653. for (var i = 0, l = keys.length; i < l; ++i) {
  3654. var listProp = parent[keys[i]];
  3655. if (Array.isArray(listProp)) {
  3656. var idx = listProp.indexOf(node);
  3657. if (idx >= 0 && idx === idxFn(listProp.length)) {
  3658. return true;
  3659. }
  3660. }
  3661. }
  3662. return false;
  3663. }
  3664. /**
  3665. * For each selector node marked as a subject, find the portion of the
  3666. * selector that the subject must match.
  3667. * @param {SelectorAST} selector
  3668. * @param {SelectorAST} [ancestor] Defaults to `selector`
  3669. * @returns {SelectorAST[]}
  3670. */
  3671. function subjects(selector, ancestor) {
  3672. if (selector == null || _typeof(selector) != 'object') {
  3673. return [];
  3674. }
  3675. if (ancestor == null) {
  3676. ancestor = selector;
  3677. }
  3678. var results = selector.subject ? [ancestor] : [];
  3679. for (var _i5 = 0, _Object$entries = _objectEntries(selector); _i5 < _Object$entries.length; _i5++) {
  3680. var _Object$entries$_i = _slicedToArray(_Object$entries[_i5], 2),
  3681. p = _Object$entries$_i[0],
  3682. sel = _Object$entries$_i[1];
  3683. results.push.apply(results, _toConsumableArray(subjects(sel, p === 'left' ? sel : ancestor)));
  3684. }
  3685. return results;
  3686. }
  3687. /**
  3688. * @callback TraverseVisitor
  3689. * @param {?external:AST} node
  3690. * @param {?external:AST} parent
  3691. * @param {external:AST[]} ancestry
  3692. */
  3693. /**
  3694. * From a JS AST and a selector AST, collect all JS AST nodes that
  3695. * match the selector.
  3696. * @param {external:AST} ast
  3697. * @param {?SelectorAST} selector
  3698. * @param {TraverseVisitor} visitor
  3699. * @returns {external:AST[]}
  3700. */
  3701. function traverse(ast, selector, visitor) {
  3702. if (!selector) {
  3703. return;
  3704. }
  3705. var ancestry = [];
  3706. var altSubjects = subjects(selector);
  3707. estraverse.traverse(ast, {
  3708. enter: function enter(node, parent) {
  3709. if (parent != null) {
  3710. ancestry.unshift(parent);
  3711. }
  3712. if (matches(node, selector, ancestry)) {
  3713. if (altSubjects.length) {
  3714. for (var i = 0, l = altSubjects.length; i < l; ++i) {
  3715. if (matches(node, altSubjects[i], ancestry)) {
  3716. visitor(node, parent, ancestry);
  3717. }
  3718. for (var k = 0, m = ancestry.length; k < m; ++k) {
  3719. var succeedingAncestry = ancestry.slice(k + 1);
  3720. if (matches(ancestry[k], altSubjects[i], succeedingAncestry)) {
  3721. visitor(ancestry[k], parent, succeedingAncestry);
  3722. }
  3723. }
  3724. }
  3725. } else {
  3726. visitor(node, parent, ancestry);
  3727. }
  3728. }
  3729. },
  3730. leave: function leave() {
  3731. ancestry.shift();
  3732. },
  3733. fallback: 'iteration'
  3734. });
  3735. }
  3736. /**
  3737. * From a JS AST and a selector AST, collect all JS AST nodes that
  3738. * match the selector.
  3739. * @param {external:AST} ast
  3740. * @param {?SelectorAST} selector
  3741. * @returns {external:AST[]}
  3742. */
  3743. function match(ast, selector) {
  3744. var results = [];
  3745. traverse(ast, selector, function (node) {
  3746. results.push(node);
  3747. });
  3748. return results;
  3749. }
  3750. /**
  3751. * Parse a selector string and return its AST.
  3752. * @param {string} selector
  3753. * @returns {SelectorAST}
  3754. */
  3755. function parse(selector) {
  3756. return parser.parse(selector);
  3757. }
  3758. /**
  3759. * Query the code AST using the selector string.
  3760. * @param {external:AST} ast
  3761. * @param {string} selector
  3762. * @returns {external:AST[]}
  3763. */
  3764. function query(ast, selector) {
  3765. return match(ast, parse(selector));
  3766. }
  3767. query.parse = parse;
  3768. query.match = match;
  3769. query.traverse = traverse;
  3770. query.matches = matches;
  3771. query.query = query;
  3772. export default query;