index.js 6.5 KB

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