mutationobserver.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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, stylesProcessor;
  14. beforeEach( () => {
  15. stylesProcessor = new StylesProcessor();
  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( 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>', { stylesProcessor } ) );
  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>', { stylesProcessor } ) );
  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>', { stylesProcessor } ) );
  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>', { stylesProcessor }
  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>', { stylesProcessor }
  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', { stylesProcessor } ) );
  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. { stylesProcessor }
  213. );
  214. view.change( writer => {
  215. viewRoot._appendChild( viewContainer );
  216. writer.setSelection( selection );
  217. } );
  218. // Appended container is third in the tree.
  219. const container = domEditor.childNodes[ 2 ];
  220. const inlineFiller = container.childNodes[ 1 ].childNodes[ 0 ];
  221. inlineFiller.data += ' ';
  222. mutationObserver.flush();
  223. expect( lastMutations.length ).to.equal( 1 );
  224. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  225. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  226. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  227. expect( lastMutations[ 0 ].newChildren[ 0 ].is( 'text' ) ).to.be.true;
  228. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( ' ' );
  229. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  230. } );
  231. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 3.
  232. it( 'should handle space after inline filler at the end of container #2', () => {
  233. const { view: viewContainer, selection } = parse(
  234. '<container:p>' +
  235. 'foo' +
  236. '<attribute:b>bar</attribute:b>' +
  237. '[]' +
  238. '</container:p>',
  239. { stylesProcessor }
  240. );
  241. view.change( writer => {
  242. viewRoot._appendChild( viewContainer );
  243. writer.setSelection( selection );
  244. } );
  245. // Appended container is third in the tree.
  246. const container = domEditor.childNodes[ 2 ];
  247. const inlineFiller = container.childNodes[ 2 ];
  248. inlineFiller.data += ' ';
  249. mutationObserver.flush();
  250. expect( lastMutations.length ).to.equal( 1 );
  251. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  252. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 2 );
  253. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 3 );
  254. // Foo and attribute is removed and reinserted.
  255. expect( lastMutations[ 0 ].oldChildren[ 0 ].is( 'text' ) ).to.be.true;
  256. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  257. expect( lastMutations[ 0 ].newChildren[ 0 ].is( 'text' ) ).to.be.true;
  258. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  259. expect( lastMutations[ 0 ].oldChildren[ 1 ].is( 'attributeElement' ) ).to.be.true;
  260. expect( lastMutations[ 0 ].oldChildren[ 1 ].name ).to.equal( 'b' );
  261. expect( lastMutations[ 0 ].newChildren[ 1 ].is( 'attributeElement' ) ).to.be.true;
  262. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  263. expect( lastMutations[ 0 ].newChildren[ 2 ].is( 'text' ) ).to.be.true;
  264. expect( lastMutations[ 0 ].newChildren[ 2 ].data ).to.equal( ' ' );
  265. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  266. } );
  267. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 2.
  268. it( 'should handle space after inline filler at the beginning of container', () => {
  269. const { view: viewContainer, selection } = parse(
  270. '<container:p>' +
  271. '<attribute:b>[]</attribute:b>' +
  272. 'foo' +
  273. '</container:p>',
  274. { stylesProcessor }
  275. );
  276. view.change( writer => {
  277. viewRoot._appendChild( viewContainer );
  278. writer.setSelection( selection );
  279. } );
  280. // Appended container is third in the tree.
  281. const container = domEditor.childNodes[ 2 ];
  282. const inlineFiller = container.childNodes[ 0 ].childNodes[ 0 ];
  283. inlineFiller.data += ' ';
  284. mutationObserver.flush();
  285. expect( lastMutations.length ).to.equal( 1 );
  286. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  287. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  288. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  289. expect( lastMutations[ 0 ].newChildren[ 0 ].is( 'text' ) ).to.be.true;
  290. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( ' ' );
  291. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  292. } );
  293. it( 'should have no block filler in mutation', () => {
  294. viewRoot._appendChild( parse( '<container:p></container:p>', { stylesProcessor } ) );
  295. view.forceRender();
  296. const domP = domEditor.childNodes[ 2 ];
  297. domP.removeChild( domP.childNodes[ 0 ] );
  298. domP.appendChild( document.createTextNode( 'foo' ) );
  299. mutationObserver.flush();
  300. expect( lastMutations.length ).to.equal( 1 );
  301. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  302. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  303. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  304. } );
  305. it( 'should ignore mutation with bogus br inserted on the end of the empty paragraph', () => {
  306. viewRoot._appendChild( parse( '<container:p></container:p>', { stylesProcessor } ) );
  307. view.forceRender();
  308. const domP = domEditor.childNodes[ 2 ];
  309. domP.appendChild( document.createElement( 'br' ) );
  310. mutationObserver.flush();
  311. expect( lastMutations ).to.be.null;
  312. } );
  313. it( 'should ignore mutation with bogus br inserted on the end of the paragraph with text', () => {
  314. viewRoot._appendChild( parse( '<container:p>foo</container:p>', { stylesProcessor } ) );
  315. view.forceRender();
  316. const domP = domEditor.childNodes[ 2 ];
  317. domP.appendChild( document.createElement( 'br' ) );
  318. mutationObserver.flush();
  319. expect( lastMutations ).to.be.null;
  320. } );
  321. it( 'should ignore mutation with bogus br inserted on the end of the paragraph while processing text mutations', () => {
  322. viewRoot._appendChild( parse( '<container:p>foo</container:p>', { stylesProcessor } ) );
  323. view.forceRender();
  324. const domP = domEditor.childNodes[ 2 ];
  325. domP.childNodes[ 0 ].data = 'foo ';
  326. domP.appendChild( document.createElement( 'br' ) );
  327. mutationObserver.flush();
  328. expect( lastMutations.length ).to.equal( 1 );
  329. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  330. expect( lastMutations[ 0 ].newText ).to.equal( 'foo ' );
  331. } );
  332. it( 'should ignore child mutations which resulted in no changes – when element contains elements', () => {
  333. viewRoot._appendChild( parse( '<container:p><container:x></container:x></container:p>', { stylesProcessor } ) );
  334. view.forceRender();
  335. const domP = domEditor.childNodes[ 2 ];
  336. const domY = document.createElement( 'y' );
  337. domP.appendChild( domY );
  338. domY.remove();
  339. mutationObserver.flush();
  340. expect( lastMutations ).to.be.null;
  341. } );
  342. // This case is more tricky than the previous one because DOMConverter will return a different
  343. // instances of view text nodes every time it converts a DOM text node.
  344. it( 'should ignore child mutations which resulted in no changes – when element contains text nodes', () => {
  345. const domP = domEditor.childNodes[ 0 ];
  346. const domText = document.createTextNode( 'x' );
  347. domP.appendChild( domText );
  348. domText.remove();
  349. const domP2 = domEditor.childNodes[ 1 ];
  350. domP2.appendChild( document.createTextNode( 'x' ) );
  351. mutationObserver.flush();
  352. // There was only P2 change. P1 must be ignored.
  353. const viewP2 = viewRoot.getChild( 1 );
  354. expect( lastMutations.length ).to.equal( 1 );
  355. expect( lastMutations[ 0 ].node ).to.equal( viewP2 );
  356. } );
  357. it( 'should not ignore mutation with br inserted not on the end of the paragraph', () => {
  358. viewRoot._appendChild( parse( '<container:p>foo</container:p>', { stylesProcessor } ) );
  359. view.forceRender();
  360. const domP = domEditor.childNodes[ 2 ];
  361. domP.insertBefore( document.createElement( 'br' ), domP.childNodes[ 0 ] );
  362. mutationObserver.flush();
  363. expect( lastMutations.length ).to.equal( 1 );
  364. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  365. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'br' );
  366. expect( lastMutations[ 0 ].newChildren[ 1 ].data ).to.equal( 'foo' );
  367. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  368. } );
  369. it( 'should not ignore mutation inserting element different than br on the end of the empty paragraph', () => {
  370. viewRoot._appendChild( parse( '<container:p></container:p>', { stylesProcessor } ) );
  371. view.forceRender();
  372. const domP = domEditor.childNodes[ 2 ];
  373. domP.appendChild( document.createElement( 'span' ) );
  374. mutationObserver.flush();
  375. expect( lastMutations.length ).to.equal( 1 );
  376. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  377. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'span' );
  378. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  379. } );
  380. it( 'should not ignore mutation inserting element different than br on the end of the paragraph with text', () => {
  381. viewRoot._appendChild( parse( '<container:p>foo</container:p>', { stylesProcessor } ) );
  382. view.forceRender();
  383. const domP = domEditor.childNodes[ 2 ];
  384. domP.appendChild( document.createElement( 'span' ) );
  385. mutationObserver.flush();
  386. expect( lastMutations.length ).to.equal( 1 );
  387. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  388. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  389. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'span' );
  390. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  391. } );
  392. describe( 'UIElement integration', () => {
  393. const renderStub = sinon.stub();
  394. function createUIElement( name ) {
  395. const element = new UIElement( name );
  396. element.render = function( domDocument ) {
  397. const root = this.toDomElement( domDocument );
  398. root.innerHTML = 'foo bar';
  399. return root;
  400. };
  401. return element;
  402. }
  403. beforeEach( () => {
  404. const uiElement = createUIElement( 'div' );
  405. viewRoot._appendChild( uiElement );
  406. view.forceRender();
  407. renderStub.reset();
  408. view.on( 'render', renderStub );
  409. } );
  410. it( 'should not collect text mutations from UIElement', () => {
  411. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  412. mutationObserver.flush();
  413. expect( lastMutations ).to.be.null;
  414. } );
  415. it( 'should not cause a render from UIElement', () => {
  416. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  417. mutationObserver.flush();
  418. expect( renderStub.callCount ).to.be.equal( 0 );
  419. } );
  420. it( 'should not collect child mutations from UIElement', () => {
  421. const span = document.createElement( 'span' );
  422. domEditor.childNodes[ 2 ].appendChild( span );
  423. mutationObserver.flush();
  424. expect( lastMutations ).to.be.null;
  425. } );
  426. it( 'should not cause a render when UIElement gets a child', () => {
  427. const span = document.createElement( 'span' );
  428. domEditor.childNodes[ 2 ].appendChild( span );
  429. mutationObserver.flush();
  430. expect( renderStub.callCount ).to.be.equal( 0 );
  431. } );
  432. } );
  433. function expectDomEditorNotToChange() {
  434. expect( domEditor.childNodes.length ).to.equal( 2 );
  435. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  436. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  437. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  438. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  439. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  440. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  441. }
  442. } );