Selaa lähdekoodia

Changed: Minor fixes.

Szymon Cofalik 8 vuotta sitten
vanhempi
commit
be8af47e2a

+ 1 - 2
packages/ckeditor5-engine/src/dev-utils/view.js

@@ -541,8 +541,7 @@ class RangeParser {
 				throw new Error( `Parse error - end of range was found '${ item.bracket }' but range was not started before.` );
 			}
 
-			// When second start of range is found when one is already opened - selection does not allow intersecting
-			// ranges.
+			// When second start of range is found when one is already opened - selection does not allow intersecting ranges.
 			if ( range && ( item.bracket == ELEMENT_RANGE_START_TOKEN || item.bracket == TEXT_RANGE_START_TOKEN ) ) {
 				throw new Error( `Parse error - start of range was found '${ item.bracket }' but one range is already started.` );
 			}

+ 5 - 19
packages/ckeditor5-engine/src/model/position.js

@@ -139,7 +139,7 @@ export default class Position {
 	 * @type {Number}
 	 */
 	get offset() {
-		return getOffset( this.path );
+		return last( this.path );
 	}
 
 	/**
@@ -365,8 +365,7 @@ export default class Position {
 	 */
 	getShiftedTo( offset ) {
 		const path = this.path.slice();
-
-		setOffset( path, offset );
+		path[ path.length - 1 ] = offset;
 
 		return new Position( this.root, path );
 	}
@@ -678,7 +677,9 @@ export default class Position {
 		// Then we have to update the rest of the path.
 
 		// Fix the offset because this position might be after `from` position and we have to reflect that.
-		setOffset( combinedPath, getOffset( combinedPath ) + this.path[ i ] - source.offset );
+		const oldOffset = last( combinedPath );
+		const newOffset = oldOffset + this.path[ i ] - source.offset;
+		combinedPath[ combinedPath.length - 1 ] = newOffset;
 
 		// Then, add the rest of the path.
 		// If this position is at the same level as `from` position nothing will get added.
@@ -828,21 +829,6 @@ export default class Position {
 	}
 }
 
-// Helper for setting offset on give path array.
-// @private
-// @param {Array.<Number>} path Position path. See {@link module:engine/model/position~Position#path}.
-function getOffset( path ) {
-	return last( path );
-}
-
-// Helper for setting offset on give path array.
-// @private
-// @param {Array.<Number>} path Position path. See {@link module:engine/model/position~Position#path}.
-// @param {Number} newOffset Offset to set.
-function setOffset( path, newOffset ) {
-	path[ path.length - 1 ] = newOffset;
-}
-
 /**
  * A flag indicating whether this position is `'before'` or `'after'` or `'same'` as given position.
  * If positions are in different roots `'different'` flag is returned.

+ 2 - 6
packages/ckeditor5-engine/src/model/range.js

@@ -311,11 +311,7 @@ export default class Range {
 				ranges.push( new Range( pos, pos.getShiftedBy( howMany ) ) );
 			}
 
-			const path = pos.getParentPath();
-			path[ path.length - 1 ]++;
-
-			pos = new Position( pos.root, path );
-
+			pos = Position.createAfter( posParent );
 			posParent = posParent.parent;
 		}
 
@@ -844,7 +840,7 @@ export default class Range {
 
 		// 5. Ranges should be checked and glued starting from the range that is closest to the reference range.
 		// Since ranges are sorted, start with the range with index that is closest to reference range index.
-		for ( let i = refIndex - 1; i >= 0; i++ ) {
+		for ( let i = refIndex - 1; i >= 0; i-- ) {
 			if ( ranges[ i ].end.isEqual( start ) ) {
 				start = ranges[ i ].start;
 			} else {

+ 2 - 2
packages/ckeditor5-engine/src/model/selection.js

@@ -236,9 +236,9 @@ export default class Selection {
 	 * @returns {module:engine/model/position~Position|null}
 	 */
 	getFirstPosition() {
-		const first = this.getFirstRange();
+		const firstRange = this.getFirstRange();
 
-		return first ? first.start : null;
+		return firstRange ? firstRange.start : null;
 	}
 
 	/**

+ 19 - 38
packages/ckeditor5-engine/src/model/treewalker.js

@@ -204,8 +204,6 @@ export default class TreeWalker {
 	 */
 	_next() {
 		const previousPosition = this.position;
-
-		let position = this.position;
 		const parent = this._visitedParent;
 
 		// We are at the end of the root.
@@ -223,17 +221,15 @@ export default class TreeWalker {
 		if ( node instanceof Element ) {
 			if ( !this.shallow ) {
 				// Manual operations on path internals for optimization purposes. Here and in the rest of the method.
-				const path = position.path.slice();
+				const path = this.position.path.slice();
 				path.push( 0 );
-				position = new Position( position.root, path );
+				this.position = new Position( this.position.root, path );
 
 				this._visitedParent = node;
 			} else {
-				position = position.getShiftedBy( 1 );
+				this.position = this.position.getShiftedBy( 1 );
 			}
 
-			this.position = position;
-
 			return formatReturnValue( 'elementStart', node, previousPosition, this.position, 1 );
 		} else if ( node instanceof Text ) {
 			let charactersCount;
@@ -247,30 +243,24 @@ export default class TreeWalker {
 					offset = this.boundaries.end.offset;
 				}
 
-				charactersCount = offset - position.offset;
+				charactersCount = offset - this.position.offset;
 			}
 
-			const offsetInTextNode = position.offset - node.startOffset;
+			const offsetInTextNode = this.position.offset - node.startOffset;
 			const item = new TextProxy( node, offsetInTextNode, charactersCount );
 
-			position = position.getShiftedBy( charactersCount );
-
-			this.position = position;
+			this.position = this.position.getShiftedBy( charactersCount );
 
 			return formatReturnValue( 'text', item, previousPosition, this.position, charactersCount );
 		} else {
 			// `node` is not set, we reached the end of current `parent`.
-			const path = position.getParentPath();
-			path[ path.length - 1 ]++;
-			position = new Position( position.root, path );
-
-			this.position = position;
+			this.position = Position.createAfter( parent );
 			this._visitedParent = parent.parent;
 
 			if ( this.ignoreElementEnd ) {
 				return this._next();
 			} else {
-				return formatReturnValue( 'elementEnd', parent, previousPosition, position );
+				return formatReturnValue( 'elementEnd', parent, previousPosition, this.position );
 			}
 		}
 	}
@@ -285,7 +275,6 @@ export default class TreeWalker {
 	 */
 	_previous() {
 		const previousPosition = this.position;
-		let position = this.position;
 		const parent = this._visitedParent;
 
 		// We are at the beginning of the root.
@@ -302,26 +291,22 @@ export default class TreeWalker {
 		const node = this.position.textNode ? this.position.textNode : this.position.nodeBefore;
 
 		if ( node instanceof Element ) {
-			position = position.getShiftedBy( -1 );
+			this.position = this.position.getShiftedBy( -1 );
 
 			if ( !this.shallow ) {
-				const path = position.path.slice();
+				const path = this.position.path.slice();
 				path.push( node.maxOffset );
 
-				position = new Position( position.root, path );
-
-				this.position = position;
+				this.position = new Position( this.position.root, path );
 				this._visitedParent = node;
 
 				if ( this.ignoreElementEnd ) {
 					return this._previous();
 				} else {
-					return formatReturnValue( 'elementEnd', node, previousPosition, position );
+					return formatReturnValue( 'elementEnd', node, previousPosition, this.position );
 				}
 			} else {
-				this.position = position;
-
-				return formatReturnValue( 'elementStart', node, previousPosition, position, 1 );
+				return formatReturnValue( 'elementStart', node, previousPosition, this.position, 1 );
 			}
 		} else if ( node instanceof Text ) {
 			let charactersCount;
@@ -335,25 +320,21 @@ export default class TreeWalker {
 					offset = this.boundaries.start.offset;
 				}
 
-				charactersCount = position.offset - offset;
+				charactersCount = this.position.offset - offset;
 			}
 
-			const offsetInTextNode = position.offset - node.startOffset;
+			const offsetInTextNode = this.position.offset - node.startOffset;
 			const item = new TextProxy( node, offsetInTextNode - charactersCount, charactersCount );
 
-			position = position.getShiftedBy( -charactersCount );
-
-			this.position = position;
+			this.position = this.position.getShiftedBy( -charactersCount );
 
-			return formatReturnValue( 'text', item, previousPosition, position, charactersCount );
+			return formatReturnValue( 'text', item, previousPosition, this.position, charactersCount );
 		} else {
 			// `node` is not set, we reached the beginning of current `parent`.
-			position = new Position( position.root, position.getParentPath() );
-
-			this.position = position;
+			this.position = Position.createBefore( parent );
 			this._visitedParent = parent.parent;
 
-			return formatReturnValue( 'elementStart', parent, previousPosition, position, 1 );
+			return formatReturnValue( 'elementStart', parent, previousPosition, this.position, 1 );
 		}
 	}
 }

+ 4 - 7
packages/ckeditor5-engine/src/view/position.js

@@ -13,9 +13,6 @@ import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import EditableElement from './editableelement';
 
-const _parent = Symbol( 'parent' );
-const _offset = Symbol( 'offset' );
-
 /**
  * Position in the tree. Position is always located before or after a node.
  */
@@ -27,8 +24,8 @@ export default class Position {
 	 * @param {Number} offset Position offset.
 	 */
 	constructor( parent, offset ) {
-		this[ _parent ] = parent;
-		this[ _offset ] = offset;
+		this._parent = parent;
+		this._offset = offset;
 	}
 
 	/**
@@ -38,7 +35,7 @@ export default class Position {
 	 * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
 	 */
 	get parent() {
-		return this[ _parent ];
+		return this._parent;
 	}
 
 	/**
@@ -48,7 +45,7 @@ export default class Position {
 	 * @type {Number}
 	 */
 	get offset() {
-		return this[ _offset ];
+		return this._offset;
 	}
 
 	/**

+ 4 - 7
packages/ckeditor5-engine/src/view/range.js

@@ -10,9 +10,6 @@
 import Position from './position';
 import TreeWalker from './treewalker';
 
-const _start = Symbol( 'start' );
-const _end = Symbol( 'end' );
-
 /**
  * Tree view range.
  */
@@ -26,8 +23,8 @@ export default class Range {
 	 * @param {module:engine/view/position~Position} [end] End position. If not set, range will be collapsed at `start` position.
 	 */
 	constructor( start, end = null ) {
-		this[ _start ] = start;
-		this[ _end ] = end ? end : start;
+		this._start = start;
+		this._end = end ? end : start;
 	}
 
 	/**
@@ -52,7 +49,7 @@ export default class Range {
 	 * @type {module:engine/view/position~Position}
 	 */
 	get start() {
-		return this[ _start ];
+		return this._start;
 	}
 
 	/**
@@ -62,7 +59,7 @@ export default class Range {
 	 * @type {module:engine/view/position~Position}
 	 */
 	get end() {
-		return this[ _end ];
+		return this._end;
 	}
 
 	/**