Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor Range class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * @class
9 *
10 * @constructor
11 * @param {number} [from=0] Anchor offset
12 * @param {number} [to=from] Focus offset
13 */
14 ve.Range = function VeRange( from, to ) {
15 // For ease of debugging, check arguments.length when applying defaults, to preserve
16 // invalid arguments such as undefined and NaN that indicate a programming error.
17 // Range calculation errors can often propagate quite far before surfacing, so the
18 // indication is important.
19 this.from = arguments.length >= 1 ? from : 0;
20 this.to = arguments.length >= 2 ? to : this.from;
21 this.start = this.from < this.to ? this.from : this.to;
22 this.end = this.from < this.to ? this.to : this.from;
23 };
24
25 /* Inheritance */
26
27 OO.initClass( ve.Range );
28
29 /**
30 * @property {number} from Starting offset
31 */
32
33 /**
34 * @property {number} to Ending offset
35 */
36
37 /**
38 * @property {number} start Starting offset (the lesser of #to and #from)
39 */
40
41 /**
42 * @property {number} end Ending offset (the greater of #to and #from)
43 */
44
45 /* Static Methods */
46
47 /**
48 * Create a new range from a JSON serialization of a range
49 *
50 * @see ve.Range#toJSON
51 *
52 * @param {string} json JSON serialization
53 * @return {ve.Range} New range
54 */
55 ve.Range.static.newFromJSON = function ( json ) {
56 return this.newFromHash( JSON.parse( json ) );
57 };
58
59 /**
60 * Create a new range from a range hash object
61 *
62 * @see ve.Range#toJSON
63 *
64 * @param {Object} hash
65 * @return {ve.Range} New range
66 */
67 ve.Range.static.newFromHash = function ( hash ) {
68 return new ve.Range( hash.from, hash.to );
69 };
70
71 /**
72 * Create a range object that covers all of the given ranges.
73 *
74 * @static
75 * @param {ve.Range[]} ranges Array of ve.Range objects (at least one)
76 * @param {boolean} backwards Return a backwards range
77 * @return {ve.Range} Range that spans all of the given ranges
78 */
79 ve.Range.static.newCoveringRange = function ( ranges, backwards ) {
80 if ( ranges.length === 0 ) {
81 throw new Error( 'newCoveringRange() requires at least one range' );
82 }
83 let minStart = ranges[ 0 ].start;
84 let maxEnd = ranges[ 0 ].end;
85 for ( let i = 1; i < ranges.length; i++ ) {
86 if ( ranges[ i ].start < minStart ) {
87 minStart = ranges[ i ].start;
88 }
89 if ( ranges[ i ].end > maxEnd ) {
90 maxEnd = ranges[ i ].end;
91 }
92 }
93 let range;
94 if ( backwards ) {
95 range = new ve.Range( maxEnd, minStart );
96 } else {
97 range = new ve.Range( minStart, maxEnd );
98 }
99 return range;
100 };
101
102 /* Methods */
103
104 /**
105 * Check if an offset is within the range.
106 *
107 * Specifically we mean the whole element at a specific offset, so in effect
108 * this is the same as #containsRange( new ve.Range( offset, offset + 1 ) ).
109 *
110 * @param {number} offset Offset to check
111 * @return {boolean} If offset is within the range
112 */
113 ve.Range.prototype.containsOffset = function ( offset ) {
114 return offset >= this.start && offset < this.end;
115 };
116
117 /**
118 * Check if another range is within the range.
119 *
120 * @param {ve.Range} range Range to check
121 * @return {boolean} If other range is within the range
122 */
123 ve.Range.prototype.containsRange = function ( range ) {
124 return range.start >= this.start && range.end <= this.end;
125 };
126
127 /**
128 * Check if another range is touching this one
129 *
130 * This includes ranges which touch this one, e.g. [1,3] & [3,5],
131 * ranges which overlap this one, and ranges which cover
132 * this one completely, e.g. [1,3] & [0,5].
133 *
134 * Useful for testing if two ranges can be joined (using #expand)
135 *
136 * @param {ve.Range} range Range to check
137 * @return {boolean} If other range touches this range
138 */
139 ve.Range.prototype.touchesRange = function ( range ) {
140 return range.end >= this.start && range.start <= this.end;
141 };
142
143 /**
144 * Check if another range overlaps this one
145 *
146 * This includes ranges which intersect this one, e.g. [1,3] & [2,4],
147 * and ranges which cover this one completely, e.g. [1,3] & [0,5],
148 * but *not* ranges which only touch, e.g. [0,2] & [2,4].
149 *
150 * @param {ve.Range} range Range to check
151 * @return {boolean} If other range overlaps this range
152 */
153 ve.Range.prototype.overlapsRange = function ( range ) {
154 return range.end > this.start && range.start < this.end;
155 };
156
157 /**
158 * Get the length of the range.
159 *
160 * @return {number} Length of range
161 */
162 ve.Range.prototype.getLength = function () {
163 return this.end - this.start;
164 };
165
166 /**
167 * Gets a range with reversed direction.
168 *
169 * @return {ve.Range} A new range
170 */
171 ve.Range.prototype.flip = function () {
172 return new ve.Range( this.to, this.from );
173 };
174
175 /**
176 * Get a range that's a translated version of this one.
177 *
178 * @param {number} distance Distance to move range by
179 * @return {ve.Range} New translated range
180 */
181 ve.Range.prototype.translate = function ( distance ) {
182 return new ve.Range( this.from + distance, this.to + distance );
183 };
184
185 /**
186 * Check if two ranges are equal, taking direction into account.
187 *
188 * @param {ve.Range|null} other
189 * @return {boolean}
190 */
191 ve.Range.prototype.equals = function ( other ) {
192 return other && this.from === other.from && this.to === other.to;
193 };
194
195 /**
196 * Check if two ranges are equal, ignoring direction.
197 *
198 * @param {ve.Range|null} other
199 * @return {boolean}
200 */
201 ve.Range.prototype.equalsSelection = function ( other ) {
202 return other && this.end === other.end && this.start === other.start;
203 };
204
205 /**
206 * Create a new range with a truncated length.
207 *
208 * @param {number} limit Maximum length of new range (negative for left-side truncation)
209 * @return {ve.Range} A new range
210 */
211 ve.Range.prototype.truncate = function ( limit ) {
212 if ( limit >= 0 ) {
213 return new ve.Range(
214 this.start, Math.min( this.start + limit, this.end )
215 );
216 } else {
217 return new ve.Range(
218 Math.max( this.end + limit, this.start ), this.end
219 );
220 }
221 };
222
223 /**
224 * Expand a range to include another range, preserving direction.
225 *
226 * @param {ve.Range} other Range to expand to include
227 * @return {ve.Range} Range covering this range and other
228 */
229 ve.Range.prototype.expand = function ( other ) {
230 return ve.Range.static.newCoveringRange( [ this, other ], this.isBackwards() );
231 };
232
233 /**
234 * Check if the range is collapsed.
235 *
236 * A collapsed range has equal start and end values making its length zero.
237 *
238 * @return {boolean} Range is collapsed
239 */
240 ve.Range.prototype.isCollapsed = function () {
241 return this.from === this.to;
242 };
243
244 /**
245 * Check if the range is backwards, i.e. from > to
246 *
247 * @return {boolean} Range is backwards
248 */
249 ve.Range.prototype.isBackwards = function () {
250 return this.from > this.to;
251 };
252
253 /**
254 * Get a object summarizing the range for JSON serialization
255 *
256 * @param {string} [key] Key in parent object
257 * @return {Object} Object for JSON serialization
258 */
259 ve.Range.prototype.toJSON = function () {
260 return {
261 type: 'range',
262 from: this.from,
263 to: this.to
264 };
265 };