table-cell-paragraph-post-fixer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/converters/table-cell-paragraph-post-fixer
  7. */
  8. /**
  9. * Injects a table cell post-fixer into the model which inserts a `paragraph` element into empty table cells.
  10. *
  11. * A table cell must contain at least one block element as a child. An empty table cell will have an empty `paragraph` as a child.
  12. *
  13. * <table>
  14. * <tableRow>
  15. * <tableCell></tableCell>
  16. * </tableRow>
  17. * </table>
  18. *
  19. * Will be fixed to:
  20. *
  21. * <table>
  22. * <tableRow>
  23. * <tableCell><paragraph></paragraph></tableCell>
  24. * </tableRow>
  25. * </table>
  26. *
  27. * @param {module:engine/model/model~Model} model
  28. */
  29. export default function injectTableCellParagraphPostFixer( model ) {
  30. model.document.registerPostFixer( writer => tableCellContentsPostFixer( writer, model ) );
  31. }
  32. // The table cell contents post-fixer.
  33. //
  34. // @param {module:engine/model/writer~Writer} writer
  35. // @param {module:engine/model/model~Model} model
  36. function tableCellContentsPostFixer( writer, model ) {
  37. const changes = model.document.differ.getChanges();
  38. let wasFixed = false;
  39. for ( const entry of changes ) {
  40. if ( entry.type == 'insert' && entry.name == 'table' ) {
  41. wasFixed = fixTable( entry.position.nodeAfter, writer ) || wasFixed;
  42. }
  43. if ( entry.type == 'insert' && entry.name == 'tableRow' ) {
  44. wasFixed = fixTableRow( entry.position.nodeAfter, writer ) || wasFixed;
  45. }
  46. if ( entry.type == 'insert' && entry.name == 'tableCell' ) {
  47. wasFixed = fixTableCellContent( entry.position.nodeAfter, writer ) || wasFixed;
  48. }
  49. if ( checkTableCellChange( entry ) ) {
  50. wasFixed = fixTableCellContent( entry.position.parent, writer ) || wasFixed;
  51. }
  52. }
  53. return wasFixed;
  54. }
  55. // Fixes all table cells in a table.
  56. //
  57. // @param {module:engine/model/element~Element} table
  58. // @param {module:engine/model/writer~Writer} writer
  59. function fixTable( table, writer ) {
  60. let wasFixed = false;
  61. for ( const row of table.getChildren() ) {
  62. wasFixed = fixTableRow( row, writer ) || wasFixed;
  63. }
  64. return wasFixed;
  65. }
  66. // Fixes all table cells in a table row.
  67. //
  68. // @param {module:engine/model/element~Element} tableRow
  69. // @param {module:engine/model/writer~Writer} writer
  70. function fixTableRow( tableRow, writer ) {
  71. let wasFixed = false;
  72. for ( const tableCell of tableRow.getChildren() ) {
  73. wasFixed = fixTableCellContent( tableCell, writer ) || wasFixed;
  74. }
  75. return wasFixed;
  76. }
  77. // Fixes all table cell content by:
  78. // - Adding a paragraph to a table cell without any child.
  79. // - Wrapping direct $text in a `<paragraph>`.
  80. //
  81. // @param {module:engine/model/element~Element} table
  82. // @param {module:engine/model/writer~Writer} writer
  83. // @returns {Boolean}
  84. function fixTableCellContent( tableCell, writer ) {
  85. // Insert paragraph to an empty table cell.
  86. if ( tableCell.childCount == 0 ) {
  87. writer.insertElement( 'paragraph', tableCell );
  88. return true;
  89. }
  90. // Check table cell children for directly placed text nodes.
  91. // Temporary solution. See https://github.com/ckeditor/ckeditor5/issues/1464.
  92. const textNodes = Array.from( tableCell.getChildren() ).filter( child => child.is( 'text' ) );
  93. for ( const child of textNodes ) {
  94. writer.wrap( writer.createRangeOn( child ), 'paragraph' );
  95. }
  96. // Return true when there were text nodes to fix.
  97. return !!textNodes.length;
  98. }
  99. // Checks if a differ change should fix the table cell. This happens on:
  100. // - Removing content from the table cell (i.e. `tableCell` can be left empty).
  101. // - Adding a text node directly into a table cell.
  102. //
  103. // @param {Object} differ change entry
  104. // @returns {Boolean}
  105. function checkTableCellChange( entry ) {
  106. if ( !entry.position || !entry.position.parent.is( 'tableCell' ) ) {
  107. return false;
  108. }
  109. return entry.type == 'insert' && entry.name == '$text' || entry.type == 'remove';
  110. }