index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.aliases = exports.pseudos = exports.filters = exports.is = exports.selectOne = exports.selectAll = exports.prepareContext = exports._compileToken = exports._compileUnsafe = exports.compile = void 0;
  27. var DomUtils = __importStar(require("domutils"));
  28. var boolbase_1 = require("boolbase");
  29. var compile_1 = require("./compile");
  30. var subselects_1 = require("./pseudo-selectors/subselects");
  31. var defaultEquals = function (a, b) { return a === b; };
  32. var defaultOptions = {
  33. adapter: DomUtils,
  34. equals: defaultEquals,
  35. };
  36. function convertOptionFormats(options) {
  37. var _a, _b, _c, _d;
  38. /*
  39. * We force one format of options to the other one.
  40. */
  41. // @ts-expect-error Default options may have incompatible `Node` / `ElementNode`.
  42. var opts = options !== null && options !== void 0 ? options : defaultOptions;
  43. // @ts-expect-error Same as above.
  44. (_a = opts.adapter) !== null && _a !== void 0 ? _a : (opts.adapter = DomUtils);
  45. // @ts-expect-error `equals` does not exist on `Options`
  46. (_b = opts.equals) !== null && _b !== void 0 ? _b : (opts.equals = (_d = (_c = opts.adapter) === null || _c === void 0 ? void 0 : _c.equals) !== null && _d !== void 0 ? _d : defaultEquals);
  47. return opts;
  48. }
  49. function wrapCompile(func) {
  50. return function addAdapter(selector, options, context) {
  51. var opts = convertOptionFormats(options);
  52. return func(selector, opts, context);
  53. };
  54. }
  55. /**
  56. * Compiles the query, returns a function.
  57. */
  58. exports.compile = wrapCompile(compile_1.compile);
  59. exports._compileUnsafe = wrapCompile(compile_1.compileUnsafe);
  60. exports._compileToken = wrapCompile(compile_1.compileToken);
  61. function getSelectorFunc(searchFunc) {
  62. return function select(query, elements, options) {
  63. var opts = convertOptionFormats(options);
  64. if (typeof query !== "function") {
  65. query = (0, compile_1.compileUnsafe)(query, opts, elements);
  66. }
  67. var filteredElements = prepareContext(elements, opts.adapter, query.shouldTestNextSiblings);
  68. return searchFunc(query, filteredElements, opts);
  69. };
  70. }
  71. function prepareContext(elems, adapter, shouldTestNextSiblings) {
  72. if (shouldTestNextSiblings === void 0) { shouldTestNextSiblings = false; }
  73. /*
  74. * Add siblings if the query requires them.
  75. * See https://github.com/fb55/css-select/pull/43#issuecomment-225414692
  76. */
  77. if (shouldTestNextSiblings) {
  78. elems = appendNextSiblings(elems, adapter);
  79. }
  80. return Array.isArray(elems)
  81. ? adapter.removeSubsets(elems)
  82. : adapter.getChildren(elems);
  83. }
  84. exports.prepareContext = prepareContext;
  85. function appendNextSiblings(elem, adapter) {
  86. // Order matters because jQuery seems to check the children before the siblings
  87. var elems = Array.isArray(elem) ? elem.slice(0) : [elem];
  88. var elemsLength = elems.length;
  89. for (var i = 0; i < elemsLength; i++) {
  90. var nextSiblings = (0, subselects_1.getNextSiblings)(elems[i], adapter);
  91. elems.push.apply(elems, nextSiblings);
  92. }
  93. return elems;
  94. }
  95. /**
  96. * @template Node The generic Node type for the DOM adapter being used.
  97. * @template ElementNode The Node type for elements for the DOM adapter being used.
  98. * @param elems Elements to query. If it is an element, its children will be queried..
  99. * @param query can be either a CSS selector string or a compiled query function.
  100. * @param [options] options for querying the document.
  101. * @see compile for supported selector queries.
  102. * @returns All matching elements.
  103. *
  104. */
  105. exports.selectAll = getSelectorFunc(function (query, elems, options) {
  106. return query === boolbase_1.falseFunc || !elems || elems.length === 0
  107. ? []
  108. : options.adapter.findAll(query, elems);
  109. });
  110. /**
  111. * @template Node The generic Node type for the DOM adapter being used.
  112. * @template ElementNode The Node type for elements for the DOM adapter being used.
  113. * @param elems Elements to query. If it is an element, its children will be queried..
  114. * @param query can be either a CSS selector string or a compiled query function.
  115. * @param [options] options for querying the document.
  116. * @see compile for supported selector queries.
  117. * @returns the first match, or null if there was no match.
  118. */
  119. exports.selectOne = getSelectorFunc(function (query, elems, options) {
  120. return query === boolbase_1.falseFunc || !elems || elems.length === 0
  121. ? null
  122. : options.adapter.findOne(query, elems);
  123. });
  124. /**
  125. * Tests whether or not an element is matched by query.
  126. *
  127. * @template Node The generic Node type for the DOM adapter being used.
  128. * @template ElementNode The Node type for elements for the DOM adapter being used.
  129. * @param elem The element to test if it matches the query.
  130. * @param query can be either a CSS selector string or a compiled query function.
  131. * @param [options] options for querying the document.
  132. * @see compile for supported selector queries.
  133. * @returns
  134. */
  135. function is(elem, query, options) {
  136. var opts = convertOptionFormats(options);
  137. return (typeof query === "function" ? query : (0, compile_1.compile)(query, opts))(elem);
  138. }
  139. exports.is = is;
  140. /**
  141. * Alias for selectAll(query, elems, options).
  142. * @see [compile] for supported selector queries.
  143. */
  144. exports.default = exports.selectAll;
  145. // Export filters, pseudos and aliases to allow users to supply their own.
  146. var pseudo_selectors_1 = require("./pseudo-selectors");
  147. Object.defineProperty(exports, "filters", { enumerable: true, get: function () { return pseudo_selectors_1.filters; } });
  148. Object.defineProperty(exports, "pseudos", { enumerable: true, get: function () { return pseudo_selectors_1.pseudos; } });
  149. Object.defineProperty(exports, "aliases", { enumerable: true, get: function () { return pseudo_selectors_1.aliases; } });