8
0

autoimage.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. /* global setTimeout */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  9. import Link from '@ckeditor/ckeditor5-link/src/link';
  10. import Table from '@ckeditor/ckeditor5-table/src/table';
  11. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  12. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  13. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  14. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import Image from '../src/image';
  16. import ImageCaption from '../src/imagecaption';
  17. import AutoImage from '../src/autoimage';
  18. describe( 'AutoImage - integration', () => {
  19. let editorElement, editor;
  20. beforeEach( () => {
  21. editorElement = global.document.createElement( 'div' );
  22. global.document.body.appendChild( editorElement );
  23. return ClassicTestEditor
  24. .create( editorElement, {
  25. plugins: [ Typing, Paragraph, Link, Image, ImageCaption, AutoImage ]
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. } );
  30. } );
  31. afterEach( () => {
  32. editorElement.remove();
  33. return editor.destroy();
  34. } );
  35. it( 'should load Clipboard plugin', () => {
  36. expect( editor.plugins.get( Clipboard ) ).to.instanceOf( Clipboard );
  37. } );
  38. it( 'should load Undo plugin', () => {
  39. expect( editor.plugins.get( Undo ) ).to.instanceOf( Undo );
  40. } );
  41. it( 'has proper name', () => {
  42. expect( AutoImage.pluginName ).to.equal( 'AutoImage' );
  43. } );
  44. describe( 'use fake timers', () => {
  45. let clock;
  46. beforeEach( () => {
  47. clock = sinon.useFakeTimers();
  48. } );
  49. afterEach( () => {
  50. clock.restore();
  51. } );
  52. it( 'replaces pasted text with image element after 100ms', () => {
  53. setData( editor.model, '<paragraph>[]</paragraph>' );
  54. pasteHtml( editor, 'http://example.com/image.png' );
  55. expect( getData( editor.model ) ).to.equal(
  56. '<paragraph>http://example.com/image.png[]</paragraph>'
  57. );
  58. clock.tick( 100 );
  59. expect( getData( editor.model ) ).to.equal(
  60. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  61. );
  62. } );
  63. it( 'can undo auto-embeding', () => {
  64. setData( editor.model, '<paragraph>[]</paragraph>' );
  65. pasteHtml( editor, 'http://example.com/image.png' );
  66. expect( getData( editor.model ) ).to.equal(
  67. '<paragraph>http://example.com/image.png[]</paragraph>'
  68. );
  69. clock.tick( 100 );
  70. editor.commands.execute( 'undo' );
  71. expect( getData( editor.model ) ).to.equal(
  72. '<paragraph>http://example.com/image.png[]</paragraph>'
  73. );
  74. } );
  75. it( 'works for a full URL (https + "www" sub-domain)', () => {
  76. setData( editor.model, '<paragraph>[]</paragraph>' );
  77. pasteHtml( editor, 'http://www.example.com/image.png' );
  78. clock.tick( 100 );
  79. expect( getData( editor.model ) ).to.equal(
  80. '[<image src="http://www.example.com/image.png"><caption></caption></image>]'
  81. );
  82. } );
  83. it( 'works for a full URL (https without "www" sub-domain)', () => {
  84. setData( editor.model, '<paragraph>[]</paragraph>' );
  85. pasteHtml( editor, 'http://example.com/image.png' );
  86. clock.tick( 100 );
  87. expect( getData( editor.model ) ).to.equal(
  88. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  89. );
  90. } );
  91. it( 'works for a full URL (http + "www" sub-domain)', () => {
  92. setData( editor.model, '<paragraph>[]</paragraph>' );
  93. pasteHtml( editor, 'http://www.example.com/image.png' );
  94. clock.tick( 100 );
  95. expect( getData( editor.model ) ).to.equal(
  96. '[<image src="http://www.example.com/image.png"><caption></caption></image>]'
  97. );
  98. } );
  99. it( 'works for a full URL (http without "www" sub-domain)', () => {
  100. setData( editor.model, '<paragraph>[]</paragraph>' );
  101. pasteHtml( editor, 'http://example.com/image.png' );
  102. clock.tick( 100 );
  103. expect( getData( editor.model ) ).to.equal(
  104. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  105. );
  106. } );
  107. it( 'works for a URL without protocol (with "www" sub-domain)', () => {
  108. setData( editor.model, '<paragraph>[]</paragraph>' );
  109. pasteHtml( editor, 'www.example.com/image.png' );
  110. clock.tick( 100 );
  111. expect( getData( editor.model ) ).to.equal(
  112. '[<image src="www.example.com/image.png"><caption></caption></image>]'
  113. );
  114. } );
  115. it( 'works for a URL without protocol (without "www" sub-domain)', () => {
  116. setData( editor.model, '<paragraph>[]</paragraph>' );
  117. pasteHtml( editor, 'example.com/image.png' );
  118. clock.tick( 100 );
  119. expect( getData( editor.model ) ).to.equal(
  120. '[<image src="example.com/image.png"><caption></caption></image>]'
  121. );
  122. } );
  123. it( 'works for URL that was pasted as a link', () => {
  124. setData( editor.model, '<paragraph>[]</paragraph>' );
  125. pasteHtml( editor, '<a href="http://example.com/image.png">' +
  126. 'http://example.com/image.png</a>' );
  127. clock.tick( 100 );
  128. expect( getData( editor.model ) ).to.equal(
  129. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  130. );
  131. } );
  132. it( 'works for URL that contains some inline styles', () => {
  133. setData( editor.model, '<paragraph>[]</paragraph>' );
  134. pasteHtml( editor, '<b>http://example.com/image.png</b>' );
  135. clock.tick( 100 );
  136. expect( getData( editor.model ) ).to.equal(
  137. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  138. );
  139. } );
  140. it( 'works for not collapsed selection inside single element', () => {
  141. setData( editor.model, '<paragraph>[Foo]</paragraph>' );
  142. pasteHtml( editor, 'http://example.com/image.png' );
  143. clock.tick( 100 );
  144. expect( getData( editor.model ) ).to.equal(
  145. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  146. );
  147. } );
  148. it( 'works for not collapsed selection over a few elements', () => {
  149. setData( editor.model, '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
  150. pasteHtml( editor, 'http://example.com/image.png' );
  151. clock.tick( 100 );
  152. expect( getData( editor.model ) ).to.equal(
  153. '<paragraph>Fo</paragraph>' +
  154. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  155. '<paragraph>r</paragraph>'
  156. );
  157. } );
  158. it( 'inserts image in-place (collapsed selection)', () => {
  159. setData( editor.model, '<paragraph>Foo []Bar</paragraph>' );
  160. pasteHtml( editor, 'http://example.com/image.png' );
  161. clock.tick( 100 );
  162. expect( getData( editor.model ) ).to.equal(
  163. '<paragraph>Foo </paragraph>' +
  164. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  165. '<paragraph>Bar</paragraph>'
  166. );
  167. } );
  168. it( 'inserts image in-place (non-collapsed selection)', () => {
  169. setData( editor.model, '<paragraph>Foo [Bar] Baz</paragraph>' );
  170. pasteHtml( editor, 'http://example.com/image.png' );
  171. clock.tick( 100 );
  172. expect( getData( editor.model ) ).to.equal(
  173. '<paragraph>Foo </paragraph>' +
  174. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  175. '<paragraph> Baz</paragraph>'
  176. );
  177. } );
  178. it( 'does nothing if a URL is invalid', () => {
  179. setData( editor.model, '<paragraph>[]</paragraph>' );
  180. pasteHtml( editor, 'https://example.com' );
  181. clock.tick( 100 );
  182. expect( getData( editor.model ) ).to.equal(
  183. '<paragraph>https://example.com[]</paragraph>'
  184. );
  185. } );
  186. it( 'does nothing if pasted two links as text', () => {
  187. setData( editor.model, '<paragraph>[]</paragraph>' );
  188. pasteHtml( editor, 'http://example.com/image.png ' +
  189. 'http://example.com/image.png' );
  190. clock.tick( 100 );
  191. expect( getData( editor.model ) ).to.equal(
  192. '<paragraph>http://example.com/image.png ' +
  193. 'http://example.com/image.png[]</paragraph>'
  194. );
  195. } );
  196. it( 'does nothing if pasted text contains a valid URL', () => {
  197. setData( editor.model, '<paragraph>[]</paragraph>' );
  198. pasteHtml( editor, 'Foo bar http://example.com/image.png bar foo.' );
  199. clock.tick( 100 );
  200. expect( getData( editor.model ) ).to.equal(
  201. '<paragraph>Foo bar http://example.com/image.png bar foo.[]</paragraph>'
  202. );
  203. } );
  204. it( 'does nothing if pasted more than single node', () => {
  205. setData( editor.model, '<paragraph>[]</paragraph>' );
  206. pasteHtml( editor,
  207. 'http://example.com/image.png ' +
  208. '<a href="http://example.com/image.png">' +
  209. 'http://example.com/image.png</a>'
  210. );
  211. clock.tick( 100 );
  212. expect( getData( editor.model, { withoutSelection: true } ) ).to.equal(
  213. '<paragraph>http://example.com/image.png ' +
  214. '<$text linkHref="http://example.com/image.png">' +
  215. 'http://example.com/image.png</$text>' +
  216. '</paragraph>'
  217. );
  218. } );
  219. it( 'does nothing if pasted a paragraph with the url', () => {
  220. setData( editor.model, '<paragraph>[]</paragraph>' );
  221. pasteHtml( editor, '<p>http://example.com/image.png</p>' );
  222. clock.tick( 100 );
  223. expect( getData( editor.model ) ).to.equal(
  224. '<paragraph>http://example.com/image.png[]</paragraph>'
  225. );
  226. } );
  227. it( 'does nothing if pasted a block of content that looks like a URL', () => {
  228. setData( editor.model, '<paragraph>[]</paragraph>' );
  229. pasteHtml( editor, '<p>https://</p><p>example.com/image</p><p>.png</p>' );
  230. clock.tick( 100 );
  231. expect( getData( editor.model ) ).to.equal(
  232. '<paragraph>https://</paragraph><paragraph>example.com/image</paragraph><paragraph>.png[]</paragraph>'
  233. );
  234. } );
  235. it( 'does nothing if a URL is invalid (space inside URL)', () => {
  236. setData( editor.model, '<paragraph>[]</paragraph>' );
  237. pasteHtml( editor, 'https://example.com/im age.png' );
  238. clock.tick( 100 );
  239. expect( getData( editor.model ) ).to.equal(
  240. '<paragraph>https://example.com/im age.png[]</paragraph>'
  241. );
  242. } );
  243. // #47
  244. it( 'does not transform a valid URL into a image if the element cannot be placed in the current position', () => {
  245. setData( editor.model, '<image src="/assets/sample.png"><caption>Foo.[]</caption></image>' );
  246. pasteHtml( editor, 'http://example.com/image.png' );
  247. clock.tick( 100 );
  248. expect( getData( editor.model ) ).to.equal(
  249. '<image src="/assets/sample.png"><caption>' +
  250. 'Foo.http://example.com/image.png[]' +
  251. '</caption></image>'
  252. );
  253. } );
  254. it( 'replaces a URL in image if pasted a link when other image element was selected', () => {
  255. setData(
  256. editor.model,
  257. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  258. );
  259. pasteHtml( editor, 'http://example.com/image2.png' );
  260. clock.tick( 100 );
  261. expect( getData( editor.model ) ).to.equal(
  262. '[<image src="http://example.com/image2.png"><caption></caption></image>]'
  263. );
  264. } );
  265. it( 'inserts a new image element if pasted a link when other image element was selected in correct place', () => {
  266. setData(
  267. editor.model,
  268. '<paragraph>Foo. <$text linkHref="https://cksource.com">Bar</$text></paragraph>' +
  269. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  270. '<paragraph><$text>Bar</$text>.</paragraph>'
  271. );
  272. pasteHtml( editor, 'http://example.com/image2.png' );
  273. clock.tick( 100 );
  274. expect( getData( editor.model ) ).to.equal(
  275. '<paragraph>Foo. <$text linkHref="https://cksource.com">Bar</$text></paragraph>' +
  276. '[<image src="http://example.com/image2.png"><caption></caption></image>]' +
  277. '<paragraph>Bar.</paragraph>'
  278. );
  279. } );
  280. it( 'works for URL with %-symbols', () => {
  281. setData( editor.model, '<paragraph>[]</paragraph>' );
  282. pasteHtml( editor, 'http://example.com%20Fimage.png' );
  283. clock.tick( 100 );
  284. expect( getData( editor.model ) ).to.equal(
  285. '[<image src="http://example.com%20Fimage.png"><caption></caption></image>]'
  286. );
  287. } );
  288. } );
  289. describe( 'use real timers', () => {
  290. const characters = Array( 10 ).fill( 1 ).map( ( x, i ) => String.fromCharCode( 65 + i ) );
  291. it( 'undo breaks the auto-image feature (undo was done before auto-image)', done => {
  292. setData( editor.model, '<paragraph>[]</paragraph>' );
  293. pasteHtml( editor, 'http://example.com/image.png' );
  294. expect( getData( editor.model ) ).to.equal(
  295. '<paragraph>http://example.com/image.png[]</paragraph>'
  296. );
  297. setTimeout( () => {
  298. editor.commands.execute( 'undo' );
  299. expect( getData( editor.model ) ).to.equal(
  300. '<paragraph>[]</paragraph>'
  301. );
  302. done();
  303. } );
  304. } );
  305. // Checking whether paste+typing calls the auto-image handler once.
  306. it( 'pasting handler should be executed once', done => {
  307. const AutoImagePlugin = editor.plugins.get( AutoImage );
  308. const autoImageHandler = AutoImagePlugin._embedImageBetweenPositions;
  309. let counter = 0;
  310. AutoImagePlugin._embedImageBetweenPositions = function( ...args ) {
  311. counter += 1;
  312. return autoImageHandler.apply( this, args );
  313. };
  314. setData( editor.model, '<paragraph>[]</paragraph>' );
  315. pasteHtml( editor, 'http://example.com/image.png' );
  316. simulateTyping( 'Foo. Bar.' );
  317. setTimeout( () => {
  318. AutoImagePlugin._embedImageBetweenPositions = autoImageHandler;
  319. expect( counter ).to.equal( 1 );
  320. done();
  321. }, 100 );
  322. } );
  323. it( 'typing before pasted link during collaboration should not blow up', done => {
  324. setData( editor.model, '<paragraph>[]</paragraph>' );
  325. pasteHtml( editor, 'http://example.com/image.png' );
  326. for ( let i = 0; i < 10; ++i ) {
  327. const rootEl = editor.model.document.getRoot();
  328. setTimeout( () => {
  329. editor.model.enqueueChange( 'transparent', writer => {
  330. writer.insertText( characters[ i ], writer.createPositionFromPath( rootEl, [ 0, i ] ) );
  331. } );
  332. }, i * 5 );
  333. }
  334. setTimeout( () => {
  335. expect( getData( editor.model ) ).to.equal(
  336. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  337. '<paragraph>ABCDEFGHIJ</paragraph>'
  338. );
  339. done();
  340. }, 100 );
  341. } );
  342. it( 'typing after pasted link during collaboration should not blow up', done => {
  343. setData( editor.model, '<paragraph>[]</paragraph>' );
  344. pasteHtml( editor, 'http://example.com/image.png' );
  345. for ( let i = 0; i < 10; ++i ) {
  346. setTimeout( () => {
  347. editor.model.enqueueChange( 'transparent', writer => {
  348. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  349. } );
  350. }, i * 5 );
  351. }
  352. setTimeout( () => {
  353. expect( getData( editor.model ) ).to.equal(
  354. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  355. '<paragraph>ABCDEFGHIJ</paragraph>'
  356. );
  357. done();
  358. }, 100 );
  359. } );
  360. it( 'should insert the image element even if parent element where the URL was pasted has been deleted', done => {
  361. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  362. pasteHtml( editor, 'http://example.com/image.png' );
  363. editor.model.enqueueChange( 'transparent', writer => {
  364. writer.remove( writer.createRangeOn( editor.model.document.getRoot().getChild( 1 ) ) );
  365. } );
  366. setTimeout( () => {
  367. expect( getData( editor.model ) ).to.equal(
  368. '<paragraph>Foo.</paragraph>' +
  369. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  370. );
  371. done();
  372. }, 100 );
  373. } );
  374. it( 'should insert the image element even if new element appeared above the pasted URL', done => {
  375. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  376. pasteHtml( editor, 'http://example.com/image.png' );
  377. editor.model.enqueueChange( 'transparent', writer => {
  378. const paragraph = writer.createElement( 'paragraph' );
  379. writer.insert( paragraph, writer.createPositionAfter( editor.model.document.getRoot().getChild( 0 ) ) );
  380. writer.setSelection( paragraph, 'in' );
  381. } );
  382. for ( let i = 0; i < 10; ++i ) {
  383. setTimeout( () => {
  384. editor.model.enqueueChange( 'transparent', writer => {
  385. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  386. } );
  387. }, i * 5 );
  388. }
  389. setTimeout( () => {
  390. expect( getData( editor.model ) ).to.equal(
  391. '<paragraph>Foo.</paragraph>' +
  392. '<paragraph>ABCDEFGHIJ</paragraph>' +
  393. '<paragraph>Bar.</paragraph>' +
  394. '[<image src="http://example.com/image.png"><caption></caption></image>]'
  395. );
  396. done();
  397. }, 100 );
  398. } );
  399. it( 'should insert the image element even if new element appeared below the pasted URL', done => {
  400. setData( editor.model, '<paragraph>Foo.</paragraph><paragraph>Bar.[]</paragraph>' );
  401. pasteHtml( editor, 'http://example.com/image.png' );
  402. editor.model.enqueueChange( 'transparent', writer => {
  403. const paragraph = writer.createElement( 'paragraph' );
  404. writer.insert( paragraph, writer.createPositionAfter( editor.model.document.getRoot().getChild( 1 ) ) );
  405. writer.setSelection( paragraph, 'in' );
  406. } );
  407. for ( let i = 0; i < 10; ++i ) {
  408. setTimeout( () => {
  409. editor.model.enqueueChange( 'transparent', writer => {
  410. writer.insertText( characters[ i ], editor.model.document.selection.getFirstPosition() );
  411. } );
  412. }, i * 5 );
  413. }
  414. setTimeout( () => {
  415. expect( getData( editor.model ) ).to.equal(
  416. '<paragraph>Foo.</paragraph>' +
  417. '<paragraph>Bar.</paragraph>' +
  418. '[<image src="http://example.com/image.png"><caption></caption></image>]' +
  419. '<paragraph>ABCDEFGHIJ</paragraph>'
  420. );
  421. done();
  422. }, 100 );
  423. } );
  424. } );
  425. it( 'should detach LiveRange', async () => {
  426. const editor = await ClassicTestEditor.create( editorElement, {
  427. plugins: [ Typing, Paragraph, Link, Image, ImageCaption, Table, AutoImage ]
  428. } );
  429. setData(
  430. editor.model,
  431. '<table>' +
  432. '<tableRow>' +
  433. '[<tableCell><paragraph>foo</paragraph></tableCell>]' +
  434. '[<tableCell><paragraph>bar</paragraph></tableCell>]' +
  435. '</tableRow>' +
  436. '</table>'
  437. );
  438. pasteHtml( editor, '<table><tr><td>one</td><td>two</td></tr></table>' );
  439. expect( getData( editor.model, { withoutSelection: true } ) ).to.equal(
  440. '<table>' +
  441. '<tableRow>' +
  442. '<tableCell><paragraph>one</paragraph></tableCell>' +
  443. '<tableCell><paragraph>two</paragraph></tableCell>' +
  444. '</tableRow>' +
  445. '</table>'
  446. );
  447. expect( () => {
  448. editor.setData( '' );
  449. } ).not.to.throw();
  450. await editor.destroy();
  451. } );
  452. function simulateTyping( text ) {
  453. // While typing, every character is an atomic change.
  454. text.split( '' ).forEach( character => {
  455. editor.execute( 'input', {
  456. text: character
  457. } );
  458. } );
  459. }
  460. function pasteHtml( editor, html ) {
  461. editor.editing.view.document.fire( 'paste', {
  462. dataTransfer: createDataTransfer( { 'text/html': html } ),
  463. stopPropagation() {},
  464. preventDefault() {}
  465. } );
  466. }
  467. function createDataTransfer( data ) {
  468. return {
  469. getData( type ) {
  470. return data[ type ];
  471. }
  472. };
  473. }
  474. } );