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

Commit

Permalink
Aligned list converters to latest conversion API.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Jan 30, 2018
1 parent c33d8c8 commit f14ba86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
31 changes: 16 additions & 15 deletions src/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ export function modelViewMergeAfter( evt, data, conversionApi ) {
* @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable Values to consume.
* @param {Object} conversionApi Conversion interface to be used by the callback.
*/
export function viewModelConverter( evt, data, consumable, conversionApi ) {
if ( consumable.consume( data.input, { name: true } ) ) {
export function viewModelConverter( evt, data, conversionApi ) {
if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
const writer = conversionApi.writer;
const conversionData = this.conversionApi.data;

Expand All @@ -363,34 +363,35 @@ export function viewModelConverter( evt, data, consumable, conversionApi ) {
writer.setAttribute( 'indent', conversionData.indent, listItem );

// Set 'bulleted' as default. If this item is pasted into a context,
const type = data.input.parent && data.input.parent.name == 'ol' ? 'numbered' : 'bulleted';
const type = data.viewItem.parent && data.viewItem.parent.name == 'ol' ? 'numbered' : 'bulleted';
writer.setAttribute( 'type', type, listItem );

// `listItem`s created recursively should have bigger indent.
conversionData.indent++;

writer.insert( listItem, data.position );
writer.insert( listItem, data.cursorPosition );

// Remember position after list item.
let nextPosition = ModelPosition.createAfter( listItem );

// Check all children of the converted `<li>`.
// At this point we assume there are no "whitespace" view text nodes in view list, between view list items.
// This should be handled by `<ul>` and `<ol>` converters.
for ( const child of data.input.getChildren() ) {
for ( const child of data.viewItem.getChildren() ) {
// If this is a view list element, we will convert it after last `listItem` model element.
if ( child.name == 'ul' || child.name == 'ol' ) {
nextPosition = conversionApi.convertItem( child, consumable, nextPosition ).end;
nextPosition = conversionApi.convertItem( child, nextPosition ).cursorPosition;
}
// If it was not a list it was a "regular" list item content. Just append it to `listItem`.
else {
conversionApi.convertItem( child, consumable, ModelPosition.createAt( listItem, 'end' ) );
conversionApi.convertItem( child, ModelPosition.createAt( listItem, 'end' ) );
}
}

conversionData.indent--;

data.output = new ModelRange( data.position, nextPosition );
data.modelRange = new ModelRange( data.cursorPosition, nextPosition );
data.cursorPosition = data.modelRange.end;
}
}

Expand All @@ -404,10 +405,10 @@ export function viewModelConverter( evt, data, consumable, conversionApi ) {
* @param {Object} data An object containing conversion input and a placeholder for conversion output and possibly other values.
* @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable Values to consume.
*/
export function cleanList( evt, data, consumable ) {
if ( consumable.test( data.input, { name: true } ) ) {
export function cleanList( evt, data, conversionApi ) {
if ( conversionApi.consumable.test( data.viewItem, { name: true } ) ) {
// Caching children because when we start removing them iterating fails.
const children = Array.from( data.input.getChildren() );
const children = Array.from( data.viewItem.getChildren() );

for ( const child of children ) {
if ( !child.is( 'li' ) ) {
Expand All @@ -425,13 +426,13 @@ export function cleanList( evt, data, consumable ) {
* @param {Object} data An object containing conversion input and a placeholder for conversion output and possibly other values.
* @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable Values to consume.
*/
export function cleanListItem( evt, data, consumable ) {
if ( consumable.test( data.input, { name: true } ) ) {
if ( data.input.childCount === 0 ) {
export function cleanListItem( evt, data, conversionApi ) {
if ( conversionApi.consumable.test( data.viewItem, { name: true } ) ) {
if ( data.viewItem.childCount === 0 ) {
return;
}

const children = [ ...data.input.getChildren() ];
const children = [ ...data.viewItem.getChildren() ];

let foundList = false;
let firstNode = true;
Expand Down
12 changes: 6 additions & 6 deletions tests/listengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3356,8 +3356,8 @@ describe( 'ListEngine', () => {
} );

it( 'view li converter should not fire if change was already consumed', () => {
editor.data.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
consumable.consume( data.input, { name: true } );
editor.data.viewToModel.on( 'element:li', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.viewItem, { name: true } );
}, { priority: 'highest' } );

editor.setData( '<p></p><ul><li></li></ul>' );
Expand All @@ -3366,18 +3366,18 @@ describe( 'ListEngine', () => {
} );

it( 'view ul converter should not fire if change was already consumed', () => {
editor.data.viewToModel.on( 'element:ul', ( evt, data, consumable ) => {
consumable.consume( data.input, { name: true } );
editor.data.viewToModel.on( 'element:ul', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.viewItem, { name: true } );
}, { priority: 'highest' } );

editor.setData( '<p></p><ul><li></li></ul>' );

expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph></paragraph>' );
} );

it( 'view converter should pass model range in data.output', () => {
it( 'view converter should pass model range in data.modelRange', () => {
editor.data.viewToModel.on( 'element:ul', ( evt, data ) => {
expect( data.output ).to.be.instanceof( ModelRange );
expect( data.modelRange ).to.be.instanceof( ModelRange );
}, { priority: 'lowest' } );

editor.setData( '<ul><li>Foo</li><li>Bar</li></ul>' );
Expand Down

0 comments on commit f14ba86

Please sign in to comment.