mutationobserver.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. /* globals document */
  6. import View from '../../../src/view/view';
  7. import MutationObserver from '../../../src/view/observer/mutationobserver';
  8. import UIElement from '../../../src/view/uielement';
  9. import RawElement from '../../../src/view/rawelement';
  10. import createViewRoot from '../_utils/createroot';
  11. import { parse } from '../../../src/dev-utils/view';
  12. import { StylesProcessor } from '../../../src/view/stylesmap';
  13. describe( 'MutationObserver', () => {
  14. let view, domEditor, viewDocument, viewRoot, mutationObserver, lastMutations, domRoot;
  15. beforeEach( () => {
  16. domRoot = document.createElement( 'div' );
  17. domRoot.innerHTML = '<div contenteditable="true" id="main"></div><div contenteditable="true" id="additional"></div>';
  18. document.body.appendChild( domRoot );
  19. view = new View( new StylesProcessor() );
  20. viewDocument = view.document;
  21. domEditor = document.getElementById( 'main' );
  22. lastMutations = null;
  23. createViewRoot( viewDocument );
  24. view.attachDomRoot( domEditor );
  25. viewDocument.selection._setTo( null );
  26. document.getSelection().removeAllRanges();
  27. mutationObserver = view.getObserver( MutationObserver );
  28. viewDocument.on( 'mutations', ( evt, mutations ) => {
  29. lastMutations = mutations;
  30. } );
  31. viewRoot = viewDocument.getRoot();
  32. viewRoot._appendChild( parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  33. view.forceRender();
  34. } );
  35. afterEach( () => {
  36. mutationObserver.disable();
  37. domRoot.parentElement.removeChild( domRoot );
  38. view.destroy();
  39. } );
  40. it( 'should handle typing', () => {
  41. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  42. mutationObserver.flush();
  43. expectDomEditorNotToChange();
  44. expect( lastMutations.length ).to.equal( 1 );
  45. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  46. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  47. expect( lastMutations[ 0 ].newText ).to.equal( 'foom' );
  48. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  49. } );
  50. it( 'should not observe if disabled', () => {
  51. const additional = document.getElementById( 'additional' );
  52. mutationObserver.disable();
  53. createViewRoot( viewDocument, 'div', 'additional' );
  54. view.attachDomRoot( additional, 'additional' );
  55. additional.textContent = 'foobar';
  56. mutationObserver.flush();
  57. expect( lastMutations ).to.be.null;
  58. } );
  59. it( 'should handle bold', () => {
  60. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  61. const domB = document.createElement( 'b' );
  62. domB.appendChild( document.createTextNode( 'oo' ) );
  63. domEditor.childNodes[ 0 ].appendChild( domB );
  64. mutationObserver.flush();
  65. expectDomEditorNotToChange();
  66. expect( lastMutations.length ).to.equal( 1 );
  67. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  68. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  69. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  70. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  71. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  72. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  73. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  74. } );
  75. it( 'should handle unbold', () => {
  76. viewRoot._removeChildren( 0, viewRoot.childCount );
  77. viewRoot._appendChild( parse( '<container:p><attribute:b>foo</attribute:b></container:p>' ) );
  78. view.forceRender();
  79. const domP = domEditor.childNodes[ 0 ];
  80. const domB = domP.childNodes[ 0 ];
  81. domP.removeChild( domB );
  82. domP.appendChild( document.createTextNode( 'foo' ) );
  83. mutationObserver.flush();
  84. // "expectDomEditorNotToChange()".
  85. expect( domEditor.childNodes.length ).to.equal( 1 );
  86. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  87. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  88. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].tagName ).to.equal( 'B' );
  89. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  90. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  91. // Check mutations.
  92. expect( lastMutations.length ).to.equal( 1 );
  93. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  94. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  95. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  96. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  97. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  98. expect( lastMutations[ 0 ].oldChildren[ 0 ].name ).to.equal( 'b' );
  99. } );
  100. it( 'should deduplicate text changes', () => {
  101. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  102. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  103. mutationObserver.flush();
  104. expectDomEditorNotToChange();
  105. expect( lastMutations.length ).to.equal( 1 );
  106. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  107. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  108. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  109. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  110. } );
  111. it( 'should ignore changes in elements not attached to tree view', () => {
  112. const domP = document.createElement( 'p' );
  113. const domB = document.createElement( 'b' );
  114. const domText = document.createTextNode( 'bom' );
  115. domEditor.appendChild( domP );
  116. domP.appendChild( domB );
  117. domB.appendChild( domText );
  118. mutationObserver.flush();
  119. expectDomEditorNotToChange();
  120. expect( lastMutations.length ).to.equal( 1 );
  121. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  122. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  123. } );
  124. it( 'should fire mutations event with view selection instance, if dom selection can be mapped to view', done => {
  125. const textNode = domEditor.childNodes[ 0 ].childNodes[ 0 ];
  126. textNode.data = 'foom';
  127. const domSelection = document.getSelection();
  128. domSelection.collapse( textNode, 4 );
  129. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  130. expect( viewSelection.anchor.parent ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  131. expect( viewSelection.anchor.offset ).to.equal( 4 );
  132. done();
  133. } );
  134. mutationObserver.flush();
  135. expectDomEditorNotToChange();
  136. } );
  137. it( 'should fire mutations event with viewSelection param set to null, if dom selection cannot be mapped to view', done => {
  138. const textNode = domEditor.ownerDocument.createTextNode( 'foo' );
  139. domEditor.childNodes[ 0 ].appendChild( textNode );
  140. const domSelection = document.getSelection();
  141. domSelection.collapse( textNode, 3 );
  142. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  143. expect( viewSelection ).to.be.null;
  144. done();
  145. } );
  146. mutationObserver.flush();
  147. expectDomEditorNotToChange();
  148. } );
  149. it( 'should be able to observe multiple roots', () => {
  150. const domAdditionalEditor = document.getElementById( 'additional' );
  151. // Prepare AdditionalEditor
  152. createViewRoot( viewDocument, 'div', 'additional' );
  153. view.attachDomRoot( domAdditionalEditor, 'additional' );
  154. viewDocument.getRoot( 'additional' )._appendChild(
  155. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  156. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  157. view.forceRender();
  158. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  159. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  160. mutationObserver.flush();
  161. expect( lastMutations.length ).to.equal( 2 );
  162. } );
  163. it( 'should fire nothing if there were no mutations', () => {
  164. mutationObserver.flush();
  165. expectDomEditorNotToChange();
  166. expect( lastMutations ).to.be.null;
  167. } );
  168. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  169. const { view: viewContainer, selection } = parse(
  170. '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>'
  171. );
  172. view.change( writer => {
  173. viewRoot._appendChild( viewContainer );
  174. writer.setSelection( selection );
  175. } );
  176. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  177. inlineFiller.data += 'x';
  178. mutationObserver.flush();
  179. expect( lastMutations.length ).to.equal( 1 );
  180. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  181. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  182. } );
  183. it( 'should have no inline filler in mutation', () => {
  184. const { view: viewContainer, selection } = parse(
  185. '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>'
  186. );
  187. view.change( writer => {
  188. viewRoot._appendChild( viewContainer );
  189. writer.setSelection( selection );
  190. } );
  191. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  192. inlineFiller.data += 'x';
  193. view.change( () => {
  194. viewContainer.getChild( 1 )._appendChild( parse( 'x' ) );
  195. mutationObserver.flush();
  196. } );
  197. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  198. inlineFiller.data += 'y';
  199. mutationObserver.flush();
  200. expect( lastMutations.length ).to.equal( 1 );
  201. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  202. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  203. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  204. } );
  205. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 1.
  206. it( 'should handle space after inline filler at the end of container', () => {
  207. const { view: viewContainer, selection } = parse(
  208. '<container:p>' +
  209. 'foo' +
  210. '<attribute:b>[]</attribute:b>' +
  211. '</container:p>'
  212. );
  213. view.change( writer => {
  214. viewRoot._appendChild( viewContainer );
  215. writer.setSelection( selection );
  216. } );
  217. // Appended container is third in the tree.
  218. const container = domEditor.childNodes[ 2 ];
  219. const inlineFiller = container.childNodes[ 1 ].childNodes[ 0 ];
  220. inlineFiller.data += ' ';
  221. mutationObserver.flush();
  222. expect( lastMutations.length ).to.equal( 1 );
  223. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  224. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  225. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  226. expect( lastMutations[ 0 ].newChildren[ 0 ].is( '$text' ) ).to.be.true;
  227. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( ' ' );
  228. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  229. } );
  230. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 3.
  231. it( 'should handle space after inline filler at the end of container #2', () => {
  232. const { view: viewContainer, selection } = parse(
  233. '<container:p>' +
  234. 'foo' +
  235. '<attribute:b>bar</attribute:b>' +
  236. '[]' +
  237. '</container:p>'
  238. );
  239. view.change( writer => {
  240. viewRoot._appendChild( viewContainer );
  241. writer.setSelection( selection );
  242. } );
  243. // Appended container is third in the tree.
  244. const container = domEditor.childNodes[ 2 ];
  245. const inlineFiller = container.childNodes[ 2 ];
  246. inlineFiller.data += ' ';
  247. mutationObserver.flush();
  248. expect( lastMutations.length ).to.equal( 1 );
  249. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  250. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 2 );
  251. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 3 );
  252. // Foo and attribute is removed and reinserted.
  253. expect( lastMutations[ 0 ].oldChildren[ 0 ].is( '$text' ) ).to.be.true;
  254. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  255. expect( lastMutations[ 0 ].newChildren[ 0 ].is( '$text' ) ).to.be.true;
  256. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  257. expect( lastMutations[ 0 ].oldChildren[ 1 ].is( 'attributeElement' ) ).to.be.true;
  258. expect( lastMutations[ 0 ].oldChildren[ 1 ].name ).to.equal( 'b' );
  259. expect( lastMutations[ 0 ].newChildren[ 1 ].is( 'attributeElement' ) ).to.be.true;
  260. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  261. expect( lastMutations[ 0 ].newChildren[ 2 ].is( '$text' ) ).to.be.true;
  262. expect( lastMutations[ 0 ].newChildren[ 2 ].data ).to.equal( ' ' );
  263. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  264. } );
  265. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 2.
  266. it( 'should handle space after inline filler at the beginning of container', () => {
  267. const { view: viewContainer, selection } = parse(
  268. '<container:p>' +
  269. '<attribute:b>[]</attribute:b>' +
  270. 'foo' +
  271. '</container:p>'
  272. );
  273. view.change( writer => {
  274. viewRoot._appendChild( viewContainer );
  275. writer.setSelection( selection );
  276. } );
  277. // Appended container is third in the tree.
  278. const container = domEditor.childNodes[ 2 ];
  279. const inlineFiller = container.childNodes[ 0 ].childNodes[ 0 ];
  280. inlineFiller.data += ' ';
  281. mutationObserver.flush();
  282. expect( lastMutations.length ).to.equal( 1 );
  283. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  284. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  285. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  286. expect( lastMutations[ 0 ].newChildren[ 0 ].is( '$text' ) ).to.be.true;
  287. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( ' ' );
  288. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  289. } );
  290. it( 'should have no block filler in mutation', () => {
  291. viewRoot._appendChild( parse( '<container:p></container:p>' ) );
  292. view.forceRender();
  293. const domP = domEditor.childNodes[ 2 ];
  294. domP.removeChild( domP.childNodes[ 0 ] );
  295. domP.appendChild( document.createTextNode( 'foo' ) );
  296. mutationObserver.flush();
  297. expect( lastMutations.length ).to.equal( 1 );
  298. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  299. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  300. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  301. } );
  302. it( 'should ignore mutation with bogus br inserted on the end of the empty paragraph', () => {
  303. viewRoot._appendChild( parse( '<container:p></container:p>' ) );
  304. view.forceRender();
  305. const domP = domEditor.childNodes[ 2 ];
  306. domP.appendChild( document.createElement( 'br' ) );
  307. mutationObserver.flush();
  308. expect( lastMutations ).to.be.null;
  309. } );
  310. it( 'should ignore mutation with bogus br inserted on the end of the paragraph with text', () => {
  311. viewRoot._appendChild( parse( '<container:p>foo</container:p>' ) );
  312. view.forceRender();
  313. const domP = domEditor.childNodes[ 2 ];
  314. domP.appendChild( document.createElement( 'br' ) );
  315. mutationObserver.flush();
  316. expect( lastMutations ).to.be.null;
  317. } );
  318. it( 'should ignore mutation with bogus br inserted on the end of the paragraph while processing text mutations', () => {
  319. viewRoot._appendChild( parse( '<container:p>foo</container:p>' ) );
  320. view.forceRender();
  321. const domP = domEditor.childNodes[ 2 ];
  322. domP.childNodes[ 0 ].data = 'foo ';
  323. domP.appendChild( document.createElement( 'br' ) );
  324. mutationObserver.flush();
  325. expect( lastMutations.length ).to.equal( 1 );
  326. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  327. expect( lastMutations[ 0 ].newText ).to.equal( 'foo ' );
  328. } );
  329. it( 'should ignore child mutations which resulted in no changes – when element contains elements', () => {
  330. viewRoot._appendChild( parse( '<container:p><container:x></container:x></container:p>' ) );
  331. view.forceRender();
  332. const domP = domEditor.childNodes[ 2 ];
  333. const domY = document.createElement( 'y' );
  334. domP.appendChild( domY );
  335. domY.remove();
  336. mutationObserver.flush();
  337. expect( lastMutations ).to.be.null;
  338. } );
  339. // This case is more tricky than the previous one because DOMConverter will return a different
  340. // instances of view text nodes every time it converts a DOM text node.
  341. it( 'should ignore child mutations which resulted in no changes – when element contains text nodes', () => {
  342. const domP = domEditor.childNodes[ 0 ];
  343. const domText = document.createTextNode( 'x' );
  344. domP.appendChild( domText );
  345. domText.remove();
  346. const domP2 = domEditor.childNodes[ 1 ];
  347. domP2.appendChild( document.createTextNode( 'x' ) );
  348. mutationObserver.flush();
  349. // There was only P2 change. P1 must be ignored.
  350. const viewP2 = viewRoot.getChild( 1 );
  351. expect( lastMutations.length ).to.equal( 1 );
  352. expect( lastMutations[ 0 ].node ).to.equal( viewP2 );
  353. } );
  354. it( 'should not ignore mutation with br inserted not on the end of the paragraph', () => {
  355. viewRoot._appendChild( parse( '<container:p>foo</container:p>' ) );
  356. view.forceRender();
  357. const domP = domEditor.childNodes[ 2 ];
  358. domP.insertBefore( document.createElement( 'br' ), domP.childNodes[ 0 ] );
  359. mutationObserver.flush();
  360. expect( lastMutations.length ).to.equal( 1 );
  361. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  362. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'br' );
  363. expect( lastMutations[ 0 ].newChildren[ 1 ].data ).to.equal( 'foo' );
  364. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  365. } );
  366. it( 'should not ignore mutation inserting element different than br on the end of the empty paragraph', () => {
  367. viewRoot._appendChild( parse( '<container:p></container:p>' ) );
  368. view.forceRender();
  369. const domP = domEditor.childNodes[ 2 ];
  370. domP.appendChild( document.createElement( 'span' ) );
  371. mutationObserver.flush();
  372. expect( lastMutations.length ).to.equal( 1 );
  373. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  374. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'span' );
  375. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  376. } );
  377. it( 'should not ignore mutation inserting element different than br on the end of the paragraph with text', () => {
  378. viewRoot._appendChild( parse( '<container:p>foo</container:p>' ) );
  379. view.forceRender();
  380. const domP = domEditor.childNodes[ 2 ];
  381. domP.appendChild( document.createElement( 'span' ) );
  382. mutationObserver.flush();
  383. expect( lastMutations.length ).to.equal( 1 );
  384. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  385. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  386. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'span' );
  387. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  388. } );
  389. describe( 'UIElement integration', () => {
  390. const renderStub = sinon.stub();
  391. function createUIElement( name ) {
  392. const element = new UIElement( name );
  393. element.render = function( domDocument ) {
  394. const root = this.toDomElement( domDocument );
  395. root.innerHTML = 'foo bar';
  396. return root;
  397. };
  398. return element;
  399. }
  400. beforeEach( () => {
  401. const uiElement = createUIElement( 'div' );
  402. viewRoot._appendChild( uiElement );
  403. view.forceRender();
  404. renderStub.reset();
  405. view.on( 'render', renderStub );
  406. } );
  407. it( 'should not collect text mutations from UIElement', () => {
  408. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  409. mutationObserver.flush();
  410. expect( lastMutations ).to.be.null;
  411. } );
  412. it( 'should not cause a render from UIElement', () => {
  413. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  414. mutationObserver.flush();
  415. expect( renderStub.callCount ).to.equal( 0 );
  416. } );
  417. it( 'should not collect child mutations from UIElement', () => {
  418. const span = document.createElement( 'span' );
  419. domEditor.childNodes[ 2 ].appendChild( span );
  420. mutationObserver.flush();
  421. expect( lastMutations ).to.be.null;
  422. } );
  423. it( 'should not cause a render when UIElement gets a child', () => {
  424. const span = document.createElement( 'span' );
  425. domEditor.childNodes[ 2 ].appendChild( span );
  426. mutationObserver.flush();
  427. expect( renderStub.callCount ).to.equal( 0 );
  428. } );
  429. } );
  430. describe( 'RawElement integration', () => {
  431. const renderStub = sinon.stub();
  432. function createRawElement( name ) {
  433. const element = new RawElement( name );
  434. element.render = function( domElement ) {
  435. domElement.innerHTML = 'foo bar';
  436. };
  437. return element;
  438. }
  439. beforeEach( () => {
  440. const rawElement = createRawElement( 'div' );
  441. viewRoot._appendChild( rawElement );
  442. view.forceRender();
  443. renderStub.reset();
  444. view.on( 'render', renderStub );
  445. } );
  446. it( 'should not collect text mutations from RawElement', () => {
  447. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  448. mutationObserver.flush();
  449. expect( lastMutations ).to.be.null;
  450. } );
  451. it( 'should not cause a render from RawElement', () => {
  452. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  453. mutationObserver.flush();
  454. expect( renderStub.callCount ).to.equal( 0 );
  455. } );
  456. it( 'should not collect child mutations from RawElement', () => {
  457. const span = document.createElement( 'span' );
  458. domEditor.childNodes[ 2 ].appendChild( span );
  459. mutationObserver.flush();
  460. expect( lastMutations ).to.be.null;
  461. } );
  462. it( 'should not cause a render when RawElement gets a child', () => {
  463. const span = document.createElement( 'span' );
  464. domEditor.childNodes[ 2 ].appendChild( span );
  465. mutationObserver.flush();
  466. expect( renderStub.callCount ).to.equal( 0 );
  467. } );
  468. } );
  469. function expectDomEditorNotToChange() {
  470. expect( domEditor.childNodes.length ).to.equal( 2 );
  471. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  472. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  473. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  474. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  475. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  476. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  477. }
  478. } );