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 19
/
table-cell-content-post-fixer.js
112 lines (94 loc) · 3.11 KB
/
table-cell-content-post-fixer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
}