imageresize.js 20 KB

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