legacy.js 5.6 KB

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