| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- 'use strict';
- CKEDITOR.define( [ 'document/rootelement', 'utils', 'ckeditorerror' ], ( RootElement, utils, CKEditorError ) => {
- const SAME = 0;
- const AFTER = 1;
- const BEFORE = 2;
- const DIFFERENT = 3;
- /**
- * Position in the tree. Position is always located before or after a node.
- * See {@link #path} property for more information.
- *
- * @class document.Position
- */
- class Position {
- /**
- * Creates a position.
- *
- * @param {Array} path Position path. See {@link #path} property for more information.
- * @param {document.RootElement} root Root element for the path. Note that this element can not have a parent.
- * @constructor
- */
- constructor( path, root ) {
- /**
- * Position of the node it the tree. For example:
- *
- * root
- * |- p Before: [ 0 ] After: [ 1 ]
- * |- ul Before: [ 1 ] After: [ 2 ]
- * |- li Before: [ 1, 0 ] After: [ 1, 1 ]
- * | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
- * | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
- * | |- o Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
- * |- li Before: [ 1, 1 ] After: [ 1, 2 ]
- * |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
- * |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
- * |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
- *
- * @type {Number[]}
- */
- this.path = path;
- if ( !( root instanceof RootElement ) ) {
- /**
- * Position root has to be an instance of RootElement.
- *
- * @error position-root-not-rootelement
- * @param root
- */
- throw new CKEditorError( 'position-root-not-rootelement: Position root has to be an instance of RootElement.', { root: root } );
- }
- /**
- * Root element for the path. Note that this element can not have a parent.
- *
- * @type {document.RootElement}
- */
- this.root = root;
- }
- /**
- * Node directly after the position.
- *
- * @readonly
- * @property {document.Node}
- */
- get nodeAfter() {
- return this.parent.getChild( this.offset ) || null;
- }
- /**
- * Node directly before the position.
- *
- * @readonly
- * @type {document.Node}
- */
- get nodeBefore() {
- return this.parent.getChild( this.offset - 1 ) || null;
- }
- /**
- * Offset at which the position is located in the {@link #parent}.
- *
- * @readonly
- * @property {Number} offset
- */
- get offset() {
- return utils.last( this.path );
- }
- /**
- * Sets offset in the parent, which is the last element of the path.
- */
- set offset( newOffset ) {
- this.path[ this.path.length - 1 ] = newOffset;
- }
- /**
- * Parent element of the position. The position is located at {@link #offset} in this element.
- *
- * @readonly
- * @property {document.Element} parent
- */
- get parent() {
- let parent = this.root;
- let i, len;
- for ( i = 0, len = this.path.length - 1; i < len; i++ ) {
- parent = parent.getChild( this.path[ i ] );
- }
- return parent;
- }
- /**
- * Creates and returns a new instance of {@link document.Position}
- * which is equal to this {@link document.Position position}.
- *
- * @returns {document.Position} Cloned {@link document.Position position}.
- */
- clone() {
- return new Position( this.path.slice(), this.root );
- }
- /**
- * Checks whether this position is before or after given position.
- *
- * @param {document.Position} otherPosition Position to compare with.
- * @returns {Number} A flag indicating whether this position is {@link #BEFORE} or
- * {@link #AFTER} or {@link #SAME} as given position. If positions are in different roots,
- * {@link #DIFFERENT} flag is returned.
- */
- compareWith( otherPosition ) {
- if ( this.root != otherPosition.root ) {
- return DIFFERENT;
- }
- const result = utils.compareArrays( this.path, otherPosition.path );
- switch ( result ) {
- case utils.compareArrays.SAME:
- return SAME;
- case utils.compareArrays.PREFIX:
- return BEFORE;
- case utils.compareArrays.EXTENSION:
- return AFTER;
- default:
- if ( this.path[ result ] < otherPosition.path[ result ] ) {
- return BEFORE;
- } else {
- return AFTER;
- }
- }
- }
- /**
- * Returns the path to the parent, which is the {@link document.Position#path} without the last element.
- *
- * This method returns the parent path even if the parent does not exists.
- *
- * @returns {Number[]} Path to the parent.
- */
- getParentPath() {
- return this.path.slice( 0, -1 );
- }
- /**
- * Returns this position after being updated by removing `howMany` nodes starting from `deletePosition`.
- * It may happen that this position is in a removed node. If that is the case, `null` is returned instead.
- *
- * @param {document.Position} deletePosition Position before the first removed node.
- * @param {Number} howMany How many nodes are removed.
- * @returns {document.Position|null} Transformed position or `null`.
- */
- getTransformedByDeletion( deletePosition, howMany ) {
- let transformed = this.clone();
- // This position can't be affected if deletion was in a different root.
- if ( this.root != deletePosition.root ) {
- return transformed;
- }
- if ( utils.compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.SAME ) {
- // If nodes are removed from the node that is pointed by this position...
- if ( deletePosition.offset < this.offset ) {
- // And are removed from before an offset of that position...
- // Decrement the offset accordingly.
- if ( deletePosition.offset + howMany > this.offset ) {
- transformed.offset = deletePosition.offset;
- } else {
- transformed.offset -= howMany;
- }
- }
- } else if ( utils.compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.PREFIX ) {
- // If nodes are removed from a node that is on a path to this position...
- const i = deletePosition.path.length - 1;
- if ( deletePosition.offset < this.path[ i ] ) {
- // And are removed from before next node of that path...
- if ( deletePosition.offset + howMany > this.path[ i ] ) {
- // If the next node of that path is removed return null
- // because the node containing this position got removed.
- return null;
- } else {
- // Otherwise, decrement index on that path.
- transformed.path[ i ] -= howMany;
- }
- }
- }
- return transformed;
- }
- static createFromPositionAndShift( position, shift ) {
- let newPosition = new Position( utils.clone( position.path ), position.root );
- newPosition.offset = newPosition.offset + shift;
- return newPosition;
- }
- /**
- * Returns this position after being updated by inserting `howMany` nodes at `insertPosition`.
- *
- * @param {document.Position} insertPosition Position where nodes are inserted.
- * @param {Number} howMany How many nodes are inserted.
- * @param {Boolean} insertBefore Flag indicating whether nodes are inserted before or after `insertPosition`.
- * This is important only when `insertPosition` and this position are same. If that is the case and the flag is
- * set to `true`, this position will get transformed. If the flag is set to `false`, it won't.
- * @returns {document.Position} Transformed position.
- */
- getTransformedByInsertion( insertPosition, howMany, insertBefore ) {
- let transformed = this.clone();
- // This position can't be affected if insertion was in a different root.
- if ( this.root != insertPosition.root ) {
- return transformed;
- }
- if ( utils.compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.SAME ) {
- // If nodes are inserted in the node that is pointed by this position...
- if ( insertPosition.offset < this.offset || ( insertPosition.offset == this.offset && insertBefore ) ) {
- // And are inserted before an offset of that position...
- // "Push" this positions offset.
- transformed.offset += howMany;
- }
- } else if ( utils.compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == utils.compareArrays.PREFIX ) {
- // If nodes are inserted in a node that is on a path to this position...
- const i = insertPosition.path.length - 1;
- if ( insertPosition.offset <= this.path[ i ] ) {
- // And are inserted before next node of that path...
- // "Push" the index on that path.
- transformed.path[ i ] += howMany;
- }
- }
- return transformed;
- }
- /**
- * Returns this position after being updated by moving `howMany` attributes from `sourcePosition` to `targetPosition`.
- *
- * @param {document.Position} sourcePosition Position before the first element to move.
- * @param {document.Position} targetPosition Position where moved elements will be inserted.
- * @param {Number} howMany How many consecutive nodes to move, starting from `sourcePosition`.
- * @param {Boolean} insertBefore Flag indicating whether moved nodes are pasted before or after `insertPosition`.
- * This is important only when `targetPosition` and this position are same. If that is the case and the flag is
- * set to `true`, this position will get transformed by range insertion. If the flag is set to `false`, it won't.
- * @returns {document.Position} Transformed position.
- */
- getTransformedByMove( sourcePosition, targetPosition, howMany, insertBefore ) {
- // Moving a range removes nodes from their original position. We acknowledge this by proper transformation.
- let transformed = this.getTransformedByDeletion( sourcePosition, howMany );
- if ( transformed !== null ) {
- // This position is not inside a removed node.
- // Next step is to reflect pasting nodes, which might further affect the position.
- transformed = transformed.getTransformedByInsertion( targetPosition, howMany, insertBefore );
- } else {
- // This position is inside a removed node. In this case, we are unable to simply transform it by range insertion.
- // Instead, we calculate a combination of this position, move source position and target position.
- transformed = this._getCombined( sourcePosition, targetPosition );
- }
- return transformed;
- }
- /**
- * Checks whether this position is after given position.
- *
- * **Note:** see {document.Position#isBefore}.
- *
- * @param {document.Position} otherPosition Position to compare with.
- * @returns {Boolean} True if this position is after given position.
- */
- isAfter( otherPosition ) {
- return this.compareWith( otherPosition ) == AFTER;
- }
- /**
- * Checks whether this position is before given position.
- *
- * **Note:** watch out when using negation of the value returned by this method, because the negation will also
- * be `true` if positions are in different roots and you might not expect this. You should probably use
- * `a.isAfter( b ) || a.isEqual( b )` or `!a.isBefore( p ) && a.root == b.root` in most scenarios. If your
- * condition uses multiple `isAfter` and `isBefore` checks, build them so they do not use negated values, i.e.:
- *
- * if ( a.isBefore( b ) && c.isAfter( d ) ) {
- * // do A.
- * } else {
- * // do B.
- * }
- *
- * or, if you have only one if-branch:
- *
- * if ( !( a.isBefore( b ) && c.isAfter( d ) ) {
- * // do B.
- * }
- *
- * rather than:
- *
- * if ( !a.isBefore( b ) || && !c.isAfter( d ) ) {
- * // do B.
- * } else {
- * // do A.
- * }
- *
- * @param {document.Position} otherPosition Position to compare with.
- * @returns {Boolean} True if this position is before given position.
- */
- isBefore( otherPosition ) {
- return this.compareWith( otherPosition ) == BEFORE;
- }
- /**
- * Checks whether this position equals given position.
- *
- * @param {document.Position} otherPosition Position to compare with.
- * @returns {Boolean} True if positions are same.
- */
- isEqual( otherPosition ) {
- return this.compareWith( otherPosition ) == SAME;
- }
- /**
- * Creates a new position after given node.
- *
- * @param {document.Node} node Node the position should be directly after.
- * @returns {document.Position}
- */
- static createAfter( node ) {
- if ( !node.parent ) {
- /**
- * You can not make position after root.
- *
- * @error position-after-root
- * @param {document.Node} root
- */
- throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
- }
- return Position.createFromParentAndOffset( node.parent, node.getIndex() + 1 );
- }
- /**
- * Creates a new position before the given node.
- *
- * @param {document.node} node Node the position should be directly before.
- * @returns {document.Position}
- */
- static createBefore( node ) {
- if ( !node.parent ) {
- /**
- * You can not make position before root.
- *
- * @error position-before-root
- * @param {document.Node} root
- */
- throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
- }
- return Position.createFromParentAndOffset( node.parent, node.getIndex() );
- }
- /**
- * Creates a new position from the parent element and the offset in that element.
- *
- * @param {document.Element} parent Position parent element.
- * @param {Number} offset Position offset.
- * @returns {document.Position}
- */
- static createFromParentAndOffset( parent, offset ) {
- const path = parent.getPath();
- path.push( offset );
- return new Position( path, parent.root );
- }
- /**
- * Returns a new position that is a combination of this position and given positions. The combined
- * position is this position transformed by moving a range starting at `from` to `to` position.
- * It is expected that this position is inside the moved range.
- *
- * In other words, this method in a smart way "cuts out" `source` path from this position and
- * injects `target` path in it's place, while doing necessary fixes in order to get a correct path.
- *
- * Example:
- *
- * let original = new Position( [ 2, 3, 1 ], root );
- * let source = new Position( [ 2, 2 ], root );
- * let target = new Position( [ 1, 1, 3 ], otherRoot );
- * let combined = original.getCombined( source, target );
- * // combined.path is [ 1, 1, 4, 1 ], combined.root is otherRoot
- *
- * Explanation:
- *
- * We have a position `[ 2, 3, 1 ]` and move some nodes from `[ 2, 2 ]` to `[ 1, 1, 3 ]`. The original position
- * was inside moved nodes and now should point to the new place. The moved nodes will be after
- * positions `[ 1, 1, 3 ]`, `[ 1, 1, 4 ]`, `[ 1, 1, 5 ]`. Since our position was in the second moved node,
- * the transformed position will be in a sub-tree of a node at `[ 1, 1, 4 ]`. Looking at original path, we
- * took care of `[ 2, 3 ]` part of it. Now we have to add the rest of the original path to the transformed path.
- * Finally, the transformed position will point to `[ 1, 1, 4, 1 ]`.
- *
- * @protected
- * @param {document.Position} source Beginning of the moved range.
- * @param {document.Position} target Position where the range is moved.
- * @returns {document.Position} Combined position.
- */
- _getCombined( source, target ) {
- const i = source.path.length - 1;
- // The first part of a path to combined position is a path to the place where nodes were moved.
- let combined = target.clone();
- // 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.
- combined.offset = combined.offset + this.path[ i ] - source.offset;
- // Then, add the rest of the path.
- // If this position is at the same level as `from` position nothing will get added.
- combined.path = combined.path.concat( this.path.slice( i + 1 ) );
- return combined;
- }
- }
- /**
- * Flag for "is after" relation between Positions.
- *
- * @type {Number}
- */
- Position.AFTER = AFTER;
- /**
- * Flag for "is before" relation between Positions.
- *
- * @type {Number}
- */
- Position.BEFORE = BEFORE;
- /**
- * Flag for "are in different roots" relation between Positions.
- *
- * @type {Number}
- */
- Position.DIFFERENT = DIFFERENT;
- /**
- * Flag for "are same" relation between Positions.
- *
- * @type {Number}
- */
- Position.SAME = SAME;
- return Position;
- } );
|