8
0
Pārlūkot izejas kodu

Using writer to set custom properties to view element.

Szymon Kupś 7 gadi atpakaļ
vecāks
revīzija
9347a2f637

+ 1 - 1
packages/ckeditor5-engine/src/conversion/downcast-converters.js

@@ -1012,7 +1012,7 @@ export function createViewElementFromHighlightDescriptor( descriptor ) {
 		viewElement.priority = descriptor.priority;
 	}
 
-	viewElement.setCustomProperty( 'highlightDescriptorId', descriptor.id );
+	viewElement._setCustomProperty( 'highlightDescriptorId', descriptor.id );
 
 	return viewElement;
 }

+ 1 - 1
packages/ckeditor5-engine/src/view/editableelement.js

@@ -74,7 +74,7 @@ export default class EditableElement extends ContainerElement {
 			throw new CKEditorError( 'view-editableelement-document-already-set: View document is already set.' );
 		}
 
-		this.setCustomProperty( documentSymbol, document );
+		this._setCustomProperty( documentSymbol, document );
 
 		this.bind( 'isReadOnly' ).to( document );
 

+ 12 - 11
packages/ckeditor5-engine/src/view/element.js

@@ -511,17 +511,6 @@ export default class Element extends Node {
 	}
 
 	/**
-	 * Sets a custom property. Unlike attributes, custom properties are not rendered to the DOM,
-	 * so they can be used to add special data to elements.
-	 *
-	 * @param {String|Symbol} key
-	 * @param {*} value
-	 */
-	setCustomProperty( key, value ) {
-		this._customProperties.set( key, value );
-	}
-
-	/**
 	 * Returns the custom property value for the given key.
 	 *
 	 * @param {String|Symbol} key
@@ -723,6 +712,18 @@ export default class Element extends Node {
 	}
 
 	/**
+	 * Sets a custom property. Unlike attributes, custom properties are not rendered to the DOM,
+	 * so they can be used to add special data to elements.
+	 *
+	 * @protected
+	 * @param {String|Symbol} key
+	 * @param {*} value
+	 */
+	_setCustomProperty( key, value ) {
+		this._customProperties.set( key, value );
+	}
+
+	/**
 	 * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
 	 *
 	 * @abstract

+ 1 - 1
packages/ckeditor5-engine/src/view/rooteditableelement.js

@@ -53,7 +53,7 @@ export default class RootEditableElement extends EditableElement {
 	}
 
 	set rootName( rootName ) {
-		this.setCustomProperty( rootNameSymbol, rootName );
+		this._setCustomProperty( rootNameSymbol, rootName );
 	}
 
 	/**

+ 4 - 0
packages/ckeditor5-engine/src/view/writer.js

@@ -206,6 +206,10 @@ export default class Writer {
 		element._removeStyle( property );
 	}
 
+	setCustomProperty( key, value, element ) {
+		element._setCustomProperty( key, value );
+	}
+
 	/**
 	 * Breaks attribute nodes at provided position or at boundaries of provided range. It breaks attribute elements inside
 	 * up to a container element.

+ 4 - 4
packages/ckeditor5-engine/tests/conversion/downcast-converters.js

@@ -1302,13 +1302,13 @@ describe( 'downcast-converters', () => {
 				dispatcher.on( 'insert:div', insertElement( () => {
 					const viewContainer = new ViewContainerElement( 'div' );
 
-					viewContainer.setCustomProperty( 'addHighlight', ( element, descriptor ) => {
+					viewContainer._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
 						controller.view.change( writer => {
 							writer.addClass( descriptor.class, element );
 						} );
 					} );
 
-					viewContainer.setCustomProperty( 'removeHighlight', element => {
+					viewContainer._setCustomProperty( 'removeHighlight', element => {
 						controller.view.change( writer => {
 							writer.setAttribute( 'class', '', element );
 						} );
@@ -1383,12 +1383,12 @@ describe( 'downcast-converters', () => {
 				dispatcher.on( 'addMarker:marker2', highlightElement( () => null ) );
 				dispatcher.on( 'removeMarker:marker2', removeHighlight( () => null ) );
 
-				viewDiv.setCustomProperty( 'addHighlight', ( element, descriptor ) => {
+				viewDiv._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
 					expect( descriptor.priority ).to.equal( 10 );
 					expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
 				} );
 
-				viewDiv.setCustomProperty( 'removeHighlight', ( element, id ) => {
+				viewDiv._setCustomProperty( 'removeHighlight', ( element, id ) => {
 					expect( id ).to.equal( 'marker:foo-bar-baz' );
 				} );
 

+ 2 - 2
packages/ckeditor5-engine/tests/conversion/downcastdispatcher.js

@@ -385,8 +385,8 @@ describe( 'DowncastDispatcher', () => {
 			const viewFigure = new ViewContainerElement( 'figure', null, viewCaption );
 
 			// Create custom highlight handler mock.
-			viewFigure.setCustomProperty( 'addHighlight', () => { } );
-			viewFigure.setCustomProperty( 'removeHighlight', () => { } );
+			viewFigure._setCustomProperty( 'addHighlight', () => { } );
+			viewFigure._setCustomProperty( 'removeHighlight', () => { } );
 
 			// Create mapper mock.
 			dispatcher.conversionApi.mapper = {

+ 9 - 9
packages/ckeditor5-engine/tests/view/element.js

@@ -198,8 +198,8 @@ describe( 'Element', () => {
 		it( 'should clone custom properties', () => {
 			const el = new Element( 'p' );
 			const symbol = Symbol( 'custom' );
-			el.setCustomProperty( 'foo', 'bar' );
-			el.setCustomProperty( symbol, 'baz' );
+			el._setCustomProperty( 'foo', 'bar' );
+			el._setCustomProperty( symbol, 'baz' );
 
 			const cloned = el.clone();
 
@@ -964,7 +964,7 @@ describe( 'Element', () => {
 	describe( 'custom properties', () => {
 		it( 'should allow to set and get custom properties', () => {
 			const el = new Element( 'p' );
-			el.setCustomProperty( 'foo', 'bar' );
+			el._setCustomProperty( 'foo', 'bar' );
 
 			expect( el.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
 		} );
@@ -972,7 +972,7 @@ describe( 'Element', () => {
 		it( 'should allow to add symbol property', () => {
 			const el = new Element( 'p' );
 			const symbol = Symbol( 'custom' );
-			el.setCustomProperty( symbol, 'bar' );
+			el._setCustomProperty( symbol, 'bar' );
 
 			expect( el.getCustomProperty( symbol ) ).to.equal( 'bar' );
 		} );
@@ -980,8 +980,8 @@ describe( 'Element', () => {
 		it( 'should allow to remove custom property', () => {
 			const el = new Element( 'foo' );
 			const symbol = Symbol( 'quix' );
-			el.setCustomProperty( 'bar', 'baz' );
-			el.setCustomProperty( symbol, 'test' );
+			el._setCustomProperty( 'bar', 'baz' );
+			el._setCustomProperty( symbol, 'test' );
 
 			expect( el.getCustomProperty( 'bar' ) ).to.equal( 'baz' );
 			expect( el.getCustomProperty( symbol ) ).to.equal( 'test' );
@@ -995,9 +995,9 @@ describe( 'Element', () => {
 
 		it( 'should allow to iterate over custom properties', () => {
 			const el = new Element( 'p' );
-			el.setCustomProperty( 'foo', 1 );
-			el.setCustomProperty( 'bar', 2 );
-			el.setCustomProperty( 'baz', 3 );
+			el._setCustomProperty( 'foo', 1 );
+			el._setCustomProperty( 'bar', 2 );
+			el._setCustomProperty( 'baz', 3 );
 
 			const properties = [ ...el.getCustomProperties() ];