Blame view

lib/jsdom/living/nodes/CharacterData-impl.js 3.37 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  "use strict";
  
  const DOMException = require("domexception/webidl2js-wrapper");
  
  const { mixin } = require("../../utils");
  const NodeImpl = require("./Node-impl").implementation;
  const ChildNodeImpl = require("./ChildNode-impl").implementation;
  const NonDocumentTypeChildNodeImpl = require("./NonDocumentTypeChildNode-impl").implementation;
  
  const { TEXT_NODE } = require("../node-type");
  const { MUTATION_TYPE, queueMutationRecord } = require("../helpers/mutation-observers");
  
  // https://dom.spec.whatwg.org/#characterdata
  class CharacterDataImpl extends NodeImpl {
    constructor(globalObject, args, privateData) {
      super(globalObject, args, privateData);
  
      this._data = privateData.data;
    }
  
    // https://dom.spec.whatwg.org/#dom-characterdata-data
    get data() {
      return this._data;
    }
    set data(data) {
      this.replaceData(0, this.length, data);
    }
  
    // https://dom.spec.whatwg.org/#dom-characterdata-length
    get length() {
      return this._data.length;
    }
  
    // https://dom.spec.whatwg.org/#dom-characterdata-substringdata
    // https://dom.spec.whatwg.org/#concept-cd-substring
    substringData(offset, count) {
      const { length } = this;
  
      if (offset > length) {
        throw DOMException.create(this._globalObject, ["The index is not in the allowed range.", "IndexSizeError"]);
      }
  
      if (offset + count > length) {
        return this._data.slice(offset);
      }
  
      return this._data.slice(offset, offset + count);
    }
  
    // https://dom.spec.whatwg.org/#dom-characterdata-appenddata
    appendData(data) {
      this.replaceData(this.length, 0, data);
    }
  
    // https://dom.spec.whatwg.org/#dom-characterdata-insertdata
    insertData(offset, data) {
      this.replaceData(offset, 0, data);
    }
  
    // https://dom.spec.whatwg.org/#dom-characterdata-deletedata
    deleteData(offset, count) {
      this.replaceData(offset, count, "");
    }
  
    // https://dom.spec.whatwg.org/#dom-characterdata-replacedata
    // https://dom.spec.whatwg.org/#concept-cd-replace
    replaceData(offset, count, data) {
      const { length } = this;
  
      if (offset > length) {
        throw DOMException.create(this._globalObject, [
          "The index is not in the allowed range.",
          "IndexSizeError"
        ]);
      }
  
      if (offset + count > length) {
        count = length - offset;
      }
  
      queueMutationRecord(MUTATION_TYPE.CHARACTER_DATA, this, null, null, this._data, [], [], null, null);
  
      const start = this._data.slice(0, offset);
      const end = this._data.slice(offset + count);
      this._data = start + data + end;
  
      for (const range of this._referencedRanges) {
        const { _start, _end } = range;
  
        if (_start.offset > offset && _start.offset <= offset + count) {
          range._setLiveRangeStart(this, offset);
        }
  
        if (_end.offset > offset && _end.offset <= offset + count) {
          range._setLiveRangeEnd(this, offset);
        }
  
        if (_start.offset > offset + count) {
          range._setLiveRangeStart(this, _start.offset + data.length - count);
        }
  
        if (_end.offset > offset + count) {
          range._setLiveRangeEnd(this, _end.offset + data.length - count);
        }
      }
  
      if (this.nodeType === TEXT_NODE && this.parentNode) {
        this.parentNode._childTextContentChangeSteps();
      }
    }
  }
  
  mixin(CharacterDataImpl.prototype, NonDocumentTypeChildNodeImpl.prototype);
  mixin(CharacterDataImpl.prototype, ChildNodeImpl.prototype);
  
  module.exports = {
    implementation: CharacterDataImpl
  };