Blame view

lib/jsdom/living/helpers/traversal.js 2.27 KB
858f2bdf5   Boyan Georgiev   fixes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  "use strict";
  const { domSymbolTree } = require("./internal-constants");
  const { HTML_NS } = require("./namespaces");
  
  // All these operate on and return impls, not wrappers!
  
  exports.closest = (e, localName, namespace = HTML_NS) => {
    while (e) {
      if (e.localName === localName && e.namespaceURI === namespace) {
        return e;
      }
      e = domSymbolTree.parent(e);
    }
  
    return null;
  };
  
  exports.childrenByLocalName = (parent, localName, namespace = HTML_NS) => {
    return domSymbolTree.childrenToArray(parent, { filter(node) {
      return node._localName === localName && node._namespaceURI === namespace;
    } });
  };
  
  exports.descendantsByLocalName = (parent, localName, namespace = HTML_NS) => {
    return domSymbolTree.treeToArray(parent, { filter(node) {
      return node._localName === localName && node._namespaceURI === namespace && node !== parent;
    } });
  };
  
  exports.childrenByLocalNames = (parent, localNamesSet, namespace = HTML_NS) => {
    return domSymbolTree.childrenToArray(parent, { filter(node) {
      return localNamesSet.has(node._localName) && node._namespaceURI === namespace;
    } });
  };
  
  exports.descendantsByLocalNames = (parent, localNamesSet, namespace = HTML_NS) => {
    return domSymbolTree.treeToArray(parent, { filter(node) {
      return localNamesSet.has(node._localName) &&
             node._namespaceURI === namespace &&
             node !== parent;
    } });
  };
  
  exports.firstChildWithLocalName = (parent, localName, namespace = HTML_NS) => {
    const iterator = domSymbolTree.childrenIterator(parent);
    for (const child of iterator) {
      if (child._localName === localName && child._namespaceURI === namespace) {
        return child;
      }
    }
    return null;
  };
  
  exports.firstChildWithLocalNames = (parent, localNamesSet, namespace = HTML_NS) => {
    const iterator = domSymbolTree.childrenIterator(parent);
    for (const child of iterator) {
      if (localNamesSet.has(child._localName) && child._namespaceURI === namespace) {
        return child;
      }
    }
    return null;
  };
  
  exports.firstDescendantWithLocalName = (parent, localName, namespace = HTML_NS) => {
    const iterator = domSymbolTree.treeIterator(parent);
    for (const descendant of iterator) {
      if (descendant._localName === localName && descendant._namespaceURI === namespace) {
        return descendant;
      }
    }
    return null;
  };