8
0

mutationobserver.js 20 KB

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