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

Commit

Permalink
Merge pull request #1107 from ckeditor/t/1102
Browse files Browse the repository at this point in the history
Internal: Added `id` property to HighlightDescriptor. Closes #1102.
  • Loading branch information
Piotr Jasiun authored Aug 25, 2017
2 parents b7cc243 + 1075ecf commit 0b2549b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/conversion/buildmodelconverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ export default function buildModelConverter() {
*
* @property {String|Array.<String>} class CSS class or array of classes that will be added to `span`
* {@link module:engine/view/attributeelement~AttributeElement} wrapping each text node in the highlighted content.
* @property {String} [id] Descriptor identifier. If not provided, marker's name from which given highlight is created
* will be used.
* @property {Number} [priority] {@link module:engine/view/attributeelement~AttributeElement#priority} of the `span`
* wrapping each text node in the highlighted content. If not provided, default 10 priority will be used.
* @property {Object} [attributes] Attributes that will be added to `span`
Expand Down
5 changes: 5 additions & 0 deletions src/conversion/model-to-view-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ export function highlightText( highlightDescriptor ) {
* priority, priority 10 will be used as default, to be compliant with
* {@link module:engine/conversion/model-to-view-converters~highlightText} method which uses default priority of
* {@link module:engine/view/attributeelement~AttributeElement}.
* If highlight descriptor will not provide id property, name of the marker will be used.
* When `addHighlight` and `removeHighlight` custom properties are not present, element is not converted
* in any special way. This means that converters will proceed to convert element's child nodes.
*
Expand All @@ -473,6 +474,10 @@ export function highlightElement( highlightDescriptor ) {
descriptor.priority = 10;
}

if ( !descriptor.id ) {
descriptor.id = data.markerName;
}

const viewElement = conversionApi.mapper.toViewElement( modelItem );
const addMarker = evt.name.split( ':' )[ 0 ] == 'addMarker';
const highlightHandlingMethod = addMarker ? 'addHighlight' : 'removeHighlight';
Expand Down
34 changes: 33 additions & 1 deletion tests/conversion/model-to-view-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ describe( 'model-to-view-converters', () => {
const highlightDescriptor = {
class: 'highlight-class',
priority: 7,
attributes: { title: 'title' }
attributes: { title: 'title' },
id: 'customId'
};

beforeEach( () => {
Expand Down Expand Up @@ -276,6 +277,37 @@ describe( 'model-to-view-converters', () => {
expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
} );

it( 'should use provide default priority and id if not provided', () => {
const highlightDescriptor = { class: 'highlight-class' };

dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
dispatcher.on( 'removeMarker:marker', highlightElement( highlightDescriptor ) );
dispatcher.on( 'insert:paragraph', insertElement( data => {
// Use special converter only for first paragraph.
if ( data.item == modelElement2 ) {
return;
}

const viewContainer = new ViewContainerElement( 'p' );

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

viewContainer.setCustomProperty( 'removeHighlight', ( element, descriptor ) => {
expect( descriptor.priority ).to.equal( 10 );
expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
} );

return viewContainer;
} ), { priority: 'high' } );

dispatcher.convertInsertion( markerRange );
modelDoc.markers.set( 'marker', markerRange );
dispatcher.convertMarker( 'addMarker', 'marker:foo-bar-baz', markerRange );
} );

it( 'should do nothing if descriptor is not provided', () => {
dispatcher.on( 'addMarker:marker', highlightElement( () => null ) );
dispatcher.on( 'removeMarker:marker', highlightElement( () => null ) );
Expand Down

0 comments on commit 0b2549b

Please sign in to comment.