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

Commit

Permalink
Merge branch 'master' into t/898
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Mar 26, 2018
2 parents dcef94f + ce37c56 commit e2d4bcf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/conversion/upcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function upcastElementToAttribute( config ) {

_normalizeModelAttributeConfig( config );

const converter = _prepareToAttributeConverter( config, true );
const converter = _prepareToAttributeConverter( config );

const elementName = _getViewElementNameFromConfig( config );
const eventName = elementName ? 'element:' + elementName : 'element';
Expand Down Expand Up @@ -223,7 +223,7 @@ export function upcastAttributeToAttribute( config ) {

_normalizeModelAttributeConfig( config, viewKey );

const converter = _prepareToAttributeConverter( config, false );
const converter = _prepareToAttributeConverter( config );

return dispatcher => {
dispatcher.on( 'element', converter, { priority: config.priority || 'low' } );
Expand Down Expand Up @@ -442,7 +442,7 @@ function _normalizeModelAttributeConfig( config, viewAttributeKeyToCopy = null )
// @param {Object|Array.<Object>} config Conversion configuration. It is possible to provide multiple configurations in an array.
// @param {Boolean} consumeName If set to `true` converter will try to consume name. If set to `false` converter will not try to
// consume name. This flag overwrites parameter returned by `Matcher#match`.
function _prepareToAttributeConverter( config, consumeName ) {
function _prepareToAttributeConverter( config ) {
const matcher = new Matcher( config.view );

return ( evt, data, conversionApi ) => {
Expand All @@ -461,11 +461,11 @@ function _prepareToAttributeConverter( config, consumeName ) {
return;
}

if ( !consumeName ) {
if ( _onlyViewNameIsDefined( config ) ) {
match.match.name = true;
} else {
// Do not test or consume `name` consumable.
delete match.match.name;
} else {
match.match.name = true;
}

// Try to consume appropriate values from consumable values list.
Expand All @@ -489,6 +489,18 @@ function _prepareToAttributeConverter( config, consumeName ) {
};
}

// Helper function that checks if element name should be consumed in attribute converters.
//
// @param {Object} config Conversion config.
// @returns {Boolean}
function _onlyViewNameIsDefined( config ) {
if ( typeof config.view == 'object' && !_getViewElementNameFromConfig( config ) ) {
return false;
}

return !config.view.class && !config.view.attribute && !config.view.style;
}

// Helper function for to-model-attribute converter. Sets model attribute on given range. Checks {@link module:engine/model/schema~Schema}
// to ensure proper model structure.
//
Expand Down
45 changes: 44 additions & 1 deletion tests/conversion/upcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe( 'upcast-helpers', () => {

schema.extend( '$text', {
allowIn: '$root',
allowAttributes: [ 'bold' ]
allowAttributes: [ 'bold', 'attribA', 'attribB' ]
} );

schema.register( 'paragraph', {
Expand Down Expand Up @@ -300,6 +300,49 @@ describe( 'upcast-helpers', () => {
'<$text bold="true">foo</$text>'
);
} );

it( 'should allow two converters to convert attributes on the same element', () => {
const helperA = upcastElementToAttribute( {
model: 'attribA',
view: { name: 'span', class: 'attrib-a' }
} );

const helperB = upcastElementToAttribute( {
model: 'attribB',
view: { name: 'span', style: { color: 'attrib-b' } }
} );

conversion.for( 'upcast' ).add( helperA ).add( helperB );

expectResult(
new ViewAttributeElement( 'span', { class: 'attrib-a', style: 'color:attrib-b;' }, new ViewText( 'foo' ) ),
'<$text attribA="true" attribB="true">foo</$text>'
);
} );

it( 'should consume element only when only is name specified', () => {
const helperBold = upcastElementToAttribute( {
model: 'bold',
view: { name: 'strong' }
} );

const helperA = upcastElementToAttribute( {
model: 'attribA',
view: { name: 'strong' }
} );

const helperB = upcastElementToAttribute( {
model: 'attribB',
view: { name: 'strong', class: 'foo' }
} );

conversion.for( 'upcast' ).add( helperBold ).add( helperA ).add( helperB );

expectResult(
new ViewAttributeElement( 'strong', { class: 'foo' }, new ViewText( 'foo' ) ),
'<$text attribB="true" bold="true">foo</$text>'
);
} );
} );

describe( 'upcastAttributeToAttribute', () => {
Expand Down

0 comments on commit e2d4bcf

Please sign in to comment.