forked from jsdom/jsdom
/
HTMLTextAreaElement-impl.js
272 lines (218 loc) · 7.06 KB
/
HTMLTextAreaElement-impl.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"use strict";
const HTMLElementImpl = require("./HTMLElement-impl").implementation;
const DefaultConstraintValidationImpl =
require("../constraint-validation/DefaultConstraintValidation-impl").implementation;
const ValidityState = require("../generated/ValidityState");
const { mixin } = require("../../utils");
const DOMException = require("domexception/webidl2js-wrapper");
const { cloningSteps } = require("../helpers/internal-constants");
const { isDisabled, getLabelsForLabelable, formOwner } = require("../helpers/form-controls");
const { childTextContent } = require("../helpers/text");
const { fireAnEvent } = require("../helpers/events");
class HTMLTextAreaElementImpl extends HTMLElementImpl {
constructor(globalObject, args, privateData) {
super(globalObject, args, privateData);
this._selectionStart = this._selectionEnd = 0;
this._selectionDirection = "none";
this._rawValue = "";
this._dirtyValue = false;
this._customValidityErrorMessage = "";
this._labels = null;
}
_formReset() {
this._rawValue = childTextContent(this);
this._dirtyValue = false;
}
_getAPIValue() {
return this._rawValue.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
}
// https://html.spec.whatwg.org/multipage/form-elements.html#textarea-wrapping-transformation
_getValue() {
const apiValue = this._getAPIValue();
const wrap = this.getAttributeNS(null, "wrap");
return wrap === "hard" ?
textareaWrappingTransformation(apiValue, this.cols) :
apiValue;
}
_childTextContentChangeSteps() {
super._childTextContentChangeSteps();
if (this._dirtyValue === false) {
this._rawValue = childTextContent(this);
}
}
get labels() {
return getLabelsForLabelable(this);
}
get form() {
return formOwner(this);
}
get defaultValue() {
return childTextContent(this);
}
set defaultValue(val) {
this.textContent = val;
}
get value() {
return this._getAPIValue();
}
set value(val) {
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
const oldAPIValue = this._getAPIValue();
this._rawValue = val;
this._dirtyValue = true;
if (oldAPIValue !== this._getAPIValue()) {
this._selectionStart = this._selectionEnd = this._getValueLength();
this._selectionDirection = "none";
}
}
get textLength() {
return this.value.length; // code unit length (16 bit)
}
get type() {
return "textarea";
}
_dispatchSelectEvent() {
fireAnEvent("select", this, undefined, { bubbles: true, cancelable: true });
}
_getValueLength() {
return typeof this.value === "string" ? this.value.length : 0;
}
select() {
this._selectionStart = 0;
this._selectionEnd = this._getValueLength();
this._selectionDirection = "none";
this._dispatchSelectEvent();
}
get selectionStart() {
return this._selectionStart;
}
set selectionStart(start) {
this.setSelectionRange(start, Math.max(start, this._selectionEnd), this._selectionDirection);
}
get selectionEnd() {
return this._selectionEnd;
}
set selectionEnd(end) {
this.setSelectionRange(this._selectionStart, end, this._selectionDirection);
}
get selectionDirection() {
return this._selectionDirection;
}
set selectionDirection(dir) {
this.setSelectionRange(this._selectionStart, this._selectionEnd, dir);
}
setSelectionRange(start, end, dir) {
this._selectionEnd = Math.min(end, this._getValueLength());
this._selectionStart = Math.min(start, this._selectionEnd);
this._selectionDirection = dir === "forward" || dir === "backward" ? dir : "none";
this._dispatchSelectEvent();
}
setRangeText(repl, start, end, selectionMode = "preserve") {
if (arguments.length < 2) {
start = this._selectionStart;
end = this._selectionEnd;
} else if (start > end) {
throw DOMException.create(this._globalObject, ["The index is not in the allowed range.", "IndexSizeError"]);
}
start = Math.min(start, this._getValueLength());
end = Math.min(end, this._getValueLength());
const val = this.value;
let selStart = this._selectionStart;
let selEnd = this._selectionEnd;
this.value = val.slice(0, start) + repl + val.slice(end);
const newEnd = start + this.value.length;
if (selectionMode === "select") {
this.setSelectionRange(start, newEnd);
} else if (selectionMode === "start") {
this.setSelectionRange(start, start);
} else if (selectionMode === "end") {
this.setSelectionRange(newEnd, newEnd);
} else { // preserve
const delta = repl.length - (end - start);
if (selStart > end) {
selStart += delta;
} else if (selStart > start) {
selStart = start;
}
if (selEnd > end) {
selEnd += delta;
} else if (selEnd > start) {
selEnd = newEnd;
}
this.setSelectionRange(selStart, selEnd);
}
}
get cols() {
if (!this.hasAttributeNS(null, "cols")) {
return 20;
}
return parseInt(this.getAttributeNS(null, "cols"));
}
set cols(value) {
if (value <= 0) {
throw DOMException.create(this._globalObject, ["The index is not in the allowed range.", "IndexSizeError"]);
}
this.setAttributeNS(null, "cols", String(value));
}
get rows() {
if (!this.hasAttributeNS(null, "rows")) {
return 2;
}
return parseInt(this.getAttributeNS(null, "rows"));
}
set rows(value) {
if (value <= 0) {
throw DOMException.create(this._globalObject, ["The index is not in the allowed range.", "IndexSizeError"]);
}
this.setAttributeNS(null, "rows", String(value));
}
_barredFromConstraintValidationSpecialization() {
return this.hasAttributeNS(null, "readonly");
}
get _mutable() {
return !isDisabled(this) && !this.hasAttributeNS(null, "readonly");
}
// https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-required
get validity() {
if (!this._validity) {
const state = {
valueMissing: () => this.hasAttributeNS(null, "required") && this._mutable && this.value === ""
};
this._validity = ValidityState.createImpl(this._globalObject, [], {
element: this,
state
});
}
return this._validity;
}
[cloningSteps](copy, node) {
copy._dirtyValue = node._dirtyValue;
copy._rawValue = node._rawValue;
}
}
mixin(HTMLTextAreaElementImpl.prototype, DefaultConstraintValidationImpl.prototype);
module.exports = {
implementation: HTMLTextAreaElementImpl
};
function textareaWrappingTransformation(text, cols) {
let lineStart = 0;
let lineEnd = text.indexOf("\n");
if (lineEnd === -1) {
lineEnd = text.length;
}
while (lineStart < text.length) {
const lineLength = lineEnd - lineStart;
if (lineLength > cols) {
// split the line
lineEnd = lineStart + cols;
text = text.slice(0, lineEnd) + "\n" + text.slice(lineEnd);
}
// move to next line
lineStart = lineEnd + 1; // step over the newline
lineEnd = text.indexOf("\n", lineStart);
if (lineEnd === -1) {
lineEnd = text.length;
}
}
return text;
}