legacy.js 4.0 KB

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