This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
table-cell-refresh-post-fixer.js
359 lines (260 loc) · 12.1 KB
/
table-cell-refresh-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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals document */
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import { defaultConversion, defaultSchema, formatTable, formattedViewTable, viewTable } from '../_utils/utils';
import injectTableCellRefreshPostFixer from '../../src/converters/table-cell-refresh-post-fixer';
import env from '@ckeditor/ckeditor5-utils/src/env';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import Delete from '@ckeditor/ckeditor5-typing/src/delete';
describe( 'Table cell refresh post-fixer', () => {
let editor, model, doc, root, view, refreshItemSpy, element;
testUtils.createSinonSandbox();
beforeEach( () => {
element = document.createElement( 'div' );
document.body.appendChild( element );
// Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
return ClassicTestEditor.create( element, { extraPlugins: [ Delete ] } )
.then( newEditor => {
editor = newEditor;
model = editor.model;
doc = model.document;
root = doc.getRoot( 'main' );
view = editor.editing.view;
defaultSchema( model.schema );
defaultConversion( editor.conversion, true );
editor.model.schema.register( 'block', {
inheritAllFrom: '$block'
} );
editor.conversion.elementToElement( { model: 'block', view: 'div' } );
model.schema.extend( '$block', { allowAttributes: [ 'foo', 'bar' ] } );
editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
editor.conversion.attributeToAttribute( { model: 'bar', view: 'bar' } );
injectTableCellRefreshPostFixer( model );
refreshItemSpy = sinon.spy( model.document.differ, 'refreshItem' );
} );
} );
afterEach( () => {
element.remove();
return editor.destroy();
} );
it( 'should rename <span> to <p> when adding more <paragraph> elements to the same table cell', () => {
editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
const paragraph = writer.createElement( 'paragraph' );
writer.insert( paragraph, nodeByPath, 'after' );
writer.setSelection( nodeByPath.nextSibling, 0 );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p>00</p><p></p>' ]
], { asWidget: true } ) );
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should rename <span> to <p> on adding other block element to the same table cell', () => {
editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
const paragraph = writer.createElement( 'block' );
writer.insert( paragraph, nodeByPath, 'after' );
writer.setSelection( nodeByPath.nextSibling, 0 );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p>00</p><div></div>' ]
], { asWidget: true } ) );
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should properly rename the same element on consecutive changes', () => {
editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
writer.insertElement( 'paragraph', nodeByPath, 'after' );
writer.setSelection( nodeByPath.nextSibling, 0 );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p>00</p><p></p>' ]
], { asWidget: true } ) );
sinon.assert.calledOnce( refreshItemSpy );
model.change( writer => {
writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '00' ]
], { asWidget: true } ) );
sinon.assert.calledTwice( refreshItemSpy );
} );
it( 'should rename <span> to <p> when setting attribute on <paragraph>', () => {
editor.setData( '<table><tr><td><p>00</p></td></tr></table>' );
const table = root.getChild( 0 );
model.change( writer => {
writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p foo="bar">00</p>' ]
], { asWidget: true } ) );
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should rename <p> to <span> when removing all but one paragraph inside table cell', () => {
editor.setData( viewTable( [ [ '<p>00</p><p>foo</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '00' ]
], { asWidget: true } ) );
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should rename <p> to <span> when removing attribute from <paragraph>', () => {
editor.setData( '<table><tr><td><p foo="bar">00</p></td></tr></table>' );
const table = root.getChild( 0 );
model.change( writer => {
writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<span>00</span>' ]
], { asWidget: true } ) );
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should keep <p> in the view when <paragraph> attribute value is changed', () => {
editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p foo="baz">00</p>' ]
], { asWidget: true } ) );
// False positive: should not be called.
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should keep <p> in the view when adding another attribute to a <paragraph> with other attributes', () => {
editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.setAttribute( 'bar', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p bar="bar" foo="bar">00</p>' ]
], { asWidget: true } ) );
// False positive
sinon.assert.notCalled( refreshItemSpy );
} );
it( 'should keep <p> in the view when adding another attribute to a <paragraph> and removing attribute that is already set', () => {
editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.setAttribute( 'bar', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p bar="bar">00</p>' ]
], { asWidget: true } ) );
// False positive: should not be called.
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
editor.setData( viewTable( [ [ '<p foo="bar">00</p><p>00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<p foo="baz">00</p><p>00</p>' ]
], { asWidget: true } ) );
sinon.assert.notCalled( refreshItemSpy );
} );
it( 'should do nothing on rename <paragraph> to other block', () => {
editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.rename( table.getNodeByPath( [ 0, 0, 0 ] ), 'block' );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<div>00</div>' ]
], { asWidget: true } ) );
sinon.assert.notCalled( refreshItemSpy );
} );
it( 'should do nothing when setting attribute on block item other then <paragraph>', () => {
editor.setData( viewTable( [ [ '<div>foo</div>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<div foo="bar">foo</div>' ]
], { asWidget: true } ) );
sinon.assert.notCalled( refreshItemSpy );
} );
it( 'should rename <p> in to <span> when removing <paragraph> (table cell with 2 paragraphs)', () => {
editor.setData( viewTable( [ [ '<p>00</p><p>00</p>' ] ] ) );
const table = root.getChild( 0 );
model.change( writer => {
writer.remove( writer.createRangeOn( table.getNodeByPath( [ 0, 0, 1 ] ) ) );
} );
expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
[ '<span>00</span>' ]
], { asWidget: true } ) );
sinon.assert.calledOnce( refreshItemSpy );
} );
it( 'should update view selection after deleting content', () => {
editor.setData( viewTable( [ [ '<p>foo</p><p>bar</p>' ] ] ) );
const tableCell = root.getNodeByPath( [ 0, 0, 0 ] );
// Replace table cell contents with paragraph - as model.deleteContent() does.
model.change( writer => {
writer.remove( writer.createRangeIn( tableCell ) );
const paragraph = writer.createElement( 'paragraph' );
writer.insert( paragraph, writer.createPositionAt( tableCell, 0 ) );
// Set selection to newly created paragraph.
writer.setSelection( paragraph, 0 );
} );
const viewRange = view.document.selection.getFirstRange();
// Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
expect( () => view.domConverter.viewRangeToDom( viewRange ) ).to.not.throw();
} );
it( 'should not update view selection after other feature set selection', () => {
editor.model.schema.register( 'widget', {
isObject: true,
isBlock: true,
allowWhere: '$block'
} );
editor.conversion.elementToElement( {
model: 'widget',
view: 'widget'
} );
editor.setData( viewTable( [ [ '<p>foo[]</p>' ] ] ) );
const spy = sinon.spy();
view.document.selection.on( 'change', spy );
// Insert a widget in table cell and select it.
model.change( writer => {
const widgetElement = writer.createElement( 'widget' );
const tableCell = root.getNodeByPath( [ 0, 0, 0, 0 ] );
writer.insert( widgetElement, writer.createPositionAfter( tableCell ) );
// Update the selection so it will be set on the widget and not in renamed paragraph.
writer.setSelection( widgetElement, 'on' );
} );
// View selection should be updated only twice - will be set to null and then to widget.
// If called thrice the selection post fixer for table cell was also called.
sinon.assert.calledTwice( spy );
} );
// https://github.com/ckeditor/ckeditor5-table/issues/191.
it( 'should not fire (and crash) for removed view elements', () => {
editor.setData( viewTable( [ [ '<p>foo</p>' ] ] ) );
const p = root.getNodeByPath( [ 0, 0, 0, 0 ] );
// Replace table cell contents with paragraph - as model.deleteContent() does.
model.change( writer => {
writer.setSelection( writer.createRangeIn( root ) );
editor.execute( 'delete' ); // For some reason it didn't crash with `writer.remove()`.
writer.setAttribute( 'foo', 'bar', p );
} );
// Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
expect( editor.getData() ).to.equal( '' );
} );
} );