all files / src/dm/ ve.dm.Selection.js

97.5% Statements 39/40
87.5% Branches 7/8
100% Functions 6/6
97.5% Lines 39/40
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                                                        186× 186×     186× 186×   185×     184×                                                                                                                                                                               232× 232× 339×   232×                   15×   15× 53×         15×               6611×                                             1347×                          
/*!
 * VisualEditor Selection class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * @class
 * @abstract
 * @constructor
 */
ve.dm.Selection = function VeDmSelection() {
};
 
/* Inheritance */
 
OO.initClass( ve.dm.Selection );
 
/* Static Properties */
 
ve.dm.Selection.static.type = null;
 
/* Static Methods */
 
/**
 * Create a new selection from a JSON serialization
 *
 * @param {string|Object} json JSON serialization or hash object
 * @return {ve.dm.Selection} New selection
 * @throws {Error} Unknown selection type
 */
ve.dm.Selection.static.newFromJSON = function ( json ) {
	var hash, constructor;
	Iif ( ve.dm.Document && arguments[ 0 ] instanceof ve.dm.Document ) {
		throw new Error( 'Got obsolete ve.dm.Document argument' );
	}
	hash = typeof json === 'string' ? JSON.parse( json ) : json;
	constructor = ve.dm.selectionFactory.lookup( hash.type );
 
	if ( !constructor ) {
		throw new Error( 'Unknown selection type ' + hash.name );
	}
 
	return constructor.static.newFromHash( hash );
};
 
/**
 * Create a new selection from a hash object
 *
 * @abstract
 * @param {Object} hash Hash object
 * @return {ve.dm.Selection} New selection
 */
ve.dm.Selection.static.newFromHash = null;
 
/* Methods */
 
/**
 * Get a JSON serialization of this selection
 *
 * @abstract
 * @param {string} [key] Key in parent object
 * @return {Object} Object for JSON serialization
 */
ve.dm.Selection.prototype.toJSON = null;
 
/**
 * Get a textual description of this selection, for debugging purposes
 *
 * @abstract
 * @return {string} Textual description
 */
ve.dm.Selection.prototype.getDescription = null;
 
/**
 * Get a new selection at the start point of this one
 *
 * @abstract
 * @return {ve.dm.Selection} Collapsed selection
 */
ve.dm.Selection.prototype.collapseToStart = null;
 
/**
 * Get a new selection at the end point of this one
 *
 * @abstract
 * @return {ve.dm.Selection} Collapsed selection
 */
ve.dm.Selection.prototype.collapseToEnd = null;
 
/**
 * Get a new selection at the 'from' point of this one
 *
 * @abstract
 * @return {ve.dm.Selection} Collapsed selection
 */
ve.dm.Selection.prototype.collapseToFrom = null;
 
/**
 * Get a new selection at the 'to' point of this one
 *
 * @abstract
 * @return {ve.dm.Selection} Collapsed selection
 */
ve.dm.Selection.prototype.collapseToTo = null;
 
/**
 * Check if a selection is collapsed
 *
 * @abstract
 * @return {boolean} Selection is collapsed
 */
ve.dm.Selection.prototype.isCollapsed = null;
 
/**
 * Apply translations from a transaction
 *
 * @abstract
 * @param {ve.dm.Transaction} tx Transaction
 * @param {boolean} [excludeInsertion] Do not grow to cover insertions at boundaries
 * @return {ve.dm.Selection} A new translated selection
 */
ve.dm.Selection.prototype.translateByTransaction = null;
 
/**
 * Apply translations from a transaction, with bias depending on author ID comparison
 *
 * @abstract
 * @param {ve.dm.Transaction} tx Transaction
 * @param {number} authorId The selection's author ID
 * @return {ve.dm.Selection} A new translated selection
 */
ve.dm.Selection.prototype.translateByTransactionWithAuthor = null;
 
/**
 * Apply translations from a set of transactions
 *
 * @param {ve.dm.Transaction[]} txs Transactions
 * @param {boolean} [excludeInsertion] Do not grow to cover insertions at boundaries
 * @return {ve.dm.Selection} A new translated selection
 */
ve.dm.Selection.prototype.translateByTransactions = function ( txs, excludeInsertion ) {
	var i, l, selection = this;
	for ( i = 0, l = txs.length; i < l; i++ ) {
		selection = selection.translateByTransaction( txs[ i ], excludeInsertion );
	}
	return selection;
};
 
/**
 * Apply translations from a change
 *
 * @param {ve.dm.Change} change The change
 * @param {number} authorId The author ID of this selection
 * @return {ve.dm.Selection} A new translated selection
 */
ve.dm.Selection.prototype.translateByChange = function ( change, authorId ) {
	var i, len,
		selection = this;
	for ( i = 0, len = change.transactions.length; i < len; i++ ) {
		selection = selection.translateByTransactionWithAuthor(
			change.transactions[ i ],
			authorId
		);
	}
	return selection;
};
 
/**
 * Check if this selection is null
 *
 * @return {boolean} The selection is null
 */
ve.dm.Selection.prototype.isNull = function () {
	return false;
};
 
/**
 * Get the content ranges for this selection
 *
 * @abstract
 * @param {ve.dm.Document} doc The document to which this selection applies
 * @return {ve.Range[]} Ranges
 */
ve.dm.Selection.prototype.getRanges = null;
 
/**
 * Get the covering linear range for this selection
 *
 * @abstract
 * @return {ve.Range|null} Covering range, if not null
 */
ve.dm.Selection.prototype.getCoveringRange = null;
 
/**
 * Get the name of the selection type
 *
 * @return {string} Selection type name
 */
ve.dm.Selection.prototype.getName = function () {
	return this.constructor.static.name;
};
 
/**
 * Check if two selections are equal
 *
 * @abstract
 * @param {ve.dm.Selection} other Other selection
 * @return {boolean} Selections are equal
 */
ve.dm.Selection.prototype.equals = null;
 
/* Factory */
 
ve.dm.selectionFactory = new OO.Factory();