Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1368 from ckeditor/t/1365
Browse files Browse the repository at this point in the history
Feature: Introduced `#isBefore` and `#isAfter` in `model.Node` and `view.Node`. Additionally, `model.Node#is` and `view.Node#is` and `view.Node#getPath` were added. Closes #1365.
  • Loading branch information
oskarwrobel committed Mar 21, 2018
2 parents f8dec1e + a163b72 commit 4c38683
Show file tree
Hide file tree
Showing 18 changed files with 378 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/model/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class Element extends Node {
*/
is( type, name = null ) {
if ( !name ) {
return type == 'element' || type == this.name;
return type == 'element' || type == this.name || super.is( type );
} else {
return type == 'element' && name == this.name;
}
Expand Down
63 changes: 62 additions & 1 deletion src/model/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';

/**
* Model node. Most basic structure of model tree.
Expand Down Expand Up @@ -273,6 +274,63 @@ export default class Node {
return i === 0 ? null : ancestorsA[ i - 1 ];
}

/**
* Returns whether this node is before given node. `false` is returned if nodes are in different trees (for example,
* in different {@link module:engine/model/documentfragment~DocumentFragment}s).
*
* @param {module:engine/model/node~Node} node Node to compare with.
* @returns {Boolean}
*/
isBefore( node ) {
// Given node is not before this node if they are same.
if ( this == node ) {
return false;
}

// Return `false` if it is impossible to compare nodes.
if ( this.root !== node.root ) {
return false;
}

const thisPath = this.getPath();
const nodePath = node.getPath();

const result = compareArrays( thisPath, nodePath );

switch ( result ) {
case 'prefix':
return true;

case 'extension':
return false;

default:
return thisPath[ result ] < nodePath[ result ];
}
}

/**
* Returns whether this node is after given node. `false` is returned if nodes are in different trees (for example,
* in different {@link module:engine/model/documentfragment~DocumentFragment}s).
*
* @param {module:engine/model/node~Node} node Node to compare with.
* @returns {Boolean}
*/
isAfter( node ) {
// Given node is not before this node if they are same.
if ( this == node ) {
return false;
}

// Return `false` if it is impossible to compare nodes.
if ( this.root !== node.root ) {
return false;
}

// In other cases, just check if the `node` is before, and return the opposite.
return !this.isBefore( node );
}

/**
* Checks if the node has an attribute with given key.
*
Expand Down Expand Up @@ -401,7 +459,7 @@ export default class Node {
* may return {@link module:engine/model/documentfragment~DocumentFragment} or {@link module:engine/model/node~Node}
* that can be either text node or element. This method can be used to check what kind of object is returned.
*
* obj.is( 'node' ); // true for any node, false for document fragment
* obj.is( 'node' ); // true for any node, false for document fragment and text fragment
* obj.is( 'documentFragment' ); // true for document fragment, false for any node
* obj.is( 'element' ); // true for any element, false for text node or document fragment
* obj.is( 'element', 'paragraph' ); // true only for element which name is 'paragraph'
Expand All @@ -413,6 +471,9 @@ export default class Node {
* @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
* @returns {Boolean}
*/
is( type ) {
return type == 'node';
}
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/model/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,7 @@ export default class Position {
return 'after';

default:
if ( this.path[ result ] < otherPosition.path[ result ] ) {
return 'before';
} else {
return 'after';
}
return this.path[ result ] < otherPosition.path[ result ] ? 'before' : 'after';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/model/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class Text extends Node {
* @inheritDoc
*/
is( type ) {
return type == 'text';
return type == 'text' || super.is( type );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/view/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default class Element extends Node {
*/
is( type, name = null ) {
if ( !name ) {
return type == 'element' || type == this.name;
return type == 'element' || type == this.name || super.is( type );
} else {
return type == 'element' && name == this.name;
}
Expand Down
119 changes: 104 additions & 15 deletions src/view/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import clone from '@ckeditor/ckeditor5-utils/src/lib/lodash/clone';
import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';

/**
* Abstract tree view node class.
Expand Down Expand Up @@ -118,6 +119,33 @@ export default class Node {
}
}

/**
* Gets a path to the node. The path is an array containing indices of consecutive ancestors of this node,
* beginning from {@link module:engine/view/node~Node#root root}, down to this node's index.
*
* const abc = new Text( 'abc' );
* const foo = new Text( 'foo' );
* const h1 = new Element( 'h1', null, new Text( 'header' ) );
* const p = new Element( 'p', null, [ abc, foo ] );
* const div = new Element( 'div', null, [ h1, p ] );
* foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3.
* h1.getPath(); // Returns [ 0 ].
* div.getPath(); // Returns [].
*
* @returns {Array.<Number>} The path.
*/
getPath() {
const path = [];
let node = this; // eslint-disable-line consistent-this

while ( node.parent ) {
path.unshift( node.index );
node = node.parent;
}

return path;
}

/**
* Returns ancestors array of this node.
*
Expand Down Expand Up @@ -162,6 +190,63 @@ export default class Node {
return i === 0 ? null : ancestorsA[ i - 1 ];
}

/**
* Returns whether this node is before given node. `false` is returned if nodes are in different trees (for example,
* in different {@link module:engine/view/documentfragment~DocumentFragment}s).
*
* @param {module:engine/view/node~Node} node Node to compare with.
* @returns {Boolean}
*/
isBefore( node ) {
// Given node is not before this node if they are same.
if ( this == node ) {
return false;
}

// Return `false` if it is impossible to compare nodes.
if ( this.root !== node.root ) {
return false;
}

const thisPath = this.getPath();
const nodePath = node.getPath();

const result = compareArrays( thisPath, nodePath );

switch ( result ) {
case 'prefix':
return true;

case 'extension':
return false;

default:
return thisPath[ result ] < nodePath[ result ];
}
}

/**
* Returns whether this node is after given node. `false` is returned if nodes are in different trees (for example,
* in different {@link module:engine/view/documentfragment~DocumentFragment}s).
*
* @param {module:engine/view/node~Node} node Node to compare with.
* @returns {Boolean}
*/
isAfter( node ) {
// Given node is not before this node if they are same.
if ( this == node ) {
return false;
}

// Return `false` if it is impossible to compare nodes.
if ( this.root !== node.root ) {
return false;
}

// In other cases, just check if the `node` is before, and return the opposite.
return !this.isBefore( node );
}

/**
* Removes node from parent.
*
Expand Down Expand Up @@ -198,28 +283,14 @@ export default class Node {
return json;
}

/**
* Clones this node.
*
* @method #clone
* @returns {module:engine/view/node~Node} Clone of this node.
*/

/**
* Checks if provided node is similar to this node.
*
* @method #isSimilar
* @returns {Boolean} True if nodes are similar.
*/

/**
* Checks whether given view tree object is of given type.
*
* This method is useful when processing view tree objects that are of unknown type. For example, a function
* may return {@link module:engine/view/documentfragment~DocumentFragment} or {@link module:engine/view/node~Node}
* that can be either text node or element. This method can be used to check what kind of object is returned.
*
* obj.is( 'node' ); // true for any node, false for document fragment
* obj.is( 'node' ); // true for any node, false for document fragment and text fragment
* obj.is( 'documentFragment' ); // true for document fragment, false for any node
* obj.is( 'element' ); // true for any element, false for text node or document fragment
* obj.is( 'element', 'p' ); // true only for element which name is 'p'
Expand All @@ -231,6 +302,24 @@ export default class Node {
* 'rootElement'|'documentFragment'|'text'|'textProxy'} type
* @returns {Boolean}
*/
is( type ) {
return type == 'node';
}

/**
* Clones this node.
*
* @protected
* @method #_clone
* @returns {module:engine/view/node~Node} Clone of this node.
*/

/**
* Checks if provided node is similar to this node.
*
* @method #isSimilar
* @returns {Boolean} True if nodes are similar.
*/
}

/**
Expand Down
54 changes: 14 additions & 40 deletions src/view/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,61 +243,35 @@ export default class Position {
* @returns {module:engine/view/position~PositionRelation}
*/
compareWith( otherPosition ) {
if ( this.isEqual( otherPosition ) ) {
return 'same';
if ( this.root !== otherPosition.root ) {
return 'different';
}

// If positions have same parent.
if ( this.parent === otherPosition.parent ) {
return this.offset - otherPosition.offset < 0 ? 'before' : 'after';
if ( this.isEqual( otherPosition ) ) {
return 'same';
}

// Get path from root to position's parent element.
const path = this.getAncestors();
const otherPath = otherPosition.getAncestors();
const thisPath = this.parent.is( 'node' ) ? this.parent.getPath() : [];
const otherPath = otherPosition.parent.is( 'node' ) ? otherPosition.parent.getPath() : [];

// Compare both path arrays to find common ancestor.
const result = compareArrays( path, otherPath );
// Add the positions' offsets to the parents offsets.
thisPath.push( this.offset );
otherPath.push( otherPosition.offset );

let commonAncestorIndex;
// Compare both path arrays to find common ancestor.
const result = compareArrays( thisPath, otherPath );

switch ( result ) {
case 0:
// No common ancestors found.
return 'different';

case 'prefix':
commonAncestorIndex = path.length - 1;
break;
return 'before';

case 'extension':
commonAncestorIndex = otherPath.length - 1;
break;
return 'after';

default:
commonAncestorIndex = result - 1;
return thisPath[ result ] < otherPath[ result ] ? 'before' : 'after';
}

// Common ancestor of two positions.
const commonAncestor = path[ commonAncestorIndex ];
const nextAncestor1 = path[ commonAncestorIndex + 1 ];
const nextAncestor2 = otherPath[ commonAncestorIndex + 1 ];

// Check if common ancestor is not one of the parents.
if ( commonAncestor === this.parent ) {
const index = this.offset - nextAncestor2.index;

return index <= 0 ? 'before' : 'after';
} else if ( commonAncestor === otherPosition.parent ) {
const index = nextAncestor1.index - otherPosition.offset;

return index < 0 ? 'before' : 'after';
}

const index = nextAncestor1.index - nextAncestor2.index;

// Compare indexes of next ancestors inside common one.
return index < 0 ? 'before' : 'after';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/view/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class Text extends Node {
* @inheritDoc
*/
is( type ) {
return type == 'text';
return type == 'text' || super.is( type );
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/model/documentfragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe( 'DocumentFragment', () => {
} );

it( 'should return false for other accept values', () => {
expect( frag.is( 'node' ) ).to.be.false;
expect( frag.is( 'text' ) ).to.be.false;
expect( frag.is( 'textProxy' ) ).to.be.false;
expect( frag.is( 'element' ) ).to.be.false;
Expand Down
3 changes: 2 additions & 1 deletion tests/model/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe( 'Element', () => {
element = new Element( 'paragraph' );
} );

it( 'should return true for element, element with same name and element name', () => {
it( 'should return true for node, element, element with same name and element name', () => {
expect( element.is( 'node' ) ).to.be.true;
expect( element.is( 'element' ) ).to.be.true;
expect( element.is( 'element', 'paragraph' ) ).to.be.true;
expect( element.is( 'paragraph' ) ).to.be.true;
Expand Down
Loading

0 comments on commit 4c38683

Please sign in to comment.