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

Commit

Permalink
Complete code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Mar 25, 2020
1 parent 4c66093 commit edefe8a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 61 deletions.
18 changes: 7 additions & 11 deletions src/tableselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,13 @@ export default class TableSelection extends Plugin {

const rangeToSelect = model.schema.getNearestSelectionRange( writer.createPositionAt( tableCellToSelect, 0 ) );

if ( rangeToSelect ) {
if ( selection.is( 'documentSelection' ) ) {
writer.setSelection( rangeToSelect );
} else {
selection.setTo( rangeToSelect );
}
// Note: we ignore the case where rangeToSelect may be null because deleteContent() will always (unless someone broke it)
// create an empty paragraph to accommodate the selection.

if ( selection.is( 'documentSelection' ) ) {
writer.setSelection( rangeToSelect );
} else {
selection.setTo( rangeToSelect );
}
} );
}
Expand Down Expand Up @@ -395,11 +396,6 @@ export default class TableSelection extends Plugin {
const modelPosition = this.editor.editing.mapper.toModelPosition( viewPosition );
const modelElement = modelPosition.parent;

// TODO: Required?
if ( !modelElement ) {
return;
}

if ( modelElement.is( 'tableCell' ) ) {
return modelElement;
}
Expand Down
50 changes: 0 additions & 50 deletions tests/tableselection-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,56 +104,6 @@ describe( 'TableSelection - integration', () => {
[ '31', '32', '33' ]
] ) );
} );

it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete backwards)', () => {
const selection = model.createSelection( [
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
),
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
)
] );

model.change( writer => {
model.deleteContent( selection );
writer.setSelection( selection );
} );

assertEqualMarkup( getModelData( model ), modelTable( [
[ '', '[]', '13' ],
[ '21', '22', '23' ],
[ '31', '32', '33' ]
] ) );
} );

it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete forwards)', () => {
const selection = model.createSelection( [
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
),
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
)
] );

model.change( writer => {
model.deleteContent( selection, {
direction: 'forward'
} );
writer.setSelection( selection );
} );

assertEqualMarkup( getModelData( model ), modelTable( [
[ '[]', '', '13' ],
[ '21', '22', '23' ],
[ '31', '32', '33' ]
] ) );
} );
} );

describe( 'on user input', () => {
Expand Down
86 changes: 86 additions & 0 deletions tests/tableselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ describe( 'table selection', () => {
expect( ranges[ 0 ].start.path ).to.deep.equal( [ 0, 0, 0, 0, 0 ] );
} );

it( 'should reenable table selection when reenabling the plugin', () => {
tableSelection.forceDisabled( 'foo' );
tableSelection.clearForceDisabled( 'foo' );

const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );

tableSelection._setCellSelection(
firstCell,
lastCell
);

assertSelectedCells( model, [
[ 1, 1, 0 ],
[ 1, 1, 0 ],
[ 0, 0, 0 ]
] );
} );

it( 'should do nothing if there were no multi-cell selections', () => {
tableSelection.forceDisabled( 'foo' );

Expand Down Expand Up @@ -743,6 +762,73 @@ describe( 'table selection', () => {
[ '31', '32', '33' ]
] ) );
} );

it( 'should work with document selection passed to Model#deleteContent()', () => {
tableSelection._setCellSelection(
modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
modelRoot.getNodeByPath( [ 0, 1, 1 ] )
);

model.change( () => {
model.deleteContent( model.document.selection );
} );

assertEqualMarkup( getModelData( model ), modelTable( [
[ '', '', '13' ],
[ '', '[]', '23' ],
[ '31', '32', '33' ]
] ) );
} );

it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete backwards)', () => {
const selection = model.createSelection( [
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
),
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
)
] );

model.change( writer => {
model.deleteContent( selection );
writer.setSelection( selection );
} );

assertEqualMarkup( getModelData( model ), modelTable( [
[ '', '[]', '13' ],
[ '21', '22', '23' ],
[ '31', '32', '33' ]
] ) );
} );

it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete forwards)', () => {
const selection = model.createSelection( [
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
),
model.createRange(
model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
)
] );

model.change( writer => {
model.deleteContent( selection, {
direction: 'forward'
} );
writer.setSelection( selection );
} );

assertEqualMarkup( getModelData( model ), modelTable( [
[ '[]', '', '13' ],
[ '21', '22', '23' ],
[ '31', '32', '33' ]
] ) );
} );
} );

function createEditor() {
Expand Down

0 comments on commit edefe8a

Please sign in to comment.