mutationobserver.js 20 KB

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