8
0

htmlembedediting.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 console, document */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import HtmlEmbedEditing from '../src/htmlembedediting';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import UpdateHtmlEmbedCommand from '../src/updatehtmlembedcommand';
  10. import InsertHtmlEmbedCommand from '../src/inserthtmlembedcommand';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import { isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  13. describe( 'HtmlEmbedEditing', () => {
  14. let element, editor, model, view, viewDocument;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. element = document.createElement( 'div' );
  18. document.body.appendChild( element );
  19. return ClassicTestEditor
  20. .create( element, {
  21. plugins: [ HtmlEmbedEditing ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. view = editor.editing.view;
  27. viewDocument = view.document;
  28. } );
  29. } );
  30. afterEach( () => {
  31. return editor.destroy()
  32. .then( () => {
  33. element.remove();
  34. } );
  35. } );
  36. it( 'should have pluginName', () => {
  37. expect( HtmlEmbedEditing.pluginName ).to.equal( 'HtmlEmbedEditing' );
  38. } );
  39. it( 'should be loaded', () => {
  40. expect( editor.plugins.get( HtmlEmbedEditing ) ).to.be.instanceOf( HtmlEmbedEditing );
  41. } );
  42. it( 'should set proper schema rules', () => {
  43. expect( model.schema.checkChild( [ '$root' ], 'rawHtml' ) ).to.be.true;
  44. expect( model.schema.checkAttribute( [ '$root', 'rawHtml' ], 'value' ) ).to.be.true;
  45. expect( model.schema.isObject( 'rawHtml' ) ).to.be.true;
  46. expect( model.schema.checkChild( [ '$root', 'rawHtml' ], '$text' ) ).to.be.false;
  47. expect( model.schema.checkChild( [ '$root', '$block' ], 'rawHtml' ) ).to.be.false;
  48. } );
  49. describe( 'commands', () => {
  50. it( 'should register updateHtmlEmbed command', () => {
  51. expect( editor.commands.get( 'updateHtmlEmbed' ) ).to.be.instanceOf( UpdateHtmlEmbedCommand );
  52. } );
  53. it( 'should register insertHtmlEmbed command', () => {
  54. expect( editor.commands.get( 'insertHtmlEmbed' ) ).to.be.instanceOf( InsertHtmlEmbedCommand );
  55. } );
  56. } );
  57. describe( 'config', () => {
  58. let htmlEmbed;
  59. beforeEach( () => {
  60. htmlEmbed = editor.config.get( 'htmlEmbed' );
  61. } );
  62. describe( 'htmlEmbed.showPreviews', () => {
  63. it( 'should be set to `false` by default', () => {
  64. expect( htmlEmbed.showPreviews ).to.equal( false );
  65. } );
  66. } );
  67. describe( 'htmlEmbed.sanitizeHtml', () => {
  68. beforeEach( () => {
  69. sinon.stub( console, 'warn' );
  70. } );
  71. it( 'should return an object with cleaned html and a note whether something has changed', () => {
  72. expect( htmlEmbed.sanitizeHtml( 'foo' ) ).to.deep.equal( {
  73. html: 'foo',
  74. hasChanged: false
  75. } );
  76. } );
  77. it( 'should return an input string (without any modifications)', () => {
  78. const unsafeHtml = '<img src="data:/xxx,<script>void</script>" onload="void;">';
  79. expect( htmlEmbed.sanitizeHtml( unsafeHtml ).html ).to.deep.equal( unsafeHtml );
  80. } );
  81. it( 'should display a warning when using the default sanitizer', () => {
  82. htmlEmbed.sanitizeHtml( 'foo' );
  83. expect( console.warn.callCount ).to.equal( 1 );
  84. expect( console.warn.firstCall.args[ 0 ] ).to.equal( 'html-embed-provide-sanitize-function' );
  85. } );
  86. } );
  87. } );
  88. describe( 'conversion in the data pipeline', () => {
  89. describe( 'model to view', () => {
  90. it( 'should convert an empty `rawHtml` element', () => {
  91. setModelData( model, '[<rawHtml></rawHtml>]' );
  92. expect( editor.getData() ).to.equal( '<div class="raw-html-embed"></div>' );
  93. } );
  94. it( 'should put the HTML from the `value` attribute (in `rawHtml`) into the data', () => {
  95. setModelData( model, '[<rawHtml></rawHtml>]' );
  96. model.change( writer => {
  97. writer.setAttribute( 'value', '<b>Foo.</b>', model.document.getRoot().getChild( 0 ) );
  98. } );
  99. expect( editor.getData() ).to.equal(
  100. '<div class="raw-html-embed">' +
  101. '<b>Foo.</b>' +
  102. '</div>'
  103. );
  104. } );
  105. } );
  106. describe( 'view to model', () => {
  107. it( 'should convert innerHTML (single element) of div.raw-html-embed', () => {
  108. editor.setData(
  109. '<div class="raw-html-embed">' +
  110. '<b>Foo.</b>' +
  111. '</div>'
  112. );
  113. const rawHtml = model.document.getRoot().getChild( 0 );
  114. expect( rawHtml.getAttribute( 'value' ) ).to.equal( '<b>Foo.</b>' );
  115. } );
  116. it( 'should convert innerHTML (single element with children) of div.raw-html-embed', () => {
  117. editor.setData(
  118. '<div class="raw-html-embed">' +
  119. '<p>' +
  120. '<b>Foo B.</b>' +
  121. '<i>Foo I.</i>' +
  122. '</p>' +
  123. '</div>'
  124. );
  125. const rawHtml = model.document.getRoot().getChild( 0 );
  126. expect( rawHtml.getAttribute( 'value' ) ).to.equal(
  127. '<p>' +
  128. '<b>Foo B.</b>' +
  129. '<i>Foo I.</i>' +
  130. '</p>'
  131. );
  132. } );
  133. it( 'should convert innerHTML (few elements) of div.raw-html-embed', () => {
  134. editor.setData(
  135. '<div class="raw-html-embed">' +
  136. '<b>Foo B.</b>' +
  137. '<i>Foo I.</i>' +
  138. '<u>Foo U.</u>' +
  139. '</div>'
  140. );
  141. const rawHtml = model.document.getRoot().getChild( 0 );
  142. expect( rawHtml.getAttribute( 'value' ) ).to.equal(
  143. '<b>Foo B.</b>' +
  144. '<i>Foo I.</i>' +
  145. '<u>Foo U.</u>'
  146. );
  147. } );
  148. it( 'should not convert in wrong context', () => {
  149. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  150. model.schema.addChildCheck( ( ctx, childDef ) => {
  151. if ( ctx.endsWith( '$root' ) && childDef.name == 'rawHtml' ) {
  152. return false;
  153. }
  154. } );
  155. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  156. editor.setData(
  157. '<div>' +
  158. '<div class="raw-html-embed">' +
  159. '<b>Foo.</b>' +
  160. '</div>' +
  161. '</div>'
  162. );
  163. expect( getModelData( model, { withoutSelection: true } ) )
  164. .to.equal( '<div></div>' );
  165. } );
  166. it( 'should not convert inner `div.raw-html-embed` that is a child of outer div.raw-html-embed', () => {
  167. editor.setData(
  168. '<div class="raw-html-embed">' +
  169. '<div class="raw-html-embed">' +
  170. '<b>Foo B.</b>' +
  171. '<i>Foo I.</i>' +
  172. '<u>Foo U.</u>' +
  173. '</div>' +
  174. '</div>'
  175. );
  176. const rawHtml = model.document.getRoot().getChild( 0 );
  177. expect( rawHtml.getAttribute( 'value' ) ).to.equal(
  178. '<div class="raw-html-embed">' +
  179. '<b>Foo B.</b>' +
  180. '<i>Foo I.</i>' +
  181. '<u>Foo U.</u>' +
  182. '</div>'
  183. );
  184. } );
  185. } );
  186. } );
  187. describe( 'conversion in the editing pipeline (model to view)', () => {
  188. describe( 'without previews (htmlEmbed.showPreviews=false)', () => {
  189. it( 'converted element should be widgetized', () => {
  190. setModelData( model, '<rawHtml></rawHtml>' );
  191. const widget = viewDocument.getRoot().getChild( 0 );
  192. expect( widget.name ).to.equal( 'div' );
  193. expect( isRawHtmlWidget( widget ) ).to.be.true;
  194. const contentWrapper = widget.getChild( 1 );
  195. expect( contentWrapper.hasClass( 'raw-html-embed__content-wrapper' ) );
  196. } );
  197. it( 'the widget should have the data-html-embed-label attribute for the CSS label', () => {
  198. setModelData( model, '<rawHtml></rawHtml>' );
  199. const widget = viewDocument.getRoot().getChild( 0 );
  200. expect( widget.getAttribute( 'data-html-embed-label' ) ).to.equal( 'HTML snippet' );
  201. } );
  202. it( 'the main element should expose rawHtmlApi custom property', () => {
  203. setModelData( model, '<rawHtml></rawHtml>' );
  204. const widget = viewDocument.getRoot().getChild( 0 );
  205. expect( widget.getCustomProperty( 'rawHtmlApi' ) ).has.keys( [ 'makeEditable', 'save', 'cancel' ] );
  206. } );
  207. it( 'renders a disabled textarea as a preview', () => {
  208. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  209. const widget = viewDocument.getRoot().getChild( 0 );
  210. const contentWrapper = widget.getChild( 1 );
  211. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  212. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'foo' );
  213. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  214. } );
  215. it( 'updates the textarea preview once the model changes', () => {
  216. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  217. editor.model.change( writer => writer.setAttribute( 'value', 'bar', editor.model.document.getRoot().getChild( 0 ) ) );
  218. const widget = viewDocument.getRoot().getChild( 0 );
  219. const contentWrapper = widget.getChild( 1 );
  220. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  221. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'bar' );
  222. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  223. } );
  224. it( 'renders the "edit" button', () => {
  225. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  226. const widget = viewDocument.getRoot().getChild( 0 );
  227. const contentWrapper = widget.getChild( 1 );
  228. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  229. // There's exactly this button, and nothing else.
  230. expect( domContentWrapper.querySelectorAll( 'button' ) ).to.have.lengthOf( 1 );
  231. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__edit-button' ) ).to.have.lengthOf( 1 );
  232. } );
  233. it( 'allows editing the source after clicking the "edit" button', () => {
  234. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  235. const widget = viewDocument.getRoot().getChild( 0 );
  236. const contentWrapper = widget.getChild( 1 );
  237. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  238. const makeEditableStub = sinon.stub( widget.getCustomProperty( 'rawHtmlApi' ), 'makeEditable' );
  239. domContentWrapper.querySelector( '.raw-html-embed__edit-button' ).click();
  240. expect( makeEditableStub.callCount ).to.equal( 1 );
  241. } );
  242. it( 'renders the "save changes" and "cancel" button in edit source mode', () => {
  243. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  244. const widget = viewDocument.getRoot().getChild( 0 );
  245. const contentWrapper = widget.getChild( 1 );
  246. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  247. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  248. expect( domContentWrapper.querySelectorAll( 'button' ) ).to.have.lengthOf( 2 );
  249. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__save-button' ) ).to.have.lengthOf( 1 );
  250. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__cancel-button' ) ).to.have.lengthOf( 1 );
  251. } );
  252. it( 'updates the model state after clicking the "save changes" button', () => {
  253. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  254. const widget = viewDocument.getRoot().getChild( 0 );
  255. const contentWrapper = widget.getChild( 1 );
  256. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  257. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  258. domContentWrapper.querySelector( 'textarea' ).value = 'Foo Bar.';
  259. domContentWrapper.querySelector( '.raw-html-embed__save-button' ).click();
  260. expect( getModelData( model ) ).to.equal( '[<rawHtml value="Foo Bar."></rawHtml>]' );
  261. } );
  262. it( 'switches to "preview mode" after saving changes', () => {
  263. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  264. let widget = viewDocument.getRoot().getChild( 0 );
  265. let contentWrapper = widget.getChild( 1 );
  266. let domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  267. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  268. domContentWrapper.querySelector( 'textarea' ).value = 'Foo Bar.';
  269. domContentWrapper.querySelector( '.raw-html-embed__save-button' ).click();
  270. // The entire DOM has rendered once again. The references were invalid.
  271. widget = viewDocument.getRoot().getChild( 0 );
  272. contentWrapper = widget.getChild( 1 );
  273. domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  274. // There's exactly this button, and nothing else.
  275. expect( domContentWrapper.querySelectorAll( 'button' ) ).to.have.lengthOf( 1 );
  276. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__edit-button' ) ).to.have.lengthOf( 1 );
  277. } );
  278. it( 'does not lose editor focus after saving changes', () => {
  279. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  280. const widget = viewDocument.getRoot().getChild( 0 );
  281. const contentWrapper = widget.getChild( 1 );
  282. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  283. const spy = sinon.spy( editor, 'focus' );
  284. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  285. domContentWrapper.querySelector( 'textarea' ).value = 'Foo Bar.';
  286. domContentWrapper.querySelector( '.raw-html-embed__save-button' ).click();
  287. sinon.assert.calledOnce( spy );
  288. } );
  289. it( 'does not update the model state after saving the same changes', () => {
  290. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  291. const widget = viewDocument.getRoot().getChild( 0 );
  292. const contentWrapper = widget.getChild( 1 );
  293. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  294. const executeStub = sinon.stub( editor.commands.get( 'updateHtmlEmbed' ), 'execute' );
  295. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  296. domContentWrapper.querySelector( '.raw-html-embed__save-button' ).click();
  297. expect( executeStub.callCount ).to.equal( 0 );
  298. } );
  299. it( 'does not update the model state after clicking the "cancel" button', () => {
  300. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  301. const widget = viewDocument.getRoot().getChild( 0 );
  302. const contentWrapper = widget.getChild( 1 );
  303. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  304. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  305. domContentWrapper.querySelector( '.raw-html-embed__cancel-button' ).click();
  306. expect( getModelData( model ) ).to.equal( '[<rawHtml value="foo"></rawHtml>]' );
  307. } );
  308. it( 'switches to "preview mode" after canceling editing', () => {
  309. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  310. const widget = viewDocument.getRoot().getChild( 0 );
  311. const contentWrapper = widget.getChild( 1 );
  312. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  313. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  314. domContentWrapper.querySelector( '.raw-html-embed__cancel-button' ).click();
  315. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'foo' );
  316. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  317. } );
  318. it( 'does not lose editor focus after canceling editing', () => {
  319. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  320. const widget = viewDocument.getRoot().getChild( 0 );
  321. const contentWrapper = widget.getChild( 1 );
  322. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  323. const spy = sinon.spy( editor, 'focus' );
  324. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  325. domContentWrapper.querySelector( '.raw-html-embed__cancel-button' ).click();
  326. sinon.assert.calledOnce( spy );
  327. } );
  328. describe( 'rawHtmlApi.makeEditable()', () => {
  329. it( 'makes the textarea editable', () => {
  330. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  331. const widget = viewDocument.getRoot().getChild( 0 );
  332. const contentWrapper = widget.getChild( 1 );
  333. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  334. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  335. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'foo' );
  336. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.false;
  337. } );
  338. } );
  339. describe( 'rawHtmlApi.save()', () => {
  340. it( 'saves the new value to the model', () => {
  341. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  342. const widget = viewDocument.getRoot().getChild( 0 );
  343. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  344. widget.getCustomProperty( 'rawHtmlApi' ).save( 'bar' );
  345. expect( getModelData( model ) ).to.equal( '[<rawHtml value="bar"></rawHtml>]' );
  346. } );
  347. it( 'turns back to the non-editable mode and updates the textarea value', () => {
  348. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  349. const widget = viewDocument.getRoot().getChild( 0 );
  350. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  351. widget.getCustomProperty( 'rawHtmlApi' ).save( 'bar' );
  352. const newWidget = viewDocument.getRoot().getChild( 0 );
  353. const contentWrapper = newWidget.getChild( 1 );
  354. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  355. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'bar' );
  356. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  357. } );
  358. } );
  359. } );
  360. describe( 'with previews (htmlEmbed.showPreviews=true)', () => {
  361. let element, editor, model, view, viewDocument, sanitizeHtml;
  362. testUtils.createSinonSandbox();
  363. beforeEach( () => {
  364. element = document.createElement( 'div' );
  365. document.body.appendChild( element );
  366. // The default sanitize function without `console.warn`.
  367. sanitizeHtml = input => ( { html: input, hasChanged: false } );
  368. return ClassicTestEditor
  369. .create( element, {
  370. plugins: [ HtmlEmbedEditing ],
  371. htmlEmbed: {
  372. showPreviews: true,
  373. sanitizeHtml
  374. }
  375. } )
  376. .then( newEditor => {
  377. editor = newEditor;
  378. model = editor.model;
  379. view = editor.editing.view;
  380. viewDocument = view.document;
  381. } );
  382. } );
  383. afterEach( () => {
  384. return editor.destroy()
  385. .then( () => {
  386. element.remove();
  387. } );
  388. } );
  389. it( 'renders a div with a preview', () => {
  390. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  391. const widget = viewDocument.getRoot().getChild( 0 );
  392. const contentWrapper = widget.getChild( 1 );
  393. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  394. expect( domContentWrapper.querySelector( 'div.raw-html-embed__preview' ).innerHTML ).to.equal( 'foo' );
  395. } );
  396. it( 'updates the preview once the model changes', () => {
  397. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  398. editor.model.change( writer => writer.setAttribute( 'value', 'bar', editor.model.document.getRoot().getChild( 0 ) ) );
  399. const widget = viewDocument.getRoot().getChild( 0 );
  400. const contentWrapper = widget.getChild( 1 );
  401. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  402. expect( domContentWrapper.querySelector( 'div.raw-html-embed__preview' ).innerHTML ).to.equal( 'bar' );
  403. } );
  404. } );
  405. } );
  406. } );
  407. function isRawHtmlWidget( viewElement ) {
  408. return !!viewElement.getCustomProperty( 'rawHtml' ) && isWidget( viewElement );
  409. }