Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pasting nested tables with unsupported by the editor elements will not blow up #8229

Merged
merged 2 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

<table>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell colspan="4"><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell colspan="4"></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell colspan="2"><paragraph></paragraph></tableCell>
<tableCell rowspan="2"><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell colspan="2"></tableCell>
<tableCell rowspan="2"></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell rowspan="2"><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell rowspan="2"></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
</tableRow>
</table>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<paragraph></paragraph>
<table>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell colspan="2"><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell colspan="2"></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
</tableRow>
</table>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<table>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
<tableCell></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell colspan="4"><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell colspan="4"></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell colspan="2"><paragraph></paragraph></tableCell>
<tableCell rowspan="2"><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell colspan="2"></tableCell>
<tableCell rowspan="2"></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell rowspan="2"><paragraph></paragraph></tableCell>
<tableCell></tableCell>
<tableCell rowspan="2"></tableCell>
</tableRow>
<tableRow>
<tableCell><paragraph></paragraph></tableCell>
<tableCell></tableCell>
</tableRow>
</table>

Expand Down
25 changes: 0 additions & 25 deletions packages/ckeditor5-table/src/converters/upcasttable.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,6 @@ export function skipEmptyTableRow() {
};
}

/**
* A converter that ensures an empty paragraph is inserted in a table cell if no other content was converted.
*
* @returns {Function} Conversion helper.
*/
export function ensureParagraphInTableCell( elementName ) {
return dispatcher => {
dispatcher.on( `element:${ elementName }`, ( evt, data, conversionApi ) => {
// The default converter will create a model range on converted table cell.
if ( !data.modelRange ) {
return;
}

const tableCell = data.modelRange.start.nodeAfter;

// Ensure a paragraph in the model for empty table cells for converted table cells.
if ( !tableCell.childCount ) {
const modelCursor = conversionApi.writer.createPositionAt( tableCell, 0 );

conversionApi.writer.insertElement( 'paragraph', modelCursor );
}
}, { priority: 'low' } );
};
}

// Scans table rows and extracts required metadata from the table:
//
// headingRows - The number of rows that go as table headers.
Expand Down
4 changes: 1 addition & 3 deletions packages/ckeditor5-table/src/tableediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

import upcastTable, { ensureParagraphInTableCell, skipEmptyTableRow } from './converters/upcasttable';
import upcastTable, { skipEmptyTableRow } from './converters/upcasttable';
import {
downcastInsertCell,
downcastInsertRow,
Expand Down Expand Up @@ -106,8 +106,6 @@ export default class TableEditing extends Plugin {
// Table cell conversion.
conversion.for( 'upcast' ).elementToElement( { model: 'tableCell', view: 'td' } );
conversion.for( 'upcast' ).elementToElement( { model: 'tableCell', view: 'th' } );
conversion.for( 'upcast' ).add( ensureParagraphInTableCell( 'td' ) );
conversion.for( 'upcast' ).add( ensureParagraphInTableCell( 'th' ) );

conversion.for( 'editingDowncast' ).add( downcastInsertCell() );

Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-table/tests/converters/upcasttable.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ describe( 'upcastTable()', () => {
);

expectModel(
'<fooTable><fooRow><fooCell><paragraph></paragraph></fooCell></fooRow></fooTable>'
'<fooTable><fooRow><fooCell></fooCell></fooRow></fooTable>'
);
} );

Expand Down
41 changes: 41 additions & 0 deletions packages/ckeditor5-table/tests/tableclipboard-paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -3917,6 +3917,36 @@ describe( 'table clipboard', () => {
[ '02', '21', '22' ]
] ) );
} );

it( 'should not blow up when pasting unsupported element in table', async () => {
await createEditor( [ TableCellPropertiesEditing ] );

pasteHtml( editor,
'<table>' +
'<tbody>' +
'<tr>' +
'<td>' +
'<div>' +
'<table>' +
'<tbody>' +
'<tr>' +
'<td style="border: 2px solid rgb(242, 242, 242);">' +
'<p>Test</p>' +
'</td>' +
'</tr>' +
'</tbody>' +
'</table>' +
'</div>' +
'</td>' +
'</tr>' +
'</tbody>' +
'</table>'
);

assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
[ '<paragraph>Test</paragraph><paragraph></paragraph>' ]
] ) );
} );
} );

async function createEditor( extraPlugins = [] ) {
Expand All @@ -3930,6 +3960,17 @@ describe( 'table clipboard', () => {
tableSelection = editor.plugins.get( 'TableSelection' );
}

function pasteHtml( editor, html ) {
const data = {
dataTransfer: createDataTransfer(),
stopPropagation() {},
preventDefault() {}
};

data.dataTransfer.setData( 'text/html', html );
editor.editing.view.document.fire( 'paste', data );
}

function pasteTable( tableData, attributes = {} ) {
const data = {
dataTransfer: createDataTransfer(),
Expand Down