Blame view

lib/jsdom/living/nodes/HTMLCollection-impl.js 2.21 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  "use strict";
  
  const idlUtils = require("../generated/utils.js");
  const { HTML_NS } = require("../helpers/namespaces");
  
  exports.implementation = class HTMLCollectionImpl {
    constructor(globalObject, args, privateData) {
      this._list = [];
      this._version = -1;
      this._element = privateData.element;
      this._query = privateData.query;
  
      this._globalObject = globalObject;
  
      this._update();
    }
    get length() {
      this._update();
      return this._list.length;
    }
    item(index) {
      this._update();
      return this._list[index] || null;
    }
    namedItem(key) {
      if (key === "") {
        return null;
      }
      this._update();
      for (const element of this._list) {
        if (element.getAttributeNS(null, "id") === key) {
          return element;
        }
        if (element._namespaceURI === HTML_NS) {
          const name = element.getAttributeNS(null, "name");
          if (name === key) {
            return element;
          }
        }
      }
      return null;
    }
    _update() {
      if (this._version < this._element._version) {
        const snapshot = this._query();
        for (let i = 0; i < snapshot.length; i++) {
          this._list[i] = snapshot[i];
        }
        this._list.length = snapshot.length;
        this._version = this._element._version;
      }
    }
    get [idlUtils.supportedPropertyIndices]() {
      this._update();
      return this._list.keys();
    }
    get [idlUtils.supportedPropertyNames]() {
      this._update();
      const result = new Set();
      for (const element of this._list) {
        const id = element.getAttributeNS(null, "id");
        if (id) {
          result.add(id);
        }
        if (element._namespaceURI === HTML_NS) {
          const name = element.getAttributeNS(null, "name");
          if (name) {
            result.add(name);
          }
        }
      }
      return result;
    }
  
    // Inherit some useful functions from Array.
    [Symbol.iterator]() {
      this._update();
      return this._list[Symbol.iterator]();
    }
    entries() {
      this._update();
      return this._list.entries();
    }
    filter(...args) {
      this._update();
      return this._list.filter(...args);
    }
    map(...args) {
      this._update();
      return this._list.map(...args);
    }
    indexOf(...args) {
      this._update();
      return this._list.indexOf(...args);
    }
  };