htmlembedediting.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 HTMLEmbedUpdateCommand from '../src/htmlembedupdatecommand';
  10. import HTMLEmbedInsertCommand from '../src/htmlembedinsertcommand';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import { isRawHtmlWidget } from '../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 htmlEmbedUpdate command', () => {
  51. expect( editor.commands.get( 'htmlEmbedUpdate' ) ).to.be.instanceOf( HTMLEmbedUpdateCommand );
  52. } );
  53. it( 'should register htmlEmbedInsert command', () => {
  54. expect( editor.commands.get( 'htmlEmbedInsert' ) ).to.be.instanceOf( HTMLEmbedInsertCommand );
  55. } );
  56. } );
  57. describe( 'config', () => {
  58. let htmlEmbed;
  59. beforeEach( () => {
  60. htmlEmbed = editor.config.get( 'htmlEmbed' );
  61. } );
  62. describe( 'htmlEmbed.previewsInData', () => {
  63. it( 'should be set to `false` by default', () => {
  64. expect( htmlEmbed.previewsInData ).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. hasModified: 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 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 editing pipeline (model to view)', () => {
  188. describe( 'without previews (htmlEmbed.dataInPreviews=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. } );
  195. it( 'should update the edit source element when the `value` attribute has changed', () => {
  196. setModelData( model, '<rawHtml></rawHtml>' );
  197. model.change( writer => {
  198. writer.setAttribute( 'value', '<b>Foo.</b>', model.document.getRoot().getChild( 0 ) );
  199. } );
  200. // TODO: Assertions.
  201. } );
  202. // TODO: More tests.
  203. it.skip( 'should render the edit source element', () => {
  204. } );
  205. it.skip( 'should render the preview placeholder element', () => {
  206. } );
  207. it.skip( 'should render the toggle mode icon', () => {
  208. } );
  209. it.skip( 'should update the `value` attribute after applying changes in the edit source element', () => {
  210. } );
  211. it.skip( 'should remove DOM events when destroying the plugin', () => {
  212. } );
  213. it.skip( 'should show the preview element by default', () => {
  214. } );
  215. } );
  216. describe( 'with previews (htmlEmbed.dataInPreviews=true)', () => {
  217. let element, editor, model, view, viewDocument, sanitizeHtml;
  218. testUtils.createSinonSandbox();
  219. beforeEach( () => {
  220. element = document.createElement( 'div' );
  221. document.body.appendChild( element );
  222. // The default sanitize function without `console.warn`.
  223. sanitizeHtml = input => ( { html: input, hasChanged: false } );
  224. return ClassicTestEditor
  225. .create( element, {
  226. plugins: [ HTMLEmbedEditing ],
  227. htmlEmbed: {
  228. previewsInData: true,
  229. sanitizeHtml
  230. }
  231. } )
  232. .then( newEditor => {
  233. editor = newEditor;
  234. model = editor.model;
  235. view = editor.editing.view;
  236. viewDocument = view.document;
  237. } );
  238. } );
  239. afterEach( () => {
  240. return editor.destroy()
  241. .then( () => {
  242. element.remove();
  243. } );
  244. } );
  245. it( 'converted element should be widgetized', () => {
  246. setModelData( model, '<rawHtml></rawHtml>' );
  247. const widget = viewDocument.getRoot().getChild( 0 );
  248. expect( widget.name ).to.equal( 'div' );
  249. expect( isRawHtmlWidget( widget ) ).to.be.true;
  250. } );
  251. it( 'should update the source and preview elements when the `value` attribute has changed', () => {
  252. setModelData( model, '<rawHtml></rawHtml>' );
  253. model.change( writer => {
  254. writer.setAttribute( 'value', '<b>Foo.</b>', model.document.getRoot().getChild( 0 ) );
  255. } );
  256. // TODO: Assertions.
  257. } );
  258. // TODO: More tests.
  259. it.skip( 'should render the edit source element', () => {
  260. } );
  261. it.skip( 'should render the preview placeholder element', () => {
  262. } );
  263. it.skip( 'should render the toggle mode icon', () => {
  264. } );
  265. it.skip( 'should update the `value` attribute after applying changes in the edit source element', () => {
  266. } );
  267. it.skip( 'should re-render the preview element after applying changes in the edit source element', () => {
  268. } );
  269. it.skip( 'should remove DOM events when destroying the plugin', () => {
  270. } );
  271. it.skip( 'should show the preview element by default', () => {
  272. } );
  273. } );
  274. } );
  275. } );