table-cell-content-post-fixer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Copyright (c) 2003-2019, 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-content-post-fixer
  7. */
  8. /**
  9. * Injects a table cell post-fixer into the model.
  10. *
  11. * The role of the table post-fixer is to ensure that the table cells have the correct content
  12. * after a {@link module:engine/model/model~Model#change `change()`} block was executed.
  13. *
  14. * A table cells must contains at least one block as a child. The empty table cell will have empty `<paragraph>` as a child.
  15. *
  16. * <table>
  17. * <tableRow>
  18. * <tableCell></tableCell>
  19. * </tableRow>
  20. * </table>
  21. *
  22. * Will be fixed to:
  23. *
  24. * <table>
  25. * <tableRow>
  26. * <tableCell><paragraph></paragraph></tableCell>
  27. * </tableRow>
  28. * </table>
  29. *
  30. * @param {module:engine/model/model~Model} model
  31. */
  32. export default function injectTableCellContentPostFixer( model ) {
  33. model.document.registerPostFixer( writer => tableCellContentsPostFixer( writer, model ) );
  34. }
  35. // The table cell contents post-fixer.
  36. //
  37. // @param {module:engine/model/writer~Writer} writer
  38. // @param {module:engine/model/model~Model} model
  39. function tableCellContentsPostFixer( writer, model ) {
  40. const changes = model.document.differ.getChanges();
  41. let wasFixed = false;
  42. for ( const entry of changes ) {
  43. // Enforce paragraph in tableCell even after other feature remove its contents.
  44. if ( entry.type == 'remove' && entry.position.parent.is( 'tableCell' ) ) {
  45. wasFixed = fixTableCellContent( entry.position.parent, writer ) || wasFixed;
  46. }
  47. // Analyze table cells on insertion.
  48. if ( entry.type == 'insert' ) {
  49. if ( entry.name == 'table' ) {
  50. wasFixed = fixTable( entry.position.nodeAfter, writer ) || wasFixed;
  51. }
  52. if ( entry.name == 'tableRow' ) {
  53. wasFixed = fixTableRow( entry.position.nodeAfter, writer ) || wasFixed;
  54. }
  55. if ( entry.name == 'tableCell' ) {
  56. wasFixed = fixTableCellContent( entry.position.nodeAfter, writer ) || wasFixed;
  57. }
  58. }
  59. }
  60. return wasFixed;
  61. }
  62. // Fixes all table cells in a table.
  63. //
  64. // @param {module:engine/model/element~Element} table
  65. // @param {module:engine/model/writer~Writer} writer
  66. function fixTable( table, writer ) {
  67. let wasFixed = false;
  68. for ( const row of table.getChildren() ) {
  69. wasFixed = fixTableRow( row, writer ) || wasFixed;
  70. }
  71. return wasFixed;
  72. }
  73. // Fixes all table cells in a table row.
  74. //
  75. // @param {module:engine/model/element~Element} tableRow
  76. // @param {module:engine/model/writer~Writer} writer
  77. function fixTableRow( tableRow, writer ) {
  78. let wasFixed = false;
  79. for ( const tableCell of tableRow.getChildren() ) {
  80. wasFixed = fixTableCellContent( tableCell, writer ) || wasFixed;
  81. }
  82. return wasFixed;
  83. }
  84. // Fixes all table cell content by:
  85. // - adding paragraph to a table cell without any child.
  86. // - wrapping direct $text in <paragraph>.
  87. //
  88. // @param {module:engine/model/element~Element} table
  89. // @param {module:engine/model/writer~Writer} writer
  90. // @returns {Boolean}
  91. function fixTableCellContent( tableCell, writer ) {
  92. // Insert paragraph to an empty table cell.
  93. if ( tableCell.childCount == 0 ) {
  94. writer.insertElement( 'paragraph', tableCell );
  95. return true;
  96. }
  97. // Check table cell children for directly placed $text nodes.
  98. // Temporary solution. See https://github.com/ckeditor/ckeditor5/issues/1464.
  99. const textNodes = Array.from( tableCell.getChildren() ).filter( child => child.is( 'text' ) );
  100. for ( const child of textNodes ) {
  101. writer.wrap( writer.createRangeOn( child ), 'paragraph' );
  102. }
  103. // Return true when there were text nodes to fix.
  104. return !!textNodes.length;
  105. }