-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
CharacterData.ts
227 lines (203 loc) · 5.56 KB
/
CharacterData.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import Node from '../node/Node';
import CharacterDataUtility from './CharacterDataUtility';
import ICharacterData from './ICharacterData';
import IElement from '../element/IElement';
import NonDocumentChildNodeUtility from '../child-node/NonDocumentChildNodeUtility';
import ChildNodeUtility from '../child-node/ChildNodeUtility';
import MutationRecord from '../../mutation-observer/MutationRecord';
import MutationTypeEnum from '../../mutation-observer/MutationTypeEnum';
/**
* Character data base class.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/CharacterData.
*/
export default abstract class CharacterData extends Node implements ICharacterData {
protected _data = '';
/**
* Constructor.
*
* @param [data] Data.
*/
constructor(data?: string) {
super();
if (data) {
this._data = data;
}
}
/**
* Returns text content.
*
* @returns Text content.
*/
public get length(): number {
return this._data.length;
}
/**
* Returns text content.
*
* @returns Text content.
*/
public get data(): string {
return this._data;
}
/**
* Sets text content.
*
* @param textContent Text content.
*/
public set data(data: string) {
const oldValue = this._data;
this._data = data;
if (this.isConnected) {
this.ownerDocument['_cacheID']++;
}
// MutationObserver
if (this._observers.length > 0) {
for (const observer of this._observers) {
if (observer.options.characterData) {
const record = new MutationRecord();
record.target = this;
record.type = MutationTypeEnum.characterData;
record.oldValue = observer.options.characterDataOldValue ? oldValue : null;
observer.callback([record]);
}
}
}
}
/**
* Returns text content.
*
* @returns Text content.
*/
public get textContent(): string {
return this._data;
}
/**
* Sets text content.
*
* @param textContent Text content.
*/
public set textContent(textContent: string) {
this.data = textContent;
}
/**
* Returns node value.
*
* @returns Node value.
*/
public get nodeValue(): string {
return this._data;
}
/**
* Sets node value.
*
* @param nodeValue Node value.
*/
public set nodeValue(nodeValue: string) {
this.textContent = nodeValue;
}
/**
* Previous element sibling.
*
* @returns Element.
*/
public get previousElementSibling(): IElement {
return NonDocumentChildNodeUtility.previousElementSibling(this);
}
/**
* Next element sibling.
*
* @returns Element.
*/
public get nextElementSibling(): IElement {
return NonDocumentChildNodeUtility.nextElementSibling(this);
}
/**
* Appends the given DOMString to the CharacterData.data string; when this method returns, data contains the concatenated DOMString.
*
* @param data Data.
*/
public appendData(data: string): void {
CharacterDataUtility.appendData(this, data);
}
/**
* Removes the specified amount of characters, starting at the specified offset, from the CharacterData.data string; when this method returns, data contains the shortened DOMString.
*
* @param offset Offset.
* @param count Count.
*/
public deleteData(offset: number, count: number): void {
CharacterDataUtility.deleteData(this, offset, count);
}
/**
* Inserts the specified characters, at the specified offset, in the CharacterData.data string; when this method returns, data contains the modified DOMString.
*
* @param offset Offset.
* @param data Data.
*/
public insertData(offset: number, data: string): void {
CharacterDataUtility.insertData(this, offset, data);
}
/**
* Replaces the specified amount of characters, starting at the specified offset, with the specified DOMString; when this method returns, data contains the modified DOMString.
*
* @param offset Offset.
* @param count Count.
* @param data Data.
*/
public replaceData(offset: number, count: number, data: string): void {
CharacterDataUtility.replaceData(this, offset, count, data);
}
/**
* Returns a DOMString containing the part of CharacterData.data of the specified length and starting at the specified offset.
*
* @param offset Offset.
* @param count Count.
*/
public substringData(offset: number, count: number): string {
return CharacterDataUtility.substringData(this, offset, count);
}
/**
* Removes the object from its parent children list.
*/
public remove(): void {
ChildNodeUtility.remove(this);
}
/**
* The Node.replaceWith() method replaces this Node in the children list of its parent with a set of Node or DOMString objects.
*
* @param nodes List of Node or DOMString.
*/
public replaceWith(...nodes: (Node | string)[]): void {
ChildNodeUtility.replaceWith(this, ...nodes);
}
/**
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just before this ChildNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public before(...nodes: (string | Node)[]): void {
ChildNodeUtility.before(this, ...nodes);
}
/**
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just after this ChildNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public after(...nodes: (string | Node)[]): void {
ChildNodeUtility.after(this, ...nodes);
}
/**
* Clones a node.
*
* @override
* @param [deep=false] "true" to clone deep.
* @returns Cloned node.
*/
public cloneNode(deep = false): ICharacterData {
const clone = <CharacterData>super.cloneNode(deep);
clone._data = this._data;
return clone;
}
}