legacy.js 5.0 KB

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