Blame view

lib/jsdom/living/nodes/HTMLTableSectionElement-impl.js 1.6 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
  "use strict";
  
  const HTMLElementImpl = require("./HTMLElement-impl").implementation;
  const { childrenByLocalName } = require("../helpers/traversal");
  const HTMLCollection = require("../generated/HTMLCollection");
  const DOMException = require("domexception/webidl2js-wrapper");
  
  class HTMLTableSectionElementImpl extends HTMLElementImpl {
    get rows() {
      if (!this._rows) {
        this._rows = HTMLCollection.createImpl(this._globalObject, [], {
          element: this,
          query: () => childrenByLocalName(this, "tr")
        });
      }
      return this._rows;
    }
  
    insertRow(index) {
      if (index < -1 || index > this.rows.length) {
        throw DOMException.create(this._globalObject, [
          "Cannot insert a row at an index that is less than -1 or greater than the number of existing rows",
          "IndexSizeError"
        ]);
      }
  
      const tr = this._ownerDocument.createElement("tr");
  
      if (index === -1 || index === this.rows.length) {
        this._append(tr);
      } else {
        const beforeTR = this.rows.item(index);
        this._insert(tr, beforeTR);
      }
  
      return tr;
    }
  
    deleteRow(index) {
      if (index < -1 || index >= this.rows.length) {
        throw DOMException.create(this._globalObject, [
          `Cannot delete a row at index ${index}, where no row exists`,
          "IndexSizeError"
        ]);
      }
  
      if (index === -1) {
        if (this.rows.length > 0) {
          const tr = this.rows.item(this.rows.length - 1);
          this._remove(tr);
        }
      } else {
        const tr = this.rows.item(index);
        this._remove(tr);
      }
    }
  }
  
  module.exports = {
    implementation: HTMLTableSectionElementImpl
  };