Blame view

lib/jsdom/living/nodes/Text-impl.js 2.73 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
  "use strict";
  
  const SlotableMixinImpl = require("./Slotable-impl").implementation;
  const CharacterDataImpl = require("./CharacterData-impl").implementation;
  
  const { domSymbolTree } = require("../helpers/internal-constants");
  const DOMException = require("domexception/webidl2js-wrapper");
  const NODE_TYPE = require("../node-type");
  const { mixin } = require("../../utils");
  
  // https://dom.spec.whatwg.org/#text
  class TextImpl extends CharacterDataImpl {
    constructor(globalObject, args, privateData) {
      super(globalObject, args, {
        data: args[0],
        ...privateData
      });
  
      this._initSlotableMixin();
  
      this.nodeType = NODE_TYPE.TEXT_NODE;
    }
  
    // https://dom.spec.whatwg.org/#dom-text-splittext
    // https://dom.spec.whatwg.org/#concept-text-split
    splitText(offset) {
      const { length } = this;
  
      if (offset > length) {
        throw DOMException.create(this._globalObject, ["The index is not in the allowed range.", "IndexSizeError"]);
      }
  
      const count = length - offset;
      const newData = this.substringData(offset, count);
  
      const newNode = this._ownerDocument.createTextNode(newData);
  
      const parent = domSymbolTree.parent(this);
  
      if (parent !== null) {
        parent._insert(newNode, this.nextSibling);
  
        for (const range of this._referencedRanges) {
          const { _start, _end } = range;
  
          if (_start.node === this && _start.offset > offset) {
            range._setLiveRangeStart(newNode, _start.offset - offset);
          }
  
          if (_end.node === this && _end.offset > offset) {
            range._setLiveRangeEnd(newNode, _end.offset - offset);
          }
        }
  
        const nodeIndex = domSymbolTree.index(this);
        for (const range of parent._referencedRanges) {
          const { _start, _end } = range;
  
          if (_start.node === parent && _start.offset === nodeIndex + 1) {
            range._setLiveRangeStart(parent, _start.offset + 1);
          }
  
          if (_end.node === parent && _end.offset === nodeIndex + 1) {
            range._setLiveRangeEnd(parent, _end.offset + 1);
          }
        }
      }
  
      this.replaceData(offset, count, "");
  
      return newNode;
    }
  
    // https://dom.spec.whatwg.org/#dom-text-wholetext
    get wholeText() {
      let wholeText = this.textContent;
      let next;
      let current = this;
      while ((next = domSymbolTree.previousSibling(current)) && next.nodeType === NODE_TYPE.TEXT_NODE) {
        wholeText = next.textContent + wholeText;
        current = next;
      }
      current = this;
      while ((next = domSymbolTree.nextSibling(current)) && next.nodeType === NODE_TYPE.TEXT_NODE) {
        wholeText += next.textContent;
        current = next;
      }
      return wholeText;
    }
  }
  
  mixin(TextImpl.prototype, SlotableMixinImpl.prototype);
  
  module.exports = {
    implementation: TextImpl
  };