| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Model from '../../../src/model/model';
- import { injectSelectionPostFixer } from '../../../src/model/utils/selection-post-fixer';
- import { getData as getModelData, setData as setModelData } from '../../../src/dev-utils/model';
- describe( 'Selection post-fixer', () => {
- describe( 'injectSelectionPostFixer()', () => {
- it( 'is a function', () => {
- expect( injectSelectionPostFixer ).to.be.a( 'function' );
- } );
- } );
- describe( 'injected behavior', () => {
- let model, modelRoot;
- beforeEach( () => {
- model = new Model();
- modelRoot = model.document.createRoot();
- model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- model.schema.register( 'table', {
- allowWhere: '$block',
- allowAttributes: [ 'headingRows', 'headingColumns' ],
- isLimit: true,
- isObject: true
- } );
- model.schema.register( 'tableRow', {
- allowIn: 'table',
- isLimit: true
- } );
- model.schema.register( 'tableCell', {
- allowIn: 'tableRow',
- allowAttributes: [ 'colspan', 'rowspan' ],
- isLimit: true
- } );
- model.schema.register( 'image', {
- isObject: true,
- isBlock: true,
- allowWhere: '$block'
- } );
- model.schema.extend( '$block', { allowIn: 'tableCell' } );
- model.schema.register( 'caption', {
- allowIn: 'image',
- allowContentOf: '$block',
- isLimit: true
- } );
- model.schema.register( 'inlineWidget', {
- isObject: true,
- allowIn: [ '$block', '$clipboardHolder' ]
- } );
- model.schema.register( 'figure', {
- allowIn: '$root',
- allowAttributes: [ 'name', 'title' ]
- } );
- } );
- it( 'should not crash if there is no correct position for model selection', () => {
- setModelData( model, '' );
- expect( getModelData( model ) ).to.equal( '[]' );
- } );
- it( 'should react to structure changes', () => {
- setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
- model.change( writer => {
- writer.remove( modelRoot.getChild( 0 ) );
- } );
- expect( getModelData( model ) ).to.equal( '[<image></image>]' );
- } );
- it( 'should react to selection changes', () => {
- setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
- // <paragraph>foo</paragraph>[]<image></image>
- model.change( writer => {
- writer.setSelection(
- writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 1 ) )
- );
- } );
- expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><image></image>' );
- } );
- describe( 'non-collapsed selection - table scenarios', () => {
- beforeEach( () => {
- setModelData( model,
- '<paragraph>[]foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #1 - range start outside table, end on table cell', () => {
- // <paragraph>f[oo</paragraph><table><tableRow><tableCell></tableCell>]<tableCell>...
- model.change( writer => {
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
- writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #2 - range start on table cell, end outside table', () => {
- // ...<table><tableRow><tableCell></tableCell>[<tableCell></tableCell></tableRow></table><paragraph>b]ar</paragraph>
- model.change( writer => {
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 ),
- writer.createPositionAt( modelRoot.getChild( 2 ), 1 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>b]ar</paragraph>'
- );
- } );
- it( 'should fix #3', () => {
- // <paragraph>f[oo</paragraph><table>]<tableRow>...
- model.change( writer => {
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
- writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #4', () => {
- // <paragraph>foo</paragraph><table><tableRow><tableCell>a[aa</tableCell><tableCell>b]bb</tableCell>
- model.change( writer => {
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 0, 0 ] ), 1 ),
- writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 1, 0 ] ), 2 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #5', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '[]' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>xxx</paragraph></tableCell>' +
- '<tableCell><paragraph>yyy</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>baz</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>xxx</paragraph></tableCell>' +
- '<tableCell><paragraph>yyy</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>baz</paragraph>'
- );
- } );
- // There's a chance that this and the following test will not be up to date with
- // how the table feature is really implemented once we'll introduce row/cells/columns selection
- // in which case all these elements will need to be marked as objects.
- it( 'should fix #6 (element selection of not an object)', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '[<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>]' +
- '</table>' +
- '<paragraph>baz</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<paragraph>baz</paragraph>'
- );
- } );
- it( 'should fix #7 (element selection of non-objects)', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '[<tableRow>' +
- '<tableCell><paragraph>1</paragraph></tableCell>' +
- '<tableCell><paragraph>2</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>3</paragraph></tableCell>' +
- '<tableCell><paragraph>4</paragraph></tableCell>]' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>5</paragraph></tableCell>' +
- '<tableCell><paragraph>6</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>baz</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>1</paragraph></tableCell>' +
- '<tableCell><paragraph>2</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>3</paragraph></tableCell>' +
- '<tableCell><paragraph>4</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>5</paragraph></tableCell>' +
- '<tableCell><paragraph>6</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<paragraph>baz</paragraph>'
- );
- } );
- it( 'should fix #8 (cross-limit selection which starts in a non-limit elements)', () => {
- model.schema.extend( 'paragraph', { allowIn: 'tableCell' } );
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>f[oo</paragraph></tableCell>' +
- '<tableCell><paragraph>b]ar</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>baz</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>foo</paragraph></tableCell>' +
- '<tableCell><paragraph>bar</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<paragraph>baz</paragraph>'
- );
- } );
- it( 'should not fix #1 - selection over paragraphs outside table', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>b[ar</paragraph>' +
- '<paragraph>ba]z</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>b[ar</paragraph>' +
- '<paragraph>ba]z</paragraph>'
- );
- } );
- it( 'should not fix #2 - selection over image in table', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>foo</paragraph><image></image></tableCell>' +
- '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- model.change( writer => {
- const image = model.document.getRoot().getNodeByPath( [ 1, 0, 0, 1 ] );
- writer.setSelection( writer.createRangeOn( image ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>foo</paragraph>[<image></image>]</tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- } );
- it( 'should not fix #3 - selection over paragraph & image in table', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>foo</paragraph><image></image></tableCell>' +
- '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- model.change( writer => {
- const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
- writer.setSelection( writer.createRangeIn( tableCell ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>[foo</paragraph><image></image>]</tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- } );
- it( 'should not fix #4 - selection over image & paragraph in table', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><image></image><paragraph>foo</paragraph></tableCell>' +
- '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- model.change( writer => {
- const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
- writer.setSelection( writer.createRangeIn( tableCell ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell>[<image></image><paragraph>foo]</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- } );
- it( 'should not fix #5 - selection over blockQuote in table', () => {
- model.schema.register( 'blockQuote', {
- allowWhere: '$block',
- allowContentOf: '$root'
- } );
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><blockQuote><paragraph>foo</paragraph></blockQuote></tableCell>' +
- '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- model.change( writer => {
- const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
- writer.setSelection( writer.createRangeIn( tableCell ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><blockQuote><paragraph>[foo]</paragraph></blockQuote></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- } );
- it( 'should fix multiple ranges #1', () => {
- model.change( writer => {
- const ranges = [
- writer.createRange(
- writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
- writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
- ),
- writer.createRange(
- writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
- writer.createPositionFromPath( modelRoot, [ 1, 1 ] )
- )
- ];
- writer.setSelection( ranges );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix multiple ranges #2', () => {
- model.change( writer => {
- const ranges = [
- writer.createRange(
- writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
- writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
- ),
- writer.createRange(
- writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
- writer.createPositionFromPath( modelRoot, [ 2, 2 ] )
- )
- ];
- writer.setSelection( ranges );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>ba]r</paragraph>'
- );
- } );
- it( 'should fix multiple ranges #3', () => {
- setModelData( model,
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>]' +
- '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>]' +
- '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>]' +
- '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>b]az</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>b]az</paragraph>'
- );
- } );
- it( 'should fix multiple ranges #4', () => {
- model.change( writer => {
- const ranges = [
- writer.createRange(
- writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
- writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
- ),
- writer.createRange(
- writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
- writer.createPositionFromPath( modelRoot, [ 2, 1 ] )
- ),
- writer.createRange(
- writer.createPositionFromPath( modelRoot, [ 2, 2 ] ),
- writer.createPositionFromPath( modelRoot, [ 2, 3 ] )
- )
- ];
- writer.setSelection( ranges );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>bar]</paragraph>'
- );
- } );
- } );
- describe( 'non-collapsed selection - image scenarios', () => {
- beforeEach( () => {
- setModelData( model,
- '<paragraph>[]foo</paragraph>' +
- '<image>' +
- '<caption>xxx</caption>' +
- '</image>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #1 (crossing object and limit boundaries)', () => {
- model.change( writer => {
- // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
- writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<image>' +
- '<caption>xxx</caption>' +
- '</image>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #2 (crossing object boundary)', () => {
- model.change( writer => {
- // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
- writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<image>' +
- '<caption>xxx</caption>' +
- '</image>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #3 (crossing object boundary)', () => {
- model.change( writer => {
- // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
- writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>f[oo</paragraph>' +
- '<image>' +
- '<caption>xxx</caption>' +
- '</image>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #4 (element selection of not an object)', () => {
- model.change( writer => {
- // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot.getChild( 1 ), 0 ),
- writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<image>' +
- '<caption>xxx</caption>' +
- '</image>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should not fix #1 (element selection of an object)', () => {
- model.change( writer => {
- // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( modelRoot, 1 ),
- writer.createPositionAt( modelRoot, 2 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '[<image>' +
- '<caption>xxx</caption>' +
- '</image>]' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should not fix #2 (inside a limit)', () => {
- model.change( writer => {
- const caption = modelRoot.getChild( 1 ).getChild( 0 );
- // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( caption, 0 ),
- writer.createPositionAt( caption, 3 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<image>' +
- '<caption>[xxx]</caption>' +
- '</image>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should not fix #3 (inside a limit - partial text selection)', () => {
- model.change( writer => {
- const caption = modelRoot.getChild( 1 ).getChild( 0 );
- // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( caption, 0 ),
- writer.createPositionAt( caption, 2 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<image>' +
- '<caption>[xx]x</caption>' +
- '</image>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should not fix #4 (inside a limit - partial text selection)', () => {
- model.change( writer => {
- const caption = modelRoot.getChild( 1 ).getChild( 0 );
- // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
- writer.setSelection( writer.createRange(
- writer.createPositionAt( caption, 1 ),
- writer.createPositionAt( caption, 3 )
- ) );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<image>' +
- '<caption>x[xx]</caption>' +
- '</image>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
- setModelData( model,
- '[<figure></figure>]'
- );
- expect( getModelData( model ) ).to.equal(
- '[<figure></figure>]'
- );
- } );
- } );
- describe( 'non-collapsed selection - other scenarios', () => {
- it( 'should fix #1 (element selection of not an object)', () => {
- setModelData( model,
- '<paragraph>aaa</paragraph>' +
- '[<paragraph>bbb</paragraph>]' +
- '<paragraph>ccc</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>aaa</paragraph>' +
- '<paragraph>[bbb]</paragraph>' +
- '<paragraph>ccc</paragraph>'
- );
- } );
- it( 'should fix #2 (elements selection of not an object)', () => {
- setModelData( model,
- '<paragraph>aaa</paragraph>' +
- '[<paragraph>bbb</paragraph>' +
- '<paragraph>ccc</paragraph>]'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>aaa</paragraph>' +
- '<paragraph>[bbb</paragraph>' +
- '<paragraph>ccc]</paragraph>'
- );
- } );
- it( 'should fix #3 (partial selection of not an object)', () => {
- setModelData( model,
- '<paragraph>aaa</paragraph>' +
- '[<paragraph>bbb</paragraph>' +
- '<paragraph>ccc]</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>aaa</paragraph>' +
- '<paragraph>[bbb</paragraph>' +
- '<paragraph>ccc]</paragraph>'
- );
- } );
- it( 'should fix #4 (partial selection of not an object)', () => {
- setModelData( model,
- '<paragraph>aaa</paragraph>' +
- '<paragraph>b[bb</paragraph>]' +
- '<paragraph>ccc</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>aaa</paragraph>' +
- '<paragraph>b[bb]</paragraph>' +
- '<paragraph>ccc</paragraph>'
- );
- } );
- it( 'should fix #5 (partial selection of not an object)', () => {
- setModelData( model,
- '<paragraph>aaa</paragraph>' +
- '[<paragraph>bb]b</paragraph>' +
- '<paragraph>ccc</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>aaa</paragraph>' +
- '<paragraph>[bb]b</paragraph>' +
- '<paragraph>ccc</paragraph>'
- );
- } );
- it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
- model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
- model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
- model.schema.register( 'c', { allowIn: 'b' } );
- model.schema.extend( '$text', { allowIn: 'c' } );
- setModelData( model,
- '<a><b><c>[</c></b></a>]'
- );
- expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
- } );
- it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
- model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
- model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
- model.schema.register( 'c', { allowIn: 'b' } );
- model.schema.extend( '$text', { allowIn: 'c' } );
- setModelData( model,
- '[<a><b><c>]</c></b></a>'
- );
- expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
- } );
- it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
- model.schema.register( 'div', { allowIn: '$root' } );
- model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
- model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
- model.schema.register( 'c', { allowIn: 'b' } );
- model.schema.extend( '$text', { allowIn: 'c' } );
- setModelData( model,
- '<div>[<a><b><c>]</c></b></a></div>'
- );
- expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
- } );
- it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
- model.schema.register( 'div', { allowIn: '$root' } );
- model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
- model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
- model.schema.register( 'c', { allowIn: 'b' } );
- model.schema.extend( '$text', { allowIn: 'c' } );
- setModelData( model,
- '<div><a><b><c>[</c></b></a>]</div>'
- );
- expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
- } );
- it( 'should not fix #1 (selection on text node)', () => {
- setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
- expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
- } );
- it( 'should not fix #2', () => {
- setModelData( model,
- '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
- );
- } );
- it( 'should not fix #3', () => {
- setModelData( model,
- '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
- );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
- );
- } );
- it( 'should not fix #4 - object in object', () => {
- model.schema.register( 'div', {
- allowIn: [ '$root', 'div' ],
- isObject: true
- } );
- setModelData( model, '<div>[<div></div>]</div>' );
- model.change( writer => {
- const innerDiv = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
- writer.setSelection( writer.createRangeOn( innerDiv ) );
- } );
- expect( getModelData( model ) ).to.equal( '<div>[<div></div>]</div>' );
- } );
- } );
- describe( 'collapsed selection', () => {
- beforeEach( () => {
- setModelData( model,
- '<paragraph>[]foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #1', () => {
- // <table>[]<tableRow>...
- model.change( writer => {
- writer.setSelection(
- writer.createRange( writer.createPositionAt( modelRoot.getChild( 1 ), 0 ) )
- );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo[]</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix #2', () => {
- // <table><tableRow>[]<tableCell>...
- model.change( writer => {
- const row = modelRoot.getChild( 1 ).getChild( 0 );
- writer.setSelection(
- writer.createRange( writer.createPositionAt( row, 0 ) )
- );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should fix multiple ranges #1', () => {
- // []<paragraph>foo</paragraph>[]<table>...
- model.change( writer => {
- writer.setSelection(
- [
- writer.createRange( writer.createPositionAt( modelRoot, 0 ) ),
- writer.createRange( writer.createPositionAt( modelRoot, 1 ) )
- ]
- );
- } );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>[foo]</paragraph>' +
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>aaa</paragraph></tableCell>' +
- '<tableCell><paragraph>bbb</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- } );
- } );
- } );
|