legacy.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { isTag, isText } from "domhandler";
  2. import { filter, findOne } from "./querying.js";
  3. /**
  4. * A map of functions to check nodes against.
  5. */
  6. const Checks = {
  7. tag_name(name) {
  8. if (typeof name === "function") {
  9. return (elem) => isTag(elem) && name(elem.name);
  10. }
  11. else if (name === "*") {
  12. return isTag;
  13. }
  14. return (elem) => isTag(elem) && elem.name === name;
  15. },
  16. tag_type(type) {
  17. if (typeof type === "function") {
  18. return (elem) => type(elem.type);
  19. }
  20. return (elem) => elem.type === type;
  21. },
  22. tag_contains(data) {
  23. if (typeof data === "function") {
  24. return (elem) => isText(elem) && data(elem.data);
  25. }
  26. return (elem) => isText(elem) && elem.data === data;
  27. },
  28. };
  29. /**
  30. * Returns a function to check whether a node has an attribute with a particular
  31. * value.
  32. *
  33. * @param attrib Attribute to check.
  34. * @param value Attribute value to look for.
  35. * @returns A function to check whether the a node has an attribute with a
  36. * particular value.
  37. */
  38. function getAttribCheck(attrib, value) {
  39. if (typeof value === "function") {
  40. return (elem) => isTag(elem) && value(elem.attribs[attrib]);
  41. }
  42. return (elem) => isTag(elem) && elem.attribs[attrib] === value;
  43. }
  44. /**
  45. * Returns a function that returns `true` if either of the input functions
  46. * returns `true` for a node.
  47. *
  48. * @param a First function to combine.
  49. * @param b Second function to combine.
  50. * @returns A function taking a node and returning `true` if either of the input
  51. * functions returns `true` for the node.
  52. */
  53. function combineFuncs(a, b) {
  54. return (elem) => a(elem) || b(elem);
  55. }
  56. /**
  57. * Returns a function that executes all checks in `options` and returns `true`
  58. * if any of them match a node.
  59. *
  60. * @param options An object describing nodes to look for.
  61. * @returns A function that executes all checks in `options` and returns `true`
  62. * if any of them match a node.
  63. */
  64. function compileTest(options) {
  65. const funcs = Object.keys(options).map((key) => {
  66. const value = options[key];
  67. return Object.prototype.hasOwnProperty.call(Checks, key)
  68. ? Checks[key](value)
  69. : getAttribCheck(key, value);
  70. });
  71. return funcs.length === 0 ? null : funcs.reduce(combineFuncs);
  72. }
  73. /**
  74. * Checks whether a node matches the description in `options`.
  75. *
  76. * @category Legacy Query Functions
  77. * @param options An object describing nodes to look for.
  78. * @param node The element to test.
  79. * @returns Whether the element matches the description in `options`.
  80. */
  81. export function testElement(options, node) {
  82. const test = compileTest(options);
  83. return test ? test(node) : true;
  84. }
  85. /**
  86. * Returns all nodes that match `options`.
  87. *
  88. * @category Legacy Query Functions
  89. * @param options An object describing nodes to look for.
  90. * @param nodes Nodes to search through.
  91. * @param recurse Also consider child nodes.
  92. * @param limit Maximum number of nodes to return.
  93. * @returns All nodes that match `options`.
  94. */
  95. export function getElements(options, nodes, recurse, limit = Infinity) {
  96. const test = compileTest(options);
  97. return test ? filter(test, nodes, recurse, limit) : [];
  98. }
  99. /**
  100. * Returns the node with the supplied ID.
  101. *
  102. * @category Legacy Query Functions
  103. * @param id The unique ID attribute value to look for.
  104. * @param nodes Nodes to search through.
  105. * @param recurse Also consider child nodes.
  106. * @returns The node with the supplied ID.
  107. */
  108. export function getElementById(id, nodes, recurse = true) {
  109. if (!Array.isArray(nodes))
  110. nodes = [nodes];
  111. return findOne(getAttribCheck("id", id), nodes, recurse);
  112. }
  113. /**
  114. * Returns all nodes with the supplied `tagName`.
  115. *
  116. * @category Legacy Query Functions
  117. * @param tagName Tag name to search for.
  118. * @param nodes Nodes to search through.
  119. * @param recurse Also consider child nodes.
  120. * @param limit Maximum number of nodes to return.
  121. * @returns All nodes with the supplied `tagName`.
  122. */
  123. export function getElementsByTagName(tagName, nodes, recurse = true, limit = Infinity) {
  124. return filter(Checks["tag_name"](tagName), nodes, recurse, limit);
  125. }
  126. /**
  127. * Returns all nodes with the supplied `type`.
  128. *
  129. * @category Legacy Query Functions
  130. * @param type Element type to look for.
  131. * @param nodes Nodes to search through.
  132. * @param recurse Also consider child nodes.
  133. * @param limit Maximum number of nodes to return.
  134. * @returns All nodes with the supplied `type`.
  135. */
  136. export function getElementsByTagType(type, nodes, recurse = true, limit = Infinity) {
  137. return filter(Checks["tag_type"](type), nodes, recurse, limit);
  138. }
  139. //# sourceMappingURL=legacy.js.map