subselects.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import boolbase from "boolbase";
  2. import { isTraversal } from "../sort.js";
  3. /** Used as a placeholder for :has. Will be replaced with the actual element. */
  4. export const PLACEHOLDER_ELEMENT = {};
  5. export function ensureIsTag(next, adapter) {
  6. if (next === boolbase.falseFunc)
  7. return boolbase.falseFunc;
  8. return (elem) => adapter.isTag(elem) && next(elem);
  9. }
  10. export function getNextSiblings(elem, adapter) {
  11. const siblings = adapter.getSiblings(elem);
  12. if (siblings.length <= 1)
  13. return [];
  14. const elemIndex = siblings.indexOf(elem);
  15. if (elemIndex < 0 || elemIndex === siblings.length - 1)
  16. return [];
  17. return siblings.slice(elemIndex + 1).filter(adapter.isTag);
  18. }
  19. function copyOptions(options) {
  20. // Not copied: context, rootFunc
  21. return {
  22. xmlMode: !!options.xmlMode,
  23. lowerCaseAttributeNames: !!options.lowerCaseAttributeNames,
  24. lowerCaseTags: !!options.lowerCaseTags,
  25. quirksMode: !!options.quirksMode,
  26. cacheResults: !!options.cacheResults,
  27. pseudos: options.pseudos,
  28. adapter: options.adapter,
  29. equals: options.equals,
  30. };
  31. }
  32. const is = (next, token, options, context, compileToken) => {
  33. const func = compileToken(token, copyOptions(options), context);
  34. return func === boolbase.trueFunc
  35. ? next
  36. : func === boolbase.falseFunc
  37. ? boolbase.falseFunc
  38. : (elem) => func(elem) && next(elem);
  39. };
  40. /*
  41. * :not, :has, :is, :matches and :where have to compile selectors
  42. * doing this in src/pseudos.ts would lead to circular dependencies,
  43. * so we add them here
  44. */
  45. export const subselects = {
  46. is,
  47. /**
  48. * `:matches` and `:where` are aliases for `:is`.
  49. */
  50. matches: is,
  51. where: is,
  52. not(next, token, options, context, compileToken) {
  53. const func = compileToken(token, copyOptions(options), context);
  54. return func === boolbase.falseFunc
  55. ? next
  56. : func === boolbase.trueFunc
  57. ? boolbase.falseFunc
  58. : (elem) => !func(elem) && next(elem);
  59. },
  60. has(next, subselect, options, _context, compileToken) {
  61. const { adapter } = options;
  62. const opts = copyOptions(options);
  63. opts.relativeSelector = true;
  64. const context = subselect.some((s) => s.some(isTraversal))
  65. ? // Used as a placeholder. Will be replaced with the actual element.
  66. [PLACEHOLDER_ELEMENT]
  67. : undefined;
  68. const compiled = compileToken(subselect, opts, context);
  69. if (compiled === boolbase.falseFunc)
  70. return boolbase.falseFunc;
  71. const hasElement = ensureIsTag(compiled, adapter);
  72. // If `compiled` is `trueFunc`, we can skip this.
  73. if (context && compiled !== boolbase.trueFunc) {
  74. /*
  75. * `shouldTestNextSiblings` will only be true if the query starts with
  76. * a traversal (sibling or adjacent). That means we will always have a context.
  77. */
  78. const { shouldTestNextSiblings = false } = compiled;
  79. return (elem) => {
  80. if (!next(elem))
  81. return false;
  82. context[0] = elem;
  83. const childs = adapter.getChildren(elem);
  84. const nextElements = shouldTestNextSiblings
  85. ? [...childs, ...getNextSiblings(elem, adapter)]
  86. : childs;
  87. return adapter.existsOne(hasElement, nextElements);
  88. };
  89. }
  90. return (elem) => next(elem) &&
  91. adapter.existsOne(hasElement, adapter.getChildren(elem));
  92. },
  93. };
  94. //# sourceMappingURL=subselects.js.map