matcher.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var type_1 = __importDefault(require("./nodes/type"));
  7. function isTag(node) {
  8. return node && node.nodeType === type_1.default.ELEMENT_NODE;
  9. }
  10. function getAttributeValue(elem, name) {
  11. return isTag(elem) ? elem.getAttribute(name) : undefined;
  12. }
  13. function getName(elem) {
  14. return ((elem && elem.rawTagName) || '').toLowerCase();
  15. }
  16. function getChildren(node) {
  17. return node && node.childNodes;
  18. }
  19. function getParent(node) {
  20. return node ? node.parentNode : null;
  21. }
  22. function getText(node) {
  23. return node.text;
  24. }
  25. function removeSubsets(nodes) {
  26. var idx = nodes.length;
  27. var node;
  28. var ancestor;
  29. var replace;
  30. // Check if each node (or one of its ancestors) is already contained in the
  31. // array.
  32. while (--idx > -1) {
  33. node = ancestor = nodes[idx];
  34. // Temporarily remove the node under consideration
  35. nodes[idx] = null;
  36. replace = true;
  37. while (ancestor) {
  38. if (nodes.indexOf(ancestor) > -1) {
  39. replace = false;
  40. nodes.splice(idx, 1);
  41. break;
  42. }
  43. ancestor = getParent(ancestor);
  44. }
  45. // If the node has been found to be unique, re-insert it.
  46. if (replace) {
  47. nodes[idx] = node;
  48. }
  49. }
  50. return nodes;
  51. }
  52. function existsOne(test, elems) {
  53. return elems.some(function (elem) {
  54. return isTag(elem) ? test(elem) || existsOne(test, getChildren(elem)) : false;
  55. });
  56. }
  57. function getSiblings(node) {
  58. var parent = getParent(node);
  59. return parent && getChildren(parent);
  60. }
  61. function hasAttrib(elem, name) {
  62. return getAttributeValue(elem, name) !== undefined;
  63. }
  64. function findOne(test, elems) {
  65. var elem = null;
  66. for (var i = 0, l = elems.length; i < l && !elem; i++) {
  67. var el = elems[i];
  68. if (test(el)) {
  69. elem = el;
  70. }
  71. else {
  72. var childs = getChildren(el);
  73. if (childs && childs.length > 0) {
  74. elem = findOne(test, childs);
  75. }
  76. }
  77. }
  78. return elem;
  79. }
  80. function findAll(test, nodes) {
  81. var result = [];
  82. for (var i = 0, j = nodes.length; i < j; i++) {
  83. if (!isTag(nodes[i]))
  84. continue;
  85. if (test(nodes[i]))
  86. result.push(nodes[i]);
  87. var childs = getChildren(nodes[i]);
  88. if (childs)
  89. result = result.concat(findAll(test, childs));
  90. }
  91. return result;
  92. }
  93. exports.default = {
  94. isTag: isTag,
  95. getAttributeValue: getAttributeValue,
  96. getName: getName,
  97. getChildren: getChildren,
  98. getParent: getParent,
  99. getText: getText,
  100. removeSubsets: removeSubsets,
  101. existsOne: existsOne,
  102. getSiblings: getSiblings,
  103. hasAttrib: hasAttrib,
  104. findOne: findOne,
  105. findAll: findAll
  106. };