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

Changed getFillerOffset methods to be added in construcor and be cloned with view.Element.clone().

Szymon Kupś 9 лет назад
Родитель
Сommit
391b97175d

+ 39 - 27
packages/ckeditor5-engine/src/view/attributeelement.js

@@ -39,6 +39,18 @@ export default class AttributeElement extends Element {
 		 * @member {Number} module:engine/view/attributeelement~AttributeElement#priority
 		 * @member {Number} module:engine/view/attributeelement~AttributeElement#priority
 		 */
 		 */
 		this.priority = DEFAULT_PRIORITY;
 		this.priority = DEFAULT_PRIORITY;
+
+		/**
+		 * Returns block {@link module:engine/view/filter~Filter filler} offset or `null` if block filler is not needed.
+		 * This method is implemented in a constructor because it is treated as an injected behaviour and it might be
+		 * changed during execution (in elements representing widgets for example).
+		 * When {@link module:engine/view/element~Element.clone element is cloned}, this method will be referenced from
+		 * source object.
+		 *
+		 * @method #getFillerOffset
+		 * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
+		 */
+		this.getFillerOffset = getFillerOffset;
 	}
 	}
 
 
 	/**
 	/**
@@ -68,40 +80,40 @@ export default class AttributeElement extends Element {
 	isSimilar( otherElement ) {
 	isSimilar( otherElement ) {
 		return super.isSimilar( otherElement ) && this.priority == otherElement.priority;
 		return super.isSimilar( otherElement ) && this.priority == otherElement.priority;
 	}
 	}
+}
 
 
-	/**
-	 * Returns block {@link module:engine/view/filter~Filter filler} offset or `null` if a block filler is not needed.
-	 *
-	 * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
-	 */
-	getFillerOffset() {
-		// <b>foo</b> does not need filler.
-		if ( this.childCount ) {
-			return null;
-		}
+/**
+ * Default attribute priority.
+ *
+ * @member {Number} module:engine/view/attributeelement~AttributeElement.DEFAULT_PRIORITY
+ */
+AttributeElement.DEFAULT_PRIORITY = DEFAULT_PRIORITY;
 
 
-		let element = this.parent;
+// Returns block {@link module:engine/view/filter~Filter filler} offset or `null` if block filler is not needed.
+//
+// @returns {Number|null} Block filler offset or `null` if block filler is not needed.
+function getFillerOffset() {
+	/*jshint validthis:true */
 
 
-		// <p><b></b></p> needs filler -> <p><b><br></b></p>
-		while ( element instanceof AttributeElement ) {
-			if ( element.childCount > 1 ) {
-				return null;
-			}
+	// <b>foo</b> does not need filler.
+	if ( this.childCount ) {
+		return null;
+	}
 
 
-			element = element.parent;
-		}
+	let element = this.parent;
 
 
-		if ( !element || element.childCount > 1 ) {
+	// <p><b></b></p> needs filler -> <p><b><br></b></p>
+	while ( element instanceof AttributeElement ) {
+		if ( element.childCount > 1 ) {
 			return null;
 			return null;
 		}
 		}
 
 
-		return 0;
+		element = element.parent;
 	}
 	}
-}
 
 
-/**
- * Default attribute priority.
- *
- * @member {Number} module:engine/view/attributeelement~AttributeElement.DEFAULT_PRIORITY
- */
-AttributeElement.DEFAULT_PRIORITY = DEFAULT_PRIORITY;
+	if ( !element || element.childCount > 1 ) {
+		return null;
+	}
+
+	return 0;
+}

+ 19 - 8
packages/ckeditor5-engine/src/view/containerelement.js

@@ -52,14 +52,25 @@ export default class ContainerElement extends Element {
 	 */
 	 */
 	constructor( name, attrs, children ) {
 	constructor( name, attrs, children ) {
 		super( name, attrs, children );
 		super( name, attrs, children );
-	}
 
 
-	/**
-	 * Returns block {@link module:engine/view/filter~Filter filler} offset or `null` if block filler is not needed.
-	 *
-	 * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
-	 */
-	getFillerOffset() {
-		return this.childCount === 0 ? 0 : null;
+		/**
+		 * Returns block {@link module:engine/view/filter~Filter filler} offset or `null` if block filler is not needed.
+		 * This method is implemented in a constructor because it is treated as an injected behaviour and it might be
+		 * changed during execution (in elements representing widgets for example).
+		 * When {@link module:engine/view/element~Element.clone element is cloned}, this method will be referenced from
+		 * source object.
+		 *
+		 * @method #getFillerOffset
+		 * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
+		 */
+		this.getFillerOffset = getFillerOffset;
 	}
 	}
 }
 }
+
+// Returns block {@link module:engine/view/filter~Filter filler} offset or `null` if block filler is not needed.
+//
+// @returns {Number|null} Block filler offset or `null` if block filler is not needed.
+function getFillerOffset() {
+	/*jshint validthis:true */
+	return this.childCount === 0 ? 0 : null;
+}

+ 5 - 0
packages/ckeditor5-engine/src/view/element.js

@@ -163,6 +163,11 @@ export default class Element extends Node {
 		// Clone custom properties.
 		// Clone custom properties.
 		cloned._customProperties = new Map( this._customProperties );
 		cloned._customProperties = new Map( this._customProperties );
 
 
+		// Clone filler offset method if one exists.
+		if ( this.getFillerOffset ) {
+			cloned.getFillerOffset = this.getFillerOffset;
+		}
+
 		return cloned;
 		return cloned;
 	}
 	}
 
 

+ 19 - 9
packages/ckeditor5-engine/src/view/emptyelement.js

@@ -29,6 +29,18 @@ export default class EmptyElement extends Element {
 		if ( arguments.length > 2 ) {
 		if ( arguments.length > 2 ) {
 			throwCannotAdd();
 			throwCannotAdd();
 		}
 		}
+
+		/**
+		 * Returns `null` because filler is not needed.
+		 * This method is implemented in a constructor because it is treated as an injected behaviour and it might be
+		 * changed during execution (in elements representing widgets for example).
+		 * When {@link module:engine/view/element~Element.clone element is cloned}, this method will be referenced from
+		 * source object.
+		 *
+		 * @method #getFillerOffset
+		 * @returns {null} Always returns null.
+		 */
+		this.getFillerOffset = getFillerOffset;
 	}
 	}
 
 
 	/**
 	/**
@@ -65,15 +77,6 @@ export default class EmptyElement extends Element {
 	insertChildren() {
 	insertChildren() {
 		throwCannotAdd();
 		throwCannotAdd();
 	}
 	}
-
-	/**
-	 * Returns `null` because block filler is not needed.
-	 *
-	 * @returns {null}
-	 */
-	getFillerOffset() {
-		return null;
-	}
 }
 }
 
 
 function throwCannotAdd() {
 function throwCannotAdd() {
@@ -84,3 +87,10 @@ function throwCannotAdd() {
 	 */
 	 */
 	throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
 	throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
 }
 }
+
+// Returns `null` because block filler is not needed for EmptyElements.
+//
+// @returns {null}
+function getFillerOffset() {
+	return null;
+}

+ 12 - 0
packages/ckeditor5-engine/tests/view/element.js

@@ -169,6 +169,18 @@ describe( 'Element', () => {
 			expect( cloned.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
 			expect( cloned.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
 			expect( cloned.getCustomProperty( symbol ) ).to.equal( 'baz' );
 			expect( cloned.getCustomProperty( symbol ) ).to.equal( 'baz' );
 		} );
 		} );
+
+		it( 'should clone getFillerOffset', () => {
+			const el = new Element( 'p' );
+			const fm = () => 'foo bar';
+
+			expect( el.getFillerOffset ).to.be.undefined;
+			el.getFillerOffset = fm;
+
+			const cloned = el.clone();
+
+			expect( cloned.getFillerOffset ).to.equal( fm );
+		} );
 	} );
 	} );
 
 
 	describe( 'isSimilar', () => {
 	describe( 'isSimilar', () => {