8
0
Просмотр исходного кода

Merged getPriorPosition and getFurtherPosition into one getLastMatchingPosition. Renamed `Range.getShrinked` to `Range.getTrimmed`.

Piotr Jasiun 9 лет назад
Родитель
Сommit
eaa205ddc8

+ 15 - 23
packages/ckeditor5-engine/src/model/position.js

@@ -246,38 +246,30 @@ export default class Position {
 	}
 
 	/**
-	 * Use forward {@link module:engine/model/treewalker~TreeWalker TreeWalker} to get the farthest position which
-	 * matches the callback.
+	 * Gets the farthest position which matches the callback using
+	 * {@link module:engine/model/treewalker~TreeWalker TreeWalker}.
 	 *
 	 * For example:
 	 *
-	 * 		getFurtherPosition( value => value.type == 'text' ); // <paragraph>[]foo</paragraph> -> <paragraph>foo[]</paragraph>
-	 * 		getFurtherPosition( value => false ); // Do not move the position.
+	 * 		getLastMatchingPosition( value => value.type == 'text' );
+	 * 		// <paragraph>[]foo</paragraph> -> <paragraph>foo[]</paragraph>
 	 *
-	 * @param {Function} skip Callback function. Gets {@link module:engine/model/treewalker~TreeWalkerValue} and should
-	 * return `true` if the value should be skipped or `false` if not.
-	 */
-	getFurtherPosition( skip ) {
-		const treeWalker = new TreeWalker( { startPosition: this } );
-		treeWalker.skip( skip );
-
-		return treeWalker.position;
-	}
-
-	/**
-	 * Use backward {@link module:engine/model/treewalker~TreeWalker TreeWalker} to get the farthest position which
-	 * matches the callback.
-	 *
-	 * For example:
+	 * 		getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } );
+	 * 		// <paragraph>foo[]</paragraph> -> <paragraph>[]foo</paragraph>
 	 *
-	 * 		getPriorPosition( value => value.type == 'text' ); // <paragraph>foo[]</paragraph> -> <paragraph>[]foo</paragraph>
-	 * 		getPriorPosition( value => false ); // Do not move the position.
+	 * 		getLastMatchingPosition( value => false );
+	 * 		// Do not move the position.
 	 *
 	 * @param {Function} skip Callback function. Gets {@link module:engine/model/treewalker~TreeWalkerValue} and should
 	 * return `true` if the value should be skipped or `false` if not.
+	 * @param {Object} options Object with configuration options. See {@link module:engine/model/treewalker~TreeWalker}.
+	 *
+	 * @returns {module:engine/model/position~Position} The position after the last item which matches the `skip` callback test.
 	 */
-	getPriorPosition( skip ) {
-		const treeWalker = new TreeWalker( { startPosition: this, direction: 'backward' } );
+	getLastMatchingPosition( skip, options = {} ) {
+		options.startPosition = this;
+
+		const treeWalker = new TreeWalker( options );
 		treeWalker.skip( skip );
 
 		return treeWalker.position;

+ 11 - 24
packages/ckeditor5-engine/src/view/position.js

@@ -142,38 +142,25 @@ export default class Position {
 	}
 
 	/**
-	 * Use forward {@link module:engine/view/treewalker~TreeWalker TreeWalker} to get the farthest position which
-	 * matches the callback.
+	 * Gets the farthest position which matches the callback using
+	 * {@link module:engine/view/treewalker~TreeWalker TreeWalker}.
 	 *
 	 * For example:
 	 *
-	 * 		getFurtherPosition( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
-	 * 		getFurtherPosition( value => false ); // Do not move the position.
+	 * 		getLastMatchingPosition( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
+	 * 		getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } ); // <p>foo[]</p> -> <p>{}foo</p>
+	 * 		getLastMatchingPosition( value => false ); // Do not move the position.
 	 *
 	 * @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~TreeWalkerValue} and should
 	 * return `true` if the value should be skipped or `false` if not.
-	 */
-	getFurtherPosition( skip ) {
-		const treeWalker = new TreeWalker( { startPosition: this } );
-		treeWalker.skip( skip );
-
-		return treeWalker.position;
-	}
-
-	/**
-	 * Use backward {@link module:engine/view/treewalker~TreeWalker TreeWalker} to get the farthest position which
-	 * matches the callback.
+	 * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
 	 *
-	 * For example:
-	 *
-	 * 		getPriorPosition( value => value.type == 'text' ); // <p>foo[]</p> -> <p>{}foo</p>
-	 * 		getPriorPosition( value => false ); // Do not move the position.
-	 *
-	 * @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~TreeWalkerValue} and should
-	 * return `true` if the value should be skipped or `false` if not.
+	 * @returns {module:engine/view/position~Position} The position after the last item which matches the `skip` callback test.
 	 */
-	getPriorPosition( skip ) {
-		const treeWalker = new TreeWalker( { startPosition: this, direction: 'backward' } );
+	getLastMatchingPosition( skip, options = {} ) {
+		options.startPosition = this;
+
+		const treeWalker = new TreeWalker( options );
 		treeWalker.skip( skip );
 
 		return treeWalker.position;

+ 15 - 13
packages/ckeditor5-engine/src/view/range.js

@@ -87,45 +87,47 @@ export default class Range {
 	}
 
 	/**
-	 * Creates a new range with the same content, but containing all surrendering tags with no semantic meaning.
+	 * Creates a maximal range that has the same content as this range but is expanded in both ways (at the beginning
+	 * and at the end).
 	 *
 	 * For example:
 	 *
 	 * 		<p>Foo</p><p><b>{Bar}</b></p> -> <p>Foo</p>[<p><b>Bar</b></p>]
 	 * 		<p><b>foo</b>{bar}<span></span></p> -> <p><b>foo[</b>bar<span></span>]</p>
 	 *
-	 * Note that in the sample above all:
+	 * Note that in the sample above:
 	 *  - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
 	 *  - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
-	 *  - `<span>` have type of {@link module:engine/view/uielement~UiElement}.
+	 *  - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
 	 *
 	 * @returns {module:engine/view/range~Range} Enlarged range.
 	 */
 	getEnlarged() {
-		const start = this.start.getPriorPosition( enlagreShrinkStartSkip );
-		const end = this.end.getFurtherPosition( enlagreShrinkEndSkip );
+		const start = this.start.getLastMatchingPosition( enlargeShrinkStartSkip, { direction: 'backward' } );
+		const end = this.end.getLastMatchingPosition( enlargeShrinkEndSkip );
 
 		return new Range( start, end );
 	}
 
 	/**
-	 * Creates a new range with the same content, but without all surrendering tags with no semantic meaning.
+	 * Creates a minimum range that has the same content as this range but is trimmed in both ways (at the beginning
+	 * and at the end).
 	 *
 	 * For example:
 	 *
 	 * 		<p>Foo</p>[<p><b>Bar</b></p>] -> <p>Foo</p><p><b>{Bar}</b></p>
 	 * 		<p><b>foo[</b>bar<span></span>]</p> -> <p><b>foo</b>{bar}<span></span></p>
 	 *
-	 * Note that in the sample above all:
+	 * Note that in the sample above:
 	 *  - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
 	 *  - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
-	 *  - `<span>` have type of {@link module:engine/view/uielement~UiElement}.
+	 *  - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
 	 *
 	 * @returns {module:engine/view/range~Range} Shrink range.
 	 */
-	getShrinked() {
-		let start = this.start.getFurtherPosition( enlagreShrinkStartSkip );
-		let end = this.end.getPriorPosition( enlagreShrinkEndSkip );
+	getTrimmed() {
+		let start = this.start.getLastMatchingPosition( enlargeShrinkStartSkip );
+		let end = this.end.getLastMatchingPosition( enlargeShrinkEndSkip, { direction: 'backward' } );
 		let nodeAfterStart = start.nodeAfter;
 		let nodeBeforeEnd = end.nodeBefore;
 
@@ -410,7 +412,7 @@ export default class Range {
 }
 
 // Function used by getEnlagred and getShrinked methods.
-function enlagreShrinkStartSkip( value ) {
+function enlargeShrinkStartSkip( value ) {
 	if ( value.item instanceof AttributeElement || value.item instanceof UIElement ) {
 		return true;
 	}
@@ -423,7 +425,7 @@ function enlagreShrinkStartSkip( value ) {
 }
 
 // Function used by getEnlagred and getShrinked methods.
-function enlagreShrinkEndSkip( value ) {
+function enlargeShrinkEndSkip( value ) {
 	if ( value.item instanceof AttributeElement || value.item instanceof UIElement ) {
 		return true;
 	}

+ 5 - 7
packages/ckeditor5-engine/tests/model/position.js

@@ -578,21 +578,19 @@ describe( 'position', () => {
 		} );
 	} );
 
-	describe( 'getFurtherPosition', () => {
-		it( 'should return skip', () => {
+	describe( 'getLastMatchingPosition', () => {
+		it( 'should skip forward', () => {
 			let position = new Position( root, [ 1, 0, 0 ] );
 
-			position = position.getFurtherPosition( ( value ) => value.type == 'text' );
+			position = position.getLastMatchingPosition( ( value ) => value.type == 'text' );
 
 			expect( position.path ).to.deep.equal( [ 1, 0, 3 ] );
 		} );
-	} );
 
-	describe( 'getPriorPosition', () => {
-		it( 'should return skip', () => {
+		it( 'should skip backward', () => {
 			let position = new Position( root, [ 1, 0, 2 ] );
 
-			position = position.getPriorPosition( ( value ) => value.type == 'text' );
+			position = position.getLastMatchingPosition( ( value ) => value.type == 'text', { direction: 'backward' } );
 
 			expect( position.path ).to.deep.equal( [ 1, 0, 0 ] );
 		} );

+ 5 - 7
packages/ckeditor5-engine/tests/view/position.js

@@ -99,23 +99,21 @@ describe( 'Position', () => {
 		} );
 	} );
 
-	describe( 'getFurtherPosition', () => {
-		it( 'should return skip', () => {
+	describe( 'getLastMatchingPosition', () => {
+		it( 'should skip forward', () => {
 			const { view, selection } = parse( '<p><b>{}foo</b></p>' );
 			let position = selection.getFirstPosition();
 
-			position = position.getFurtherPosition( ( value ) => value.type == 'text' );
+			position = position.getLastMatchingPosition( ( value ) => value.type == 'text' );
 
 			expect( stringify( view, position ) ).to.equal( '<p><b>foo[]</b></p>' );
 		} );
-	} );
 
-	describe( 'getPriorPosition', () => {
-		it( 'should return skip', () => {
+		it( 'should skip backward', () => {
 			const { view, selection } = parse( '<p><b>foo{}</b></p>' );
 			let position = selection.getFirstPosition();
 
-			position = position.getPriorPosition( ( value ) => value.type == 'text' );
+			position = position.getLastMatchingPosition( ( value ) => value.type == 'text', { direction: 'backward' } );
 
 			expect( stringify( view, position ) ).to.equal( '<p><b>[]foo</b></p>' );
 		} );

+ 11 - 11
packages/ckeditor5-engine/tests/view/range.js

@@ -136,43 +136,43 @@ describe( 'Range', () => {
 		}
 	} );
 
-	describe( 'getShrinked', () => {
+	describe( 'getTrimmed', () => {
 		it( 'case 1', () => {
-			expect( shrink( '<p>f[<b>oo</b></p>]<p>bar</p>' ) )
+			expect( trim( '<p>f[<b>oo</b></p>]<p>bar</p>' ) )
 				.to.equal( '<p>f<b>{oo}</b></p><p>bar</p>' );
 		} );
 
 		it( 'case 2', () => {
-			expect( shrink( '<p>f{oo}bar</p>' ) )
+			expect( trim( '<p>f{oo}bar</p>' ) )
 				.to.equal( '<p>f{oo}bar</p>' );
 		} );
 
 		it( 'case 3', () => {
-			expect( shrink( '<p>f[<span></span>oo<span></span>]bar</p>' ) )
+			expect( trim( '<p>f[<span></span>oo<span></span>]bar</p>' ) )
 				.to.equal( '<p>f<span></span>{oo}<span></span>bar</p>' );
 		} );
 
 		it( 'case 4', () => {
-			expect( shrink( '<p>f<img></img>[oo]<img></img>bar</p>' ) )
+			expect( trim( '<p>f<img></img>[oo]<img></img>bar</p>' ) )
 				.to.equal( '<p>f<img></img>{oo}<img></img>bar</p>' );
 		} );
 
 		it( 'case 5', () => {
-			expect( shrink( '<p><b>f[</b>oo<b><span></span>]bar</b></p>' ) )
+			expect( trim( '<p><b>f[</b>oo<b><span></span>]bar</b></p>' ) )
 				.to.equal( '<p><b>f</b>{oo}<b><span></span>bar</b></p>' );
 		} );
 
 		it( 'case6', () => {
-			expect( shrink( '<p>foo[</p><p>bar</p><p>]bom</p>' ) )
+			expect( trim( '<p>foo[</p><p>bar</p><p>]bom</p>' ) )
 				.to.equal( '<p>foo[</p><p>bar</p><p>]bom</p>' );
 		} );
 
 		it( 'case7', () => {
-			expect( shrink( '<p>foo[<b><img></img></b>]bom</p>' ) )
+			expect( trim( '<p>foo[<b><img></img></b>]bom</p>' ) )
 				.to.equal( '<p>foo<b>[<img></img>]</b>bom</p>' );
 		} );
 
-		function shrink( data ) {
+		function trim( data ) {
 			data = data
 				.replace( /<p>/g, '<container:p>' )
 				.replace( /<\/p>/g, '</container:p>' )
@@ -185,9 +185,9 @@ describe( 'Range', () => {
 			const { view, selection } = parse( data, { rootElement: viewFrag } );
 			const range = selection.getFirstRange();
 
-			const shrinkedRange = range.getShrinked();
+			const trimmedRange = range.getTrimmed();
 
-			return stringify( view, shrinkedRange );
+			return stringify( view, trimmedRange );
 		}
 	} );