Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ve.SelectionState.js

raw symbols SCIP
1 /*!
2 * VisualEditor DOM selection-like class
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Like the DOM Selection object, but not updated live from the actual selection
9 *
10 * WARNING: the Nodes are still live and mutable, which can change the meaning
11 * of the offsets or invalidate the value of isBackwards.
12 *
13 * @class
14 *
15 * @constructor
16 * @param {ve.SelectionState|Selection|Object} selection DOM Selection-like object
17 * @param {Node|null} selection.anchorNode The anchor node (null if no selection)
18 * @param {number} selection.anchorOffset The anchor offset (0 if no selection)
19 * @param {Node|null} selection.focusNode The focus node (null if no selection)
20 * @param {number} selection.focusOffset The focus offset (0 if no selection)
21 * @param {boolean} [selection.isCollapsed] Whether the anchor and focus are the same
22 * @param {boolean} [selection.isBackwards] Whether the focus is before the anchor in document order
23 */
24 ve.SelectionState = function VeSelectionState( selection ) {
25 this.anchorNode = selection.anchorNode;
26 this.anchorOffset = selection.anchorOffset;
27 this.focusNode = selection.focusNode;
28 this.focusOffset = selection.focusOffset;
29
30 this.isCollapsed = selection.isCollapsed;
31 if ( this.isCollapsed === undefined ) {
32 // Set to true if nodes are null (matches DOM Selection object's behaviour)
33 this.isCollapsed = this.anchorNode === this.focusNode &&
34 this.anchorOffset === this.focusOffset;
35 }
36 this.isBackwards = selection.isBackwards;
37 if ( this.isBackwards === undefined ) {
38 // Set to false if nodes are null or focus is no earlier than anchor
39 this.isBackwards = ve.compareDocumentOrder(
40 this.focusNode,
41 this.focusOffset,
42 this.anchorNode,
43 this.anchorOffset
44 ) < 0;
45 }
46 };
47
48 /* Inheritance */
49
50 OO.initClass( ve.SelectionState );
51
52 /* Static methods */
53
54 /**
55 * Create a selection state object representing no selection
56 *
57 * @return {ve.SelectionState} Object representing no selection
58 */
59 ve.SelectionState.static.newNullSelection = function () {
60 return new ve.SelectionState( {
61 focusNode: null,
62 focusOffset: 0,
63 anchorNode: null,
64 anchorOffset: 0
65 } );
66 };
67
68 /* Methods */
69
70 /**
71 * Returns the selection with the anchor and focus swapped
72 *
73 * @return {ve.SelectionState} selection with anchor/focus swapped. Object-identical to this if isCollapsed
74 */
75 ve.SelectionState.prototype.flip = function () {
76 if ( this.isCollapsed ) {
77 return this;
78 }
79 return new ve.SelectionState( {
80 anchorNode: this.focusNode,
81 anchorOffset: this.focusOffset,
82 focusNode: this.anchorNode,
83 focusOffset: this.anchorOffset,
84 isCollapsed: false,
85 isBackwards: !this.isBackwards
86 } );
87 };
88
89 /**
90 * Whether the selection represents is the same range as another DOM Selection-like object
91 *
92 * @param {Object} other DOM Selection-like object
93 * @return {boolean} True if the anchors/focuses are equal (including null)
94 */
95 ve.SelectionState.prototype.equalsSelection = function ( other ) {
96 return this.anchorNode === other.anchorNode &&
97 this.anchorOffset === other.anchorOffset &&
98 this.focusNode === other.focusNode &&
99 this.focusOffset === other.focusOffset;
100 };
101
102 /**
103 * Get a range representation of the selection
104 *
105 * N.B. Range objects do not show whether the selection is backwards
106 *
107 * @param {HTMLDocument} doc The owner document of the selection nodes
108 * @return {Range|null}
109 */
110 ve.SelectionState.prototype.getNativeRange = function ( doc ) {
111 if ( this.anchorNode === null ) {
112 return null;
113 }
114 const range = doc.createRange();
115 try {
116 if ( this.isBackwards ) {
117 range.setStart( this.focusNode, this.focusOffset );
118 range.setEnd( this.anchorNode, this.anchorOffset );
119 } else {
120 range.setStart( this.anchorNode, this.anchorOffset );
121 if ( !this.isCollapsed ) {
122 range.setEnd( this.focusNode, this.focusOffset );
123 }
124 }
125 } catch ( e ) {
126 // Range#setStart/setEnd can throw exceptions with invalid offsets (T258191)
127 return null;
128 }
129 return range;
130 };