Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 42041x 42041x 42041x 42041x 1x 1x 2x 1x 211x 1x 12x 1x 11x 11x 11x 11x 3x 11x 8x 11x 3x 8x 11x 1x 12x 1x 1326x 1x 7x 1x 7x 1x 1757x 1x 32x 1x 5x 1x 2531x 1x 1879x 1x 14x 10x 4x 1x 7x 1x 38233x 1x 4236x 1x 938x | /*!
* VisualEditor Range class.
*
* @copyright See AUTHORS.txt
*/
/**
* @class
*
* @constructor
* @param {number} [from=0] Anchor offset
* @param {number} [to=from] Focus offset
*/
ve.Range = function VeRange( from, to ) {
// For ease of debugging, check arguments.length when applying defaults, to preserve
// invalid arguments such as undefined and NaN that indicate a programming error.
// Range calculation errors can often propagate quite far before surfacing, so the
// indication is important.
this.from = arguments.length >= 1 ? from : 0;
this.to = arguments.length >= 2 ? to : this.from;
this.start = this.from < this.to ? this.from : this.to;
this.end = this.from < this.to ? this.to : this.from;
};
/* Inheritance */
OO.initClass( ve.Range );
/**
* @property {number} from Starting offset
*/
/**
* @property {number} to Ending offset
*/
/**
* @property {number} start Starting offset (the lesser of #to and #from)
*/
/**
* @property {number} end Ending offset (the greater of #to and #from)
*/
/* Static Methods */
/**
* Create a new range from a JSON serialization of a range
*
* @see ve.Range#toJSON
*
* @param {string} json JSON serialization
* @return {ve.Range} New range
*/
ve.Range.static.newFromJSON = function ( json ) {
return this.newFromHash( JSON.parse( json ) );
};
/**
* Create a new range from a range hash object
*
* @see ve.Range#toJSON
*
* @param {Object} hash
* @return {ve.Range} New range
*/
ve.Range.static.newFromHash = function ( hash ) {
return new ve.Range( hash.from, hash.to );
};
/**
* Create a range object that covers all of the given ranges.
*
* @static
* @param {ve.Range[]} ranges Array of ve.Range objects (at least one)
* @param {boolean} backwards Return a backwards range
* @return {ve.Range} Range that spans all of the given ranges
*/
ve.Range.static.newCoveringRange = function ( ranges, backwards ) {
if ( ranges.length === 0 ) {
throw new Error( 'newCoveringRange() requires at least one range' );
}
let minStart = ranges[ 0 ].start;
let maxEnd = ranges[ 0 ].end;
for ( let i = 1; i < ranges.length; i++ ) {
if ( ranges[ i ].start < minStart ) {
minStart = ranges[ i ].start;
}
if ( ranges[ i ].end > maxEnd ) {
maxEnd = ranges[ i ].end;
}
}
let range;
if ( backwards ) {
range = new ve.Range( maxEnd, minStart );
} else {
range = new ve.Range( minStart, maxEnd );
}
return range;
};
/* Methods */
/**
* Check if an offset is within the range.
*
* Specifically we mean the whole element at a specific offset, so in effect
* this is the same as #containsRange( new ve.Range( offset, offset + 1 ) ).
*
* @param {number} offset Offset to check
* @return {boolean} If offset is within the range
*/
ve.Range.prototype.containsOffset = function ( offset ) {
return offset >= this.start && offset < this.end;
};
/**
* Check if another range is within the range.
*
* @param {ve.Range} range Range to check
* @return {boolean} If other range is within the range
*/
ve.Range.prototype.containsRange = function ( range ) {
return range.start >= this.start && range.end <= this.end;
};
/**
* Check if another range is touching this one
*
* This includes ranges which touch this one, e.g. [1,3] & [3,5],
* ranges which overlap this one, and ranges which cover
* this one completely, e.g. [1,3] & [0,5].
*
* Useful for testing if two ranges can be joined (using #expand)
*
* @param {ve.Range} range Range to check
* @return {boolean} If other range touches this range
*/
ve.Range.prototype.touchesRange = function ( range ) {
return range.end >= this.start && range.start <= this.end;
};
/**
* Check if another range overlaps this one
*
* This includes ranges which intersect this one, e.g. [1,3] & [2,4],
* and ranges which cover this one completely, e.g. [1,3] & [0,5],
* but *not* ranges which only touch, e.g. [0,2] & [2,4].
*
* @param {ve.Range} range Range to check
* @return {boolean} If other range overlaps this range
*/
ve.Range.prototype.overlapsRange = function ( range ) {
return range.end > this.start && range.start < this.end;
};
/**
* Get the length of the range.
*
* @return {number} Length of range
*/
ve.Range.prototype.getLength = function () {
return this.end - this.start;
};
/**
* Gets a range with reversed direction.
*
* @return {ve.Range} A new range
*/
ve.Range.prototype.flip = function () {
return new ve.Range( this.to, this.from );
};
/**
* Get a range that's a translated version of this one.
*
* @param {number} distance Distance to move range by
* @return {ve.Range} New translated range
*/
ve.Range.prototype.translate = function ( distance ) {
return new ve.Range( this.from + distance, this.to + distance );
};
/**
* Check if two ranges are equal, taking direction into account.
*
* @param {ve.Range|null} other
* @return {boolean}
*/
ve.Range.prototype.equals = function ( other ) {
return other && this.from === other.from && this.to === other.to;
};
/**
* Check if two ranges are equal, ignoring direction.
*
* @param {ve.Range|null} other
* @return {boolean}
*/
ve.Range.prototype.equalsSelection = function ( other ) {
return other && this.end === other.end && this.start === other.start;
};
/**
* Create a new range with a truncated length.
*
* @param {number} limit Maximum length of new range (negative for left-side truncation)
* @return {ve.Range} A new range
*/
ve.Range.prototype.truncate = function ( limit ) {
if ( limit >= 0 ) {
return new ve.Range(
this.start, Math.min( this.start + limit, this.end )
);
} else {
return new ve.Range(
Math.max( this.end + limit, this.start ), this.end
);
}
};
/**
* Expand a range to include another range, preserving direction.
*
* @param {ve.Range} other Range to expand to include
* @return {ve.Range} Range covering this range and other
*/
ve.Range.prototype.expand = function ( other ) {
return ve.Range.static.newCoveringRange( [ this, other ], this.isBackwards() );
};
/**
* Check if the range is collapsed.
*
* A collapsed range has equal start and end values making its length zero.
*
* @return {boolean} Range is collapsed
*/
ve.Range.prototype.isCollapsed = function () {
return this.from === this.to;
};
/**
* Check if the range is backwards, i.e. from > to
*
* @return {boolean} Range is backwards
*/
ve.Range.prototype.isBackwards = function () {
return this.from > this.to;
};
/**
* Get a object summarizing the range for JSON serialization
*
* @param {string} [key] Key in parent object
* @return {Object} Object for JSON serialization
*/
ve.Range.prototype.toJSON = function () {
return {
type: 'range',
from: this.from,
to: this.to
};
};
|