imageresize.js 20 KB

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