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

Rename needsFiller to getBlockFillerOffset. Docs added.

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

+ 10 - 5
packages/ckeditor5-engine/src/treeview/attributeelement.js

@@ -73,24 +73,29 @@ export default class AttributeElement extends Element {
 		return super.isSimilar( otherElement ) && this.priority == otherElement.priority;
 	}
 
-	needsFiller() {
+	/**
+	 * Returns block filler offset or null if block filler is not needed.
+	 *
+	 * @returns {Number|false} Block filler offset or null if block filler is not needed.
+	 */
+	getBlockFillerOffset() {
 		// <b>foo</b> does not need filler
 		if ( this.getChildCount() ) {
-			return false;
+			return null;
 		}
 
 		let element = this.parent;
 
 		// <p><b></b></p> needs filler -> <p><b><br></b></p>
 		while ( !( element instanceof ContainerElement ) ) {
-			if ( this.getChildCount() > 1 ) {
-				return false;
+			if ( element.getChildCount() > 1 ) {
+				return null;
 			}
 			element = element.parent;
 		}
 
 		if ( element.getChildCount() > 1 ) {
-			return false;
+			return null;
 		}
 
 		return 0;

+ 7 - 2
packages/ckeditor5-engine/src/treeview/containerelement.js

@@ -52,7 +52,12 @@ export default class ContainerElement extends Element {
 		super( name, attrs, children );
 	}
 
-	needsFiller() {
-		return this.getChildCount() === 0 ? 0 : false;
+	/**
+	 * Returns block filler offset or null if block filler is not needed.
+	 *
+	 * @returns {Number|false} Block filler offset or null if block filler is not needed.
+	 */
+	getBlockFillerOffset() {
+		return this.getChildCount() === 0 ? 0 : null;
 	}
 }

+ 1 - 1
packages/ckeditor5-engine/src/treeview/domconverter.js

@@ -154,7 +154,7 @@ export default class DomConverter {
 	}
 
 	*viewChildrenToDom( viewElement, domDocument, options = {} ) {
-		let fillerPositionOffset = viewElement.needsFiller && viewElement.needsFiller();
+		let fillerPositionOffset = viewElement.getBlockFillerOffset && viewElement.getBlockFillerOffset();
 		let offset = 0;
 
 		for ( let childView of viewElement.getChildren() ) {

+ 1 - 1
packages/ckeditor5-engine/src/treeview/renderer.js

@@ -231,7 +231,7 @@ export default class Renderer {
 		}
 
 		// We have block filler, we do not need inline one.
-		if ( selectionOffset === selectionParent.needsFiller() ) {
+		if ( selectionOffset === selectionParent.getBlockFillerOffset() ) {
 			return false;
 		}
 

+ 45 - 0
packages/ckeditor5-engine/tests/treeview/attributeelement.js

@@ -10,6 +10,7 @@
 import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
 import Element from '/ckeditor5/engine/treeview/element.js';
 import { DEFAULT_PRIORITY } from '/ckeditor5/engine/treeview/attributeelement.js';
+import { parse } from '/tests/engine/_utils/view.js';
 
 describe( 'AttributeElement', () => {
 	describe( 'constructor', () => {
@@ -56,4 +57,48 @@ describe( 'AttributeElement', () => {
 			expect( b1.isSimilar( b2 ) ).to.be.false;
 		} );
 	} );
+
+	describe( 'getBlockFillerOffset', () => {
+		it( 'should return position 0 if it is the only element in the container', () => {
+			const { selection } = parse( '<container:p><attribute:b>[]</attribute:b></container:p>' );
+			const attribute = selection.getFirstPosition().parent;
+
+			expect( attribute.getBlockFillerOffset() ).to.equals( 0 );
+		} );
+
+		it( 'should return position 0 if it is the only nested element in the container', () => {
+			const { selection } = parse(
+				'<container:p><attribute:b><attribute:i>[]</attribute:i></attribute:b></container:p>' );
+			const attribute = selection.getFirstPosition().parent;
+
+			expect( attribute.getBlockFillerOffset() ).to.equals( 0 );
+		} );
+
+		it( 'should return null if element contains another element', () => {
+			const attribute = parse( '<attribute:b><attribute:i></attribute:i></attribute:b>' );
+
+			expect( attribute.getBlockFillerOffset() ).to.be.null;
+		} );
+
+		it( 'should return null if element contains text', () => {
+			const attribute = parse( '<attribute:b>text</attribute:b>' );
+
+			expect( attribute.getBlockFillerOffset() ).to.be.null;
+		} );
+
+		it( 'should return null if container element contains text', () => {
+			const { selection } = parse( '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
+			const attribute = selection.getFirstPosition().parent;
+
+			expect( attribute.getBlockFillerOffset() ).to.be.null;
+		} );
+
+		it( 'should return null if it is the parent contains text', () => {
+			const { selection } = parse(
+				'<container:p><attribute:b><attribute:i>[]</attribute:i>foo</attribute:b></container:p>' );
+			const attribute = selection.getFirstPosition().parent;
+
+			expect( attribute.getBlockFillerOffset() ).to.be.null;
+		} );
+	} );
 } );

+ 11 - 0
packages/ckeditor5-engine/tests/treeview/containerelement.js

@@ -9,6 +9,7 @@
 
 import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
 import Element from '/ckeditor5/engine/treeview/element.js';
+import { parse } from '/tests/engine/_utils/view.js';
 
 describe( 'ContainerElement', () => {
 	describe( 'constructor', () => {
@@ -20,4 +21,14 @@ describe( 'ContainerElement', () => {
 			expect( el ).to.have.property( 'name' ).that.equals( 'p' );
 		} );
 	} );
+
+	describe( 'getBlockFillerOffset', () => {
+		it( 'should return position 0 if element is empty', () => {
+			expect( parse( '<container:p></container:p>' ).getBlockFillerOffset() ).to.equals( 0 );
+		} );
+
+		it( 'should return null if element is not empty', () => {
+			expect( parse( '<container:p>foo</container:p>' ).getBlockFillerOffset() ).to.be.null;
+		} );
+	} );
 } );