index.js 7.0 KB

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