8
0

htmlembedediting.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 main element should expose rawHtmlApi custom property', () => {
  198. setModelData( model, '<rawHtml></rawHtml>' );
  199. const widget = viewDocument.getRoot().getChild( 0 );
  200. expect( widget.getCustomProperty( 'rawHtmlApi' ) ).has.keys( [ 'makeEditable', 'save', 'cancel' ] );
  201. } );
  202. it( 'renders a disabled textarea as a preview', () => {
  203. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  204. const widget = viewDocument.getRoot().getChild( 0 );
  205. const contentWrapper = widget.getChild( 1 );
  206. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  207. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'foo' );
  208. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  209. } );
  210. it( 'updates the textarea preview once the model changes', () => {
  211. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  212. editor.model.change( writer => writer.setAttribute( 'value', 'bar', editor.model.document.getRoot().getChild( 0 ) ) );
  213. const widget = viewDocument.getRoot().getChild( 0 );
  214. const contentWrapper = widget.getChild( 1 );
  215. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  216. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'bar' );
  217. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  218. } );
  219. it( 'renders the "edit" button', () => {
  220. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  221. const widget = viewDocument.getRoot().getChild( 0 );
  222. const contentWrapper = widget.getChild( 1 );
  223. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  224. // There's exactly this button, and nothing else.
  225. expect( domContentWrapper.querySelectorAll( 'button' ) ).to.have.lengthOf( 1 );
  226. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__edit-button' ) ).to.have.lengthOf( 1 );
  227. } );
  228. it( 'allows editing the source after clicking the "edit" button', () => {
  229. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  230. const widget = viewDocument.getRoot().getChild( 0 );
  231. const contentWrapper = widget.getChild( 1 );
  232. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  233. const makeEditableStub = sinon.stub( widget.getCustomProperty( 'rawHtmlApi' ), 'makeEditable' );
  234. domContentWrapper.querySelector( '.raw-html-embed__edit-button' ).click();
  235. expect( makeEditableStub.callCount ).to.equal( 1 );
  236. } );
  237. it( 'renders the "save changes" and "cancel" button in edit source mode', () => {
  238. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  239. const widget = viewDocument.getRoot().getChild( 0 );
  240. const contentWrapper = widget.getChild( 1 );
  241. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  242. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  243. expect( domContentWrapper.querySelectorAll( 'button' ) ).to.have.lengthOf( 2 );
  244. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__save-button' ) ).to.have.lengthOf( 1 );
  245. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__cancel-button' ) ).to.have.lengthOf( 1 );
  246. } );
  247. it( 'updates the model state after clicking the "save changes" button', () => {
  248. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  249. const widget = viewDocument.getRoot().getChild( 0 );
  250. const contentWrapper = widget.getChild( 1 );
  251. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  252. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  253. domContentWrapper.querySelector( 'textarea' ).value = 'Foo Bar.';
  254. domContentWrapper.querySelector( '.raw-html-embed__save-button' ).click();
  255. expect( getModelData( model ) ).to.equal( '[<rawHtml value="Foo Bar."></rawHtml>]' );
  256. } );
  257. it( 'switches to "preview mode" after saving changes', () => {
  258. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  259. let widget = viewDocument.getRoot().getChild( 0 );
  260. let contentWrapper = widget.getChild( 1 );
  261. let domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  262. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  263. domContentWrapper.querySelector( 'textarea' ).value = 'Foo Bar.';
  264. domContentWrapper.querySelector( '.raw-html-embed__save-button' ).click();
  265. // The entire DOM has rendered once again. The references were invalid.
  266. widget = viewDocument.getRoot().getChild( 0 );
  267. contentWrapper = widget.getChild( 1 );
  268. domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  269. // There's exactly this button, and nothing else.
  270. expect( domContentWrapper.querySelectorAll( 'button' ) ).to.have.lengthOf( 1 );
  271. expect( domContentWrapper.querySelectorAll( '.raw-html-embed__edit-button' ) ).to.have.lengthOf( 1 );
  272. } );
  273. it( 'does not update the model state after saving the same changes', () => {
  274. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  275. const widget = viewDocument.getRoot().getChild( 0 );
  276. const contentWrapper = widget.getChild( 1 );
  277. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  278. const executeStub = sinon.stub( editor.commands.get( 'updateHtmlEmbed' ), 'execute' );
  279. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  280. domContentWrapper.querySelector( '.raw-html-embed__save-button' ).click();
  281. expect( executeStub.callCount ).to.equal( 0 );
  282. } );
  283. it( 'does not update the model state after clicking the "cancel" button', () => {
  284. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  285. const widget = viewDocument.getRoot().getChild( 0 );
  286. const contentWrapper = widget.getChild( 1 );
  287. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  288. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  289. domContentWrapper.querySelector( '.raw-html-embed__cancel-button' ).click();
  290. expect( getModelData( model ) ).to.equal( '[<rawHtml value="foo"></rawHtml>]' );
  291. } );
  292. it( 'switches to "preview mode" after canceling editing', () => {
  293. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  294. const widget = viewDocument.getRoot().getChild( 0 );
  295. const contentWrapper = widget.getChild( 1 );
  296. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  297. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  298. domContentWrapper.querySelector( '.raw-html-embed__cancel-button' ).click();
  299. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'foo' );
  300. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  301. } );
  302. describe( 'rawHtmlApi.makeEditable()', () => {
  303. it( 'makes the textarea editable', () => {
  304. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  305. const widget = viewDocument.getRoot().getChild( 0 );
  306. const contentWrapper = widget.getChild( 1 );
  307. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  308. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  309. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'foo' );
  310. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.false;
  311. } );
  312. } );
  313. describe( 'rawHtmlApi.save()', () => {
  314. it( 'saves the new value to the model', () => {
  315. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  316. const widget = viewDocument.getRoot().getChild( 0 );
  317. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  318. widget.getCustomProperty( 'rawHtmlApi' ).save( 'bar' );
  319. expect( getModelData( model ) ).to.equal( '[<rawHtml value="bar"></rawHtml>]' );
  320. } );
  321. it( 'turns back to the non-editable mode and updates the textarea value', () => {
  322. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  323. const widget = viewDocument.getRoot().getChild( 0 );
  324. widget.getCustomProperty( 'rawHtmlApi' ).makeEditable();
  325. widget.getCustomProperty( 'rawHtmlApi' ).save( 'bar' );
  326. const newWidget = viewDocument.getRoot().getChild( 0 );
  327. const contentWrapper = newWidget.getChild( 1 );
  328. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  329. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).value ).to.equal( 'bar' );
  330. expect( domContentWrapper.querySelector( 'textarea.raw-html-embed__source' ).disabled ).to.be.true;
  331. } );
  332. } );
  333. } );
  334. describe( 'with previews (htmlEmbed.showPreviews=true)', () => {
  335. let element, editor, model, view, viewDocument, sanitizeHtml;
  336. testUtils.createSinonSandbox();
  337. beforeEach( () => {
  338. element = document.createElement( 'div' );
  339. document.body.appendChild( element );
  340. // The default sanitize function without `console.warn`.
  341. sanitizeHtml = input => ( { html: input, hasChanged: false } );
  342. return ClassicTestEditor
  343. .create( element, {
  344. plugins: [ HtmlEmbedEditing ],
  345. htmlEmbed: {
  346. showPreviews: true,
  347. sanitizeHtml
  348. }
  349. } )
  350. .then( newEditor => {
  351. editor = newEditor;
  352. model = editor.model;
  353. view = editor.editing.view;
  354. viewDocument = view.document;
  355. } );
  356. } );
  357. afterEach( () => {
  358. return editor.destroy()
  359. .then( () => {
  360. element.remove();
  361. } );
  362. } );
  363. it( 'renders a div with a preview', () => {
  364. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  365. const widget = viewDocument.getRoot().getChild( 0 );
  366. const contentWrapper = widget.getChild( 1 );
  367. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  368. expect( domContentWrapper.querySelector( 'div.raw-html-embed__preview' ).innerHTML ).to.equal( 'foo' );
  369. } );
  370. it( 'updates the preview once the model changes', () => {
  371. setModelData( model, '<rawHtml value="foo"></rawHtml>' );
  372. editor.model.change( writer => writer.setAttribute( 'value', 'bar', editor.model.document.getRoot().getChild( 0 ) ) );
  373. const widget = viewDocument.getRoot().getChild( 0 );
  374. const contentWrapper = widget.getChild( 1 );
  375. const domContentWrapper = editor.editing.view.domConverter.mapViewToDom( contentWrapper );
  376. expect( domContentWrapper.querySelector( 'div.raw-html-embed__preview' ).innerHTML ).to.equal( 'bar' );
  377. } );
  378. } );
  379. } );
  380. } );
  381. function isRawHtmlWidget( viewElement ) {
  382. return !!viewElement.getCustomProperty( 'rawHtml' ) && isWidget( viewElement );
  383. }