utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  6. import ViewDowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
  7. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  8. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  9. import {
  10. toImageWidget,
  11. isImageWidget,
  12. getSelectedImageWidget,
  13. isImage,
  14. isImageAllowed,
  15. insertImage,
  16. getViewImgFromWidget
  17. } from '../../src/image/utils';
  18. import { isWidget, getLabel } from '@ckeditor/ckeditor5-widget/src/utils';
  19. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  20. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  21. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  22. import Image from '../../src/image/imageediting';
  23. import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
  24. describe( 'image widget utils', () => {
  25. let element, image, writer, viewDocument;
  26. beforeEach( () => {
  27. viewDocument = new ViewDocument( new StylesProcessor() );
  28. writer = new ViewDowncastWriter( viewDocument );
  29. image = writer.createContainerElement( 'img' );
  30. element = writer.createContainerElement( 'figure' );
  31. writer.insert( writer.createPositionAt( element, 0 ), image );
  32. toImageWidget( element, writer, 'image widget' );
  33. } );
  34. describe( 'toImageWidget()', () => {
  35. it( 'should be widgetized', () => {
  36. expect( isWidget( element ) ).to.be.true;
  37. } );
  38. it( 'should set element\'s label', () => {
  39. expect( getLabel( element ) ).to.equal( 'image widget' );
  40. } );
  41. it( 'should set element\'s label combined with alt attribute', () => {
  42. writer.setAttribute( 'alt', 'foo bar baz', image );
  43. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  44. } );
  45. it( 'provided label creator should always return same label', () => {
  46. writer.setAttribute( 'alt', 'foo bar baz', image );
  47. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  48. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  49. } );
  50. } );
  51. describe( 'isImageWidget()', () => {
  52. it( 'should return true for elements marked with toImageWidget()', () => {
  53. expect( isImageWidget( element ) ).to.be.true;
  54. } );
  55. it( 'should return false for non-widgetized elements', () => {
  56. expect( isImageWidget( writer.createContainerElement( 'p' ) ) ).to.be.false;
  57. } );
  58. } );
  59. describe( 'getSelectedImageWidget()', () => {
  60. let frag;
  61. it( 'should return true when image widget is the only element in the selection', () => {
  62. // We need to create a container for the element to be able to create a Range on this element.
  63. frag = new ViewDocumentFragment( viewDocument, [ element ] );
  64. const selection = writer.createSelection( element, 'on' );
  65. expect( getSelectedImageWidget( selection ) ).to.equal( element );
  66. } );
  67. it( 'should return false when non-widgetized elements is the only element in the selection', () => {
  68. const notWidgetizedElement = writer.createContainerElement( 'p' );
  69. // We need to create a container for the element to be able to create a Range on this element.
  70. frag = new ViewDocumentFragment( viewDocument, [ notWidgetizedElement ] );
  71. const selection = writer.createSelection( notWidgetizedElement, 'on' );
  72. expect( getSelectedImageWidget( selection ) ).to.be.null;
  73. } );
  74. it( 'should return false when widget element is not the only element in the selection', () => {
  75. const notWidgetizedElement = writer.createContainerElement( 'p' );
  76. frag = new ViewDocumentFragment( viewDocument, [ element, notWidgetizedElement ] );
  77. const selection = writer.createSelection( writer.createRangeIn( frag ) );
  78. expect( getSelectedImageWidget( selection ) ).to.be.null;
  79. } );
  80. } );
  81. describe( 'isImage()', () => {
  82. it( 'should return true for image element', () => {
  83. const image = new ModelElement( 'image' );
  84. expect( isImage( image ) ).to.be.true;
  85. } );
  86. it( 'should return true false for different elements', () => {
  87. const image = new ModelElement( 'foo' );
  88. expect( isImage( image ) ).to.be.false;
  89. } );
  90. it( 'should return true false for null and undefined', () => {
  91. expect( isImage( null ) ).to.be.false;
  92. expect( isImage( undefined ) ).to.be.false;
  93. } );
  94. } );
  95. describe( 'isImageAllowed()', () => {
  96. let editor, model;
  97. beforeEach( () => {
  98. return VirtualTestEditor
  99. .create( {
  100. plugins: [ Image, Paragraph ]
  101. } )
  102. .then( newEditor => {
  103. editor = newEditor;
  104. model = editor.model;
  105. const schema = model.schema;
  106. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  107. } );
  108. } );
  109. it( 'should return true when the selection directly in the root', () => {
  110. model.enqueueChange( 'transparent', () => {
  111. setModelData( model, '[]' );
  112. expect( isImageAllowed( model ) ).to.be.true;
  113. } );
  114. } );
  115. it( 'should return true when the selection is in empty block', () => {
  116. setModelData( model, '<paragraph>[]</paragraph>' );
  117. expect( isImageAllowed( model ) ).to.be.true;
  118. } );
  119. it( 'should return true when the selection directly in a paragraph', () => {
  120. setModelData( model, '<paragraph>foo[]</paragraph>' );
  121. expect( isImageAllowed( model ) ).to.be.true;
  122. } );
  123. it( 'should return true when the selection directly in a block', () => {
  124. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  125. model.schema.extend( '$text', { allowIn: 'block' } );
  126. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  127. setModelData( model, '<block>foo[]</block>' );
  128. expect( isImageAllowed( model ) ).to.be.true;
  129. } );
  130. it( 'should return false when the selection is on other image', () => {
  131. setModelData( model, '[<image></image>]' );
  132. expect( isImageAllowed( model ) ).to.be.false;
  133. } );
  134. it( 'should return false when the selection is inside other image', () => {
  135. model.schema.register( 'caption', {
  136. allowIn: 'image',
  137. allowContentOf: '$block',
  138. isLimit: true
  139. } );
  140. editor.conversion.for( 'downcast' ).elementToElement( { model: 'caption', view: 'figcaption' } );
  141. setModelData( model, '<image><caption>[]</caption></image>' );
  142. expect( isImageAllowed( model ) ).to.be.false;
  143. } );
  144. it( 'should return false when the selection is on other object', () => {
  145. model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
  146. editor.conversion.for( 'downcast' ).elementToElement( { model: 'object', view: 'object' } );
  147. setModelData( model, '[<object></object>]' );
  148. expect( isImageAllowed( model ) ).to.be.false;
  149. } );
  150. it( 'should be true when the selection is inside isLimit element which allows image', () => {
  151. model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
  152. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  153. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true } );
  154. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  155. editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
  156. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tableRow' } );
  157. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'tableCell' } );
  158. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  159. expect( isImageAllowed( model ) ).to.be.true;
  160. } );
  161. it( 'should return false when schema disallows image', () => {
  162. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  163. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  164. // Block image in block.
  165. model.schema.addChildCheck( ( context, childDefinition ) => {
  166. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  167. return false;
  168. }
  169. } );
  170. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  171. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  172. expect( isImageAllowed( model ) ).to.be.false;
  173. } );
  174. } );
  175. describe( 'insertImage()', () => {
  176. let editor, model;
  177. beforeEach( () => {
  178. return VirtualTestEditor
  179. .create( {
  180. plugins: [ Image, Paragraph ]
  181. } )
  182. .then( newEditor => {
  183. editor = newEditor;
  184. model = editor.model;
  185. const schema = model.schema;
  186. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  187. } );
  188. } );
  189. it( 'should insert image at selection position as other widgets', () => {
  190. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  191. model.change( writer => {
  192. insertImage( writer, model );
  193. } );
  194. expect( getModelData( model ) ).to.equal( '[<image></image>]<paragraph>foo</paragraph>' );
  195. } );
  196. it( 'should insert image with given attributes', () => {
  197. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  198. model.change( writer => {
  199. insertImage( writer, model, { src: 'bar' } );
  200. } );
  201. expect( getModelData( model ) ).to.equal( '[<image src="bar"></image>]<paragraph>foo</paragraph>' );
  202. } );
  203. it( 'should not insert image nor crash when image could not be inserted', () => {
  204. model.schema.register( 'other', {
  205. allowIn: '$root',
  206. isLimit: true
  207. } );
  208. model.schema.extend( '$text', { allowIn: 'other' } );
  209. editor.conversion.for( 'downcast' ).elementToElement( { model: 'other', view: 'p' } );
  210. setModelData( model, '<other>[]</other>' );
  211. model.change( writer => {
  212. insertImage( writer, model );
  213. } );
  214. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  215. } );
  216. } );
  217. describe( 'getViewImgFromWidget()', () => {
  218. // figure
  219. // img
  220. it( 'returns the the img element from widget if the img is the first children', () => {
  221. expect( getViewImgFromWidget( element ) ).to.equal( image );
  222. } );
  223. // figure
  224. // div
  225. // img
  226. it( 'returns the the img element from widget if the img is not the first children', () => {
  227. writer.insert( writer.createPositionAt( element, 0 ), writer.createContainerElement( 'div' ) );
  228. expect( getViewImgFromWidget( element ) ).to.equal( image );
  229. } );
  230. // figure
  231. // div
  232. // img
  233. it( 'returns the the img element from widget if the img is a child of another element', () => {
  234. const divElement = writer.createContainerElement( 'div' );
  235. writer.insert( writer.createPositionAt( element, 0 ), divElement );
  236. writer.move( writer.createRangeOn( image ), writer.createPositionAt( divElement, 0 ) );
  237. expect( getViewImgFromWidget( element ) ).to.equal( image );
  238. } );
  239. // figure
  240. // div
  241. // "Bar"
  242. // img
  243. // "Foo"
  244. it( 'does not throw an error if text node found', () => {
  245. const divElement = writer.createContainerElement( 'div' );
  246. writer.insert( writer.createPositionAt( element, 0 ), divElement );
  247. writer.insert( writer.createPositionAt( element, 0 ), writer.createText( 'Foo' ) );
  248. writer.insert( writer.createPositionAt( divElement, 0 ), writer.createText( 'Bar' ) );
  249. writer.move( writer.createRangeOn( image ), writer.createPositionAt( divElement, 1 ) );
  250. expect( getViewImgFromWidget( element ) ).to.equal( image );
  251. } );
  252. } );
  253. } );