100.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import Bold from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
  8. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
  9. import LinkEngine from '@ckeditor/ckeditor5-link/src/linkengine';
  10. import Input from '../../src/input';
  11. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  12. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  13. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  14. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  15. import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  16. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  17. import MutationObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mutationobserver';
  18. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  20. /* global document */
  21. // NOTE: In all these tests we need to simulate the mutations. However, it's really tricky to tell what
  22. // should be in "newChildren" because we don't have yet access to these nodes. We pass new instances,
  23. // but this means that DomConverter which is used somewhere internally may return a different instance
  24. // (which wouldn't happen in practice because it'd cache it). Besides, it's really hard to tell if the
  25. // browser will keep the instances of the old elements when modifying the tree when the user is typing
  26. // or if it will create new instances itself too.
  27. // However, the code handling these mutations doesn't really care what's inside new/old children. It
  28. // just needs the mutations common ancestor to understand how big fragment of the tree has changed.
  29. describe( 'Bug ckeditor5-typing#100', () => {
  30. let domElement, domRoot, editor, model, view, viewDocument, viewRoot;
  31. beforeEach( () => {
  32. domElement = document.createElement( 'div' );
  33. document.body.appendChild( domElement );
  34. return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, LinkEngine ] } )
  35. .then( newEditor => {
  36. editor = newEditor;
  37. model = editor.model;
  38. view = editor.editing.view;
  39. viewDocument = view.document;
  40. viewRoot = viewDocument.getRoot();
  41. domRoot = view.getDomRoot();
  42. // Mock image feature.
  43. newEditor.model.schema.register( 'image', { allowWhere: '$text' } );
  44. editor.conversion.for( 'downcast' ).add( downcastElementToElement( {
  45. model: 'image',
  46. view: 'img'
  47. } ) );
  48. editor.conversion.for( 'upcast' ).add( upcastElementToElement( {
  49. view: 'img',
  50. model: 'image'
  51. } ) );
  52. // Disable MO completely and in a way it won't be reenabled on some Document#render() call.
  53. const mutationObserver = view.getObserver( MutationObserver );
  54. mutationObserver.disable();
  55. mutationObserver.enable = () => {};
  56. } );
  57. } );
  58. afterEach( () => {
  59. domElement.remove();
  60. return editor.destroy();
  61. } );
  62. // This happens when browser automatically switches parent and child nodes.
  63. it( 'should handle mutations switching inner and outer node when adding new text node after', () => {
  64. setModelData( model,
  65. '<paragraph>' +
  66. '<$text italic="true" linkHref="foo">' +
  67. 'text[]' +
  68. '</$text>' +
  69. '</paragraph>'
  70. );
  71. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>text{}</i></a></p>' );
  72. const paragraph = viewRoot.getChild( 0 );
  73. const link = paragraph.getChild( 0 );
  74. const italic = link.getChild( 0 );
  75. const text = italic.getChild( 0 );
  76. // Simulate mutations and DOM change.
  77. domRoot.childNodes[ 0 ].innerHTML = '<i><a href="foo">text</a>x</i>';
  78. viewDocument.fire( 'mutations', [
  79. // First mutation - remove all children from link element.
  80. {
  81. type: 'children',
  82. node: link,
  83. oldChildren: [ italic ],
  84. newChildren: []
  85. },
  86. // Second mutation - remove link from paragraph and put italic there.
  87. {
  88. type: 'children',
  89. node: paragraph,
  90. oldChildren: [ link ],
  91. newChildren: [ new ViewElement( 'i' ) ]
  92. },
  93. // Third mutation - italic's new children.
  94. {
  95. type: 'children',
  96. node: italic,
  97. oldChildren: [ text ],
  98. newChildren: [ new ViewElement( 'a', null, text.clone() ), new ViewText( 'x' ) ]
  99. }
  100. ] );
  101. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>textx{}</i></a></p>' );
  102. } );
  103. it( 'should handle mutations switching inner and outer node when adding new text node before', () => {
  104. setModelData( model,
  105. '<paragraph>' +
  106. '<$text italic="true" linkHref="foo">' +
  107. '[]text' +
  108. '</$text>' +
  109. '</paragraph>'
  110. );
  111. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>{}text</i></a></p>' );
  112. const paragraph = viewRoot.getChild( 0 );
  113. const link = paragraph.getChild( 0 );
  114. const italic = link.getChild( 0 );
  115. const text = italic.getChild( 0 );
  116. // Simulate mutations and DOM change.
  117. domRoot.childNodes[ 0 ].innerHTML = '<i>x<a href="foo">text</a></i>';
  118. viewDocument.fire( 'mutations', [
  119. // First mutation - remove all children from link element.
  120. {
  121. type: 'children',
  122. node: link,
  123. oldChildren: [ italic ],
  124. newChildren: []
  125. },
  126. // Second mutation - remove link from paragraph and put italic there.
  127. {
  128. type: 'children',
  129. node: paragraph,
  130. oldChildren: [ link ],
  131. newChildren: [ new ViewElement( 'i' ) ]
  132. },
  133. // Third mutation - italic's new children.
  134. {
  135. type: 'children',
  136. node: italic,
  137. oldChildren: [ text ],
  138. newChildren: [ new ViewText( 'x' ), new ViewElement( 'a', null, 'text' ) ]
  139. }
  140. ] );
  141. expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>x{}text</i></a></p>' );
  142. } );
  143. it( 'should handle mutations switching inner and outer node - with text before', () => {
  144. setModelData( model,
  145. '<paragraph>' +
  146. 'xxx<$text italic="true" linkHref="foo">' +
  147. 'text[]' +
  148. '</$text>' +
  149. '</paragraph>'
  150. );
  151. expect( getViewData( view ) ).to.equal( '<p>xxx<a href="foo"><i>text{}</i></a></p>' );
  152. const paragraph = viewRoot.getChild( 0 );
  153. const textBefore = paragraph.getChild( 0 );
  154. const link = paragraph.getChild( 1 );
  155. const italic = link.getChild( 0 );
  156. const text = italic.getChild( 0 );
  157. // Simulate mutations and DOM change.
  158. domRoot.childNodes[ 0 ].innerHTML = 'xxx<i><a href="foo">text</a>x</i>';
  159. viewDocument.fire( 'mutations', [
  160. // First mutation - remove all children from link element.
  161. {
  162. type: 'children',
  163. node: link,
  164. oldChildren: [ italic ],
  165. newChildren: []
  166. },
  167. // Second mutation - remove link from paragraph and put italic there.
  168. {
  169. type: 'children',
  170. node: paragraph,
  171. oldChildren: [ textBefore, link ],
  172. newChildren: [ new ViewText( 'xxx' ), new ViewElement( 'i' ) ]
  173. },
  174. // Third mutation - italic's new children.
  175. {
  176. type: 'children',
  177. node: italic,
  178. oldChildren: [ text ],
  179. newChildren: [ new ViewElement( 'a', null, 'text' ), new ViewText( 'x' ) ]
  180. }
  181. ] );
  182. expect( getViewData( view ) ).to.equal( '<p>xxx<a href="foo"><i>textx{}</i></a></p>' );
  183. } );
  184. // This happens when spell checker is applied on <strong> element and changes it to <b>.
  185. it( 'should handle mutations replacing node', () => {
  186. setModelData( model,
  187. '<paragraph>' +
  188. '<$text bold="true">' +
  189. 'text[]' +
  190. '</$text>' +
  191. '</paragraph>'
  192. );
  193. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  194. const paragraph = viewRoot.getChild( 0 );
  195. const strong = paragraph.getChild( 0 );
  196. // Simulate mutations and DOM change.
  197. domRoot.childNodes[ 0 ].innerHTML = '<b>fixed text</b>';
  198. viewDocument.fire( 'mutations', [
  199. // Replace `<strong>` with `<b>`.
  200. {
  201. type: 'children',
  202. node: paragraph,
  203. oldChildren: [ strong ],
  204. newChildren: [ new ViewElement( 'b', null, 'fixed text' ) ]
  205. }
  206. ] );
  207. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>fixed text</strong></p>' );
  208. } );
  209. // Spell checker splits text inside attributes to two text nodes.
  210. it( 'should handle mutations inside attribute element', () => {
  211. setModelData( model,
  212. '<paragraph>' +
  213. '<$text bold="true">' +
  214. 'this is foo text[]' +
  215. '</$text>' +
  216. '</paragraph>'
  217. );
  218. expect( getViewData( view ) ).to.equal( '<p><strong>this is foo text{}</strong></p>' );
  219. const paragraph = viewRoot.getChild( 0 );
  220. const strong = paragraph.getChild( 0 );
  221. const text = strong.getChild( 0 );
  222. // Simulate mutations and DOM change.
  223. domRoot.childNodes[ 0 ].childNodes[ 0 ].innerHTML = 'this is bar text';
  224. viewDocument.fire( 'mutations', [
  225. {
  226. type: 'children',
  227. node: strong,
  228. oldChildren: [ text ],
  229. newChildren: [ new ViewText( 'this is bar' ), new ViewText( ' text' ) ]
  230. }
  231. ] );
  232. expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>this is bar text</strong></p>' );
  233. } );
  234. it( 'should do nothing if elements mutated', () => {
  235. setModelData( model,
  236. '<paragraph>' +
  237. '<$text bold="true">' +
  238. 'text[]' +
  239. '</$text>' +
  240. '</paragraph>'
  241. );
  242. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  243. const paragraph = viewRoot.getChild( 0 );
  244. const strong = paragraph.getChild( 0 );
  245. // Simulate mutations and DOM change.
  246. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong><img />';
  247. viewDocument.fire( 'mutations', [
  248. {
  249. type: 'children',
  250. node: paragraph,
  251. oldChildren: [ strong ],
  252. newChildren: [
  253. new ViewElement( 'strong', null, new ViewText( 'text' ) ),
  254. new ViewElement( 'img' )
  255. ]
  256. }
  257. ] );
  258. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  259. } );
  260. it( 'should do nothing if text is not changed', () => {
  261. setModelData( model,
  262. '<paragraph>' +
  263. '<$text bold="true">' +
  264. 'text[]' +
  265. '</$text>' +
  266. '</paragraph>'
  267. );
  268. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  269. const paragraph = viewRoot.getChild( 0 );
  270. const strong = paragraph.getChild( 0 );
  271. // Simulate mutations and DOM change.
  272. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  273. viewDocument.fire( 'mutations', [
  274. {
  275. type: 'children',
  276. node: paragraph,
  277. oldChildren: [ strong ],
  278. newChildren: [ new ViewElement( 'strong', null, new ViewText( 'text' ) ) ]
  279. }
  280. ] );
  281. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  282. } );
  283. it( 'should do nothing on empty mutations', () => {
  284. setModelData( model,
  285. '<paragraph>' +
  286. '<$text bold="true">' +
  287. 'text[]' +
  288. '</$text>' +
  289. '</paragraph>'
  290. );
  291. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  292. // Simulate mutations and DOM change.
  293. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  294. viewDocument.fire( 'mutations', [] );
  295. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  296. } );
  297. it( 'should do nothing if mutations does not have common ancestor', () => {
  298. setModelData( model,
  299. '<paragraph>' +
  300. '<$text bold="true">' +
  301. 'text[]' +
  302. '</$text>' +
  303. '</paragraph>'
  304. );
  305. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  306. const paragraph = viewRoot.getChild( 0 );
  307. const strong = paragraph.getChild( 0 );
  308. // Simulate mutations and DOM change.
  309. domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
  310. viewDocument.fire( 'mutations', [
  311. {
  312. type: 'children',
  313. node: paragraph,
  314. oldChildren: [ strong ],
  315. newChildren: [ strong ]
  316. },
  317. {
  318. type: 'children',
  319. node: new ViewContainerElement( 'div' ),
  320. oldChildren: [],
  321. newChildren: [ new ViewText( 'foo' ), new ViewText( 'bar' ) ]
  322. }
  323. ] );
  324. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  325. } );
  326. it( 'should handle view selection if one is returned from mutations', () => {
  327. setModelData( model,
  328. '<paragraph>' +
  329. '<$text bold="true">' +
  330. 'text[]' +
  331. '</$text>' +
  332. '</paragraph>'
  333. );
  334. expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
  335. const paragraph = viewRoot.getChild( 0 );
  336. const strong = paragraph.getChild( 0 );
  337. const viewSelection = new ViewSelection( paragraph, 0 );
  338. // Simulate mutations and DOM change.
  339. domRoot.childNodes[ 0 ].innerHTML = '<b>textx</b>';
  340. viewDocument.fire( 'mutations', [
  341. // Replace `<strong>` with `<b>`.
  342. {
  343. type: 'children',
  344. node: paragraph,
  345. oldChildren: [ strong ],
  346. newChildren: [ new ViewElement( 'b', null, new ViewText( 'textx' ) ) ]
  347. }
  348. ], viewSelection );
  349. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">[]textx</$text></paragraph>' );
  350. expect( getViewData( view ) ).to.equal( '<p><strong>{}textx</strong></p>' );
  351. } );
  352. // #117.
  353. it( 'should handle mixed mutations', () => {
  354. setModelData( model, '<paragraph>[]<$text bold="true">Foo bar aple</$text></paragraph>' );
  355. const paragraph = viewRoot.getChild( 0 );
  356. const strong = paragraph.getChild( 0 );
  357. const viewSelection = new ViewSelection( paragraph );
  358. // Simulate mutations and DOM change.
  359. domRoot.childNodes[ 0 ].innerHTML = '<strong>Foo bar </strong><b>apple</b>';
  360. viewDocument.fire( 'mutations', [
  361. {
  362. type: 'text',
  363. oldText: 'Foo bar aple',
  364. newText: 'Foo bar ',
  365. node: viewRoot.getChild( 0 ).getChild( 0 )
  366. },
  367. {
  368. type: 'children',
  369. oldChildren: [ strong ],
  370. newChildren: [ strong, new ViewElement( 'b', null, new ViewText( 'apple' ) ) ],
  371. node: paragraph
  372. }
  373. ], viewSelection );
  374. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">[]Foo bar apple</$text></paragraph>' );
  375. expect( getViewData( view ) ).to.equal( '<p><strong>{}Foo bar apple</strong></p>' );
  376. } );
  377. } );