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 #1507 from CHItA/t/1415
Browse files Browse the repository at this point in the history
Other: Allowed using `Mapper` outside the conversion scope. Closes #1415.

Thanks to [Mate Bartus](https://github.com/CHItA) for this contribution!
  • Loading branch information
scofalik authored Aug 24, 2018
2 parents 49cd795 + b37802a commit 6de6a00
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export default class DataController {
* @returns {module:engine/view/documentfragment~DocumentFragment} Output view DocumentFragment.
*/
toView( modelElementOrFragment ) {
// Clear bindings so the call to this method gives correct results.
this.mapper.clearBindings();

// First, convert elements.
const modelRange = ModelRange.createIn( modelElementOrFragment );

Expand All @@ -167,9 +170,6 @@ export default class DataController {
}
}

// Clear bindings so the next call to this method gives correct results.
this.mapper.clearBindings();

return viewDocumentFragment;
}

Expand Down
31 changes: 27 additions & 4 deletions tests/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

import Model from '../../src/model/model';
import Range from '../../src/model/range';
import ModelRange from '../../src/model/range';
import ViewRange from '../../src/view/range';
import DataController from '../../src/controller/datacontroller';
import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
Expand Down Expand Up @@ -406,7 +407,7 @@ describe( 'DataController', () => {

model.change( writer => {
writer.insert( modelElement, modelRoot, 0 );
const range = Range.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 1 );
const range = ModelRange.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 1 );
writer.addMarker( 'marker:a', { range, usingOperation: true } );
} );

Expand All @@ -429,8 +430,8 @@ describe( 'DataController', () => {
model.change( writer => {
writer.insert( modelElement, modelRoot, 0 );

const rangeA = Range.createFromParentsAndOffsets( modelP1, 1, modelP1, 3 );
const rangeB = Range.createFromParentsAndOffsets( modelP2, 0, modelP2, 2 );
const rangeA = ModelRange.createFromParentsAndOffsets( modelP1, 1, modelP1, 3 );
const rangeB = ModelRange.createFromParentsAndOffsets( modelP2, 0, modelP2, 2 );

writer.addMarker( 'marker:a', { range: rangeA, usingOperation: true } );
writer.addMarker( 'marker:b', { range: rangeB, usingOperation: true } );
Expand All @@ -454,6 +455,28 @@ describe( 'DataController', () => {
expect( viewElement.childCount ).to.equal( 1 );
expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
} );

it( 'should keep view-model mapping', () => {
const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
const viewDocumentFragment = data.toView( modelDocumentFragment );

const firstModelElement = modelDocumentFragment.getChild( 0 );
const firstViewElement = viewDocumentFragment.getChild( 0 );

const modelRange = ModelRange.createOn( firstModelElement );
const viewRange = ViewRange.createOn( firstViewElement );

const mappedModelRange = data.mapper.toModelRange( viewRange );
const mappedViewRange = data.mapper.toViewRange( modelRange );

expect( mappedModelRange ).to.be.instanceOf( ModelRange );
expect( mappedViewRange ).to.be.instanceOf( ViewRange );

expect( mappedModelRange.end.nodeBefore ).to.equal( firstModelElement );
expect( mappedModelRange.end.nodeAfter ).to.equal( modelDocumentFragment.getChild( 1 ) );
expect( mappedViewRange.end.nodeBefore ).to.equal( firstViewElement );
expect( mappedViewRange.end.nodeAfter ).to.equal( viewDocumentFragment.getChild( 1 ) );
} );
} );

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

0 comments on commit 6de6a00

Please sign in to comment.