This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from ckeditor/t/125
Fix: A table cell should always have a `<paragraph>` in the model. Closes #125. BREAKING CHANGE: The `injectTablePostFixer()` function from `table/converters/table-post-fixer` is now `injectTableLayoutPostFixer()`and is moved to `table/converters/table-layout-post-fixer` module.
- Loading branch information
Showing
5 changed files
with
270 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module table/converters/table-cell-content-post-fixer | ||
*/ | ||
|
||
/** | ||
* Injects a table cell post-fixer into the model. | ||
* | ||
* The role of the table post-fixer is to ensure that the table cells have the correct content | ||
* after a {@link module:engine/model/model~Model#change `change()`} block was executed. | ||
* | ||
* A table cells must contains at least one block as a child. The empty table cell will have empty `<paragraph>` as a child. | ||
* | ||
* <table> | ||
* <tableRow> | ||
* <tableCell></tableCell> | ||
* </tableRow> | ||
* </table> | ||
* | ||
* Will be fixed to: | ||
* | ||
* <table> | ||
* <tableRow> | ||
* <tableCell><paragraph></paragraph></tableCell> | ||
* </tableRow> | ||
* </table> | ||
* | ||
* @param {module:engine/model/model~Model} model | ||
*/ | ||
export default function injectTableCellContentPostFixer( model ) { | ||
model.document.registerPostFixer( writer => tableCellContentsPostFixer( writer, model ) ); | ||
} | ||
|
||
// The table cell contents post-fixer. | ||
// | ||
// @param {module:engine/model/writer~Writer} writer | ||
// @param {module:engine/model/model~Model} model | ||
function tableCellContentsPostFixer( writer, model ) { | ||
const changes = model.document.differ.getChanges(); | ||
|
||
let wasFixed = false; | ||
|
||
for ( const entry of changes ) { | ||
// Enforce paragraph in tableCell even after other feature remove its contents. | ||
if ( entry.type == 'remove' && entry.position.parent.is( 'tableCell' ) ) { | ||
wasFixed = fixTableCellContent( entry.position.parent, writer ) || wasFixed; | ||
} | ||
|
||
// Analyze table cells on insertion. | ||
if ( entry.type == 'insert' ) { | ||
if ( entry.name == 'table' ) { | ||
wasFixed = fixTable( entry.position.nodeAfter, writer ) || wasFixed; | ||
} | ||
|
||
if ( entry.name == 'tableRow' ) { | ||
wasFixed = fixTableRow( entry.position.nodeAfter, writer ) || wasFixed; | ||
} | ||
|
||
if ( entry.name == 'tableCell' ) { | ||
wasFixed = fixTableCellContent( entry.position.nodeAfter, writer ) || wasFixed; | ||
} | ||
} | ||
} | ||
|
||
return wasFixed; | ||
} | ||
|
||
// Fixes all table cells in a table. | ||
// | ||
// @param {module:engine/model/element~Element} table | ||
// @param {module:engine/model/writer~Writer} writer | ||
function fixTable( table, writer ) { | ||
let wasFixed = false; | ||
|
||
for ( const row of table.getChildren() ) { | ||
wasFixed = fixTableRow( row, writer ) || wasFixed; | ||
} | ||
|
||
return wasFixed; | ||
} | ||
|
||
// Fixes all table cells in a table row. | ||
// | ||
// @param {module:engine/model/element~Element} tableRow | ||
// @param {module:engine/model/writer~Writer} writer | ||
function fixTableRow( tableRow, writer ) { | ||
let wasFixed = false; | ||
|
||
for ( const tableCell of tableRow.getChildren() ) { | ||
wasFixed = fixTableCellContent( tableCell, writer ) || wasFixed; | ||
} | ||
|
||
return wasFixed; | ||
} | ||
|
||
// Fixes all table cell content by adding paragraph to a table cell without any child. | ||
// | ||
// @param {module:engine/model/element~Element} table | ||
// @param {module:engine/model/writer~Writer} writer | ||
function fixTableCellContent( tableCell, writer ) { | ||
if ( tableCell.childCount == 0 ) { | ||
writer.insertElement( 'paragraph', tableCell ); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.