Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Introduced "engine/view/elementdefinition~ElementDefinition#priority"…
Browse files Browse the repository at this point in the history
… property.
  • Loading branch information
Kamil Piechaczek committed Apr 19, 2018
1 parent f0fd2d8 commit aa2b611
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/conversion/downcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,19 @@ function _createViewElementFromDefinition( viewElementDefinition, viewWriter, vi
}

let element;
const attributes = Object.assign( {}, viewElementDefinition.attributes );

if ( viewElementType == 'container' ) {
element = viewWriter.createContainerElement( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attributes ) );
element = viewWriter.createContainerElement( viewElementDefinition.name, attributes );
} else if ( viewElementType == 'attribute' ) {
element = viewWriter.createAttributeElement( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attributes ) );
const options = {
priority: viewElementDefinition.priority || ViewAttributeElement.DEFAULT_PRIORITY
};

element = viewWriter.createAttributeElement( viewElementDefinition.name, attributes, options );
} else {
// 'ui'.
element = viewWriter.createUIElement( viewElementDefinition.name, Object.assign( {}, viewElementDefinition.attributes ) );
element = viewWriter.createUIElement( viewElementDefinition.name, attributes );
}

if ( viewElementDefinition.styles ) {
Expand Down
1 change: 1 addition & 0 deletions src/view/elementdefinition.jsdoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@
* Value under that key must be a string.
* @property {Object} [attributes] Object with key-value pairs representing attributes. Each object key represents
* attribute name. Value under that key must be a string.
* @property {Number} [priority] Element's {@link module:engine/view/attributeelement~AttributeElement#priority priority}.
*/
29 changes: 26 additions & 3 deletions tests/conversion/downcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ModelRange from '../../src/model/range';
import ModelPosition from '../../src/model/position';

import ViewElement from '../../src/view/element';
import ViewAttributeElement from '../../src/view/attributeelement';
import ViewContainerElement from '../../src/view/containerelement';
import ViewUIElement from '../../src/view/uielement';
import ViewText from '../../src/view/text';
Expand Down Expand Up @@ -148,6 +149,25 @@ describe( 'downcast-helpers', () => {
} );

expectResult( '<span class="bg-dark font-light">foo</span>' );
expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
} );

it( 'config.view allows specifying the element\'s priority', () => {
const helper = downcastAttributeToElement( {
model: 'invert',
view: {
name: 'span',
priority: 5
}
} );

conversion.for( 'downcast' ).add( helper );

model.change( writer => {
writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
} );

expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
} );

it( 'model attribute value is enum', () => {
Expand All @@ -167,7 +187,8 @@ describe( 'downcast-helpers', () => {
name: 'span',
styles: {
'font-size': '0.8em'
}
},
priority: 5
}
}
} );
Expand All @@ -178,13 +199,15 @@ describe( 'downcast-helpers', () => {
writer.insertText( 'foo', { fontSize: 'big' }, modelRoot, 0 );
} );

expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
expectResult( '<span style="font-size:1.2em">foo</span>' );

model.change( writer => {
writer.setAttribute( 'fontSize', 'small', modelRoot.getChild( 0 ) );
} );

expectResult( '<span style="font-size:0.8em">foo</span>' );
expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );

model.change( writer => {
writer.removeAttribute( 'fontSize', modelRoot.getChild( 0 ) );
Expand Down Expand Up @@ -1602,7 +1625,7 @@ describe( 'downcast-converters', () => {
dispatcher.on( 'removeMarker:marker2', removeHighlight( () => null ) );

viewDiv._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
expect( descriptor.priority ).to.equal( 10 );
expect( descriptor.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
} );

Expand Down Expand Up @@ -1698,7 +1721,7 @@ describe( 'downcast-converters', () => {

expect( element.is( 'attributeElement' ) ).to.be.true;
expect( element.name ).to.equal( 'span' );
expect( element.priority ).to.equal( 10 );
expect( element.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
expect( element.hasClass( 'foo-class' ) ).to.be.true;

for ( const key of Object.keys( descriptor.attributes ) ) {
Expand Down

0 comments on commit aa2b611

Please sign in to comment.