3
3
* For licensing, see LICENSE.md.
4
4
*/
5
5
6
- import { viewToModelImage , createImageAttributeConverter } from '../../src/image/converters' ;
6
+ import { viewFigureToModel , createImageAttributeConverter } from '../../src/image/converters' ;
7
7
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor' ;
8
8
import { createImageViewElement } from '../../src/image/imageengine' ;
9
9
import { toImageWidget } from '../../src/image/utils' ;
10
10
import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter' ;
11
+ import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter' ;
11
12
import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element' ;
12
13
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view' ;
13
14
import { setData as setModelData , getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model' ;
@@ -28,10 +29,6 @@ describe( 'Image converters', () => {
28
29
schema . allow ( { name : 'image' , attributes : [ 'alt' , 'src' ] , inside : '$root' } ) ;
29
30
schema . objects . add ( 'image' ) ;
30
31
31
- buildModelConverter ( ) . for ( )
32
- . fromElement ( 'image' )
33
- . toElement ( ( ) => toImageWidget ( createImageViewElement ( ) ) ) ;
34
-
35
32
buildModelConverter ( ) . for ( editor . editing . modelToView )
36
33
. fromElement ( 'image' )
37
34
. toElement ( ( ) => toImageWidget ( createImageViewElement ( ) ) ) ;
@@ -41,46 +38,87 @@ describe( 'Image converters', () => {
41
38
} ) ;
42
39
} ) ;
43
40
44
- describe ( 'viewToModelImage' , ( ) => {
45
- let dispatcher , schema ;
41
+ describe ( 'viewFigureToModel' , ( ) => {
42
+ function expectModel ( model ) {
43
+ expect ( getModelData ( document , { withoutSelection : true } ) ) . to . equal ( model ) ;
44
+ }
45
+
46
+ let dispatcher , schema , imgConverterCalled ;
46
47
47
48
beforeEach ( ( ) => {
49
+ imgConverterCalled = false ;
50
+
48
51
schema = document . schema ;
52
+ schema . allow ( { name : '$text' , inside : 'image' } ) ;
53
+
49
54
dispatcher = editor . data . viewToModel ;
50
- dispatcher . on ( 'element:figure' , viewToModelImage ( ) ) ;
55
+ dispatcher . on ( 'element:figure' , viewFigureToModel ( ) ) ;
56
+ dispatcher . on ( 'element:img' , ( evt , data , consumable ) => {
57
+ if ( consumable . consume ( data . input , { name : true , attribute : 'src' } ) ) {
58
+ data . output = new ModelElement ( 'image' , { src : data . input . getAttribute ( 'src' ) } ) ;
59
+
60
+ imgConverterCalled = true ;
61
+ }
62
+ } ) ;
51
63
} ) ;
52
64
53
- it ( 'should convert view figure element' , ( ) => {
54
- editor . setData ( '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>' ) ;
55
- expect ( getModelData ( document , { withoutSelection : true } ) ) . to . equal ( '<image alt="bar baz" src="foo.png"></image>' ) ;
65
+ it ( 'should find img element among children and convert it using already defined converters' , ( ) => {
66
+ editor . setData ( '<figure class="image"><img src="foo.png" /></figure>' ) ;
67
+
68
+ expectModel ( '<image src="foo.png"></image>' ) ;
69
+ expect ( imgConverterCalled ) . to . be . true ;
56
70
} ) ;
57
71
58
- it ( 'should convert without alt' , ( ) => {
59
- editor . setData ( '<figure class="image"><img src="foo.png"></img></figure>' ) ;
60
- expect ( getModelData ( document , { withoutSelection : true } ) ) . to . equal ( '<image src="foo.png"></image>' ) ;
72
+ it ( 'should convert non-img children in image context and append them to model image element' , ( ) => {
73
+ buildViewConverter ( ) . for ( editor . data . viewToModel ) . fromElement ( 'foo' ) . toElement ( 'foo' ) ;
74
+ buildViewConverter ( ) . for ( editor . data . viewToModel ) . fromElement ( 'bar' ) . toElement ( 'bar' ) ;
75
+
76
+ schema . registerItem ( 'foo' ) ;
77
+ schema . registerItem ( 'bar' ) ;
78
+
79
+ schema . allow ( { name : 'foo' , inside : 'image' } ) ;
80
+
81
+ editor . setData ( '<figure class="image">x<img src="foo.png" />y<foo></foo><bar></bar></figure>' ) ;
82
+
83
+ // Element bar not converted because schema does not allow it.
84
+ expectModel ( '<image src="foo.png">xy<foo></foo></image>' ) ;
61
85
} ) ;
62
86
63
- it ( 'should not convert if figure element is already consumed ' , ( ) => {
87
+ it ( 'should be possible to overwrite ' , ( ) => {
64
88
dispatcher . on ( 'element:figure' , ( evt , data , consumable ) => {
65
- consumable . consume ( data . input , { name : true , class : 'image' } ) ;
89
+ consumable . consume ( data . input , { name : true } ) ;
90
+ consumable . consume ( data . input . getChild ( 0 ) , { name : true } ) ;
66
91
67
- data . output = new ModelElement ( 'not-image' ) ;
92
+ data . output = new ModelElement ( 'myImage' , { data : { src : data . input . getChild ( 0 ) . getAttribute ( 'src' ) } } ) ;
68
93
} , { priority : 'high' } ) ;
69
94
70
- editor . setData ( '<figure class="image"><img src="foo.png" alt="bar baz"></img></figure>' ) ;
71
- expect ( getModelData ( document , { withoutSelection : true } ) ) . to . equal ( '<not-image></not-image>' ) ;
95
+ editor . setData ( '<figure class="image"><img src="foo.png" />xyz</figure>' ) ;
96
+
97
+ expectModel ( '<myImage data="{"src":"foo.png"}"></myImage>' ) ;
72
98
} ) ;
73
99
74
- it ( 'should not convert image if schema disallows it' , ( ) => {
75
- schema . disallow ( { name : 'image' , attributes : [ 'alt' , 'src' ] , inside : '$root' } ) ;
100
+ // Test exactly what figure converter does, which is putting it's children element to image element.
101
+ // If this has not been done, it means that figure converter was not used.
102
+ it ( 'should not convert if figure do not have class="image" attribute' , ( ) => {
103
+ editor . setData ( '<figure><img src="foo.png" />xyz</figure>' ) ;
76
104
77
- editor . setData ( '<figure class=" image"><img src="foo.png"></img></figure>' ) ;
78
- expect ( getModelData ( document , { withoutSelection : true } ) ) . to . equal ( ' ' ) ;
105
+ // Default image converter will be fired.
106
+ expectModel ( '<image src="foo.png"></image> ' ) ;
79
107
} ) ;
80
108
81
- it ( 'should not convert image if there is no img element' , ( ) => {
82
- editor . setData ( '<figure class="image"></figure>' ) ;
83
- expect ( getModelData ( document , { withoutSelection : true } ) ) . to . equal ( '' ) ;
109
+ it ( 'should not convert if there is no img element among children' , ( ) => {
110
+ editor . setData ( '<figure class="image">xyz</figure>' ) ;
111
+
112
+ // Figure converter outputs nothing and text is disallowed in root.
113
+ expectModel ( '' ) ;
114
+ } ) ;
115
+
116
+ it ( 'should not convert if img element was not converted' , ( ) => {
117
+ // Image element missing src attribute.
118
+ editor . setData ( '<figure class="image"><img alt="abc" />xyz</figure>' ) ;
119
+
120
+ // Figure converter outputs nothing and text is disallowed in root.
121
+ expectModel ( '' ) ;
84
122
} ) ;
85
123
} ) ;
86
124
0 commit comments