legacy.js 6.1 KB

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