All files / src/ce/selections ve.ce.LinearSelection.js

92.92% Statements 92/99
80.3% Branches 53/66
100% Functions 9/9
92.78% Lines 90/97

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                          1x   1163x       1163x 1163x         1x       1x             1x 101x   101x 101x   101x 2x     99x 99x       99x           99x 99x 99x 7x     7x 7x 7x       99x 99x         99x 99x 154x   99x           1x 28x   28x 28x   28x 9x     19x           1x 92x   92x 92x   92x 11x     81x 81x         81x 81x       81x 5x     81x 81x 2x   79x                             1x 12x 12x       12x   12x 10x   10x 1x       1x 1x       2x     12x 2x     12x                 12x 12x   2x     10x 10x   10x   2x 2x     2x   8x     10x   1x 1x                 9x                           1x 13x                 1x 161x           1x 4504x           1x 3x 3x   3x         1x  
/*!
 * VisualEditor Linear Selection class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * @class
 * @extends ve.ce.Selection
 * @constructor
 * @param {ve.ce.Surface} surface
 * @param {ve.dm.Selection} model
 */
ve.ce.LinearSelection = function VeCeLinearSelection() {
	// Parent constructor
	ve.ce.LinearSelection.super.apply( this, arguments );
 
	// Properties
	// The focused node in the view when this selection was created, if one exists
	this.focusedNode = this.getSurface().getFocusedNode( this.getModel().getRange() );
	this.directionality = null;
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.LinearSelection, ve.ce.Selection );
 
/* Static Properties */
 
ve.ce.LinearSelection.static.name = 'linear';
 
/* Method */
 
/**
 * @inheritdoc
 */
ve.ce.LinearSelection.prototype.getSelectionRects = function () {
	const surface = this.getSurface();
 
	const range = this.getModel().getRange();
	const focusedNode = surface.getFocusedNode( range );
 
	if ( focusedNode ) {
		return focusedNode.getRects();
	}
 
	const nativeRange = surface.getNativeRange( range );
	Iif ( !nativeRange ) {
		return null;
	}
 
	let rects = [];
	// Support: Firefox, IE
	// Calling getClientRects sometimes fails:
	// * in Firefox on page load when the address bar is still focused
	// * in empty paragraphs
	// * near annotation nails
	try {
		rects = RangeFix.getClientRects( nativeRange );
		if ( !rects.length ) {
			throw new Error( 'getClientRects returned empty list' );
		}
	} catch ( e ) {
		const rect = this.getNodeClientRectFromRange( nativeRange );
		Eif ( rect ) {
			rects = [ rect ];
		}
	}
 
	const surfaceRect = surface.getSurface().getBoundingClientRect();
	Iif ( !rects || !surfaceRect ) {
		return null;
	}
 
	// TODO: Use .map
	const relativeRects = [];
	for ( let i = 0, l = rects.length; i < l; i++ ) {
		relativeRects.push( ve.translateRect( rects[ i ], -surfaceRect.left, -surfaceRect.top ) );
	}
	return relativeRects;
};
 
/**
 * @inheritdoc
 */
ve.ce.LinearSelection.prototype.getSelectionStartAndEndRects = function () {
	const surface = this.getSurface();
 
	const range = this.getModel().getRange();
	const focusedNode = surface.getFocusedNode( range );
 
	if ( focusedNode ) {
		return focusedNode.getStartAndEndRects();
	}
 
	return ve.getStartAndEndRects( this.getSelectionRects() );
};
 
/**
 * @inheritdoc
 */
ve.ce.LinearSelection.prototype.getSelectionBoundingRect = function () {
	const surface = this.getSurface();
 
	const range = this.getModel().getRange();
	const focusedNode = surface.getFocusedNode( range );
 
	if ( focusedNode ) {
		return focusedNode.getBoundingRect();
	}
 
	const nativeRange = surface.getNativeRange( range );
	Iif ( !nativeRange ) {
		return null;
	}
 
	let boundingRect;
	try {
		boundingRect = RangeFix.getBoundingClientRect( nativeRange );
	} catch ( e ) {
		boundingRect = null;
	}
	if ( !boundingRect ) {
		boundingRect = this.getNodeClientRectFromRange( nativeRange );
	}
 
	const surfaceRect = surface.getSurface().getBoundingClientRect();
	if ( !boundingRect || !surfaceRect ) {
		return null;
	}
	return ve.translateRect( boundingRect, -surfaceRect.left, -surfaceRect.top );
};
 
/**
 * Get a client rect from the range's end node
 *
 * This function is used internally by getSelectionRects and
 * getSelectionBoundingRect as a fallback when Range.getClientRects
 * fails. The width is hard-coded to 0 as the function is used to
 * locate the selection focus position.
 *
 * @private
 * @param {Range} range Range to get client rect for
 * @return {Object|null} ClientRect-like object
 */
ve.ce.LinearSelection.prototype.getNodeClientRectFromRange = function ( range ) {
	const containerNode = range.endContainer,
		offset = range.endOffset;
 
	let node;
	let fixHeight;
	Iif ( containerNode.nodeType === Node.TEXT_NODE && ( offset === 0 || offset === containerNode.length ) ) {
		node = offset ? containerNode.previousSibling : containerNode.nextSibling;
	} else if ( containerNode.nodeType === Node.ELEMENT_NODE ) {
		node = offset === containerNode.childNodes.length ? containerNode.lastChild : containerNode.childNodes[ offset ];
		// Nail heights are 0, so use the annotation's height
		if ( node && node.nodeType === Node.ELEMENT_NODE && node.classList.contains( 've-ce-nail' ) ) {
			const annotationNode = offset ? node.previousSibling : node.nextSibling;
			// Sometimes annotationNode isn't an HTMLElement (T261992). Not sure
			// when this happens, but we will still return a sensible rectangle
			// without fixHeight isn't set.
			Eif ( annotationNode instanceof HTMLElement ) {
				fixHeight = annotationNode.offsetHeight;
			}
		}
	} else {
		node = containerNode;
	}
 
	while ( node && node.nodeType !== Node.ELEMENT_NODE ) {
		node = node.parentNode;
	}
 
	Iif ( !node ) {
		return null;
	}
 
	// When possible, pretend the cursor is the left/right border of the node
	// (depending on directionality) as a fallback.
 
	// We would use getBoundingClientRect(), but in iOS7 that's relative to the
	// document rather than to the viewport
	const rect = node.getClientRects()[ 0 ];
	if ( !rect ) {
		// FF can return null when focusNode is invisible
		return null;
	}
 
	const side = $( node ).css( 'direction' ) === 'rtl' ? 'right' : 'left';
	const adjacentNode = range.endContainer.childNodes[ range.endOffset ];
	let x;
	if ( range.collapsed && adjacentNode && adjacentNode.classList && adjacentNode.classList.contains( 've-ce-unicorn' ) ) {
		// We're next to a unicorn; use its left/right position
		const unicornRect = adjacentNode.getClientRects()[ 0 ];
		Iif ( !unicornRect ) {
			return null;
		}
		x = unicornRect[ side ];
	} else {
		x = rect[ side ];
	}
 
	if ( fixHeight ) {
		// Use a pre-computed height from above, maintaining the vertical center
		const middle = ( rect.top + rect.bottom ) / 2;
		return {
			top: middle - ( fixHeight / 2 ),
			bottom: middle + ( fixHeight / 2 ),
			left: x,
			right: x,
			width: 0,
			height: fixHeight
		};
	} else {
		return {
			top: rect.top,
			bottom: rect.bottom,
			left: x,
			right: x,
			width: 0,
			height: rect.height
		};
	}
};
 
/**
 * @inheritdoc
 */
ve.ce.LinearSelection.prototype.getSelectionFocusRect = function () {
	return !this.isNativeCursor() ?
		// Don't collapse selection for focus rect if we are on a focusable node.
		this.getSelectionBoundingRect() :
		ve.ce.LinearSelection.super.prototype.getSelectionFocusRect.call( this );
};
 
/**
 * @inheritdoc
 */
ve.ce.LinearSelection.prototype.isFocusedNode = function () {
	return !!this.focusedNode;
};
 
/**
 * @inheritdoc
 */
ve.ce.LinearSelection.prototype.isNativeCursor = function () {
	return !this.focusedNode;
};
 
/**
 * @inheritdoc
 */
ve.ce.LinearSelection.prototype.getDirectionality = function ( doc ) {
	Eif ( !this.directionality ) {
		this.directionality = doc.getDirectionalityFromRange( this.getModel().getRange() );
	}
	return this.directionality;
};
 
/* Registration */
 
ve.ce.selectionFactory.register( ve.ce.LinearSelection );