htmlembedediting.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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, HTMLTextAreaElement, HTMLDivElement, HTMLButtonElement, Event */
  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 { 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 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. const rawHtmlContainer = widget.getChild( 0 );
  195. expect( rawHtmlContainer ).to.not.be.undefined;
  196. } );
  197. it( 'widget should not contain a class that informs about available preview mode', () => {
  198. setModelData( model, '<rawHtml></rawHtml>' );
  199. const widget = viewDocument.getRoot().getChild( 0 );
  200. expect( widget.hasClass( 'raw-html--preview-enabled' ) ).to.equal( false );
  201. } );
  202. it( 'should render the toggle button and edit source elements', () => {
  203. setModelData( model, '<rawHtml></rawHtml>' );
  204. const widget = viewDocument.getRoot().getChild( 0 );
  205. const viewHtmlContainer = widget.getChild( 0 );
  206. // Expecting two children: toggle button and editable textarea.
  207. expect( viewHtmlContainer.childCount ).to.equal( 2 );
  208. const toggleIconElement = viewHtmlContainer.getChild( 0 );
  209. expect( toggleIconElement.is( 'uiElement' ) ).to.equal( true );
  210. expect( toggleIconElement.getCustomProperty( 'domElement' ) ).to.be.an.instanceOf( HTMLButtonElement );
  211. const sourceElement = viewHtmlContainer.getChild( 1 );
  212. expect( sourceElement.is( 'uiElement' ) ).to.equal( true );
  213. expect( sourceElement.getCustomProperty( 'domElement' ) ).to.be.an.instanceOf( HTMLTextAreaElement );
  214. } );
  215. it( 'source element should have a placeholder', () => {
  216. setModelData( model, '<rawHtml></rawHtml>' );
  217. const widget = viewDocument.getRoot().getChild( 0 );
  218. const viewHtmlContainer = widget.getChild( 0 );
  219. const sourceElement = viewHtmlContainer.getChild( 1 );
  220. expect( sourceElement.getAttribute( 'placeholder' ) ).to.equal( 'Paste the raw code here.' );
  221. } );
  222. it( 'should update the edit source element when the `value` attribute has changed', () => {
  223. setModelData( model, '<rawHtml></rawHtml>' );
  224. model.change( writer => {
  225. writer.setAttribute( 'value', '<b>Foo.</b>', model.document.getRoot().getChild( 0 ) );
  226. } );
  227. const widget = viewDocument.getRoot().getChild( 0 );
  228. const viewHtmlContainer = widget.getChild( 0 );
  229. const sourceElement = viewHtmlContainer.getChild( 1 );
  230. expect( sourceElement.getCustomProperty( 'domElement' ).value ).to.equal( '<b>Foo.</b>' );
  231. } );
  232. it( 'should update the `value` attribute after applying changes in the edit source element', () => {
  233. setModelData( model, '<rawHtml></rawHtml>' );
  234. const widget = viewDocument.getRoot().getChild( 0 );
  235. const viewHtmlContainer = widget.getChild( 0 );
  236. const sourceElement = viewHtmlContainer.getChild( 1 );
  237. const textarea = sourceElement.getCustomProperty( 'domElement' );
  238. textarea.value = '<b>Foo.</b>';
  239. textarea.dispatchEvent( new Event( 'input' ) );
  240. expect( model.document.getRoot().getChild( 0 ).getAttribute( 'value' ) ).to.equal( '<b>Foo.</b>' );
  241. } );
  242. it( 'should show the HTML in the preview mode by default (source element should be disabled)', () => {
  243. setModelData( model, '<rawHtml value="Foo"></rawHtml>' );
  244. const widget = viewDocument.getRoot().getChild( 0 );
  245. const viewHtmlContainer = widget.getChild( 0 );
  246. const sourceElement = viewHtmlContainer.getChild( 1 );
  247. const textarea = sourceElement.getCustomProperty( 'domElement' );
  248. expect( textarea.disabled ).to.equal( true );
  249. expect( textarea.value ).to.equal( 'Foo' );
  250. } );
  251. it( 'should allows modifying the source after clicking the toggle button', () => {
  252. setModelData( model, '<rawHtml></rawHtml>' );
  253. const widget = viewDocument.getRoot().getChild( 0 );
  254. const viewHtmlContainer = widget.getChild( 0 );
  255. const toggleButtonElement = viewHtmlContainer.getChild( 0 );
  256. toggleButtonElement.getCustomProperty( 'domElement' ).click();
  257. const sourceElement = viewHtmlContainer.getChild( 1 );
  258. expect( sourceElement.getCustomProperty( 'domElement' ).disabled ).to.equal( false );
  259. } );
  260. it( 'should update the toggle button icon after switching to "edit source mode"', () => {
  261. setModelData( model, '<rawHtml></rawHtml>' );
  262. const widget = viewDocument.getRoot().getChild( 0 );
  263. const viewHtmlContainer = widget.getChild( 0 );
  264. const toggleButtonElement = viewHtmlContainer.getChild( 0 );
  265. const toggleButton = toggleButtonElement.getCustomProperty( 'domElement' );
  266. const toggleIconBeforeClick = toggleButton.innerHTML;
  267. toggleButton.click();
  268. // Check whether the icon has been updated.
  269. expect( toggleButton.innerHTML ).to.not.equal( toggleIconBeforeClick );
  270. } );
  271. it( 'should disable the source element after clicking the toggle button when edit source mode is enabled', () => {
  272. setModelData( model, '<rawHtml></rawHtml>' );
  273. const widget = viewDocument.getRoot().getChild( 0 );
  274. const viewHtmlContainer = widget.getChild( 0 );
  275. const toggleIconElement = viewHtmlContainer.getChild( 0 );
  276. const toggleButton = toggleIconElement.getCustomProperty( 'domElement' );
  277. // Switch to edit source mode.
  278. toggleButton.click();
  279. // Switch to preview mode.
  280. toggleButton.click();
  281. expect( viewHtmlContainer.getChild( 1 ).getCustomProperty( 'domElement' ).disabled ).to.equal( true );
  282. } );
  283. it( 'should disable the source element after clicking the toggle button when edit source mode is enabled', () => {
  284. setModelData( model, '<rawHtml></rawHtml>' );
  285. const widget = viewDocument.getRoot().getChild( 0 );
  286. const viewHtmlContainer = widget.getChild( 0 );
  287. const toggleButtonElement = viewHtmlContainer.getChild( 0 );
  288. const toggleButton = toggleButtonElement.getCustomProperty( 'domElement' );
  289. // The icon before switching mode.
  290. const toggleIconBeforeClick = toggleButton.innerHTML;
  291. // Switch to edit source mode.
  292. toggleButton.click();
  293. // The icon for edit source mode.
  294. const toggleIconAfterFirstClick = toggleButton.innerHTML;
  295. // Switch to preview mode.
  296. toggleButton.click();
  297. // The first click: the icon has been changed.
  298. expect( toggleButton.innerHTML ).to.equal( toggleIconBeforeClick );
  299. // The second click: the icon has been restored.
  300. expect( toggleButton.innerHTML ).to.not.equal( toggleIconAfterFirstClick );
  301. } );
  302. } );
  303. describe( 'with previews (htmlEmbed.dataInPreviews=true)', () => {
  304. let element, editor, model, view, viewDocument, sanitizeHtml;
  305. testUtils.createSinonSandbox();
  306. beforeEach( () => {
  307. element = document.createElement( 'div' );
  308. document.body.appendChild( element );
  309. // The default sanitize function without `console.warn`.
  310. sanitizeHtml = input => ( { html: input, hasChanged: false } );
  311. return ClassicTestEditor
  312. .create( element, {
  313. plugins: [ HtmlEmbedEditing ],
  314. htmlEmbed: {
  315. previewsInData: true,
  316. sanitizeHtml
  317. }
  318. } )
  319. .then( newEditor => {
  320. editor = newEditor;
  321. model = editor.model;
  322. view = editor.editing.view;
  323. viewDocument = view.document;
  324. } );
  325. } );
  326. afterEach( () => {
  327. return editor.destroy()
  328. .then( () => {
  329. element.remove();
  330. } );
  331. } );
  332. it( 'converted element should be widgetized', () => {
  333. setModelData( model, '<rawHtml></rawHtml>' );
  334. const widget = viewDocument.getRoot().getChild( 0 );
  335. expect( widget.name ).to.equal( 'div' );
  336. expect( isRawHtmlWidget( widget ) ).to.be.true;
  337. const rawHtmlContainer = widget.getChild( 0 );
  338. expect( rawHtmlContainer ).to.not.be.undefined;
  339. } );
  340. it( 'widget should contain a class that informs about available preview mode', () => {
  341. setModelData( model, '<rawHtml></rawHtml>' );
  342. const widget = viewDocument.getRoot().getChild( 0 );
  343. expect( widget.hasClass( 'raw-html--preview-enabled' ) ).to.equal( true );
  344. } );
  345. it( 'should render the toggle button, edit source and preview container elements', () => {
  346. setModelData( model, '<rawHtml></rawHtml>' );
  347. const widget = viewDocument.getRoot().getChild( 0 );
  348. const viewHtmlContainer = widget.getChild( 0 );
  349. // Expecting three children: toggle button, editable textarea, and the preview container.
  350. expect( viewHtmlContainer.childCount ).to.equal( 3 );
  351. const toggleIconElement = viewHtmlContainer.getChild( 0 );
  352. expect( toggleIconElement.is( 'uiElement' ) ).to.equal( true );
  353. expect( toggleIconElement.getCustomProperty( 'domElement' ) ).to.be.an.instanceOf( HTMLButtonElement );
  354. const sourceElement = viewHtmlContainer.getChild( 1 );
  355. expect( sourceElement.is( 'uiElement' ) ).to.equal( true );
  356. expect( sourceElement.getCustomProperty( 'domElement' ) ).to.be.an.instanceOf( HTMLTextAreaElement );
  357. const previewElement = viewHtmlContainer.getChild( 2 );
  358. expect( previewElement.is( 'rawElement' ) ).to.equal( true );
  359. expect( previewElement.getCustomProperty( 'domElement' ) ).to.be.an.instanceOf( HTMLDivElement );
  360. } );
  361. it( 'source element should have a placeholder', () => {
  362. setModelData( model, '<rawHtml></rawHtml>' );
  363. const widget = viewDocument.getRoot().getChild( 0 );
  364. const viewHtmlContainer = widget.getChild( 0 );
  365. const sourceElement = viewHtmlContainer.getChild( 1 );
  366. expect( sourceElement.getAttribute( 'placeholder' ) ).to.equal( 'Paste the raw code here.' );
  367. } );
  368. it( 'should update the source and preview elements when the `value` attribute has changed', () => {
  369. setModelData( model, '<rawHtml></rawHtml>' );
  370. const widget = viewDocument.getRoot().getChild( 0 );
  371. const viewHtmlContainer = widget.getChild( 0 );
  372. const sourceElement = viewHtmlContainer.getChild( 1 );
  373. const previewElement = viewHtmlContainer.getChild( 2 );
  374. // The preview container should be empty.
  375. expect( previewElement.getCustomProperty( 'domElement' ).innerHTML ).to.equal( '' );
  376. model.change( writer => {
  377. writer.setAttribute( 'value', '<b>Foo.</b>', model.document.getRoot().getChild( 0 ) );
  378. } );
  379. expect( sourceElement.getCustomProperty( 'domElement' ).value ).to.equal( '<b>Foo.</b>' );
  380. expect( previewElement.getCustomProperty( 'domElement' ).innerHTML ).to.equal( '<b>Foo.</b>' );
  381. } );
  382. it( 'should update the `value` attribute after applying changes in the edit source element', () => {
  383. setModelData( model, '<rawHtml></rawHtml>' );
  384. const event = new Event( 'input' );
  385. const widget = viewDocument.getRoot().getChild( 0 );
  386. const viewHtmlContainer = widget.getChild( 0 );
  387. const sourceElement = viewHtmlContainer.getChild( 1 );
  388. const textarea = sourceElement.getCustomProperty( 'domElement' );
  389. // The preview container should be empty.
  390. textarea.value = '<b>Foo.</b>';
  391. textarea.dispatchEvent( event );
  392. expect( model.document.getRoot().getChild( 0 ).getAttribute( 'value' ) ).to.equal( '<b>Foo.</b>' );
  393. } );
  394. it( 'should re-render the preview element after applying changes in the edit source element', () => {
  395. setModelData( model, '<rawHtml></rawHtml>' );
  396. const event = new Event( 'input' );
  397. const widget = viewDocument.getRoot().getChild( 0 );
  398. const viewHtmlContainer = widget.getChild( 0 );
  399. const sourceElement = viewHtmlContainer.getChild( 1 );
  400. const previewElement = viewHtmlContainer.getChild( 2 );
  401. // The preview container should be empty.
  402. expect( previewElement.getCustomProperty( 'domElement' ).innerHTML ).to.equal( '' );
  403. const textarea = sourceElement.getCustomProperty( 'domElement' );
  404. textarea.value = '<b>Foo.</b>';
  405. textarea.dispatchEvent( event );
  406. expect( previewElement.getCustomProperty( 'domElement' ).innerHTML ).to.equal( '<b>Foo.</b>' );
  407. } );
  408. it( 'should render the HTML in the preview element by default', () => {
  409. setModelData( model, '<rawHtml value="Foo"></rawHtml>' );
  410. const widget = viewDocument.getRoot().getChild( 0 );
  411. expect( widget.hasClass( 'raw-html--display-preview' ) ).to.equal( true );
  412. const viewHtmlContainer = widget.getChild( 0 );
  413. const sourceElement = viewHtmlContainer.getChild( 1 );
  414. const textarea = sourceElement.getCustomProperty( 'domElement' );
  415. expect( textarea.disabled ).to.equal( true );
  416. expect( textarea.value ).to.equal( 'Foo' );
  417. const previewElement = viewHtmlContainer.getChild( 2 ).getCustomProperty( 'domElement' );
  418. expect( previewElement.innerHTML ).to.equal( 'Foo' );
  419. } );
  420. it( 'should allows modifying the source after clicking the toggle button', () => {
  421. setModelData( model, '<rawHtml></rawHtml>' );
  422. const widget = viewDocument.getRoot().getChild( 0 );
  423. const viewHtmlContainer = widget.getChild( 0 );
  424. const toggleIconElement = viewHtmlContainer.getChild( 0 );
  425. toggleIconElement.getCustomProperty( 'domElement' ).click();
  426. const sourceElement = viewHtmlContainer.getChild( 1 );
  427. expect( sourceElement.getCustomProperty( 'domElement' ).disabled ).to.equal( false );
  428. expect( widget.hasClass( 'raw-html--display-preview' ) ).to.equal( false );
  429. } );
  430. it( 'should update the toggle button icon after switching to "edit source mode"', () => {
  431. setModelData( model, '<rawHtml></rawHtml>' );
  432. const widget = viewDocument.getRoot().getChild( 0 );
  433. const viewHtmlContainer = widget.getChild( 0 );
  434. const toggleIconElement = viewHtmlContainer.getChild( 0 );
  435. const toggleButton = toggleIconElement.getCustomProperty( 'domElement' );
  436. const toggleIconBeforeClick = toggleButton.innerHTML;
  437. toggleButton.click();
  438. // Check whether the icon has been updated.
  439. expect( toggleButton.innerHTML ).to.not.equal( toggleIconBeforeClick );
  440. } );
  441. it( 'should display preview element after clicking the toggle button when displaying edit source mode', () => {
  442. setModelData( model, '<rawHtml></rawHtml>' );
  443. const widget = viewDocument.getRoot().getChild( 0 );
  444. const viewHtmlContainer = widget.getChild( 0 );
  445. const toggleIconElement = viewHtmlContainer.getChild( 0 );
  446. const toggleButton = toggleIconElement.getCustomProperty( 'domElement' );
  447. // Switch to edit source mode.
  448. toggleButton.click();
  449. // Switch to preview mode.
  450. toggleButton.click();
  451. expect( widget.hasClass( 'raw-html--display-preview' ) ).to.equal( true );
  452. expect( viewHtmlContainer.getChild( 1 ).getCustomProperty( 'domElement' ).disabled ).to.equal( true );
  453. } );
  454. it( 'should disable the source element after clicking the toggle button when edit source mode is enabled', () => {
  455. setModelData( model, '<rawHtml></rawHtml>' );
  456. const widget = viewDocument.getRoot().getChild( 0 );
  457. const viewHtmlContainer = widget.getChild( 0 );
  458. const toggleIconElement = viewHtmlContainer.getChild( 0 );
  459. const toggleButton = toggleIconElement.getCustomProperty( 'domElement' );
  460. // The icon before switching mode.
  461. const toggleIconBeforeClick = toggleButton.innerHTML;
  462. // Switch to edit source mode.
  463. toggleButton.click();
  464. // The icon for edit source mode.
  465. const toggleIconAfterFirstClick = toggleButton.innerHTML;
  466. // Switch to preview mode.
  467. toggleButton.click();
  468. // The first click: the icon has been changed.
  469. expect( toggleButton.innerHTML ).to.equal( toggleIconBeforeClick );
  470. // The second click: the icon has been restored.
  471. expect( toggleButton.innerHTML ).to.not.equal( toggleIconAfterFirstClick );
  472. } );
  473. } );
  474. } );
  475. } );
  476. function isRawHtmlWidget( viewElement ) {
  477. return !!viewElement.getCustomProperty( 'rawHtml' ) && isWidget( viewElement );
  478. }