@@ -44,33 +44,19 @@ describe( 'ImageResize', () => {
44
44
absoluteContainer . remove ( ) ;
45
45
} ) ;
46
46
47
- beforeEach ( ( ) => {
48
- editorElement = document . createElement ( 'div' ) ;
49
-
50
- absoluteContainer . appendChild ( editorElement ) ;
51
-
52
- return ClassicEditor
53
- . create ( editorElement , {
54
- plugins : [ Image , ImageStyle , Paragraph , Undo , Table , ImageResize ] ,
55
- image : {
56
- resizeUnit : 'px'
57
- }
58
- } )
59
- . then ( newEditor => {
60
- editor = newEditor ;
61
- view = editor . editing . view ;
62
- viewDocument = view . document ;
63
- widget = viewDocument . getRoot ( ) . getChild ( 1 ) ;
64
- } ) ;
65
- } ) ;
66
-
67
47
afterEach ( ( ) => {
68
- editorElement . remove ( ) ;
48
+ if ( editorElement ) {
49
+ editorElement . remove ( ) ;
50
+ }
69
51
70
- return editor . destroy ( ) ;
52
+ if ( editor ) {
53
+ return editor . destroy ( ) ;
54
+ }
71
55
} ) ;
72
56
73
57
describe ( 'conversion' , ( ) => {
58
+ beforeEach ( ( ) => createEditor ( ) ) ;
59
+
74
60
it ( 'upcasts 100px width correctly' , ( ) => {
75
61
editor . setData ( `<figure class="image" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE } "></figure>` ) ;
76
62
@@ -99,6 +85,8 @@ describe( 'ImageResize', () => {
99
85
} ) ;
100
86
101
87
describe ( 'schema' , ( ) => {
88
+ beforeEach ( ( ) => createEditor ( ) ) ;
89
+
102
90
it ( 'allows the width attribute' , ( ) => {
103
91
expect ( editor . model . schema . checkAttribute ( 'image' , 'width' ) ) . to . be . true ;
104
92
} ) ;
@@ -109,6 +97,8 @@ describe( 'ImageResize', () => {
109
97
} ) ;
110
98
111
99
describe ( 'command' , ( ) => {
100
+ beforeEach ( ( ) => createEditor ( ) ) ;
101
+
112
102
it ( 'defines the imageResize command' , ( ) => {
113
103
expect ( editor . commands . get ( 'imageResize' ) ) . to . be . instanceOf ( ImageResizeCommand ) ;
114
104
} ) ;
@@ -180,6 +170,8 @@ describe( 'ImageResize', () => {
180
170
} ) ;
181
171
182
172
describe ( 'visual resizers' , ( ) => {
173
+ beforeEach ( ( ) => createEditor ( ) ) ;
174
+
183
175
beforeEach ( ( ) => {
184
176
setData ( editor . model , `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE } "></image>]` ) ;
185
177
@@ -202,7 +194,6 @@ describe( 'ImageResize', () => {
202
194
} ) ;
203
195
204
196
it ( 'is shown when image is focused' , ( ) => {
205
- const widget = viewDocument . getRoot ( ) . getChild ( 1 ) ;
206
197
const allResizers = editor . ui . getEditableElement ( ) . querySelectorAll ( '.ck-widget__resizer__handle' ) ;
207
198
const domEventDataMock = {
208
199
target : widget ,
@@ -221,6 +212,8 @@ describe( 'ImageResize', () => {
221
212
} ) ;
222
213
223
214
describe ( 'standard image resizing' , ( ) => {
215
+ beforeEach ( ( ) => createEditor ( ) ) ;
216
+
224
217
beforeEach ( ( ) => {
225
218
setData ( editor . model , `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE } "></image>]` ) ;
226
219
@@ -321,6 +314,8 @@ describe( 'ImageResize', () => {
321
314
} ) ;
322
315
323
316
describe ( 'side image resizing' , ( ) => {
317
+ beforeEach ( ( ) => createEditor ( ) ) ;
318
+
324
319
beforeEach ( ( ) => {
325
320
setData ( editor . model , `<paragraph>foo</paragraph>[<image imageStyle="side" src="${ IMAGE_SRC_FIXTURE } "></image>]` ) ;
326
321
@@ -435,7 +430,79 @@ describe( 'ImageResize', () => {
435
430
}
436
431
} ) ;
437
432
433
+ describe ( 'percent resizing' , ( ) => {
434
+ beforeEach ( ( ) => createEditor ( {
435
+ plugins : [ Image , ImageStyle , Paragraph , Undo , Table , ImageResize ]
436
+ } ) ) ;
437
+
438
+ describe ( 'standard image' , ( ) => {
439
+ beforeEach ( ( ) => {
440
+ setData ( editor . model , `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE } "></image>]` ) ;
441
+
442
+ widget = viewDocument . getRoot ( ) . getChild ( 1 ) ;
443
+ } ) ;
444
+
445
+ it ( 'shrinks correctly with left-bottom handler' , generateResizeTest ( {
446
+ expectedWidth : 16 ,
447
+ modelRegExp : / < p a r a g r a p h > f o o < \/ p a r a g r a p h > < i m a g e s r c = " .+ ?" w i d t h = " ( [ \d ] { 2 } (?: \. [ \d ] { 1 , 2 } ) ) % " > < \/ i m a g e > / ,
448
+ pointerOffset : {
449
+ x : 10 ,
450
+ y : - 10
451
+ } ,
452
+ resizerPosition : 'bottom-left'
453
+ } ) ) ;
454
+
455
+ it ( 'enlarges correctly with right-bottom handler' , generateResizeTest ( {
456
+ expectedWidth : 22 ,
457
+ modelRegExp : / < p a r a g r a p h > f o o < \/ p a r a g r a p h > < i m a g e s r c = " .+ ?" w i d t h = " ( [ \d ] { 2 } (?: \. [ \d ] { 1 , 2 } ) ) % " > < \/ i m a g e > / ,
458
+ pointerOffset : {
459
+ x : 0 ,
460
+ y : 5
461
+ } ,
462
+ resizerPosition : 'bottom-right'
463
+ } ) ) ;
464
+
465
+ it ( 'enlarges correctly an image with unsupported width unit' , async ( ) => {
466
+ setData ( editor . model , `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE } " width="50pt"></image>]` ) ;
467
+
468
+ widget = viewDocument . getRoot ( ) . getChild ( 1 ) ;
469
+
470
+ await generateResizeTest ( {
471
+ expectedWidth : 15 ,
472
+ modelRegExp : / < p a r a g r a p h > f o o < \/ p a r a g r a p h > < i m a g e s r c = " .+ ?" w i d t h = " ( [ \d ] { 2 } (?: \. [ \d ] { 1 , 2 } ) ) % " > < \/ i m a g e > / ,
473
+ pointerOffset : {
474
+ x : 0 ,
475
+ y : 5
476
+ } ,
477
+ resizerPosition : 'bottom-right'
478
+ } ) ( ) ;
479
+ } ) ;
480
+ } ) ;
481
+
482
+ describe ( 'side image' , ( ) => {
483
+ beforeEach ( ( ) => {
484
+ setData ( editor . model , `<paragraph>foo</paragraph>[<image imageStyle="side" src="${ IMAGE_SRC_FIXTURE } "></image>]` ) ;
485
+
486
+ view = editor . editing . view ;
487
+ viewDocument = view . document ;
488
+ widget = viewDocument . getRoot ( ) . getChild ( 1 ) ;
489
+ } ) ;
490
+
491
+ it ( 'shrinks correctly with left-bottom handler' , generateResizeTest ( {
492
+ expectedWidth : 18 ,
493
+ modelRegExp : / < p a r a g r a p h > f o o < \/ p a r a g r a p h > < i m a g e i m a g e S t y l e = " s i d e " s r c = " .+ ?" w i d t h = " ( [ \d ] { 2 } (?: \. [ \d ] { 1 , 2 } ) ) % " > < \/ i m a g e > / ,
494
+ pointerOffset : {
495
+ x : 10 ,
496
+ y : - 10
497
+ } ,
498
+ resizerPosition : 'bottom-left'
499
+ } ) ) ;
500
+ } ) ;
501
+ } ) ;
502
+
438
503
describe ( 'undo integration' , ( ) => {
504
+ beforeEach ( ( ) => createEditor ( ) ) ;
505
+
439
506
beforeEach ( ( ) => {
440
507
setData ( editor . model , `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE } "></image>]` ) ;
441
508
@@ -465,6 +532,8 @@ describe( 'ImageResize', () => {
465
532
} ) ;
466
533
467
534
describe ( 'table integration' , ( ) => {
535
+ beforeEach ( ( ) => createEditor ( ) ) ;
536
+
468
537
beforeEach ( ( ) => {
469
538
setData ( editor . model ,
470
539
'<table>' +
@@ -488,6 +557,8 @@ describe( 'ImageResize', () => {
488
557
} ) ;
489
558
490
559
describe ( 'srcset integration' , ( ) => {
560
+ beforeEach ( ( ) => createEditor ( ) ) ;
561
+
491
562
// The image is 96x96 pixels.
492
563
const imageBaseUrl = '/assets/sample.png' ;
493
564
const getModel = ( ) => editor . model . document . getRoot ( ) . getChild ( 0 ) ;
@@ -561,6 +632,8 @@ describe( 'ImageResize', () => {
561
632
562
633
// TODO move to Resizer tests.
563
634
describe ( 'Resizer' , ( ) => {
635
+ beforeEach ( ( ) => createEditor ( ) ) ;
636
+
564
637
it ( 'uses rounded (int) values' , async ( ) => {
565
638
setData ( editor . model , `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE } "></image>]` ) ;
566
639
@@ -688,4 +761,24 @@ describe( 'ImageResize', () => {
688
761
editor . editing . view . focus ( ) ;
689
762
editor . ui . focusTracker . isFocused = true ;
690
763
}
764
+
765
+ function createEditor ( config ) {
766
+ editorElement = document . createElement ( 'div' ) ;
767
+
768
+ absoluteContainer . appendChild ( editorElement ) ;
769
+
770
+ return ClassicEditor
771
+ . create ( editorElement , config || {
772
+ plugins : [ Image , ImageStyle , Paragraph , Undo , Table , ImageResize ] ,
773
+ image : {
774
+ resizeUnit : 'px'
775
+ }
776
+ } )
777
+ . then ( newEditor => {
778
+ editor = newEditor ;
779
+ view = editor . editing . view ;
780
+ viewDocument = view . document ;
781
+ widget = viewDocument . getRoot ( ) . getChild ( 1 ) ;
782
+ } ) ;
783
+ }
691
784
} ) ;
0 commit comments