Parcourir la source

Added nodeBefore/nodeAfter getters to treeView.Position.

Szymon Kupś il y a 9 ans
Parent
commit
cd29f06a7b

+ 32 - 1
packages/ckeditor5-engine/src/treeview/position.js

@@ -6,6 +6,7 @@
 'use strict';
 
 import utils from '../../utils/utils.js';
+import Text from './text.js';
 
 /**
  * Position in the tree. Position is always located before or after a node.
@@ -16,7 +17,7 @@ export default class Position {
 	/**
 	 * Creates a position.
 	 *
-	 * @param {engine.treeView.Element} parent Position parent element.
+	 * @param {engine.treeView.Node} parent Position parent node.
 	 * @param {Number} offset Position offset.
 	 */
 	constructor( parent, offset ) {
@@ -35,6 +36,36 @@ export default class Position {
 		this.offset = offset;
 	}
 
+	/**
+	 * Node directly after the position. Equals `null` when there is no node after position or position is located
+	 * inside text node.
+	 *
+	 * @readonly
+	 * @type {engine.treeView.Node}
+	 */
+	get nodeAfter() {
+		if ( this.parent instanceof Text ) {
+			return null;
+		}
+
+		return this.parent.getChild( this.offset ) || null;
+	}
+
+	/**
+	 * Node directly before the position. Equals `null` when there is no node before position or position is located
+	 * inside text node.
+	 *
+	 * @readonly
+	 * @type {engine.treeView.Node}
+	 */
+	get nodeBefore() {
+		if ( this.parent instanceof Text ) {
+			return null;
+		}
+
+		return this.parent.getChild( this.offset - 1 ) || null;
+	}
+
 	/**
 	 * Returns a new instance of Position with offset incremented by `shift` value.
 	 *

+ 50 - 0
packages/ckeditor5-engine/tests/treeview/position.js

@@ -24,6 +24,56 @@ describe( 'Position', () => {
 		} );
 	} );
 
+	describe( 'nodeBefore', () => {
+		it( 'should equal to node that is before position', () => {
+			const b1 = new Element( 'b' );
+			const el = new Element( 'p', null, [ b1 ] );
+			const position = new Position( el, 1 );
+
+			expect( position.nodeBefore ).to.equal( b1 );
+		} );
+
+		it( 'should equal null if there is no node before', () => {
+			const b1 = new Element( 'b' );
+			const el = new Element( 'p', null, [ b1 ] );
+			const position = new Position( el, 0 );
+
+			expect( position.nodeBefore ).to.be.null;
+		} );
+
+		it( 'should equal null if position is located inside text node', () => {
+			const text = new Text( 'foobar' );
+			const position = new Position( text, 3 );
+
+			expect( position.nodeBefore ).to.be.null;
+		} );
+	} );
+
+	describe( 'nodeAfter', () => {
+		it( 'should equal to node that is after position', () => {
+			const b1 = new Element( 'b' );
+			const el = new Element( 'p', null, [ b1 ] );
+			const position = new Position( el, 0 );
+
+			expect( position.nodeAfter ).to.equal( b1 );
+		} );
+
+		it( 'should equal null if there is no node before', () => {
+			const b1 = new Element( 'b' );
+			const el = new Element( 'p', null, [ b1 ] );
+			const position = new Position( el, 1 );
+
+			expect( position.nodeAfter ).to.be.null;
+		} );
+
+		it( 'should equal null if position is located inside text node', () => {
+			const text = new Text( 'foobar' );
+			const position = new Position( text, 3 );
+
+			expect( position.nodeAfter ).to.be.null;
+		} );
+	} );
+
 	describe( 'getShiftedBy', () => {
 		it( 'returns new instance with shifted offset', () => {
 			const position = new Position( parentMock, 10 );