CharacterData-impl.js
1.94 KB
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
"use strict";
const { mixin } = require("../../utils");
const NodeImpl = require("./Node-impl").implementation;
const ChildNodeImpl = require("./ChildNode-impl").implementation;
const NonDocumentTypeChildNodeImpl = require("./NonDocumentTypeChildNode-impl").implementation;
const DOMException = require("domexception");
const { TEXT_NODE } = require("../node-type");
class CharacterDataImpl extends NodeImpl {
constructor(args, privateData) {
super(args, privateData);
this._data = privateData.data;
}
get data() {
return this._data;
}
set data(data) {
this.replaceData(0, this.length, data);
}
get length() {
return this._data.length;
}
substringData(offset, count) {
const { length } = this;
if (offset > length) {
throw new DOMException("The index is not in the allowed range.", "IndexSizeError");
}
if (offset + count > length) {
return this._data.substring(offset);
}
return this._data.substring(offset, offset + count);
}
appendData(data) {
this.replaceData(this.length, 0, data);
}
insertData(offset, data) {
this.replaceData(offset, 0, data);
}
deleteData(offset, count) {
this.replaceData(offset, count, "");
}
replaceData(offset, count, data) {
const { length } = this;
if (offset > length) {
throw new DOMException("The index is not in the allowed range.", "IndexSizeError");
}
if (offset + count > length) {
count = length - offset;
}
const start = this._data.substring(0, offset);
const end = this._data.substring(offset + count);
this._data = start + data + end;
// TODO: range stuff
if (this.nodeType === TEXT_NODE && this.parentNode) {
this.parentNode._childTextContentChangeSteps();
}
}
}
mixin(CharacterDataImpl.prototype, NonDocumentTypeChildNodeImpl.prototype);
mixin(CharacterDataImpl.prototype, ChildNodeImpl.prototype);
module.exports = {
implementation: CharacterDataImpl
};