imageresize.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, window */
  6. // ClassicTestEditor can't be used, as it doesn't handle the focus, which is needed to test resizer visual cues.
  7. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Image from '../src/image';
  10. import ImageResize from '../src/imageresize';
  11. import ImageResizeCommand from '../src/imageresize/imageresizecommand';
  12. import ImageStyle from '../src/imagestyle';
  13. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  14. import Table from '@ckeditor/ckeditor5-table/src/table';
  15. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  16. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. describe( 'ImageResize', () => {
  18. // Id of the left mouse button.
  19. const MOUSE_BUTTON_MAIN = 0;
  20. // 60x50 black png image
  21. const IMAGE_SRC_FIXTURE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAQAAAAAPLY1AAAAQklEQVR42u3PQREAAAgDoK1/' +
  22. 'aM3g14MGNJMXKiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiJysRFNMgH0RpujAAAAAElFTkSuQmCC';
  23. let absoluteContainer, widget, editor, view, viewDocument, editorElement;
  24. before( () => {
  25. // This container is required to position editor element in a reliable element.
  26. // See fireMouseEvent method for more information regarding imprecise mouse position.
  27. absoluteContainer = document.createElement( 'div' );
  28. absoluteContainer.style.top = '50px';
  29. absoluteContainer.style.left = '50px';
  30. absoluteContainer.style.height = '1000px';
  31. absoluteContainer.style.width = '500px';
  32. absoluteContainer.style.position = 'absolute';
  33. document.body.appendChild( absoluteContainer );
  34. } );
  35. after( () => {
  36. absoluteContainer.remove();
  37. } );
  38. beforeEach( () => {
  39. editorElement = document.createElement( 'div' );
  40. absoluteContainer.appendChild( editorElement );
  41. return ClassicEditor
  42. .create( editorElement, {
  43. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResize ]
  44. } )
  45. .then( newEditor => {
  46. editor = newEditor;
  47. view = editor.editing.view;
  48. viewDocument = view.document;
  49. widget = viewDocument.getRoot().getChild( 1 );
  50. } );
  51. } );
  52. afterEach( () => {
  53. editorElement.remove();
  54. return editor.destroy();
  55. } );
  56. describe( 'conversion', () => {
  57. it( 'upcasts 100px width correctly', () => {
  58. editor.setData( `<figure class="image" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }></figure>` );
  59. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '100px' );
  60. } );
  61. it( 'upcasts 50% width correctly', () => {
  62. editor.setData( `<figure class="image" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  63. expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'width' ) ).to.equal( '50%' );
  64. } );
  65. it( 'downcasts 100px width correctly', () => {
  66. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="100px"></image>` );
  67. expect( editor.getData() )
  68. .to.equal( `<figure class="image image_resized" style="width:100px;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  69. } );
  70. it( 'downcasts 50% width correctly', () => {
  71. setData( editor.model, `<image src="${ IMAGE_SRC_FIXTURE }" width="50%"></image>` );
  72. expect( editor.getData() )
  73. .to.equal( `<figure class="image image_resized" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );
  74. } );
  75. } );
  76. describe( 'schema', () => {
  77. it( 'allows the width attribute', () => {
  78. expect( editor.model.schema.checkAttribute( 'image', 'width' ) ).to.be.true;
  79. } );
  80. it( 'defines width as a formatting attribute', () => {
  81. expect( editor.model.schema.getAttributeProperties( 'width' ) ).to.have.property( 'isFormatting', true );
  82. } );
  83. } );
  84. describe( 'command', () => {
  85. it( 'defines the imageResize command', () => {
  86. expect( editor.commands.get( 'imageResize' ) ).to.be.instanceOf( ImageResizeCommand );
  87. } );
  88. it( 'uses the command on commit', async () => {
  89. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  90. widget = viewDocument.getRoot().getChild( 1 );
  91. const spy = sinon.spy( editor.commands.get( 'imageResize' ), 'execute' );
  92. await generateResizeTest( {
  93. expectedWidth: 80,
  94. pointerOffset: {
  95. x: 10,
  96. y: -10
  97. },
  98. resizerPosition: 'bottom-left'
  99. } )();
  100. expect( spy.calledOnce ).to.be.true;
  101. expect( spy.args[ 0 ][ 0 ] ).to.deep.equal( { width: '80px' } );
  102. } );
  103. it( 'disables the resizer if the command is disabled', () => {
  104. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  105. const resizer = editor.plugins.get( 'WidgetResize' ).resizers[ 0 ];
  106. let isEnabled = false;
  107. editor.commands.get( 'imageResize' ).on( 'set:isEnabled', evt => {
  108. evt.return = isEnabled;
  109. evt.stop();
  110. }, { priority: 'highest' } );
  111. editor.commands.get( 'imageResize' ).refresh();
  112. expect( resizer.isEnabled ).to.be.false;
  113. isEnabled = true;
  114. editor.commands.get( 'imageResize' ).refresh();
  115. expect( resizer.isEnabled ).to.be.true;
  116. } );
  117. it( 'the resizer is disabled from the beginning when the command is disabled when the image is inserted', () => {
  118. setData( editor.model, '<paragraph>foo[]</paragraph>' );
  119. editor.commands.get( 'imageResize' ).on( 'set:isEnabled', evt => {
  120. evt.return = false;
  121. evt.stop();
  122. }, { priority: 'highest' } );
  123. editor.commands.get( 'imageResize' ).refresh();
  124. editor.model.change( writer => {
  125. editor.model.insertContent( writer.createElement( 'image', { src: IMAGE_SRC_FIXTURE } ) );
  126. } );
  127. const resizer = editor.plugins.get( 'WidgetResize' ).resizers[ 0 ];
  128. const resizerWrapper = editor.ui.getEditableElement().querySelector( '.ck-widget__resizer' );
  129. expect( resizer.isEnabled ).to.be.false;
  130. expect( resizerWrapper.style.display ).to.equal( 'none' );
  131. } );
  132. } );
  133. describe( 'visual resizers', () => {
  134. beforeEach( () => {
  135. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  136. widget = viewDocument.getRoot().getChild( 1 );
  137. } );
  138. it( 'correct number is added by default', () => {
  139. const resizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  140. expect( resizers.length ).to.be.equal( 4 );
  141. } );
  142. describe( 'visibility', () => {
  143. it( 'is hidden by default', () => {
  144. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  145. for ( const resizer of allResizers ) {
  146. expect( isVisible( resizer ) ).to.be.false;
  147. }
  148. } );
  149. it( 'is shown when image is focused', () => {
  150. const widget = viewDocument.getRoot().getChild( 1 );
  151. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  152. const domEventDataMock = {
  153. target: widget,
  154. preventDefault: sinon.spy()
  155. };
  156. focusEditor( editor );
  157. viewDocument.fire( 'mousedown', domEventDataMock );
  158. for ( const resizer of allResizers ) {
  159. expect( isVisible( resizer ) ).to.be.true;
  160. }
  161. } );
  162. } );
  163. } );
  164. describe( 'standard image resizing', () => {
  165. beforeEach( () => {
  166. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  167. widget = viewDocument.getRoot().getChild( 1 );
  168. } );
  169. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  170. expectedWidth: 80,
  171. pointerOffset: {
  172. x: 10,
  173. y: -10
  174. },
  175. resizerPosition: 'bottom-left'
  176. } ) );
  177. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  178. expectedWidth: 80,
  179. pointerOffset: {
  180. x: -10,
  181. y: -10
  182. },
  183. resizerPosition: 'bottom-right'
  184. } ) );
  185. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  186. expectedWidth: 120,
  187. pointerOffset: {
  188. x: 10,
  189. y: 0
  190. },
  191. resizerPosition: 'bottom-right'
  192. } ) );
  193. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  194. expectedWidth: 120,
  195. pointerOffset: {
  196. x: 0,
  197. y: 10
  198. },
  199. resizerPosition: 'bottom-right'
  200. } ) );
  201. it( 'enlarges correctly with left-bottom handler, x axis only', generateResizeTest( {
  202. expectedWidth: 120,
  203. pointerOffset: {
  204. x: -10,
  205. y: 0
  206. },
  207. resizerPosition: 'bottom-left'
  208. } ) );
  209. it( 'enlarges correctly with left-bottom handler, y axis only', generateResizeTest( {
  210. expectedWidth: 120,
  211. pointerOffset: {
  212. x: 0,
  213. y: 10
  214. },
  215. resizerPosition: 'bottom-left'
  216. } ) );
  217. // --- top handlers ---
  218. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  219. expectedWidth: 120,
  220. pointerOffset: {
  221. x: -10,
  222. y: -10
  223. },
  224. resizerPosition: 'top-left'
  225. } ) );
  226. it( 'enlarges correctly with left-top handler, y axis only', generateResizeTest( {
  227. expectedWidth: 120,
  228. pointerOffset: {
  229. x: 0,
  230. y: -10
  231. },
  232. resizerPosition: 'top-left'
  233. } ) );
  234. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  235. expectedWidth: 120,
  236. pointerOffset: {
  237. x: 10,
  238. y: -10
  239. },
  240. resizerPosition: 'top-right'
  241. } ) );
  242. it( 'enlarges correctly with right-top handler, y axis only', generateResizeTest( {
  243. expectedWidth: 120,
  244. pointerOffset: {
  245. x: 0,
  246. y: -10
  247. },
  248. resizerPosition: 'top-right'
  249. } ) );
  250. } );
  251. describe( 'side image resizing', () => {
  252. beforeEach( () => {
  253. setData( editor.model, `<paragraph>foo</paragraph>[<image imageStyle="side" src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  254. widget = viewDocument.getRoot().getChild( 1 );
  255. } );
  256. it( 'shrinks correctly with left-bottom handler', generateSideResizeTest( {
  257. isSideImage: true,
  258. expectedWidth: 80,
  259. pointerOffset: {
  260. x: 20,
  261. y: -10
  262. },
  263. resizerPosition: 'bottom-left'
  264. } ) );
  265. it( 'shrinks correctly with right-bottom handler', generateSideResizeTest( {
  266. isSideImage: true,
  267. expectedWidth: 80,
  268. pointerOffset: {
  269. x: -20,
  270. y: -10
  271. },
  272. resizerPosition: 'bottom-right'
  273. } ) );
  274. it( 'shrinks correctly with left-top handler', generateSideResizeTest( {
  275. isSideImage: true,
  276. expectedWidth: 80,
  277. pointerOffset: {
  278. x: 20,
  279. y: 10
  280. },
  281. resizerPosition: 'top-left'
  282. } ) );
  283. it( 'shrinks correctly with right-top handler', generateSideResizeTest( {
  284. isSideImage: true,
  285. expectedWidth: 80,
  286. pointerOffset: {
  287. x: -20,
  288. y: 10
  289. },
  290. resizerPosition: 'top-right'
  291. } ) );
  292. it( 'enlarges correctly with left-bottom handler', generateSideResizeTest( {
  293. isSideImage: true,
  294. expectedWidth: 120,
  295. pointerOffset: {
  296. x: -10,
  297. y: 10
  298. },
  299. resizerPosition: 'bottom-left'
  300. } ) );
  301. it( 'enlarges correctly with right-bottom handler', generateSideResizeTest( {
  302. isSideImage: true,
  303. expectedWidth: 120,
  304. pointerOffset: {
  305. x: 10,
  306. y: 10
  307. },
  308. resizerPosition: 'bottom-right'
  309. } ) );
  310. it( 'enlarges correctly with right-bottom handler, y axis only', generateSideResizeTest( {
  311. isSideImage: true,
  312. expectedWidth: 140,
  313. pointerOffset: {
  314. x: 0,
  315. y: 20
  316. },
  317. resizerPosition: 'bottom-right'
  318. } ) );
  319. it( 'enlarges correctly with right-bottom handler, x axis only', generateSideResizeTest( {
  320. isSideImage: true,
  321. expectedWidth: 140,
  322. pointerOffset: {
  323. x: 40,
  324. y: 0
  325. },
  326. resizerPosition: 'bottom-right'
  327. } ) );
  328. it( 'enlarges correctly with left-top handler', generateSideResizeTest( {
  329. isSideImage: true,
  330. expectedWidth: 120,
  331. pointerOffset: {
  332. x: -20,
  333. y: -10
  334. },
  335. resizerPosition: 'top-left'
  336. } ) );
  337. it( 'enlarges correctly with right-top handler', generateSideResizeTest( {
  338. isSideImage: true,
  339. expectedWidth: 120,
  340. pointerOffset: {
  341. x: 20,
  342. y: 10
  343. },
  344. resizerPosition: 'top-right'
  345. } ) );
  346. function generateSideResizeTest( options ) {
  347. return generateResizeTest( Object.assign( {
  348. isSideImage: true,
  349. modelRegExp: /<paragraph>foo<\/paragraph><image imageStyle="side" src=".+?" width="([\d.]+)px"><\/image>/
  350. }, options ) );
  351. }
  352. } );
  353. describe( 'undo integration', () => {
  354. beforeEach( () => {
  355. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  356. widget = viewDocument.getRoot().getChild( 1 );
  357. } );
  358. it( 'has correct border size after undo', async () => {
  359. await generateResizeTest( {
  360. expectedWidth: 120,
  361. pointerOffset: {
  362. x: 0,
  363. y: 10
  364. },
  365. resizerPosition: 'bottom-left'
  366. } )();
  367. editor.commands.get( 'undo' ).execute();
  368. await wait( 40 );
  369. const resizerWrapper = document.querySelector( '.ck-widget__resizer' );
  370. const shadowBoundingRect = resizerWrapper.getBoundingClientRect();
  371. expect( shadowBoundingRect.width ).to.be.equal( 100 );
  372. expect( shadowBoundingRect.height ).to.be.equal( 50 );
  373. } );
  374. } );
  375. describe( 'table integration', () => {
  376. beforeEach( () => {
  377. setData( editor.model,
  378. '<table>' +
  379. `<tableRow><tableCell>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]</tableCell></tableRow>` +
  380. '</table>'
  381. );
  382. widget = viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  383. } );
  384. it( 'works when resizing in a table', generateResizeTest( {
  385. getModel: () => editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 ),
  386. expectedWidth: 60,
  387. modelRegExp: /.+/,
  388. pointerOffset: {
  389. x: -40,
  390. y: -20
  391. },
  392. resizerPosition: 'bottom-right'
  393. } ) );
  394. } );
  395. describe( 'srcset integration', () => {
  396. // The image is 96x96 pixels.
  397. const imageBaseUrl = '/assets/sample.png';
  398. const getModel = () => editor.model.document.getRoot().getChild( 0 );
  399. let image;
  400. before( async () => {
  401. image = await preloadImage( imageBaseUrl );
  402. } );
  403. after( () => {
  404. image.remove();
  405. } );
  406. beforeEach( () => {
  407. editor.setData(
  408. `<figure class="image">
  409. <img src="${ imageBaseUrl }"
  410. srcset="${ imageBaseUrl }?a 110w,
  411. ${ imageBaseUrl }?b 440w,
  412. ${ imageBaseUrl }?c 1025w"
  413. sizes="100vw" width="96">
  414. </figure>`
  415. );
  416. widget = viewDocument.getRoot().getChild( 0 );
  417. } );
  418. it( 'works with images containing srcset', generateResizeTest( {
  419. getModel,
  420. expectedWidth: 76,
  421. modelRegExp: /.+/,
  422. pointerOffset: {
  423. x: -20,
  424. y: -20
  425. },
  426. resizerPosition: 'bottom-right'
  427. } ) );
  428. it( 'retains width after removing srcset', async () => {
  429. await generateResizeTest( {
  430. getModel,
  431. expectedWidth: 80,
  432. modelRegExp: /.+/,
  433. pointerOffset: {
  434. x: -16,
  435. y: -16
  436. },
  437. resizerPosition: 'bottom-right'
  438. } )();
  439. editor.model.change( writer => {
  440. writer.removeAttribute( 'srcset', getModel() );
  441. } );
  442. expect( editor.getData() )
  443. .to.match( /<figure class="image image_resized" style="width:[\d.]{2,}px;"><img src="\/assets\/sample.png"><\/figure>/ );
  444. } );
  445. async function preloadImage( imageUrl ) {
  446. const image = document.createElement( 'img' );
  447. image.src = imageUrl;
  448. return new Promise( ( resolve, reject ) => {
  449. image.addEventListener( 'load', () => resolve( image ) );
  450. image.addEventListener( 'error', () => reject( image ) );
  451. document.body.appendChild( image );
  452. } );
  453. }
  454. } );
  455. // TODO move to Resizer tests.
  456. describe( 'Resizer', () => {
  457. it( 'uses rounded (int) values', async () => {
  458. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  459. widget = viewDocument.getRoot().getChild( 1 );
  460. await generateResizeTest( {
  461. expectedWidth: 97,
  462. // Makes it resize the image to 97.2188px, unless there's a rounding.
  463. pointerOffset: {
  464. x: 7.3,
  465. y: -1
  466. },
  467. resizerPosition: 'bottom-left',
  468. checkBeforeMouseUp( domFigure, domResizeWrapper ) {
  469. expect( domFigure.style.width ).to.match( /^\d\dpx$/ );
  470. expect( domResizeWrapper.style.width ).to.match( /^\d\dpx$/ );
  471. }
  472. } )();
  473. expect( editor.model.document.getRoot().getChild( 1 ).getAttribute( 'width' ) ).to.match( /^\d\dpx$/ );
  474. } );
  475. it( 'hides the resize wrapper when its disabled', () => {
  476. setData( editor.model, `<paragraph>foo</paragraph>[<image src="${ IMAGE_SRC_FIXTURE }"></image>]` );
  477. const resizer = editor.plugins.get( 'WidgetResize' ).resizers[ 0 ];
  478. const resizerWrapper = editor.ui.getEditableElement().querySelector( '.ck-widget__resizer' );
  479. expect( resizerWrapper.style.display ).to.equal( '' );
  480. resizer.isEnabled = false;
  481. expect( resizerWrapper.style.display ).to.equal( 'none' );
  482. } );
  483. } );
  484. function isVisible( element ) {
  485. // Checks if the DOM element is visible to the end user.
  486. return element.offsetParent !== null && !element.classList.contains( 'ck-hidden' );
  487. }
  488. function fireMouseEvent( target, eventType, eventData ) {
  489. // Using initMouseEvent instead of MouseEvent constructor, as MouseEvent constructor doesn't support passing pageX
  490. // and pageY. See https://stackoverflow.com/questions/45843458/setting-click-events-pagex-and-pagey-always-reverts-to-0
  491. // However there's still a problem, that events created with `initMouseEvent` have **floored** pageX, pageY numbers.
  492. const event = document.createEvent( 'MouseEvent' );
  493. event.initMouseEvent( eventType, true, true, window, null, 0, 0, eventData.pageX, eventData.pageY, false, false, false, false,
  494. MOUSE_BUTTON_MAIN, null );
  495. target.dispatchEvent( event );
  496. }
  497. function wait( delay ) {
  498. return new Promise( resolve => window.setTimeout( () => resolve(), delay ) );
  499. }
  500. function generateResizeTest( options ) {
  501. // options.resizerPosition - top-left / top-right / bottom-right / bottom-left
  502. // options.pointerOffset - object - pointer offset relative to the dragged corner. Negative values are perfectly fine.
  503. // e.g. { x: 10, y: -5 }
  504. // options.expectedWidth
  505. // [options.isSideImage=false]
  506. // Returns a test case that puts
  507. return function() {
  508. const domResizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 1 ) );
  509. const domResizeHandle = domResizeWrapper.querySelector( `.ck-widget__resizer__handle-${ options.resizerPosition }` );
  510. const domFigure = view.domConverter.mapViewToDom( widget );
  511. const domImage = domFigure.querySelector( 'img' );
  512. const imageRect = new Rect( domImage );
  513. const resizerPositionParts = options.resizerPosition.split( '-' );
  514. const modelRegExp = options.modelRegExp ? options.modelRegExp :
  515. /<paragraph>foo<\/paragraph><image src=".+?" width="([\d]+)px"><\/image>/;
  516. focusEditor( editor );
  517. const initialPointerPosition = {
  518. pageX: imageRect.left,
  519. pageY: imageRect.top
  520. };
  521. if ( resizerPositionParts.includes( 'right' ) ) {
  522. initialPointerPosition.pageX = imageRect.right;
  523. }
  524. if ( resizerPositionParts.includes( 'bottom' ) ) {
  525. initialPointerPosition.pageY = imageRect.bottom;
  526. }
  527. const finishPointerPosition = Object.assign( {}, initialPointerPosition );
  528. finishPointerPosition.pageX += options.pointerOffset.x || 0;
  529. finishPointerPosition.pageY += options.pointerOffset.y || 0;
  530. fireMouseEvent( domResizeHandle, 'mousedown', initialPointerPosition );
  531. fireMouseEvent( domResizeHandle, 'mousemove', initialPointerPosition );
  532. // We need to wait as mousemove events are throttled.
  533. return wait( 40 )
  534. .then( () => {
  535. fireMouseEvent( domResizeHandle, 'mousemove', finishPointerPosition );
  536. expect( parseInt( domFigure.style.width ) ).to.be.closeTo( options.expectedWidth, 2, 'DOM width check' );
  537. if ( options.checkBeforeMouseUp ) {
  538. options.checkBeforeMouseUp( domFigure, domResizeWrapper );
  539. }
  540. fireMouseEvent( domResizeHandle, 'mouseup', finishPointerPosition );
  541. expect( getData( editor.model, {
  542. withoutSelection: true
  543. } ) ).to.match( modelRegExp );
  544. const modelItem = options.getModel ? options.getModel() : editor.model.document.getRoot().getChild( 1 );
  545. const modelWidth = modelItem.getAttribute( 'width' );
  546. expect( parseFloat( modelWidth, 0 ) )
  547. .to.be.closeTo( options.expectedWidth, 2, 'Model width check' );
  548. } );
  549. };
  550. }
  551. function focusEditor( editor ) {
  552. editor.editing.view.focus();
  553. editor.ui.focusTracker.isFocused = true;
  554. }
  555. } );