Blame view

lib/jsdom/living/selection/Selection-impl.js 10.1 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  "use strict";
  
  const DOMException = require("domexception/webidl2js-wrapper");
  
  const NODE_TYPE = require("../node-type");
  const { nodeLength, nodeRoot } = require("../helpers/node");
  const { domSymbolTree } = require("../helpers/internal-constants");
  const { compareBoundaryPointsPosition } = require("../range/boundary-point");
  
  const { setBoundaryPointStart, setBoundaryPointEnd } = require("../range/Range-impl");
  
  const Range = require("../generated/Range");
  const { implForWrapper } = require("../generated/utils");
  
  // https://w3c.github.io/selection-api/#dfn-direction
  const SELECTION_DIRECTION = {
    FORWARDS: 1,
    BACKWARDS: -1,
    DIRECTIONLESS: 0
  };
  
  // https://w3c.github.io/selection-api/#dom-selection
  class SelectionImpl {
    constructor(globalObject) {
      this._range = null;
      this._direction = SELECTION_DIRECTION.DIRECTIONLESS;
  
      this._globalObject = globalObject;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-anchornode
    get anchorNode() {
      const anchor = this._anchor;
      return anchor ? anchor.node : null;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-anchoroffset
    get anchorOffset() {
      const anchor = this._anchor;
      return anchor ? anchor.offset : 0;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-focusnode
    get focusNode() {
      const focus = this._focus;
      return focus ? focus.node : null;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-focusoffset
    get focusOffset() {
      const focus = this._focus;
      return focus ? focus.offset : 0;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-iscollapsed
    get isCollapsed() {
      return this._range === null || this._range.collapsed;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-rangecount
    get rangeCount() {
      return this._isEmpty() ? 0 : 1;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-type
    get type() {
      if (this._isEmpty()) {
        return "None";
      } else if (this._range.collapsed) {
        return "Caret";
      }
  
      return "Range";
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-getrangeat
    getRangeAt(index) {
      if (index !== 0 || this._isEmpty()) {
        throw DOMException.create(this._globalObject, ["Invalid range index.", "IndexSizeError"]);
      }
  
      return this._range;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-addrange
    addRange(range) {
      if (range._root === implForWrapper(this._globalObject._document) && this.rangeCount === 0) {
        this._associateRange(range);
      }
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-removerange
    removeRange(range) {
      if (range !== this._range) {
        throw DOMException.create(this._globalObject, ["Invalid range.", "NotFoundError"]);
      }
  
      this._associateRange(null);
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-removeallranges
    removeAllRanges() {
      this._associateRange(null);
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-empty
    empty() {
      this.removeAllRanges();
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-collapse
    collapse(node, offset) {
      if (node === null) {
        this.removeAllRanges();
        return;
      }
  
      if (node.nodeType === NODE_TYPE.DOCUMENT_TYPE_NODE) {
        throw DOMException.create(this._globalObject, [
          "DocumentType Node can't be used as boundary point.",
          "InvalidNodeTypeError"
        ]);
      }
  
      if (offset > nodeLength(node)) {
        throw DOMException.create(this._globalObject, ["Invalid range index.", "IndexSizeError"]);
      }
  
      if (nodeRoot(node) !== implForWrapper(this._globalObject._document)) {
        return;
      }
  
      const newRange = Range.createImpl(this._globalObject, [], {
        start: { node, offset: 0 },
        end: { node, offset: 0 }
      });
  
      setBoundaryPointStart(newRange, node, offset);
      setBoundaryPointEnd(newRange, node, offset);
  
      this._associateRange(newRange);
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-setposition
    setPosition(node, offset) {
      this.collapse(node, offset);
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-collapsetostart
    collapseToStart() {
      if (this._isEmpty()) {
        throw DOMException.create(this._globalObject, ["There is no selection to collapse.", "InvalidStateError"]);
      }
  
      const { node, offset } = this._range._start;
      const newRange = Range.createImpl(this._globalObject, [], {
        start: { node, offset },
        end: { node, offset }
      });
  
      this._associateRange(newRange);
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-collapsetoend
    collapseToEnd() {
      if (this._isEmpty()) {
        throw DOMException.create(this._globalObject, ["There is no selection to collapse.", "InvalidStateError"]);
      }
  
      const { node, offset } = this._range._end;
      const newRange = Range.createImpl(this._globalObject, [], {
        start: { node, offset },
        end: { node, offset }
      });
  
      this._associateRange(newRange);
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-extend
    extend(node, offset) {
      if (nodeRoot(node) !== implForWrapper(this._globalObject._document)) {
        return;
      }
  
      if (this._isEmpty()) {
        throw DOMException.create(this._globalObject, ["There is no selection to extend.", "InvalidStateError"]);
      }
  
      const { _anchor: oldAnchor } = this;
      const newFocus = { node, offset };
  
      const newRange = Range.createImpl(this._globalObject, [], {
        start: { node, offset: 0 },
        end: { node, offset: 0 }
      });
  
      if (nodeRoot(node) !== this._range._root) {
        setBoundaryPointStart(newRange, newFocus.node, newFocus.offset);
        setBoundaryPointEnd(newRange, newFocus.node, newFocus.offset);
      } else if (compareBoundaryPointsPosition(oldAnchor, newFocus) <= 0) {
        setBoundaryPointStart(newRange, oldAnchor.node, oldAnchor.offset);
        setBoundaryPointEnd(newRange, newFocus.node, newFocus.offset);
      } else {
        setBoundaryPointStart(newRange, newFocus.node, newFocus.offset);
        setBoundaryPointEnd(newRange, oldAnchor.node, oldAnchor.offset);
      }
  
      this._associateRange(newRange);
  
      this._direction = compareBoundaryPointsPosition(newFocus, oldAnchor) === -1 ?
        SELECTION_DIRECTION.BACKWARDS :
        SELECTION_DIRECTION.FORWARDS;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-setbaseandextent
    setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset) {
      if (anchorOffset > nodeLength(anchorNode) || focusOffset > nodeLength(focusNode)) {
        throw DOMException.create(this._globalObject, ["Invalid anchor or focus offset.", "IndexSizeError"]);
      }
  
      const document = implForWrapper(this._globalObject._document);
      if (document !== nodeRoot(anchorNode) || document !== nodeRoot(focusNode)) {
        return;
      }
  
      const anchor = { node: anchorNode, offset: anchorOffset };
      const focus = { node: focusNode, offset: focusOffset };
  
      let newRange;
      if (compareBoundaryPointsPosition(anchor, focus) === -1) {
        newRange = Range.createImpl(this._globalObject, [], {
          start: { node: anchor.node, offset: anchor.offset },
          end: { node: focus.node, offset: focus.offset }
        });
      } else {
        newRange = Range.createImpl(this._globalObject, [], {
          start: { node: focus.node, offset: focus.offset },
          end: { node: anchor.node, offset: anchor.offset }
        });
      }
  
      this._associateRange(newRange);
  
      this._direction = compareBoundaryPointsPosition(focus, anchor) === -1 ?
        SELECTION_DIRECTION.BACKWARDS :
        SELECTION_DIRECTION.FORWARDS;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-selectallchildren
    selectAllChildren(node) {
      if (node.nodeType === NODE_TYPE.DOCUMENT_TYPE_NODE) {
        throw DOMException.create(this._globalObject, [
          "DocumentType Node can't be used as boundary point.",
          "InvalidNodeTypeError"
        ]);
      }
  
      const document = implForWrapper(this._globalObject._document);
      if (document !== nodeRoot(node)) {
        return;
      }
  
      const length = domSymbolTree.childrenCount(node);
  
      const newRange = Range.createImpl(this._globalObject, [], {
        start: { node, offset: 0 },
        end: { node, offset: 0 }
      });
  
      setBoundaryPointStart(newRange, node, 0);
      setBoundaryPointEnd(newRange, node, length);
  
      this._associateRange(newRange);
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-deletefromdocument
    deleteFromDocument() {
      if (!this._isEmpty()) {
        this._range.deleteContents();
      }
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-containsnode
    containsNode(node, allowPartialContainment) {
      if (this._isEmpty() || nodeRoot(node) !== implForWrapper(this._globalObject._document)) {
        return false;
      }
  
      const { _start, _end } = this._range;
  
      const startIsBeforeNode = compareBoundaryPointsPosition(_start, { node, offset: 0 }) === -1;
      const endIsAfterNode = compareBoundaryPointsPosition(_end, { node, offset: nodeLength(node) }) === 1;
  
      return allowPartialContainment ?
        startIsBeforeNode || endIsAfterNode :
        startIsBeforeNode && endIsAfterNode;
    }
  
    // https://w3c.github.io/selection-api/#dom-selection-stringifier
    toString() {
      return this._range ? this._range.toString() : "";
    }
  
    // https://w3c.github.io/selection-api/#dfn-empty
    _isEmpty() {
      return this._range === null;
    }
  
    // https://w3c.github.io/selection-api/#dfn-anchor
    get _anchor() {
      if (!this._range) {
        return null;
      }
  
      return this._direction === SELECTION_DIRECTION.FORWARDS ?
        this._range._start :
        this._range._end;
    }
  
    // https://w3c.github.io/selection-api/#dfn-focus
    get _focus() {
      if (!this._range) {
        return null;
      }
  
      return this._direction === SELECTION_DIRECTION.FORWARDS ?
        this._range._end :
        this._range._start;
    }
  
    _associateRange(newRange) {
      this._range = newRange;
      this._direction = newRange === null ? SELECTION_DIRECTION.DIRECTIONLESS : SELECTION_DIRECTION.FORWARDS;
  
      // TODO: Emit "selectionchange" event. At this time, there is currently no test in WPT covering this.
      // https://w3c.github.io/selection-api/#selectionchange-event
    }
  }
  
  module.exports = {
    implementation: SelectionImpl
  };