utils.js 9.8 KB

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