浏览代码

Added support for namespeces in View class, respecting Template syntax.

Aleksander Nowodzinski 9 年之前
父节点
当前提交
d8f457bbf3
共有 2 个文件被更改,包括 38 次插入0 次删除
  1. 12 0
      packages/ckeditor5-ui/src/view.js
  2. 26 0
      packages/ckeditor5-ui/tests/view.js

+ 12 - 0
packages/ckeditor5-ui/src/view.js

@@ -445,6 +445,12 @@ export default class View {
 	 * @return {Function}
 	 */
 	_getModelBinder( valueSchema ) {
+		// Normalize attributes with additional data like namespace:
+		// class: { ns: 'abc', value: [ ... ] }
+		if ( valueSchema.value ) {
+			valueSchema = valueSchema.value;
+		}
+
 		valueSchema = normalizeBinderValueSchema( valueSchema );
 
 		// Assembles the value using {@link ui.TemplateValueSchema} and stores it in a form of
@@ -677,6 +683,12 @@ function normalizeBinderValueSchema( valueSchema ) {
  * @returns {Boolean}
  */
 function hasModelBinding( valueSchema ) {
+	// Normalize attributes with additional data like namespace:
+	// class: { ns: 'abc', value: [ ... ] }
+	if ( valueSchema.value ) {
+		valueSchema = valueSchema.value;
+	}
+
 	if ( Array.isArray( valueSchema ) ) {
 		return valueSchema.some( hasModelBinding );
 	} else if ( valueSchema.model ) {

+ 26 - 0
packages/ckeditor5-ui/tests/view.js

@@ -279,6 +279,7 @@ describe( 'View', () => {
 
 				view.model.foo = 'baz';
 				expect( view.element.outerHTML ).to.equal( '<p class="baz">abc</p>' );
+				expect( view.element.attributes.getNamedItem( 'class' ).namespaceURI ).to.be.null;
 			} );
 
 			it( 'allows binding attribute to the model – simple (Text Node)', () => {
@@ -467,6 +468,31 @@ describe( 'View', () => {
 				view.model.foo = '';
 				expect( view.element.outerHTML ).to.equal( '<p>abc</p>' );
 			} );
+
+			it( 'allows binding attribute to the model – a custom namespace', () => {
+				setTestViewClass( function() {
+					const bind = this.attributeBinder;
+
+					return {
+						tag: 'p',
+						attributes: {
+							'class': {
+								ns: 'foo',
+								value: bind.to( 'foo' )
+							}
+						},
+						children: [ 'abc' ]
+					};
+				} );
+
+				setTestViewInstance( { foo: 'bar' } );
+				expect( view.element.outerHTML ).to.equal( '<p class="bar">abc</p>' );
+				expect( view.element.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
+
+				view.model.foo = 'baz';
+				expect( view.element.outerHTML ).to.equal( '<p class="baz">abc</p>' );
+				expect( view.element.attributes.getNamedItem( 'class' ).namespaceURI ).to.equal( 'foo' );
+			} );
 		} );
 
 		describe( 'if', () => {