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

Extended docs and tests for Template DOM updater helper functions.

Aleksander Nowodzinski 9 лет назад
Родитель
Сommit
523c9d4f44

+ 11 - 7
packages/ckeditor5-engine/src/ui/template.js

@@ -260,11 +260,13 @@ export default class Template {
 }
 
 /**
- * Returns a function which, when called in the context of HTMLElement,
- * it replaces element children with a text node of given value.
+ * Returns an object consisting of `set` and `remove` functions, which
+ * can be used in the context of DOM Node to set or reset `textContent`.
+ * See {@link View#_getModelBinder}.
  *
  * @private
- * @param {Function}
+ * @param {Node} node DOM Node to be modified.
+ * @returns {Object}
  */
 function getTextNodeUpdater( node ) {
 	return {
@@ -279,12 +281,14 @@ function getTextNodeUpdater( node ) {
 }
 
 /**
- * Returns a function which, when called in the context of HTMLElement,
- * it updates element's attribute with given value.
+ * Returns an object consisting of `set` and `remove` functions, which
+ * can be used in the context of DOM Node to set or reset an attribute.
+ * See {@link View#_getModelBinder}.
  *
  * @private
- * @param {String} attr A name of the attribute to be updated.
- * @param {Function}
+ * @param {Node} node DOM Node to be modified.
+ * @param {String} attrName Name of the attribute to be modified.
+ * @returns {Object}
  */
 function getElementAttributeUpdater( el, attrName ) {
 	return {

+ 44 - 0
packages/ckeditor5-engine/tests/ui/template.js

@@ -291,6 +291,50 @@ describe( 'Template', () => {
 			sinon.assert.calledWithExactly( spy1, el.firstChild, sinon.match.object );
 			sinon.assert.calledWithExactly( spy2, el.lastChild.firstChild, sinon.match.object );
 		} );
+
+		it( 'uses DOM updater – attributes', () => {
+			const spy = testUtils.sinon.spy();
+			const el = new Template( {
+				tag: 'p',
+				attributes: {
+					'class': {}
+				},
+				_modelBinders: {
+					attributes: {
+						class: spy
+					}
+				}
+			} ).render();
+
+			// Check whether DOM updater is correct.
+			spy.firstCall.args[ 1 ].set( 'x' );
+			expect( el.outerHTML ).to.be.equal( '<p class="x"></p>' );
+
+			spy.firstCall.args[ 1 ].remove();
+			expect( el.outerHTML ).to.be.equal( '<p></p>' );
+		} );
+
+		it( 'uses DOM updater – text', () => {
+			const spy = testUtils.sinon.spy();
+			const el = new Template( {
+				tag: 'p',
+				children: [
+					{
+						text: {},
+						_modelBinders: {
+							text: spy
+						}
+					}
+				],
+			} ).render();
+
+			// Check whether DOM updater is correct.
+			spy.firstCall.args[ 1 ].set( 'x' );
+			expect( el.outerHTML ).to.be.equal( '<p>x</p>' );
+
+			spy.firstCall.args[ 1 ].remove();
+			expect( el.outerHTML ).to.be.equal( '<p></p>' );
+		} );
 	} );
 
 	describe( 'apply', () => {