8
0

renderer.js 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, window, NodeFilter */
  6. import View from '../../src/view/view';
  7. import ViewElement from '../../src/view/element';
  8. import ViewContainerElement from '../../src/view/containerelement';
  9. import ViewAttributeElement from '../../src/view/attributeelement';
  10. import ViewText from '../../src/view/text';
  11. import ViewRange from '../../src/view/range';
  12. import ViewPosition from '../../src/view/position';
  13. import DocumentSelection from '../../src/view/documentselection';
  14. import DomConverter from '../../src/view/domconverter';
  15. import Renderer from '../../src/view/renderer';
  16. import DocumentFragment from '../../src/view/documentfragment';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import { parse, setData as setViewData, getData as getViewData } from '../../src/dev-utils/view';
  19. import { INLINE_FILLER, INLINE_FILLER_LENGTH, isBlockFiller, BR_FILLER } from '../../src/view/filler';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. import createViewRoot from './_utils/createroot';
  22. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  23. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  24. testUtils.createSinonSandbox();
  25. describe( 'Renderer', () => {
  26. let selection, domConverter, renderer;
  27. beforeEach( () => {
  28. selection = new DocumentSelection();
  29. domConverter = new DomConverter();
  30. renderer = new Renderer( domConverter, selection );
  31. renderer.domDocuments.add( document );
  32. } );
  33. describe( 'markToSync', () => {
  34. let viewRoot;
  35. beforeEach( () => {
  36. viewRoot = new ViewElement( 'p' );
  37. const domRoot = document.createElement( 'p' );
  38. domConverter.bindElements( domRoot, viewRoot );
  39. viewRoot._appendChild( new ViewText( 'foo' ) );
  40. renderer.markedTexts.clear();
  41. renderer.markedAttributes.clear();
  42. renderer.markedChildren.clear();
  43. } );
  44. it( 'should mark attributes which need update', () => {
  45. viewRoot._setAttribute( 'class', 'foo' );
  46. renderer.markToSync( 'attributes', viewRoot );
  47. expect( renderer.markedAttributes.has( viewRoot ) ).to.be.true;
  48. } );
  49. it( 'should mark children which need update', () => {
  50. viewRoot._appendChild( new ViewText( 'foo' ) );
  51. renderer.markToSync( 'children', viewRoot );
  52. expect( renderer.markedChildren.has( viewRoot ) ).to.be.true;
  53. } );
  54. it( 'should not mark children if element has no corresponding node', () => {
  55. // Overwrite viewRoot with node without coresponding DOM node.
  56. viewRoot = new ViewElement( 'p' );
  57. viewRoot._appendChild( new ViewText( 'foo' ) );
  58. renderer.markToSync( 'children', viewRoot );
  59. expect( renderer.markedTexts.has( viewRoot ) ).to.be.false;
  60. } );
  61. it( 'should mark text which need update', () => {
  62. const viewText = new ViewText( 'foo' );
  63. viewRoot._appendChild( viewText );
  64. viewText._data = 'bar';
  65. renderer.markToSync( 'text', viewText );
  66. expect( renderer.markedTexts.has( viewText ) ).to.be.true;
  67. } );
  68. it( 'should not mark text if parent has no corresponding node', () => {
  69. const viewText = new ViewText( 'foo' );
  70. // Overwrite viewRoot with node without coresponding DOM node.
  71. viewRoot = new ViewElement( 'p' );
  72. viewRoot._appendChild( viewText );
  73. viewText._data = 'bar';
  74. renderer.markToSync( 'text', viewText );
  75. expect( renderer.markedTexts.has( viewText ) ).to.be.false;
  76. } );
  77. it( 'should throw if the type is unknown', () => {
  78. expect( () => {
  79. renderer.markToSync( 'UNKNOWN', viewRoot );
  80. } ).to.throw( CKEditorError, /^view-renderer-unknown-type/ );
  81. } );
  82. } );
  83. describe( 'render', () => {
  84. let viewRoot, domRoot, selectionEditable;
  85. beforeEach( () => {
  86. viewRoot = new ViewElement( 'div' );
  87. domRoot = document.createElement( 'div' );
  88. document.body.appendChild( domRoot );
  89. domConverter.bindElements( domRoot, viewRoot );
  90. renderer.markedTexts.clear();
  91. renderer.markedAttributes.clear();
  92. renderer.markedChildren.clear();
  93. selection._setTo( null );
  94. selectionEditable = viewRoot;
  95. renderer.isFocused = true;
  96. // Fake selection editable - it is needed to render selection properly.
  97. Object.defineProperty( selection, 'editableElement', {
  98. get() {
  99. return selectionEditable;
  100. }
  101. } );
  102. } );
  103. afterEach( () => {
  104. domRoot.remove();
  105. } );
  106. it( 'should update attributes', () => {
  107. viewRoot._setAttribute( 'class', 'foo' );
  108. renderer.markToSync( 'attributes', viewRoot );
  109. renderer.render();
  110. expect( domRoot.getAttribute( 'class' ) ).to.equal( 'foo' );
  111. expect( renderer.markedAttributes.size ).to.equal( 0 );
  112. } );
  113. it( 'should remove attributes', () => {
  114. viewRoot._setAttribute( 'class', 'foo' );
  115. domRoot.setAttribute( 'id', 'bar' );
  116. domRoot.setAttribute( 'class', 'bar' );
  117. renderer.markToSync( 'attributes', viewRoot );
  118. renderer.render();
  119. expect( domRoot.getAttribute( 'class' ) ).to.equal( 'foo' );
  120. expect( domRoot.getAttribute( 'id' ) ).to.be.not.ok;
  121. expect( renderer.markedAttributes.size ).to.equal( 0 );
  122. } );
  123. it( 'should add children', () => {
  124. viewRoot._appendChild( new ViewText( 'foo' ) );
  125. renderer.markToSync( 'children', viewRoot );
  126. renderer.render();
  127. expect( domRoot.childNodes.length ).to.equal( 1 );
  128. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  129. expect( renderer.markedChildren.size ).to.equal( 0 );
  130. } );
  131. it( 'should remove children', () => {
  132. viewRoot._appendChild( new ViewText( 'foo' ) );
  133. renderer.markToSync( 'children', viewRoot );
  134. renderer.render();
  135. expect( domRoot.childNodes.length ).to.equal( 1 );
  136. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  137. viewRoot._removeChildren( 0, 1 );
  138. renderer.markToSync( 'children', viewRoot );
  139. renderer.render();
  140. expect( domRoot.childNodes.length ).to.equal( 0 );
  141. expect( renderer.markedChildren.size ).to.equal( 0 );
  142. } );
  143. it( 'should update text', () => {
  144. const viewText = new ViewText( 'foo' );
  145. viewRoot._appendChild( viewText );
  146. renderer.markToSync( 'children', viewRoot );
  147. renderer.render();
  148. expect( domRoot.childNodes.length ).to.equal( 1 );
  149. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  150. viewText._data = 'bar';
  151. renderer.markToSync( 'text', viewText );
  152. renderer.render();
  153. expect( domRoot.childNodes.length ).to.equal( 1 );
  154. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'bar' );
  155. expect( renderer.markedTexts.size ).to.equal( 0 );
  156. } );
  157. it( 'should not update same text', () => {
  158. const viewText = new ViewText( 'foo' );
  159. viewRoot._appendChild( viewText );
  160. renderer.markToSync( 'children', viewRoot );
  161. renderer.render();
  162. expect( domRoot.childNodes.length ).to.equal( 1 );
  163. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  164. viewText._data = 'foo';
  165. renderer.markToSync( 'text', viewText );
  166. renderAndExpectNoChanges( renderer, domRoot );
  167. expect( domRoot.childNodes.length ).to.equal( 1 );
  168. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  169. expect( renderer.markedTexts.size ).to.equal( 0 );
  170. } );
  171. it( 'should not update text parent child list changed', () => {
  172. const viewImg = new ViewElement( 'img' );
  173. const viewText = new ViewText( 'foo' );
  174. viewRoot._appendChild( [ viewImg, viewText ] );
  175. renderer.markToSync( 'children', viewRoot );
  176. renderer.markToSync( 'text', viewText );
  177. renderer.render();
  178. expect( domRoot.childNodes.length ).to.equal( 2 );
  179. expect( domRoot.childNodes[ 0 ].tagName ).to.equal( 'IMG' );
  180. expect( domRoot.childNodes[ 1 ].data ).to.equal( 'foo' );
  181. } );
  182. it( 'should not change text if it is the same during text rendering', () => {
  183. const viewText = new ViewText( 'foo' );
  184. viewRoot._appendChild( viewText );
  185. renderer.markToSync( 'children', viewRoot );
  186. renderer.render();
  187. // This should not be changed during the render.
  188. const domText = domRoot.childNodes[ 0 ];
  189. renderer.markToSync( 'text', viewText );
  190. renderer.render();
  191. expect( domRoot.childNodes.length ).to.equal( 1 );
  192. expect( domRoot.childNodes[ 0 ] ).to.equal( domText );
  193. } );
  194. it( 'should not change text if it is the same during children rendering', () => {
  195. const viewText = new ViewText( 'foo' );
  196. viewRoot._appendChild( viewText );
  197. renderer.markToSync( 'children', viewRoot );
  198. renderer.render();
  199. // This should not be changed during the render.
  200. const domText = domRoot.childNodes[ 0 ];
  201. renderer.markToSync( 'children', viewRoot );
  202. renderer.render();
  203. expect( domRoot.childNodes.length ).to.equal( 1 );
  204. expect( domRoot.childNodes[ 0 ] ).to.equal( domText );
  205. } );
  206. it( 'should not change element if it is the same', () => {
  207. const viewImg = new ViewElement( 'img' );
  208. viewRoot._appendChild( viewImg );
  209. // This should not be changed during the render.
  210. const domImg = document.createElement( 'img' );
  211. domRoot.appendChild( domImg );
  212. domConverter.bindElements( domImg, viewImg );
  213. renderer.markToSync( 'children', viewRoot );
  214. renderer.render();
  215. expect( domRoot.childNodes.length ).to.equal( 1 );
  216. expect( domRoot.childNodes[ 0 ] ).to.equal( domImg );
  217. } );
  218. it( 'should change element if it is different', () => {
  219. const viewImg = new ViewElement( 'img' );
  220. viewRoot._appendChild( viewImg );
  221. renderer.markToSync( 'children', viewRoot );
  222. renderer.render();
  223. const viewP = new ViewElement( 'p' );
  224. viewRoot._removeChildren( 0, 1 );
  225. viewRoot._appendChild( viewP );
  226. renderer.markToSync( 'children', viewRoot );
  227. renderer.render();
  228. expect( domRoot.childNodes.length ).to.equal( 1 );
  229. expect( domRoot.childNodes[ 0 ].tagName ).to.equal( 'P' );
  230. } );
  231. it( 'should update removed item when it is reinserted', () => {
  232. const viewFoo = new ViewText( 'foo' );
  233. const viewP = new ViewElement( 'p', null, viewFoo );
  234. const viewDiv = new ViewElement( 'div', null, viewP );
  235. viewRoot._appendChild( viewDiv );
  236. renderer.markToSync( 'children', viewRoot );
  237. renderer.render();
  238. viewDiv._removeChildren( 0, 1 );
  239. renderer.markToSync( 'children', viewDiv );
  240. renderer.render();
  241. viewP._removeChildren( 0, 1 );
  242. viewDiv._appendChild( viewP );
  243. renderer.markToSync( 'children', viewDiv );
  244. renderer.render();
  245. expect( domRoot.childNodes.length ).to.equal( 1 );
  246. const domDiv = domRoot.childNodes[ 0 ];
  247. expect( domDiv.tagName ).to.equal( 'DIV' );
  248. expect( domDiv.childNodes.length ).to.equal( 1 );
  249. const domP = domDiv.childNodes[ 0 ];
  250. expect( domP.tagName ).to.equal( 'P' );
  251. expect( domP.childNodes.length ).to.equal( 0 );
  252. } );
  253. it( 'should update removed item when it is reinserted #2', () => {
  254. // Prepare view: root -> div "outer" -> div "inner" -> p.
  255. const viewP = new ViewElement( 'p' );
  256. const viewDivInner = new ViewElement( 'div', null, viewP );
  257. const viewDivOuter = new ViewElement( 'div', null, viewDivInner );
  258. viewRoot._appendChild( viewDivOuter );
  259. // Render view tree to DOM.
  260. renderer.markToSync( 'children', viewRoot );
  261. renderer.render();
  262. // Remove div "outer" from root and render it.
  263. viewDivOuter._remove();
  264. renderer.markToSync( 'children', viewRoot );
  265. renderer.render();
  266. // Remove p from div "child" -- div "inner" won't be marked because it is in document fragment not view root.
  267. viewP._remove();
  268. // Add div "outer" back to root.
  269. viewRoot._appendChild( viewDivOuter );
  270. renderer.markToSync( 'children', viewRoot );
  271. // Render changes, view is: root -> div "outer" -> div "inner".
  272. renderer.render();
  273. // Same is expected in DOM.
  274. expect( domRoot.childNodes.length ).to.equal( 1 );
  275. const domDivOuter = domRoot.childNodes[ 0 ];
  276. expect( renderer.domConverter.viewToDom( viewDivOuter, domRoot.document ) ).to.equal( domDivOuter );
  277. expect( domDivOuter.tagName ).to.equal( 'DIV' );
  278. expect( domDivOuter.childNodes.length ).to.equal( 1 );
  279. const domDivInner = domDivOuter.childNodes[ 0 ];
  280. expect( renderer.domConverter.viewToDom( viewDivInner, domRoot.document ) ).to.equal( domDivInner );
  281. expect( domDivInner.tagName ).to.equal( 'DIV' );
  282. expect( domDivInner.childNodes.length ).to.equal( 0 );
  283. } );
  284. it( 'should not throw when trying to update children of view element that got removed and lost its binding', () => {
  285. const viewFoo = new ViewText( 'foo' );
  286. const viewP = new ViewElement( 'p', null, viewFoo );
  287. const viewDiv = new ViewElement( 'div', null, viewP );
  288. viewRoot._appendChild( viewDiv );
  289. renderer.markToSync( 'children', viewRoot );
  290. renderer.render();
  291. viewRoot._removeChildren( 0, 1 );
  292. renderer.markToSync( 'children', viewRoot );
  293. viewDiv._removeChildren( 0, 1 );
  294. renderer.markToSync( 'children', viewDiv );
  295. viewP._removeChildren( 0, 1 );
  296. renderer.markToSync( 'children', viewP );
  297. renderer.render();
  298. expect( domRoot.childNodes.length ).to.equal( 0 );
  299. } );
  300. it( 'should not care about filler if there is no DOM', () => {
  301. selectionEditable = null;
  302. const { view: viewP, selection: newSelection } = parse(
  303. '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  304. const viewRoot = new ViewElement( 'p' );
  305. viewRoot._appendChild( viewP );
  306. selection._setTo( newSelection );
  307. renderer.markToSync( 'children', viewRoot );
  308. renderer.render();
  309. // Expect no error on render.
  310. expect( viewRoot ).to.be.ok;
  311. } );
  312. it( 'should not add filler when inside contenteditable=false parent', () => {
  313. const { view: viewP, selection: newSelection } = parse(
  314. '<container:p>foo<attribute:b contenteditable="false">[]</attribute:b>bar</container:p>' );
  315. viewRoot._appendChild( viewP );
  316. selection._setTo( newSelection );
  317. renderer.markToSync( 'children', viewRoot );
  318. renderer.render();
  319. expect( domRoot.childNodes.length ).to.equal( 1 );
  320. expect( domRoot.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'p' );
  321. const domP = domRoot.childNodes[ 0 ];
  322. expect( domP.childNodes.length ).to.equal( 3 );
  323. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  324. expect( domP.childNodes[ 2 ].data ).to.equal( 'bar' );
  325. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  326. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 0 );
  327. } );
  328. it( 'should not add filler when inside contenteditable=false ancestor', () => {
  329. const { view: viewP, selection: newSelection } = parse(
  330. '<container:p contenteditable="false">foo<attribute:b>[]</attribute:b>bar</container:p>' );
  331. viewRoot._appendChild( viewP );
  332. selection._setTo( newSelection );
  333. renderer.markToSync( 'children', viewRoot );
  334. renderer.render();
  335. expect( domRoot.childNodes.length ).to.equal( 1 );
  336. expect( domRoot.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'p' );
  337. const domP = domRoot.childNodes[ 0 ];
  338. expect( domP.childNodes.length ).to.equal( 3 );
  339. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  340. expect( domP.childNodes[ 2 ].data ).to.equal( 'bar' );
  341. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  342. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 0 );
  343. } );
  344. it( 'should add and remove inline filler in case <p>foo<b>[]</b>bar</p>', () => {
  345. const domSelection = document.getSelection();
  346. // Step 1: <p>foo<b>"FILLER{}"</b></p>
  347. const { view: viewP, selection: newSelection } = parse(
  348. '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  349. viewRoot._appendChild( viewP );
  350. selection._setTo( newSelection );
  351. renderer.markToSync( 'children', viewRoot );
  352. renderer.render();
  353. expect( domRoot.childNodes.length ).to.equal( 1 );
  354. expect( domRoot.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'p' );
  355. const domP = domRoot.childNodes[ 0 ];
  356. expect( domP.childNodes.length ).to.equal( 3 );
  357. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  358. expect( domP.childNodes[ 2 ].data ).to.equal( 'bar' );
  359. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  360. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  361. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  362. expect( domSelection.rangeCount ).to.equal( 1 );
  363. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 1 ].childNodes[ 0 ] );
  364. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  365. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  366. // Step 2: No mutation on second render
  367. renderer.markToSync( 'children', viewRoot );
  368. renderer.markToSync( 'children', viewP );
  369. renderAndExpectNoChanges( renderer, domRoot );
  370. // Step 3: <p>foo{}<b></b></p>
  371. selection._setTo( ViewRange.createFromParentsAndOffsets( viewP.getChild( 0 ), 3, viewP.getChild( 0 ), 3 ) );
  372. renderer.render();
  373. expect( domP.childNodes.length ).to.equal( 3 );
  374. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  375. expect( domP.childNodes[ 2 ].data ).to.equal( 'bar' );
  376. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  377. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 0 );
  378. expect( domSelection.rangeCount ).to.equal( 1 );
  379. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  380. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 3 );
  381. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  382. // Step 4: No mutation on second render
  383. renderer.markToSync( 'children', viewRoot );
  384. renderer.markToSync( 'children', viewP );
  385. renderAndExpectNoChanges( renderer, domRoot );
  386. } );
  387. it( 'should add and remove inline filler in case <p>[]<b>foo</b></p>', () => {
  388. const domSelection = document.getSelection();
  389. // Step 1: <p>"FILLER{}"<b>foo</b></p>
  390. const { view: viewP, selection: newSelection } = parse(
  391. '<container:p>[]<attribute:b>foo</attribute:b></container:p>' );
  392. viewRoot._appendChild( viewP );
  393. selection._setTo( newSelection );
  394. renderer.markToSync( 'children', viewRoot );
  395. renderer.render();
  396. const domP = domRoot.childNodes[ 0 ];
  397. expect( domP.childNodes.length ).to.equal( 2 );
  398. expect( domP.childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  399. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  400. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  401. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  402. expect( domSelection.rangeCount ).to.equal( 1 );
  403. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  404. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  405. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  406. // Step 2: No mutation on second render
  407. renderer.markToSync( 'children', viewP );
  408. renderAndExpectNoChanges( renderer, domRoot );
  409. // Step 3: <p><b>{}foo</b></p>
  410. selection._setTo( ViewRange.createFromParentsAndOffsets(
  411. viewP.getChild( 0 ).getChild( 0 ), 0, viewP.getChild( 0 ).getChild( 0 ), 0 ) );
  412. renderer.render();
  413. expect( domP.childNodes.length ).to.equal( 1 );
  414. expect( domP.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  415. expect( domP.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  416. expect( domP.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  417. expect( domSelection.rangeCount ).to.equal( 1 );
  418. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ].childNodes[ 0 ] );
  419. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 0 );
  420. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  421. // Step 4: No mutation on second render
  422. renderer.markToSync( 'children', viewP );
  423. renderAndExpectNoChanges( renderer, domRoot );
  424. } );
  425. it( 'should add and remove inline filler in case <p><b>foo</b>[]</p>', () => {
  426. const domSelection = document.getSelection();
  427. // Step 1: <p>"FILLER{}"<b>foo</b></p>
  428. const { view: viewP, selection: newSelection } = parse(
  429. '<container:p><attribute:b>foo</attribute:b>[]</container:p>' );
  430. viewRoot._appendChild( viewP );
  431. selection._setTo( newSelection );
  432. renderer.markToSync( 'children', viewRoot );
  433. renderer.render();
  434. const domP = domRoot.childNodes[ 0 ];
  435. expect( domP.childNodes.length ).to.equal( 2 );
  436. expect( domP.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  437. expect( domP.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  438. expect( domP.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  439. expect( domP.childNodes[ 1 ].data ).to.equal( INLINE_FILLER );
  440. expect( domSelection.rangeCount ).to.equal( 1 );
  441. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 1 ] );
  442. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  443. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  444. // Step 2: No mutation on second render
  445. renderer.markToSync( 'children', viewP );
  446. renderAndExpectNoChanges( renderer, domRoot );
  447. // Step 3: <p><b>foo{}</b></p>
  448. selection._setTo( ViewRange.createFromParentsAndOffsets(
  449. viewP.getChild( 0 ).getChild( 0 ), 3, viewP.getChild( 0 ).getChild( 0 ), 3 ) );
  450. renderer.render();
  451. expect( domP.childNodes.length ).to.equal( 1 );
  452. expect( domP.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  453. expect( domP.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  454. expect( domP.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  455. expect( domSelection.rangeCount ).to.equal( 1 );
  456. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ].childNodes[ 0 ] );
  457. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 3 );
  458. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  459. // Step 4: No mutation on second render
  460. renderer.markToSync( 'children', viewP );
  461. renderAndExpectNoChanges( renderer, domRoot );
  462. } );
  463. it( 'should add and remove inline filler in case <p><b>foo</b>[]<b>bar</b></p>', () => {
  464. const domSelection = document.getSelection();
  465. const { view: viewP, selection: newSelection } = parse(
  466. '<container:p><attribute:b>foo</attribute:b>[]<attribute:b>bar</attribute:b></container:p>' );
  467. viewRoot._appendChild( viewP );
  468. selection._setTo( newSelection );
  469. renderer.markToSync( 'children', viewRoot );
  470. renderer.render();
  471. const domP = domRoot.childNodes[ 0 ];
  472. expect( domP.childNodes.length ).to.equal( 3 );
  473. expect( domP.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  474. expect( domP.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  475. expect( domP.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  476. expect( domP.childNodes[ 1 ].data ).to.equal( INLINE_FILLER );
  477. expect( domP.childNodes[ 2 ].tagName.toLowerCase() ).to.equal( 'b' );
  478. expect( domP.childNodes[ 2 ].childNodes.length ).to.equal( 1 );
  479. expect( domP.childNodes[ 2 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  480. expect( domSelection.rangeCount ).to.equal( 1 );
  481. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 1 ] );
  482. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  483. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  484. } );
  485. it( 'should move filler when selection is moved', () => {
  486. // Step 1: <p>foo<b>"FILLER{}"</b></p>
  487. const { view: viewP, selection: newSelection } = parse(
  488. '<container:p>foo<attribute:b>[]</attribute:b><attribute:i></attribute:i></container:p>' );
  489. viewRoot._appendChild( viewP );
  490. selection._setTo( newSelection );
  491. renderer.markToSync( 'children', viewRoot );
  492. renderer.render();
  493. const domP = domRoot.childNodes[ 0 ];
  494. expect( domP.childNodes.length ).to.equal( 3 );
  495. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  496. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  497. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  498. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  499. expect( domP.childNodes[ 2 ].tagName.toLowerCase() ).to.equal( 'i' );
  500. expect( domP.childNodes[ 2 ].childNodes.length ).to.equal( 0 );
  501. // Step 2: <p>foo<b></b><i>"FILLER{}"</i></p>
  502. const viewI = viewP.getChild( 2 );
  503. selection._setTo( ViewRange.createFromParentsAndOffsets( viewI, 0, viewI, 0 ) );
  504. renderer.render();
  505. expect( domP.childNodes.length ).to.equal( 3 );
  506. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  507. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  508. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 0 );
  509. expect( domP.childNodes[ 2 ].tagName.toLowerCase() ).to.equal( 'i' );
  510. expect( domP.childNodes[ 2 ].childNodes.length ).to.equal( 1 );
  511. expect( domP.childNodes[ 2 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  512. } );
  513. it( 'should remove filler when text is added and selection removed', () => {
  514. // Step 1: <p>foo<b>"FILLER{}"</b></p>
  515. const { view: viewP, selection: newSelection } = parse( '<container:p>foo<attribute:b>[]</attribute:b></container:p>' );
  516. const viewB = viewP.getChild( 1 );
  517. viewRoot._appendChild( viewP );
  518. selection._setTo( newSelection );
  519. renderer.markToSync( 'children', viewRoot );
  520. renderer.render();
  521. const domP = domRoot.childNodes[ 0 ];
  522. expect( domP.childNodes.length ).to.equal( 2 );
  523. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  524. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  525. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  526. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  527. // Step 2: Add text node.
  528. const viewText = new ViewText( 'x' );
  529. viewB._appendChild( viewText );
  530. selection._setTo( ViewRange.createFromParentsAndOffsets( viewText, 1, viewText, 1 ) );
  531. renderer.markToSync( 'children', viewB );
  532. renderer.render();
  533. // Step 3: Remove selection from the view.
  534. selection._setTo( null );
  535. renderer.render();
  536. expect( domP.childNodes.length ).to.equal( 2 );
  537. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  538. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  539. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  540. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'x' );
  541. } );
  542. // #659
  543. // The element before the filler is removed so the position of the filler
  544. // cannot be remembered as parent+offset.
  545. it( 'should remove filler from a modified DOM in case <p>bar<b>foo</b>[]</p>', () => {
  546. // Step 1: <p>bar<b>foo</b>"FILLER{}"</p>
  547. const { view: viewP, selection: newSelection } = parse( '<container:p>bar<attribute:b>foo</attribute:b>[]</container:p>' );
  548. viewRoot._appendChild( viewP );
  549. selection._setTo( newSelection );
  550. renderer.markToSync( 'children', viewRoot );
  551. renderer.render();
  552. const domP = domRoot.childNodes[ 0 ];
  553. expect( domP.childNodes.length ).to.equal( 3 );
  554. expect( domP.childNodes[ 2 ].data ).to.equal( INLINE_FILLER );
  555. // Step 2: Remove the <b> and update the selection (<p>bar[]</p>).
  556. viewP._removeChildren( 1 );
  557. selection._setTo( ViewRange.createFromParentsAndOffsets( viewP, 1, viewP, 1 ) );
  558. renderer.markToSync( 'children', viewP );
  559. renderer.render();
  560. // Step 3: Check whether there's no filler in the DOM.
  561. expect( domP.childNodes.length ).to.equal( 1 );
  562. expect( domP.childNodes[ 0 ].data ).to.equal( 'bar' );
  563. } );
  564. // #659
  565. it( 'should remove filler from a modified DOM when children moved', () => {
  566. // Step 1: <p><b>foo</b>"FILLER{}"<b>bar</b></p><p></p>
  567. const { view: viewFragment, selection: newSelection } = parse(
  568. '<container:p><attribute:b>foo</attribute:b>[]<attribute:b>bar</attribute:b></container:p><container:p></container:p>'
  569. );
  570. viewRoot._appendChild( viewFragment );
  571. selection._setTo( newSelection );
  572. renderer.markToSync( 'children', viewRoot );
  573. renderer.render();
  574. expect( domRoot.childNodes.length ).to.equal( 2 );
  575. const domP = domRoot.childNodes[ 0 ];
  576. const domP2 = domRoot.childNodes[ 1 ];
  577. expect( domP.childNodes.length ).to.equal( 3 );
  578. expect( domP.childNodes[ 1 ].data ).to.equal( INLINE_FILLER );
  579. // Step 2: Move <b>foo</b><b>bar</b> to the second paragraph and leave collapsed selection in the first one.
  580. // <p>[]</p><p><b>foo</b><b>bar</b></p>
  581. const viewP = viewRoot.getChild( 0 );
  582. const viewP2 = viewRoot.getChild( 1 );
  583. const removedChildren = viewP._removeChildren( 0, 2 );
  584. viewP2._appendChild( removedChildren );
  585. selection._setTo( ViewRange.createFromParentsAndOffsets( viewP, 0, viewP, 0 ) );
  586. renderer.markToSync( 'children', viewP );
  587. renderer.markToSync( 'children', viewP2 );
  588. renderer.render();
  589. // Step 3: Check whether in the first paragraph there's a <br> filler and that
  590. // in the second one there are two <b> tags.
  591. expect( domP.childNodes.length ).to.equal( 1 );
  592. expect( isBlockFiller( domP.childNodes[ 0 ], BR_FILLER ) ).to.be.true;
  593. expect( domP2.childNodes.length ).to.equal( 2 );
  594. expect( domP2.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  595. expect( domP2.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  596. } );
  597. // Test for an edge case in the _isSelectionInInlineFiller which can be triggered like
  598. // in one of ckeditor/ckeditor5-typing#59 automated tests.
  599. it( 'should not break when selection is moved to a new element, when filler exists', () => {
  600. // Step 1: <p>bar<b>"FILLER{}"</b></p>
  601. const { view: viewP, selection: newSelection } = parse( '<container:p>bar<attribute:b>[]</attribute:b></container:p>' );
  602. viewRoot._appendChild( viewP );
  603. selection._setTo( newSelection );
  604. renderer.markToSync( 'children', viewRoot );
  605. renderer.render();
  606. const domP = domRoot.childNodes[ 0 ];
  607. expect( domP.childNodes.length ).to.equal( 2 );
  608. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  609. // Step 2: Move selection to a new attribute element and remove the previous one
  610. viewP._removeChildren( 1 ); // Remove <b>.
  611. const viewI = parse( '<attribute:i></attribute:i>' );
  612. viewP._appendChild( viewI );
  613. selection._setTo( ViewRange.createFromParentsAndOffsets( viewI, 0, viewI, 0 ) );
  614. renderer.markToSync( 'children', viewP );
  615. renderer.render();
  616. // Step 3: Check whether new filler was created in the <i> element.
  617. expect( domP.childNodes.length ).to.equal( 2 );
  618. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'i' );
  619. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  620. } );
  621. // Test for an edge case in the _isSelectionInInlineFiller, when selection is before a view element
  622. // that has not been yet rendered/bound to DOM.
  623. it( 'should remove inline filler if selection is before a view element not bound to dom', () => {
  624. // Step 1: <p>bar<b>abc</b>"FILLER"{}</p>
  625. const { view: viewP, selection: newSelection } = parse( '<container:p>bar<attribute:b>abc</attribute:b>[]</container:p>' );
  626. viewRoot._appendChild( viewP );
  627. selection._setTo( newSelection );
  628. renderer.markToSync( 'children', viewRoot );
  629. renderer.render();
  630. const domP = domRoot.childNodes[ 0 ];
  631. expect( domP.childNodes.length ).to.equal( 3 );
  632. expect( domP.childNodes[ 2 ].data ).to.equal( INLINE_FILLER );
  633. // Step 2: Move selection to a new attribute element.
  634. const viewAbc = parse( 'abc' );
  635. viewP._appendChild( viewAbc );
  636. selection._setTo( ViewRange.createFromParentsAndOffsets( viewP, 3, viewP, 3 ) );
  637. renderer.markToSync( 'children', viewP );
  638. renderer.render();
  639. // Step 3: Check whether old filler was removed.
  640. expect( domP.childNodes.length ).to.equal( 3 );
  641. expect( domP.textContent.indexOf( INLINE_FILLER ) ).to.equal( -1 );
  642. } );
  643. it( 'should handle typing in empty block, do nothing if changes are already applied', () => {
  644. const domSelection = document.getSelection();
  645. const { view: viewP, selection: newSelection } = parse( '<container:p>[]</container:p>' );
  646. viewRoot._appendChild( viewP );
  647. selection._setTo( newSelection );
  648. renderer.markToSync( 'children', viewRoot );
  649. renderer.render();
  650. const domP = domRoot.childNodes[ 0 ];
  651. expect( domP.childNodes.length ).to.equal( 1 );
  652. expect( isBlockFiller( domP.childNodes[ 0 ], BR_FILLER ) ).to.be.true;
  653. expect( domSelection.rangeCount ).to.equal( 1 );
  654. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP );
  655. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 0 );
  656. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  657. // Remove filler and add text node to both DOM and View <p>x{}</p>
  658. domP.removeChild( domP.childNodes[ 0 ] );
  659. domP.appendChild( document.createTextNode( 'x' ) );
  660. domSelection.removeAllRanges();
  661. const domRange = document.createRange();
  662. domRange.setStart( domP.childNodes[ 0 ], 1 );
  663. domRange.collapse( true );
  664. domSelection.addRange( domRange );
  665. const viewText = new ViewText( 'x' );
  666. viewP._appendChild( viewText );
  667. selection._setTo( ViewRange.createFromParentsAndOffsets( viewText, 1, viewText, 1 ) );
  668. renderer.markToSync( 'children', viewP );
  669. renderAndExpectNoChanges( renderer, domRoot );
  670. } );
  671. it( 'should handle typing in empty block, render if needed', () => {
  672. const domSelection = document.getSelection();
  673. const { view: viewP, selection: newSelection } = parse( '<container:p>[]</container:p>' );
  674. viewRoot._appendChild( viewP );
  675. selection._setTo( newSelection );
  676. renderer.markToSync( 'children', viewRoot );
  677. renderer.render();
  678. const domP = domRoot.childNodes[ 0 ];
  679. expect( domP.childNodes.length ).to.equal( 1 );
  680. expect( isBlockFiller( domP.childNodes[ 0 ], BR_FILLER ) ).to.be.true;
  681. expect( domSelection.rangeCount ).to.equal( 1 );
  682. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP );
  683. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 0 );
  684. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  685. // Add text node only in View <p>x{}</p>
  686. const viewText = new ViewText( 'x' );
  687. viewP._appendChild( viewText );
  688. selection._setTo( ViewRange.createFromParentsAndOffsets( viewText, 1, viewText, 1 ) );
  689. renderer.markToSync( 'children', viewP );
  690. renderer.render();
  691. expect( domP.childNodes.length ).to.equal( 1 );
  692. expect( domP.childNodes[ 0 ].data ).to.equal( 'x' );
  693. expect( domSelection.rangeCount ).to.equal( 1 );
  694. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  695. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 1 );
  696. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  697. } );
  698. it( 'should handle removing last character', () => {
  699. const domSelection = document.getSelection();
  700. const { view: viewP, selection: newSelection } = parse( '<container:p>x{}</container:p>' );
  701. viewRoot._appendChild( viewP );
  702. selection._setTo( newSelection );
  703. renderer.markToSync( 'children', viewRoot );
  704. renderer.render();
  705. const domP = domRoot.childNodes[ 0 ];
  706. expect( domP.childNodes.length ).to.equal( 1 );
  707. expect( domP.childNodes[ 0 ].data ).to.equal( 'x' );
  708. expect( domSelection.rangeCount ).to.equal( 1 );
  709. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  710. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 1 );
  711. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  712. // Remove text and add filler to both DOM and View <p>{}</p>
  713. domP.removeChild( domP.childNodes[ 0 ] );
  714. domP.appendChild( BR_FILLER( document ) ); // eslint-disable-line new-cap
  715. domSelection.removeAllRanges();
  716. const domRange = document.createRange();
  717. domRange.setStart( domP.childNodes[ 0 ], 0 );
  718. domRange.collapse( true );
  719. domSelection.addRange( domRange );
  720. viewP._removeChildren( 0 );
  721. selection._setTo( ViewRange.createFromParentsAndOffsets( viewP, 0, viewP, 0 ) );
  722. renderer.markToSync( 'children', viewP );
  723. renderAndExpectNoChanges( renderer, domRoot );
  724. } );
  725. it( 'should handle typing in empty attribute, do nothing if changes are already applied', () => {
  726. const domSelection = document.getSelection();
  727. // 1. Render <p><b>FILLER{}</b>foo</p>.
  728. const { view: viewP, selection: newSelection } = parse(
  729. '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
  730. viewRoot._appendChild( viewP );
  731. selection._setTo( newSelection );
  732. renderer.markToSync( 'children', viewRoot );
  733. renderer.render();
  734. // 2. Check the DOM.
  735. const domP = domRoot.childNodes[ 0 ];
  736. expect( domP.childNodes.length ).to.equal( 2 );
  737. expect( domP.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  738. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  739. const domB = domP.childNodes[ 0 ];
  740. const viewB = viewP.getChild( 0 );
  741. expect( domB.childNodes.length ).to.equal( 1 );
  742. expect( domB.childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  743. expect( domSelection.rangeCount ).to.equal( 1 );
  744. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ] );
  745. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  746. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  747. // 3. Add text node to both the DOM and the view: <p><b>FILLERx</b>foo</p>.
  748. domB.childNodes[ 0 ].data += 'x';
  749. domSelection.removeAllRanges();
  750. const domRange = document.createRange();
  751. domRange.setStart( domB.childNodes[ 0 ], INLINE_FILLER_LENGTH + 1 );
  752. domRange.collapse( true );
  753. domSelection.addRange( domRange );
  754. const viewText = new ViewText( 'x' );
  755. viewB._appendChild( viewText );
  756. selection._setTo( ViewRange.createFromParentsAndOffsets( viewText, 1, viewText, 1 ) );
  757. renderer.markToSync( 'children', viewP );
  758. renderAndExpectNoChanges( renderer, domRoot );
  759. } );
  760. it( 'should handle typing in empty attribute as a children change, render if needed', () => {
  761. const domSelection = document.getSelection();
  762. // 1. Render <p><b>FILLER{}</b>foo</p>.
  763. const { view: viewP, selection: newSelection } = parse(
  764. '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
  765. viewRoot._appendChild( viewP );
  766. selection._setTo( newSelection );
  767. renderer.markToSync( 'children', viewRoot );
  768. renderer.render();
  769. // 2. Check the DOM.
  770. const domP = domRoot.childNodes[ 0 ];
  771. expect( domP.childNodes.length ).to.equal( 2 );
  772. expect( domP.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  773. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  774. const domB = domP.childNodes[ 0 ];
  775. const viewB = viewP.getChild( 0 );
  776. expect( domB.childNodes.length ).to.equal( 1 );
  777. expect( domB.childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  778. expect( domSelection.rangeCount ).to.equal( 1 );
  779. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ] );
  780. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  781. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  782. domSelection.removeAllRanges();
  783. // 3. Add text node only to the view: <p><b>x{}</b>foo</p>.
  784. const viewText = new ViewText( 'x' );
  785. viewB._appendChild( viewText );
  786. selection._setTo( ViewRange.createFromParentsAndOffsets( viewText, 1, viewText, 1 ) );
  787. renderer.markToSync( 'children', viewB );
  788. renderer.render();
  789. expect( domB.childNodes.length ).to.equal( 1 );
  790. expect( domB.childNodes[ 0 ].data ).to.equal( INLINE_FILLER + 'x' );
  791. expect( domSelection.rangeCount ).to.equal( 1 );
  792. // Depending on the browser selection may end up at the end of the text node or after the text node.
  793. const firstRange = domSelection.getRangeAt( 0 );
  794. const assertSelectionAtEndOfTextNode = () => {
  795. expect( firstRange.startOffset ).to.equal( INLINE_FILLER_LENGTH + 1 );
  796. };
  797. const assertSelectionInsideTextNode = () => {
  798. expect( firstRange.startContainer ).to.equal( domB );
  799. expect( firstRange.startOffset ).to.equal( 1 );
  800. };
  801. testUtils.checkAssertions( assertSelectionAtEndOfTextNode, assertSelectionInsideTextNode );
  802. expect( firstRange.collapsed ).to.be.true;
  803. } );
  804. it( 'should handle typing in empty attribute as a text change, render if needed', () => {
  805. const domSelection = document.getSelection();
  806. // 1. Render <p><b>FILLER{}</b>foo</p>.
  807. const { view: viewP, selection: newSelection } = parse(
  808. '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
  809. viewRoot._appendChild( viewP );
  810. selection._setTo( newSelection );
  811. renderer.markToSync( 'children', viewRoot );
  812. renderer.render();
  813. // 2. Check the DOM.
  814. const domP = domRoot.childNodes[ 0 ];
  815. expect( domP.childNodes.length ).to.equal( 2 );
  816. expect( domP.childNodes[ 0 ].tagName.toLowerCase() ).to.equal( 'b' );
  817. expect( domP.childNodes[ 1 ].data ).to.equal( 'foo' );
  818. const domB = domP.childNodes[ 0 ];
  819. const viewB = viewP.getChild( 0 );
  820. expect( domB.childNodes.length ).to.equal( 1 );
  821. expect( domB.childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  822. expect( domSelection.rangeCount ).to.equal( 1 );
  823. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ] );
  824. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  825. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  826. // 3. Add text node only to the view: <p><b>x{}</b>foo</p>.
  827. const viewText = new ViewText( 'x' );
  828. viewB._appendChild( viewText );
  829. selection._setTo( ViewRange.createFromParentsAndOffsets( viewText, 1, viewText, 1 ) );
  830. renderer.markToSync( 'text', viewText );
  831. renderer.render();
  832. // 4. Check the DOM.
  833. expect( domB.childNodes.length ).to.equal( 1 );
  834. expect( domB.childNodes[ 0 ].data ).to.equal( INLINE_FILLER + 'x' );
  835. expect( domSelection.rangeCount ).to.equal( 1 );
  836. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ] );
  837. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH + 1 );
  838. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  839. } );
  840. it( 'should handle not collapsed range', () => {
  841. const domSelection = document.getSelection();
  842. const { view: viewP, selection: newSelection } = parse(
  843. '<container:p>fo{o<attribute:b>b}ar</attribute:b></container:p>' );
  844. viewRoot._appendChild( viewP );
  845. selection._setTo( newSelection );
  846. renderer.markToSync( 'children', viewRoot );
  847. renderer.render();
  848. const domP = domRoot.childNodes[ 0 ];
  849. expect( domP.childNodes.length ).to.equal( 2 );
  850. expect( domP.childNodes[ 0 ].data ).to.equal( 'foo' );
  851. expect( domP.childNodes[ 1 ].tagName.toLowerCase() ).to.equal( 'b' );
  852. expect( domP.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  853. expect( domP.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  854. expect( domSelection.rangeCount ).to.equal( 1 );
  855. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  856. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 2 );
  857. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domP.childNodes[ 1 ].childNodes[ 0 ] );
  858. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( 1 );
  859. renderer.markToSync( 'children', viewP );
  860. renderAndExpectNoChanges( renderer, domRoot );
  861. } );
  862. it( 'should not change selection if there is no editable with selection', () => {
  863. const domDiv = createElement( document, 'div', { contenteditable: true }, 'not editable' );
  864. document.body.appendChild( domDiv );
  865. domDiv.focus();
  866. const domSelection = document.getSelection();
  867. domSelection.removeAllRanges();
  868. domSelection.collapse( domDiv, 0 );
  869. selectionEditable = null;
  870. const { view: viewP, selection: newSelection } = parse( '<container:p>fo{o}</container:p>' );
  871. viewRoot._appendChild( viewP );
  872. selection._setTo( newSelection );
  873. renderer.render();
  874. expect( domSelection.rangeCount ).to.equal( 1 );
  875. // Depending on the browser selection may end up before the text node or at the beginning of it.
  876. const domRange = domSelection.getRangeAt( 0 );
  877. const assertSelectionAtEndOfTextNode = () => {
  878. expect( domRange.startContainer ).to.equal( domDiv );
  879. };
  880. const assertSelectionInsideTextNode = () => {
  881. expect( domRange.startContainer ).to.equal( domDiv.childNodes[ 0 ] );
  882. };
  883. testUtils.checkAssertions( assertSelectionAtEndOfTextNode, assertSelectionInsideTextNode );
  884. expect( domRange.startOffset ).to.equal( 0 );
  885. expect( domRange.collapsed ).to.be.true;
  886. domDiv.remove();
  887. } );
  888. it( 'should not change selection if there is no focus', () => {
  889. const domDiv = createElement( document, 'div', null, 'not editable' );
  890. document.body.appendChild( domDiv );
  891. const domSelection = document.getSelection();
  892. domSelection.removeAllRanges();
  893. const domRange = document.createRange();
  894. domRange.setStart( domDiv, 0 );
  895. domRange.collapse( true );
  896. domSelection.addRange( domRange );
  897. renderer.isFocused = false;
  898. const { view: viewP, selection: newSelection } = parse( '<container:p>fo{o}</container:p>' );
  899. viewRoot._appendChild( viewP );
  900. selection._setTo( newSelection );
  901. renderer.render();
  902. expect( domSelection.rangeCount ).to.equal( 1 );
  903. // Depending on the browser selection may end up before the text node or at the beginning of it.
  904. const domSelectionRange = domSelection.getRangeAt( 0 );
  905. const assertSelectionAtEndOfTextNode = () => {
  906. expect( domSelectionRange.startContainer ).to.equal( domDiv );
  907. };
  908. const assertSelectionInsideTextNode = () => {
  909. expect( domSelectionRange.startContainer ).to.equal( domDiv.childNodes[ 0 ] );
  910. };
  911. testUtils.checkAssertions( assertSelectionAtEndOfTextNode, assertSelectionInsideTextNode );
  912. expect( domSelectionRange.startOffset ).to.equal( 0 );
  913. expect( domSelectionRange.collapsed ).to.be.true;
  914. domDiv.remove();
  915. } );
  916. it( 'should not add inline filler after text node', () => {
  917. const { view: viewP, selection: newSelection } = parse( '<container:p>foo[]</container:p>' );
  918. viewRoot._appendChild( viewP );
  919. selection._setTo( newSelection );
  920. renderer.markToSync( 'children', viewRoot );
  921. renderer.render();
  922. const domP = domRoot.childNodes[ 0 ];
  923. expect( domP.innerHTML.indexOf( INLINE_FILLER ) ).to.equal( -1 );
  924. } );
  925. it( 'should throw if there is no filler in expected position', () => {
  926. const { view: viewP, selection: newSelection } = parse(
  927. '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  928. viewRoot._appendChild( viewP );
  929. selection._setTo( newSelection );
  930. renderer.markToSync( 'children', viewRoot );
  931. renderer.render();
  932. const domB = domRoot.childNodes[ 0 ].childNodes[ 1 ];
  933. const viewB = viewP.getChild( 1 );
  934. expect( domB.childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  935. // Remove filler.
  936. domB.childNodes[ 0 ].data = '';
  937. selection._setTo( null );
  938. renderer.markToSync( 'children', viewB );
  939. expect( () => {
  940. renderer.render();
  941. } ).to.throw( CKEditorError, /^view-renderer-filler-was-lost/ );
  942. } );
  943. // #1014.
  944. it( 'should create inline filler in newly created dom nodes', () => {
  945. // 1. Create the view structure which needs inline filler.
  946. const inputView =
  947. '<container:ul>' +
  948. '<container:li>Foobar.</container:li>' +
  949. '<container:li>[]<container:div></container:div></container:li>' +
  950. '</container:ul>';
  951. const { view: view, selection: newSelection } = parse( inputView );
  952. viewRoot._appendChild( view );
  953. selection._setTo( newSelection );
  954. renderer.markToSync( 'children', viewRoot );
  955. renderer.render();
  956. // 2. Check if filler element has been (correctly) created.
  957. expect( domRoot.innerHTML.indexOf( INLINE_FILLER ) ).not.to.equal( -1 );
  958. // 3. Move the inline filler parent to a newly created element.
  959. const viewLi = view.getChild( 0 );
  960. const viewLiIndented = view._removeChildren( 1, 1 ); // Array with one element.
  961. const viewUl = new ViewContainerElement( 'ul', null, viewLiIndented );
  962. viewLi._appendChild( viewUl );
  963. // 4. Mark changed items and render the view.
  964. renderer.markToSync( 'children', view );
  965. renderer.markToSync( 'children', viewLi );
  966. renderer.render();
  967. expect( domRoot.innerHTML.indexOf( INLINE_FILLER ) ).not.to.equal( -1 );
  968. const domSelection = document.getSelection();
  969. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 7 ); // After inline filler.
  970. } );
  971. it( 'should handle focusing element', () => {
  972. const domFocusSpy = testUtils.sinon.spy( domRoot, 'focus' );
  973. const editable = selection.editableElement;
  974. renderer.render();
  975. expect( editable ).to.equal( viewRoot );
  976. expect( domFocusSpy.calledOnce ).to.be.true;
  977. } );
  978. it( 'should not focus editable if isFocues is set to false', () => {
  979. const domFocusSpy = testUtils.sinon.spy( domRoot, 'focus' );
  980. renderer.isFocused = false;
  981. renderer.render();
  982. expect( domFocusSpy.calledOnce ).to.be.false;
  983. } );
  984. it( 'should not focus already focused DOM element', () => {
  985. domRoot.setAttribute( 'contentEditable', true );
  986. domRoot.focus();
  987. const domFocusSpy = testUtils.sinon.spy( domRoot, 'focus' );
  988. renderer.render();
  989. expect( domFocusSpy.called ).to.be.false;
  990. } );
  991. it( 'should render NBSP as first space in inline element after another space', () => {
  992. const viewP = parse( '<container:p>x <attribute:b>y</attribute:b></container:p>' );
  993. viewRoot._appendChild( viewP );
  994. renderer.markToSync( 'children', viewRoot );
  995. renderer.render();
  996. const domP = domRoot.childNodes[ 0 ];
  997. const domB = domP.childNodes[ 1 ];
  998. expect( domP.childNodes[ 0 ].data ).to.equal( 'x ' );
  999. expect( domB.childNodes[ 0 ].data ).to.equal( 'y' );
  1000. expect( domP.innerHTML ).to.equal( 'x <b>y</b>' );
  1001. // Insert space resulting in '<p>x <b> y</b></p>'.
  1002. const viewB = viewP.getChild( 1 );
  1003. viewB._removeChildren( 0 );
  1004. viewB._appendChild( new ViewText( ' y' ) );
  1005. renderer.markToSync( 'children', viewP );
  1006. renderer.render();
  1007. expect( domP.childNodes[ 0 ].data ).to.equal( 'x ' );
  1008. expect( domB.childNodes[ 0 ].data ).to.equal( '\u00A0y' );
  1009. expect( domP.innerHTML ).to.equal( 'x <b>&nbsp;y</b>' );
  1010. } );
  1011. it( 'should update sibling after, when node before is removed', () => {
  1012. const viewP = parse( '<container:p>x<attribute:b> y</attribute:b></container:p>' );
  1013. viewRoot._appendChild( viewP );
  1014. renderer.markToSync( 'children', viewRoot );
  1015. renderer.render();
  1016. const domP = domRoot.childNodes[ 0 ];
  1017. const domB = domP.childNodes[ 1 ];
  1018. expect( domP.childNodes[ 0 ].data ).to.equal( 'x' );
  1019. expect( domB.childNodes[ 0 ].data ).to.equal( ' y' );
  1020. expect( domP.innerHTML ).to.equal( 'x<b> y</b>' );
  1021. // Remove 'x' resulting in '<p><b> y</b></p>'.
  1022. viewP._removeChildren( 0 );
  1023. renderer.markToSync( 'children', viewP );
  1024. renderer.render();
  1025. expect( domB.childNodes[ 0 ].data ).to.equal( '\u00A0y' );
  1026. expect( domP.innerHTML ).to.equal( '<b>&nbsp;y</b>' );
  1027. } );
  1028. it( 'should update sibling before, when node after is removed', () => {
  1029. const viewP = parse( '<container:p>x <attribute:b>y</attribute:b></container:p>' );
  1030. viewRoot._appendChild( viewP );
  1031. renderer.markToSync( 'children', viewRoot );
  1032. renderer.render();
  1033. const domP = domRoot.childNodes[ 0 ];
  1034. const domB = domP.childNodes[ 1 ];
  1035. expect( domP.childNodes[ 0 ].data ).to.equal( 'x ' );
  1036. expect( domB.childNodes[ 0 ].data ).to.equal( 'y' );
  1037. expect( domP.innerHTML ).to.equal( 'x <b>y</b>' );
  1038. // Remove '<b>y</b>' resulting in '<p>x </p>'.
  1039. viewP._removeChildren( 1 );
  1040. renderer.markToSync( 'children', viewP );
  1041. renderer.render();
  1042. expect( domP.childNodes[ 0 ].data ).to.equal( 'x\u00A0' );
  1043. expect( domP.innerHTML ).to.equal( 'x&nbsp;' );
  1044. } );
  1045. // #1093
  1046. it( 'should update siblings after space is inserted in element before - text-element', () => {
  1047. const viewP = parse( '<container:p>x<attribute:b> y</attribute:b></container:p>' );
  1048. viewRoot._appendChild( viewP );
  1049. renderer.markToSync( 'children', viewRoot );
  1050. renderer.render();
  1051. const domP = domRoot.childNodes[ 0 ];
  1052. const domB = domP.childNodes[ 1 ];
  1053. expect( domP.childNodes[ 0 ].data ).to.equal( 'x' );
  1054. expect( domB.childNodes[ 0 ].data ).to.equal( ' y' );
  1055. expect( domP.innerHTML ).to.equal( 'x<b> y</b>' );
  1056. // Insert space resulting in '<p>x <b> y</b></p>'.
  1057. viewP._removeChildren( 0 );
  1058. viewP._insertChild( 0, new ViewText( 'x ' ) );
  1059. renderer.markToSync( 'children', viewP );
  1060. renderer.render();
  1061. expect( domP.childNodes[ 0 ].data ).to.equal( 'x ' );
  1062. expect( domB.childNodes[ 0 ].data ).to.equal( '\u00A0y' );
  1063. expect( domP.innerHTML ).to.equal( 'x <b>&nbsp;y</b>' );
  1064. } );
  1065. // #1093
  1066. it( 'should update siblings after space is inserted in element before - element-text', () => {
  1067. const viewP = parse( '<container:p><attribute:b>x</attribute:b> y</container:p>' );
  1068. viewRoot._appendChild( viewP );
  1069. renderer.markToSync( 'children', viewRoot );
  1070. renderer.render();
  1071. const domP = domRoot.childNodes[ 0 ];
  1072. const domB = domP.childNodes[ 0 ];
  1073. expect( domB.childNodes[ 0 ].data ).to.equal( 'x' );
  1074. expect( domP.childNodes[ 1 ].data ).to.equal( ' y' );
  1075. expect( domP.innerHTML ).to.equal( '<b>x</b> y' );
  1076. // Insert space resulting in '<p><b>x </b> y</p>'.
  1077. const viewB = viewP.getChild( 0 );
  1078. viewB._removeChildren( 0 );
  1079. viewB._insertChild( 0, new ViewText( 'x ' ) );
  1080. renderer.markToSync( 'children', viewP );
  1081. renderer.render();
  1082. expect( domB.childNodes[ 0 ].data ).to.equal( 'x ' );
  1083. expect( domP.childNodes[ 1 ].data ).to.equal( '\u00A0y' );
  1084. expect( domP.innerHTML ).to.equal( '<b>x </b>&nbsp;y' );
  1085. } );
  1086. // #1093
  1087. it( 'should update siblings after space is inserted in element before - element-element', () => {
  1088. const viewP = parse( '<container:p><attribute:b>x</attribute:b><attribute:i> y</attribute:i></container:p>' );
  1089. viewRoot._appendChild( viewP );
  1090. renderer.markToSync( 'children', viewRoot );
  1091. renderer.render();
  1092. const domP = domRoot.childNodes[ 0 ];
  1093. const domB = domP.childNodes[ 0 ];
  1094. const domI = domP.childNodes[ 1 ];
  1095. expect( domB.childNodes[ 0 ].data ).to.equal( 'x' );
  1096. expect( domI.childNodes[ 0 ].data ).to.equal( ' y' );
  1097. expect( domP.innerHTML ).to.equal( '<b>x</b><i> y</i>' );
  1098. // Insert space resulting in '<p><b>x </b><i> y</i></p>'.
  1099. const viewB = viewP.getChild( 0 );
  1100. viewB._removeChildren( 0 );
  1101. viewB._insertChild( 0, new ViewText( 'x ' ) );
  1102. renderer.markToSync( 'children', viewP );
  1103. renderer.render();
  1104. expect( domB.childNodes[ 0 ].data ).to.equal( 'x ' );
  1105. expect( domI.childNodes[ 0 ].data ).to.equal( '\u00A0y' );
  1106. expect( domP.innerHTML ).to.equal( '<b>x </b><i>&nbsp;y</i>' );
  1107. } );
  1108. // #1125
  1109. it( 'should properly rerender many changes done in one batch', () => {
  1110. // This test transforms:
  1111. //
  1112. // <h2>He<strong>ding 1</strong></h2>
  1113. // <p>Ph <strong>Bold</strong> <i>It<strong>alic</strong></i> <a><strong>Lin</strong>k</a></p>
  1114. // <blockquote><ul><li>Quoted <strong>item 1</strong></li></ul></blockquote>
  1115. //
  1116. // into:
  1117. //
  1118. // <h2>Heading 2</h2>
  1119. // <p>Ph <i><strong>Italic</strong></i> <a><strong>L</strong>ink 1</a></p>
  1120. // <blockquote><p>Qu<strong>ote</strong></p><ul><li><strong>Quoted item 1</strong></li></ul></blockquote>
  1121. //
  1122. // during one rerender to check if complex structure changes are rerendered correctly.
  1123. const viewContent = parse( '' +
  1124. '<container:h2>He' +
  1125. '<attribute:i>ading 1</attribute:i>' +
  1126. '</container:h2>' +
  1127. '<container:p>Ph ' +
  1128. '<attribute:strong>Bold</attribute:strong> ' +
  1129. '<attribute:i>It' +
  1130. '<attribute:strong>alic</attribute:strong>' +
  1131. '</attribute:i> ' +
  1132. '<attribute:a href="https://ckeditor.com">' +
  1133. '<attribute:strong>Lin</attribute:strong>' +
  1134. 'k</attribute:a>' +
  1135. '</container:p>' +
  1136. '<container:blockquote>' +
  1137. '<container:ul>' +
  1138. '<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
  1139. '</container:ul>' +
  1140. '</container:blockquote>' );
  1141. viewRoot._appendChild( viewContent );
  1142. renderer.markToSync( 'children', viewRoot );
  1143. renderer.render();
  1144. // '<h2>He<i>ading 1</i></h2>' -> '<h2>Heading 2</h2>'
  1145. const viewHeading = viewRoot.getChild( 0 );
  1146. viewHeading._removeChildren( 0, viewHeading.childCount );
  1147. viewHeading._insertChild( 0, new ViewText( 'Heading 2' ) );
  1148. // Usually whole subtree is marked to sync so we mark root, changed element and all its direct children.
  1149. renderer.markToSync( 'children', viewRoot );
  1150. renderer.markToSync( 'children', viewHeading );
  1151. renderer.markToSync( 'text', viewHeading.getChild( 0 ) );
  1152. // '<p>Ph <strong>Bold</strong> <i>It<strong>alic</strong></i> <a><strong>Lin</strong>k</a></p>'
  1153. // ->
  1154. // '<p>Ph <i><strong>Italic</strong></i> <a><strong>L</strong>ink 1</a></p>'
  1155. const viewP = viewRoot.getChild( 1 );
  1156. viewP._removeChildren( 0, viewP.childCount );
  1157. viewP._insertChild(
  1158. 0,
  1159. parse(
  1160. 'Ph <attribute:i><attribute:strong>Italic</attribute:strong></attribute:i> ' +
  1161. '<attribute:a href="https://ckeditor.com"><attribute:strong>L</attribute:strong>ink 1</attribute:a>'
  1162. )
  1163. );
  1164. renderer.markToSync( 'children', viewRoot );
  1165. renderer.markToSync( 'children', viewP );
  1166. renderer.markToSync( 'children', viewP.getChild( 1 ) );
  1167. renderer.markToSync( 'children', viewP.getChild( 3 ) );
  1168. renderer.markToSync( 'text', viewP.getChild( 0 ) );
  1169. renderer.markToSync( 'text', viewP.getChild( 2 ) );
  1170. // '<blockquote><ul><li>Quoted <strong>item 1</strong></li></ul></blockquote>'
  1171. // -> '<blockquote><p>Qu<strong>ote</strong></p><ul><li><strong>Quoted item 1</strong></li></ul></blockquote>'
  1172. const viewBq = viewRoot.getChild( 2 );
  1173. viewBq._removeChildren( 0, viewBq.childCount );
  1174. viewBq._insertChild(
  1175. 0,
  1176. parse(
  1177. '<container:p>Qu<attribute:strong>ote</attribute:strong></container:p>' +
  1178. '<container:ul><container:li><attribute:strong>Quoted item 1</attribute:strong></container:li></container:ul>'
  1179. )
  1180. );
  1181. renderer.markToSync( 'children', viewRoot );
  1182. renderer.markToSync( 'children', viewBq );
  1183. renderer.markToSync( 'children', viewBq.getChild( 0 ) );
  1184. renderer.markToSync( 'children', viewBq.getChild( 1 ) );
  1185. renderer.render();
  1186. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal(
  1187. '<h2>Heading 2</h2>' +
  1188. '<p>Ph <i><strong>Italic</strong></i> <a href="https://ckeditor.com"><strong>L</strong>ink 1</a></p>' +
  1189. '<blockquote><p>Qu<strong>ote</strong></p><ul><li><strong>Quoted item 1</strong></li></ul></blockquote>'
  1190. );
  1191. } );
  1192. // #1125
  1193. it( 'should properly rerender changes when whole content replaced at once', () => {
  1194. // This test replaces:
  1195. //
  1196. // <h1>Header</h1>
  1197. // <blockquote><ul><li>Quoted <strong>item 1</strong></li><li>Item 2</li></ul></blockquote>
  1198. //
  1199. // with:
  1200. //
  1201. // <h2>Header</h2>
  1202. // <p>Not Quoted <strong>item 1</strong> and item 2</p>
  1203. //
  1204. // during one rerender to check if complex structure changes are rerendered correctly.
  1205. const viewContent = parse(
  1206. '<container:h1>Header</container:h1>' +
  1207. '<container:blockquote>' +
  1208. '<container:ul>' +
  1209. '<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
  1210. '<container:li>Item 2</container:li>' +
  1211. '</container:ul>' +
  1212. '</container:blockquote>'
  1213. );
  1214. viewRoot._appendChild( viewContent );
  1215. renderer.markToSync( 'children', viewRoot );
  1216. renderer.render();
  1217. const newViewContent = parse(
  1218. '<container:h2>Header</container:h2>' +
  1219. '<container:p>Not Quoted <attribute:strong>item 1</attribute:strong> and item 2</container:p>'
  1220. );
  1221. viewRoot._removeChildren( 0, viewRoot.childCount );
  1222. viewRoot._appendChild( newViewContent );
  1223. renderer.markToSync( 'children', viewRoot );
  1224. renderer.render();
  1225. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal(
  1226. '<h2>Header</h2><p>Not Quoted <strong>item 1</strong> and item 2</p>'
  1227. );
  1228. } );
  1229. describe( 'fake selection', () => {
  1230. beforeEach( () => {
  1231. const { view: viewP, selection: newSelection } = parse(
  1232. '<container:p>[foo bar]</container:p>'
  1233. );
  1234. viewRoot._appendChild( viewP );
  1235. selection._setTo( newSelection );
  1236. renderer.markToSync( 'children', viewRoot );
  1237. renderer.render();
  1238. } );
  1239. it( 'should render fake selection', () => {
  1240. const label = 'fake selection label';
  1241. selection._setTo( selection.getRanges(), { fake: true, label } );
  1242. renderer.render();
  1243. expect( domRoot.childNodes.length ).to.equal( 2 );
  1244. const container = domRoot.childNodes[ 1 ];
  1245. expect( domConverter.mapDomToView( container ) ).to.be.undefined;
  1246. expect( container.childNodes.length ).to.equal( 1 );
  1247. const textNode = container.childNodes[ 0 ];
  1248. expect( textNode.textContent ).to.equal( label );
  1249. const domSelection = domRoot.ownerDocument.getSelection();
  1250. assertDomSelectionContents( domSelection, container, /^fake selection label$/ );
  1251. } );
  1252. it( 'should render &nbsp; if no selection label is provided', () => {
  1253. selection._setTo( selection.getRanges(), { fake: true } );
  1254. renderer.render();
  1255. expect( domRoot.childNodes.length ).to.equal( 2 );
  1256. const container = domRoot.childNodes[ 1 ];
  1257. expect( container.childNodes.length ).to.equal( 1 );
  1258. const textNode = container.childNodes[ 0 ];
  1259. expect( textNode.textContent ).to.equal( '\u00A0' );
  1260. const domSelection = domRoot.ownerDocument.getSelection();
  1261. assertDomSelectionContents( domSelection, container, /^[ \u00A0]$/ );
  1262. } );
  1263. it( 'should remove fake selection container when selection is no longer fake', () => {
  1264. selection._setTo( selection.getRanges(), { fake: true } );
  1265. renderer.render();
  1266. selection._setTo( selection.getRanges(), { fake: false } );
  1267. renderer.render();
  1268. expect( domRoot.childNodes.length ).to.equal( 1 );
  1269. const domParagraph = domRoot.childNodes[ 0 ];
  1270. expect( domParagraph.childNodes.length ).to.equal( 1 );
  1271. expect( domParagraph.tagName.toLowerCase() ).to.equal( 'p' );
  1272. const domSelection = domRoot.ownerDocument.getSelection();
  1273. assertDomSelectionContents( domSelection, domParagraph, /^foo bar$/ );
  1274. } );
  1275. it( 'should reuse fake selection container #1', () => {
  1276. const label = 'fake selection label';
  1277. selection._setTo( selection.getRanges(), { fake: true, label } );
  1278. renderer.render();
  1279. expect( domRoot.childNodes.length ).to.equal( 2 );
  1280. const container = domRoot.childNodes[ 1 ];
  1281. selection._setTo( selection.getRanges(), { fake: true, label } );
  1282. renderer.render();
  1283. expect( domRoot.childNodes.length ).to.equal( 2 );
  1284. const newContainer = domRoot.childNodes[ 1 ];
  1285. expect( newContainer ).equals( container );
  1286. expect( newContainer.childNodes.length ).to.equal( 1 );
  1287. const textNode = newContainer.childNodes[ 0 ];
  1288. expect( textNode.textContent ).to.equal( label );
  1289. } );
  1290. it( 'should reuse fake selection container #2', () => {
  1291. selection._setTo( selection.getRanges(), { fake: true, label: 'label 1' } );
  1292. renderer.render();
  1293. expect( domRoot.childNodes.length ).to.equal( 2 );
  1294. const container = domRoot.childNodes[ 1 ];
  1295. selection._setTo( selection.getRanges(), { fake: false } );
  1296. renderer.render();
  1297. expect( domRoot.childNodes.length ).to.equal( 1 );
  1298. selection._setTo( selection.getRanges(), { fake: true, label: 'label 2' } );
  1299. renderer.render();
  1300. expect( domRoot.childNodes.length ).to.equal( 2 );
  1301. const newContainer = domRoot.childNodes[ 1 ];
  1302. expect( newContainer ).equals( container );
  1303. expect( newContainer.childNodes.length ).to.equal( 1 );
  1304. const textNode = newContainer.childNodes[ 0 ];
  1305. expect( textNode.textContent ).to.equal( 'label 2' );
  1306. } );
  1307. it( 'should reuse fake selection container #3', () => {
  1308. selection._setTo( selection.getRanges(), { fake: true, label: 'label 1' } );
  1309. renderer.render();
  1310. expect( domRoot.childNodes.length ).to.equal( 2 );
  1311. const container = domRoot.childNodes[ 1 ];
  1312. selection._setTo( selection.getRanges(), { fake: true, label: 'label 2' } );
  1313. renderer.render();
  1314. expect( domRoot.childNodes.length ).to.equal( 2 );
  1315. const newContainer = domRoot.childNodes[ 1 ];
  1316. expect( newContainer ).equals( container );
  1317. expect( newContainer.childNodes.length ).to.equal( 1 );
  1318. const textNode = newContainer.childNodes[ 0 ];
  1319. expect( textNode.textContent ).to.equal( 'label 2' );
  1320. } );
  1321. it( 'should style fake selection container properly', () => {
  1322. selection._setTo( selection.getRanges(), { fake: true, label: 'fake selection' } );
  1323. renderer.render();
  1324. expect( domRoot.childNodes.length ).to.equal( 2 );
  1325. const container = domRoot.childNodes[ 1 ];
  1326. expect( container.style.position ).to.equal( 'fixed' );
  1327. expect( container.style.top ).to.equal( '0px' );
  1328. expect( container.style.left ).to.equal( '-9999px' );
  1329. } );
  1330. it( 'should bind fake selection container to view selection', () => {
  1331. selection._setTo( selection.getRanges(), { fake: true, label: 'fake selection' } );
  1332. renderer.render();
  1333. expect( domRoot.childNodes.length ).to.equal( 2 );
  1334. const container = domRoot.childNodes[ 1 ];
  1335. const bindSelection = renderer.domConverter.fakeSelectionToView( container );
  1336. expect( bindSelection ).to.not.be.undefined;
  1337. expect( bindSelection.isEqual( selection ) ).to.be.true;
  1338. } );
  1339. // Use a forgiving way of checking what the selection contains
  1340. // because Safari normalizes the selection ranges so precise checking is troublesome.
  1341. // Also, Edge returns a normal space instead of nbsp so we need to use even more alternatives.
  1342. function assertDomSelectionContents( domSelection, expectedContainer, expectedText ) {
  1343. const domSelectionContainer = domSelection.getRangeAt( 0 ).commonAncestorContainer;
  1344. expect( domSelection.toString() ).to.match( expectedText );
  1345. expect(
  1346. domSelectionContainer == expectedContainer.firstChild || domSelectionContainer == expectedContainer
  1347. ).to.be.true;
  1348. }
  1349. } );
  1350. // #887
  1351. describe( 'similar selection', () => {
  1352. // Use spies to check selection updates. Some selection positions are not achievable in some
  1353. // browsers (e.g. <p>Foo<b>{}Bar</b></p> in Chrome) so asserting dom selection after rendering would fail.
  1354. let selectionCollapseSpy, selectionExtendSpy;
  1355. afterEach( () => {
  1356. if ( selectionCollapseSpy ) {
  1357. selectionCollapseSpy.restore();
  1358. selectionCollapseSpy = null;
  1359. }
  1360. if ( selectionExtendSpy ) {
  1361. selectionExtendSpy.restore();
  1362. selectionExtendSpy = null;
  1363. }
  1364. } );
  1365. it( 'should always render collapsed selection even if it is similar', () => {
  1366. const domSelection = document.getSelection();
  1367. const { view: viewP, selection: newSelection } = parse(
  1368. '<container:p>foo{}<attribute:b>bar</attribute:b></container:p>' );
  1369. viewRoot._appendChild( viewP );
  1370. selection._setTo( newSelection );
  1371. renderer.markToSync( 'children', viewRoot );
  1372. renderer.render();
  1373. const domP = domRoot.childNodes[ 0 ];
  1374. const domB = domP.childNodes[ 1 ];
  1375. const viewB = viewRoot.getChild( 0 ).getChild( 1 );
  1376. expect( domSelection.isCollapsed ).to.true;
  1377. expect( domSelection.rangeCount ).to.equal( 1 );
  1378. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  1379. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 3 );
  1380. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domP.childNodes[ 0 ] );
  1381. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( 3 );
  1382. selectionCollapseSpy = sinon.spy( window.Selection.prototype, 'collapse' );
  1383. selectionExtendSpy = sinon.spy( window.Selection.prototype, 'extend' );
  1384. // <container:p>foo<attribute:b>{}bar</attribute:b></container:p>
  1385. selection._setTo( [
  1386. new ViewRange( new ViewPosition( viewB.getChild( 0 ), 0 ), new ViewPosition( viewB.getChild( 0 ), 0 ) )
  1387. ] );
  1388. renderer.markToSync( 'children', viewP );
  1389. renderer.render();
  1390. expect( selectionCollapseSpy.calledOnce ).to.true;
  1391. expect( selectionCollapseSpy.calledWith( domB.childNodes[ 0 ], 0 ) ).to.true;
  1392. expect( selectionExtendSpy.calledOnce ).to.true;
  1393. expect( selectionExtendSpy.calledWith( domB.childNodes[ 0 ], 0 ) ).to.true;
  1394. } );
  1395. it( 'should always render collapsed selection even if it is similar (with empty element)', () => {
  1396. const domSelection = document.getSelection();
  1397. const { view: viewP, selection: newSelection } = parse(
  1398. '<container:p>foo<attribute:b>[]</attribute:b></container:p>' );
  1399. viewRoot._appendChild( viewP );
  1400. selection._setTo( newSelection );
  1401. renderer.markToSync( 'children', viewRoot );
  1402. renderer.render();
  1403. const domP = domRoot.childNodes[ 0 ];
  1404. const domB = domP.childNodes[ 1 ];
  1405. expect( domSelection.isCollapsed ).to.true;
  1406. expect( domSelection.rangeCount ).to.equal( 1 );
  1407. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ] );
  1408. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( INLINE_FILLER_LENGTH );
  1409. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domB.childNodes[ 0 ] );
  1410. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( INLINE_FILLER_LENGTH );
  1411. selectionCollapseSpy = sinon.spy( window.Selection.prototype, 'collapse' );
  1412. selectionExtendSpy = sinon.spy( window.Selection.prototype, 'extend' );
  1413. // <container:p>foo{}<attribute:b></attribute:b></container:p>
  1414. selection._setTo( [
  1415. new ViewRange( new ViewPosition( viewP.getChild( 0 ), 3 ), new ViewPosition( viewP.getChild( 0 ), 3 ) )
  1416. ] );
  1417. renderer.markToSync( 'children', viewP );
  1418. renderer.render();
  1419. expect( selectionCollapseSpy.calledOnce ).to.true;
  1420. expect( selectionCollapseSpy.calledWith( domP.childNodes[ 0 ], 3 ) ).to.true;
  1421. expect( selectionExtendSpy.calledOnce ).to.true;
  1422. expect( selectionExtendSpy.calledWith( domP.childNodes[ 0 ], 3 ) ).to.true;
  1423. } );
  1424. it( 'should always render non-collapsed selection if it not is similar', () => {
  1425. const domSelection = document.getSelection();
  1426. const { view: viewP, selection: newSelection } = parse(
  1427. '<container:p>fo{o}<attribute:b>bar</attribute:b></container:p>' );
  1428. viewRoot._appendChild( viewP );
  1429. selection._setTo( newSelection );
  1430. renderer.markToSync( 'children', viewRoot );
  1431. renderer.render();
  1432. const domP = domRoot.childNodes[ 0 ];
  1433. const domB = domP.childNodes[ 1 ];
  1434. const viewB = viewRoot.getChild( 0 ).getChild( 1 );
  1435. expect( domSelection.isCollapsed ).to.false;
  1436. expect( domSelection.rangeCount ).to.equal( 1 );
  1437. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  1438. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 2 );
  1439. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domP.childNodes[ 0 ] );
  1440. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( 3 );
  1441. selectionCollapseSpy = sinon.spy( window.Selection.prototype, 'collapse' );
  1442. selectionExtendSpy = sinon.spy( window.Selection.prototype, 'extend' );
  1443. // <container:p>fo{o<attribute:b>b}ar</attribute:b></container:p>
  1444. selection._setTo( [
  1445. new ViewRange( new ViewPosition( viewP.getChild( 0 ), 2 ), new ViewPosition( viewB.getChild( 0 ), 1 ) )
  1446. ] );
  1447. renderer.markToSync( 'children', viewP );
  1448. renderer.render();
  1449. expect( selectionCollapseSpy.calledOnce ).to.true;
  1450. expect( selectionCollapseSpy.calledWith( domP.childNodes[ 0 ], 2 ) ).to.true;
  1451. expect( selectionExtendSpy.calledOnce ).to.true;
  1452. expect( selectionExtendSpy.calledWith( domB.childNodes[ 0 ], 1 ) ).to.true;
  1453. } );
  1454. it( 'should always render selection (even if it is same in view) if current dom selection is in incorrect place', () => {
  1455. const domSelection = document.getSelection();
  1456. const { view: viewP, selection: newSelection } = parse( '<container:p>foo[]<ui:span></ui:span></container:p>' );
  1457. viewRoot._appendChild( viewP );
  1458. selection._setTo( newSelection );
  1459. renderer.markToSync( 'children', viewRoot );
  1460. renderer.render();
  1461. // In DOM, set position to: <p>foo<span>[]</span></p>. This is incorrect DOM selection (it is in view ui element).
  1462. // Do not change view selection.
  1463. // When renderer will check if the DOM selection changed, it will convert DOM selection to a view selection.
  1464. // Selections (current view selection and view-from-dom selection) will be equal but we will still expect re-render
  1465. // because DOM selection is in incorrect place.
  1466. const domP = domRoot.childNodes[ 0 ];
  1467. const domSpan = domP.childNodes[ 1 ];
  1468. domSelection.collapse( domSpan, 0 );
  1469. renderer.render();
  1470. // Expect that after calling `renderer.render()` the DOM selection was re-rendered (and set at correct position).
  1471. // Depending on the browser selection may end up at the end of the text node or after the text node.
  1472. const assertSelectionAtEndOfTextNode = () => {
  1473. expect( domSelection.anchorNode ).to.equal( domP );
  1474. expect( domSelection.anchorOffset ).to.equal( 1 );
  1475. };
  1476. const assertSelectionInsideTextNode = () => {
  1477. const textNode = domP.childNodes[ 0 ];
  1478. expect( domSelection.anchorNode ).to.equal( textNode );
  1479. expect( domSelection.anchorOffset ).to.equal( 3 );
  1480. };
  1481. testUtils.checkAssertions( assertSelectionAtEndOfTextNode, assertSelectionInsideTextNode );
  1482. expect( domSelection.getRangeAt( 0 ).collapsed ).to.be.true;
  1483. } );
  1484. it( 'should not render non-collapsed selection it is similar (element start)', () => {
  1485. const domSelection = document.getSelection();
  1486. const { view: viewP, selection: newSelection } = parse(
  1487. '<container:p>foo<attribute:b>{ba}r</attribute:b></container:p>' );
  1488. viewRoot._appendChild( viewP );
  1489. selection._setTo( newSelection );
  1490. renderer.markToSync( 'children', viewRoot );
  1491. renderer.render();
  1492. const domP = domRoot.childNodes[ 0 ];
  1493. const domB = domP.childNodes[ 1 ];
  1494. const viewB = viewRoot.getChild( 0 ).getChild( 1 );
  1495. expect( domSelection.isCollapsed ).to.false;
  1496. expect( domSelection.rangeCount ).to.equal( 1 );
  1497. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ] );
  1498. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 0 );
  1499. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domB.childNodes[ 0 ] );
  1500. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( 2 );
  1501. selectionCollapseSpy = sinon.spy( window.Selection.prototype, 'collapse' );
  1502. selectionExtendSpy = sinon.spy( window.Selection.prototype, 'extend' );
  1503. // <container:p>foo{<attribute:b>ba}r</attribute:b></container:p>
  1504. selection._setTo( [
  1505. new ViewRange( new ViewPosition( viewP.getChild( 0 ), 3 ), new ViewPosition( viewB.getChild( 0 ), 2 ) )
  1506. ] );
  1507. renderer.markToSync( 'children', viewP );
  1508. renderer.render();
  1509. expect( selectionCollapseSpy.notCalled ).to.true;
  1510. expect( selectionExtendSpy.notCalled ).to.true;
  1511. } );
  1512. it( 'should not render non-collapsed selection it is similar (element end)', () => {
  1513. const domSelection = document.getSelection();
  1514. const { view: viewP, selection: newSelection } = parse(
  1515. '<container:p>foo<attribute:b>b{ar}</attribute:b>baz</container:p>' );
  1516. viewRoot._appendChild( viewP );
  1517. selection._setTo( newSelection );
  1518. renderer.markToSync( 'children', viewRoot );
  1519. renderer.render();
  1520. const domP = domRoot.childNodes[ 0 ];
  1521. const domB = domP.childNodes[ 1 ];
  1522. const viewB = viewRoot.getChild( 0 ).getChild( 1 );
  1523. expect( domSelection.isCollapsed ).to.false;
  1524. expect( domSelection.rangeCount ).to.equal( 1 );
  1525. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ] );
  1526. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 1 );
  1527. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domB.childNodes[ 0 ] );
  1528. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( 3 );
  1529. selectionCollapseSpy = sinon.spy( window.Selection.prototype, 'collapse' );
  1530. selectionExtendSpy = sinon.spy( window.Selection.prototype, 'extend' );
  1531. // <container:p>foo<attribute:b>b{ar</attribute:b>}baz</container:p>
  1532. selection._setTo( [
  1533. new ViewRange( new ViewPosition( viewB.getChild( 0 ), 1 ), new ViewPosition( viewP.getChild( 2 ), 0 ) )
  1534. ] );
  1535. renderer.markToSync( 'children', viewP );
  1536. renderer.render();
  1537. expect( selectionCollapseSpy.notCalled ).to.true;
  1538. expect( selectionExtendSpy.notCalled ).to.true;
  1539. } );
  1540. it( 'should not render non-collapsed selection it is similar (element start - nested)', () => {
  1541. const domSelection = document.getSelection();
  1542. const { view: viewP, selection: newSelection } = parse(
  1543. '<container:p>foo<attribute:b><attribute:i>{ba}r</attribute:i></attribute:b></container:p>' );
  1544. viewRoot._appendChild( viewP );
  1545. selection._setTo( newSelection );
  1546. renderer.markToSync( 'children', viewRoot );
  1547. renderer.render();
  1548. const domP = domRoot.childNodes[ 0 ];
  1549. const domB = domP.childNodes[ 1 ];
  1550. const viewI = viewRoot.getChild( 0 ).getChild( 1 ).getChild( 0 );
  1551. expect( domSelection.isCollapsed ).to.false;
  1552. expect( domSelection.rangeCount ).to.equal( 1 );
  1553. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domB.childNodes[ 0 ].childNodes[ 0 ] );
  1554. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 0 );
  1555. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domB.childNodes[ 0 ].childNodes[ 0 ] );
  1556. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( 2 );
  1557. selectionCollapseSpy = sinon.spy( window.Selection.prototype, 'collapse' );
  1558. selectionExtendSpy = sinon.spy( window.Selection.prototype, 'extend' );
  1559. // <container:p>foo{<attribute:b><attribute:i>ba}r</attribute:i></attribute:b></container:p>
  1560. selection._setTo( [
  1561. new ViewRange( new ViewPosition( viewP.getChild( 0 ), 3 ), new ViewPosition( viewI.getChild( 0 ), 2 ) )
  1562. ] );
  1563. renderer.markToSync( 'children', viewP );
  1564. renderer.render();
  1565. expect( selectionCollapseSpy.notCalled ).to.true;
  1566. expect( selectionExtendSpy.notCalled ).to.true;
  1567. } );
  1568. it( 'should not render non-collapsed selection it is similar (element end - nested)', () => {
  1569. const domSelection = document.getSelection();
  1570. const { view: viewP, selection: newSelection } = parse(
  1571. '<container:p>f{oo<attribute:b><attribute:i>bar}</attribute:i></attribute:b>baz</container:p>' );
  1572. viewRoot._appendChild( viewP );
  1573. selection._setTo( newSelection );
  1574. renderer.markToSync( 'children', viewRoot );
  1575. renderer.render();
  1576. const domP = domRoot.childNodes[ 0 ];
  1577. const domB = domP.childNodes[ 1 ];
  1578. expect( domSelection.isCollapsed ).to.false;
  1579. expect( domSelection.rangeCount ).to.equal( 1 );
  1580. expect( domSelection.getRangeAt( 0 ).startContainer ).to.equal( domP.childNodes[ 0 ] );
  1581. expect( domSelection.getRangeAt( 0 ).startOffset ).to.equal( 1 );
  1582. expect( domSelection.getRangeAt( 0 ).endContainer ).to.equal( domB.childNodes[ 0 ].childNodes[ 0 ] );
  1583. expect( domSelection.getRangeAt( 0 ).endOffset ).to.equal( 3 );
  1584. selectionCollapseSpy = sinon.spy( window.Selection.prototype, 'collapse' );
  1585. selectionExtendSpy = sinon.spy( window.Selection.prototype, 'extend' );
  1586. // <container:p>f{oo<attribute:b><attribute:i>bar</attribute:i></attribute:b>}baz</container:p>
  1587. selection._setTo( [
  1588. new ViewRange( new ViewPosition( viewP.getChild( 0 ), 1 ), new ViewPosition( viewP.getChild( 2 ), 0 ) )
  1589. ] );
  1590. renderer.markToSync( 'children', viewP );
  1591. renderer.render();
  1592. expect( selectionCollapseSpy.notCalled ).to.true;
  1593. expect( selectionExtendSpy.notCalled ).to.true;
  1594. } );
  1595. } );
  1596. // #1417
  1597. describe( 'optimal rendering', () => {
  1598. it( 'should render inline element replacement (before text)', () => {
  1599. viewRoot._appendChild( parse( '<container:p><attribute:i>A</attribute:i>1</container:p>' ) );
  1600. renderer.markToSync( 'children', viewRoot );
  1601. renderer.render();
  1602. expect( domRoot.innerHTML ).to.equal( '<p><i>A</i>1</p>' );
  1603. const viewP = viewRoot.getChild( 0 );
  1604. viewP._removeChildren( 0, 2 );
  1605. viewP._insertChild( 0, parse( '<attribute:i>B</attribute:i>2' ) );
  1606. const domI = domRoot.childNodes[ 0 ].childNodes[ 0 ];
  1607. renderer.markToSync( 'children', viewRoot );
  1608. renderer.markToSync( 'children', viewP );
  1609. renderer.render();
  1610. expect( domRoot.innerHTML ).to.equal( '<p><i>B</i>2</p>' );
  1611. expect( domI ).to.equal( domRoot.childNodes[ 0 ].childNodes[ 0 ] );
  1612. } );
  1613. it( 'should render inline element replacement (after text)', () => {
  1614. viewRoot._appendChild( parse( '<container:p>1<attribute:i>A</attribute:i></container:p>' ) );
  1615. renderer.markToSync( 'children', viewRoot );
  1616. renderer.render();
  1617. expect( domRoot.innerHTML ).to.equal( '<p>1<i>A</i></p>' );
  1618. const viewP = viewRoot.getChild( 0 );
  1619. viewP._removeChildren( 0, 2 );
  1620. viewP._insertChild( 0, parse( '2<attribute:i>B</attribute:i>' ) );
  1621. const domI = domRoot.childNodes[ 0 ].childNodes[ 1 ];
  1622. renderer.markToSync( 'children', viewRoot );
  1623. renderer.markToSync( 'children', viewP );
  1624. renderer.render();
  1625. expect( domRoot.innerHTML ).to.equal( '<p>2<i>B</i></p>' );
  1626. expect( domI ).to.equal( domRoot.childNodes[ 0 ].childNodes[ 1 ] );
  1627. } );
  1628. it( 'should render inline element replacement (before text swapped order)', () => {
  1629. viewRoot._appendChild( parse( '<container:p><attribute:i>A</attribute:i>1</container:p>' ) );
  1630. renderer.markToSync( 'children', viewRoot );
  1631. renderer.render();
  1632. expect( domRoot.innerHTML ).to.equal( '<p><i>A</i>1</p>' );
  1633. const viewP = viewRoot.getChild( 0 );
  1634. viewP._removeChildren( 0, 2 );
  1635. viewP._insertChild( 0, parse( '2<attribute:i>B</attribute:i>' ) );
  1636. const domI = domRoot.childNodes[ 0 ].childNodes[ 0 ];
  1637. renderer.markToSync( 'children', viewRoot );
  1638. renderer.markToSync( 'children', viewP );
  1639. renderer.render();
  1640. expect( domRoot.innerHTML ).to.equal( '<p>2<i>B</i></p>' );
  1641. expect( domI ).to.equal( domRoot.childNodes[ 0 ].childNodes[ 1 ] );
  1642. } );
  1643. it( 'should render inline element replacement (after text swapped order)', () => {
  1644. viewRoot._appendChild( parse( '<container:p>1<attribute:i>A</attribute:i></container:p>' ) );
  1645. renderer.markToSync( 'children', viewRoot );
  1646. renderer.render();
  1647. expect( domRoot.innerHTML ).to.equal( '<p>1<i>A</i></p>' );
  1648. const viewP = viewRoot.getChild( 0 );
  1649. viewP._removeChildren( 0, 2 );
  1650. viewP._insertChild( 0, parse( '<attribute:i>B</attribute:i>2' ) );
  1651. const domI = domRoot.childNodes[ 0 ].childNodes[ 1 ];
  1652. renderer.markToSync( 'children', viewRoot );
  1653. renderer.markToSync( 'children', viewP );
  1654. renderer.render();
  1655. expect( domRoot.innerHTML ).to.equal( '<p><i>B</i>2</p>' );
  1656. expect( domI ).to.equal( domRoot.childNodes[ 0 ].childNodes[ 0 ] );
  1657. } );
  1658. it( 'should render single replacement in p group', () => {
  1659. const content = '' +
  1660. '<container:p>1</container:p>' +
  1661. '<container:p>2</container:p>' +
  1662. '<container:p>3</container:p>';
  1663. viewRoot._appendChild( parse( content ) );
  1664. renderer.markToSync( 'children', viewRoot );
  1665. renderer.render();
  1666. expect( domRoot.innerHTML ).to.equal( '<p>1</p><p>2</p><p>3</p>' );
  1667. viewRoot._removeChildren( 1 );
  1668. viewRoot._insertChild( 1, parse( '<container:p>4</container:p>' ) );
  1669. const domP = domRoot.childNodes[ 1 ];
  1670. renderer.markToSync( 'children', viewRoot );
  1671. renderer.render();
  1672. expect( domRoot.innerHTML ).to.equal( '<p>1</p><p>4</p><p>3</p>' );
  1673. expect( domP ).to.equal( domRoot.childNodes[ 1 ] );
  1674. } );
  1675. it( 'should render replacement and insertion in p group', () => {
  1676. const content = '' +
  1677. '<container:p>1<attribute:i>A</attribute:i></container:p>' +
  1678. '<container:p>2<attribute:i>B</attribute:i></container:p>' +
  1679. '<container:p>3<attribute:i>C</attribute:i></container:p>';
  1680. const replacement = '' +
  1681. '<container:p><attribute:i>D</attribute:i></container:p>' +
  1682. '<container:p>5<attribute:i>E</attribute:i></container:p>';
  1683. viewRoot._appendChild( parse( content ) );
  1684. renderer.markToSync( 'children', viewRoot );
  1685. renderer.render();
  1686. expect( domRoot.innerHTML ).to.equal( '<p>1<i>A</i></p><p>2<i>B</i></p><p>3<i>C</i></p>' );
  1687. viewRoot._removeChildren( 1 );
  1688. viewRoot._insertChild( 1, parse( replacement ) );
  1689. const domP2 = domRoot.childNodes[ 1 ];
  1690. const domP3 = domRoot.childNodes[ 2 ];
  1691. renderer.markToSync( 'children', viewRoot );
  1692. renderer.render();
  1693. expect( domRoot.innerHTML ).to.equal( '<p>1<i>A</i></p><p><i>D</i></p><p>5<i>E</i></p><p>3<i>C</i></p>' );
  1694. expect( domP2 ).to.equal( domRoot.childNodes[ 1 ] );
  1695. expect( domP3 ).to.equal( domRoot.childNodes[ 3 ] );
  1696. } );
  1697. it( 'should render replacement and deletion in p group', () => {
  1698. const content = '' +
  1699. '<container:p><attribute:i>A</attribute:i>1</container:p>' +
  1700. '<container:p><attribute:i>B</attribute:i>2</container:p>' +
  1701. '<container:p><attribute:i>C</attribute:i>3</container:p>';
  1702. viewRoot._appendChild( parse( content ) );
  1703. renderer.markToSync( 'children', viewRoot );
  1704. renderer.render();
  1705. expect( domRoot.innerHTML ).to.equal( '<p><i>A</i>1</p><p><i>B</i>2</p><p><i>C</i>3</p>' );
  1706. viewRoot._removeChildren( 0, 2 );
  1707. viewRoot._insertChild( 0, parse( '<container:p>4</container:p>' ) );
  1708. const domP0 = domRoot.childNodes[ 0 ];
  1709. const domP2 = domRoot.childNodes[ 2 ];
  1710. renderer.markToSync( 'children', viewRoot );
  1711. renderer.render();
  1712. expect( domRoot.innerHTML ).to.equal( '<p>4</p><p><i>C</i>3</p>' );
  1713. expect( domP0 ).to.equal( domRoot.childNodes[ 0 ] );
  1714. expect( domP2 ).to.equal( domRoot.childNodes[ 1 ] );
  1715. } );
  1716. it( 'should render multiple continuous replacement in p group', () => {
  1717. const content = '' +
  1718. '<container:p>1</container:p>' +
  1719. '<container:p>2</container:p>' +
  1720. '<container:p>3</container:p>' +
  1721. '<container:p>4</container:p>' +
  1722. '<container:p>5</container:p>';
  1723. viewRoot._appendChild( parse( content ) );
  1724. renderer.markToSync( 'children', viewRoot );
  1725. renderer.render();
  1726. expect( domRoot.innerHTML ).to.equal( '<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p>' );
  1727. viewRoot._removeChildren( 0, 3 );
  1728. viewRoot._insertChild( 0, parse( '<container:p>6<attribute:i>A</attribute:i></container:p><container:p>7</container:p>' ) );
  1729. const domP1 = domRoot.childNodes[ 0 ];
  1730. const domP2 = domRoot.childNodes[ 1 ];
  1731. const domP4 = domRoot.childNodes[ 3 ];
  1732. const domP5 = domRoot.childNodes[ 4 ];
  1733. renderer.markToSync( 'children', viewRoot );
  1734. renderer.render();
  1735. expect( domRoot.innerHTML ).to.equal( '<p>6<i>A</i></p><p>7</p><p>4</p><p>5</p>' );
  1736. expect( domP1 ).to.equal( domRoot.childNodes[ 0 ] );
  1737. expect( domP2 ).to.equal( domRoot.childNodes[ 1 ] );
  1738. expect( domP4 ).to.equal( domRoot.childNodes[ 2 ] );
  1739. expect( domP5 ).to.equal( domRoot.childNodes[ 3 ] );
  1740. } );
  1741. it( 'should render multiple replacement in p group', () => {
  1742. const content = '' +
  1743. '<container:p>1</container:p>' +
  1744. '<container:p>2</container:p>' +
  1745. '<container:p>3</container:p>' +
  1746. '<container:p>4</container:p>' +
  1747. '<container:p>5</container:p>';
  1748. viewRoot._appendChild( parse( content ) );
  1749. renderer.markToSync( 'children', viewRoot );
  1750. renderer.render();
  1751. expect( domRoot.innerHTML ).to.equal( '<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p>' );
  1752. viewRoot._removeChildren( 4 );
  1753. viewRoot._removeChildren( 1, 2 );
  1754. viewRoot._insertChild( 2, parse( '<container:p>6</container:p>' ) );
  1755. viewRoot._insertChild( 1, parse( '<container:p><attribute:i>A</attribute:i>7</container:p>' ) );
  1756. const domP1 = domRoot.childNodes[ 0 ];
  1757. const domP2 = domRoot.childNodes[ 1 ];
  1758. const domP4 = domRoot.childNodes[ 3 ];
  1759. const domP5 = domRoot.childNodes[ 4 ];
  1760. renderer.markToSync( 'children', viewRoot );
  1761. renderer.render();
  1762. expect( domRoot.innerHTML ).to.equal( '<p>1</p><p><i>A</i>7</p><p>4</p><p>6</p>' );
  1763. expect( domP1 ).to.equal( domRoot.childNodes[ 0 ] );
  1764. expect( domP2 ).to.equal( domRoot.childNodes[ 1 ] );
  1765. expect( domP4 ).to.equal( domRoot.childNodes[ 2 ] );
  1766. expect( domP5 ).to.equal( domRoot.childNodes[ 3 ] );
  1767. } );
  1768. it( 'should not rerender DOM when view replaced with the same structure', () => {
  1769. const content = '' +
  1770. '<container:h2>He' +
  1771. '<attribute:i>ading 1</attribute:i>' +
  1772. '</container:h2>' +
  1773. '<container:p>Ph ' +
  1774. '<attribute:strong>Bold</attribute:strong>' +
  1775. '<attribute:a href="https://ckeditor.com">' +
  1776. '<attribute:strong>Lin<attribute:i>k</attribute:i></attribute:strong>' +
  1777. '</attribute:a>' +
  1778. '</container:p>' +
  1779. '<container:blockquote>' +
  1780. '<container:ul>' +
  1781. '<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
  1782. '</container:ul>' +
  1783. '</container:blockquote>';
  1784. viewRoot._appendChild( parse( content ) );
  1785. renderer.markToSync( 'children', viewRoot );
  1786. renderer.render();
  1787. expect( domRoot.innerHTML ).to.equal( '<h2>He<i>ading 1</i></h2><p>Ph <strong>Bold</strong>' +
  1788. '<a href="https://ckeditor.com"><strong>Lin<i>k</i></strong></a></p><blockquote><ul><li>' +
  1789. 'Quoted <strong>item 1</strong></li></ul></blockquote>' );
  1790. viewRoot._removeChildren( 0, viewRoot.childCount );
  1791. viewRoot._appendChild( parse( content ) );
  1792. const viewH = viewRoot.getChild( 0 );
  1793. const viewP = viewRoot.getChild( 1 );
  1794. const viewQ = viewRoot.getChild( 2 );
  1795. const domH = domRoot.childNodes[ 0 ];
  1796. const domHI = domH.childNodes[ 1 ];
  1797. const domP = domRoot.childNodes[ 1 ];
  1798. const domPT = domP.childNodes[ 0 ];
  1799. const domPABI = domP.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 1 ];
  1800. const domQ = domRoot.childNodes[ 2 ];
  1801. const domQULB = domQ.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 1 ];
  1802. renderer.markToSync( 'children', viewRoot );
  1803. renderer.render();
  1804. // Assert content.
  1805. expect( domRoot.innerHTML ).to.equal( '<h2>He<i>ading 1</i></h2><p>Ph <strong>Bold</strong>' +
  1806. '<a href="https://ckeditor.com"><strong>Lin<i>k</i></strong></a></p><blockquote><ul><li>' +
  1807. 'Quoted <strong>item 1</strong></li></ul></blockquote>' );
  1808. // Assert if DOM elements did not change.
  1809. expect( domRoot.childNodes[ 0 ] ).to.equal( domH );
  1810. expect( domH.childNodes[ 1 ] ).to.equal( domHI );
  1811. expect( domRoot.childNodes[ 1 ] ).to.equal( domP );
  1812. expect( domP.childNodes[ 0 ] ).to.equal( domPT );
  1813. expect( domP.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 1 ] ).to.equal( domPABI );
  1814. expect( domRoot.childNodes[ 2 ] ).to.equal( domQ );
  1815. expect( domQ.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 1 ] ).to.equal( domQULB );
  1816. // Assert mappings.
  1817. const mappings = renderer.domConverter._domToViewMapping;
  1818. expect( mappings.get( domH ) ).to.equal( viewH );
  1819. expect( mappings.get( domHI ) ).to.equal( viewH.getChild( 1 ) );
  1820. expect( mappings.get( domP ) ).to.equal( viewP );
  1821. expect( mappings.get( domPABI ) ).to.equal( viewP.getChild( 2 ).getChild( 0 ).getChild( 1 ) );
  1822. expect( mappings.get( domQ ) ).to.equal( viewQ );
  1823. expect( mappings.get( domQULB ) ).to.equal( viewQ.getChild( 0 ).getChild( 0 ).getChild( 1 ) );
  1824. } );
  1825. it( 'should not rerender DOM when view replaced with the same structure without first node', () => {
  1826. const content = '' +
  1827. '<container:h2>He' +
  1828. '<attribute:i>ading 1</attribute:i>' +
  1829. '</container:h2>' +
  1830. '<container:p>Ph ' +
  1831. '<attribute:strong>Bold</attribute:strong>' +
  1832. '<attribute:a href="https://ckeditor.com">' +
  1833. '<attribute:strong>Lin<attribute:i>k</attribute:i></attribute:strong>' +
  1834. '</attribute:a>' +
  1835. '</container:p>' +
  1836. '<container:blockquote>' +
  1837. '<container:ul>' +
  1838. '<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
  1839. '</container:ul>' +
  1840. '</container:blockquote>';
  1841. const content2 = '' +
  1842. '<container:p>Ph ' +
  1843. '<attribute:strong>Bold</attribute:strong>' +
  1844. '<attribute:a href="https://ckeditor.com">' +
  1845. '<attribute:strong>Lin<attribute:i>k</attribute:i></attribute:strong>' +
  1846. '</attribute:a>' +
  1847. '</container:p>' +
  1848. '<container:blockquote>' +
  1849. '<container:ul>' +
  1850. '<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
  1851. '</container:ul>' +
  1852. '</container:blockquote>';
  1853. viewRoot._appendChild( parse( content ) );
  1854. renderer.markToSync( 'children', viewRoot );
  1855. renderer.render();
  1856. expect( domRoot.innerHTML ).to.equal( '<h2>He<i>ading 1</i></h2><p>Ph <strong>Bold</strong>' +
  1857. '<a href="https://ckeditor.com"><strong>Lin<i>k</i></strong></a></p><blockquote><ul><li>' +
  1858. 'Quoted <strong>item 1</strong></li></ul></blockquote>' );
  1859. viewRoot._removeChildren( 0, viewRoot.childCount );
  1860. viewRoot._appendChild( parse( content2 ) );
  1861. const viewP = viewRoot.getChild( 0 );
  1862. const viewQ = viewRoot.getChild( 1 );
  1863. const domP = domRoot.childNodes[ 1 ];
  1864. const domPT = domP.childNodes[ 0 ];
  1865. const domPABI = domP.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 1 ];
  1866. const domQ = domRoot.childNodes[ 2 ];
  1867. const domQULB = domQ.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 1 ];
  1868. renderer.markToSync( 'children', viewRoot );
  1869. renderer.render();
  1870. // Assert content.
  1871. expect( domRoot.innerHTML ).to.equal( '<p>Ph <strong>Bold</strong>' +
  1872. '<a href="https://ckeditor.com"><strong>Lin<i>k</i></strong></a></p><blockquote><ul><li>' +
  1873. 'Quoted <strong>item 1</strong></li></ul></blockquote>' );
  1874. // Assert if DOM elements did not change.
  1875. expect( domRoot.childNodes[ 0 ] ).to.equal( domP );
  1876. expect( domP.childNodes[ 0 ] ).to.equal( domPT );
  1877. expect( domP.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 1 ] ).to.equal( domPABI );
  1878. expect( domRoot.childNodes[ 1 ] ).to.equal( domQ );
  1879. expect( domQ.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 1 ] ).to.equal( domQULB );
  1880. // Assert mappings.
  1881. const mappings = renderer.domConverter._domToViewMapping;
  1882. expect( mappings.get( domP ) ).to.equal( viewP );
  1883. expect( mappings.get( domPABI ) ).to.equal( viewP.getChild( 2 ).getChild( 0 ).getChild( 1 ) );
  1884. expect( mappings.get( domQ ) ).to.equal( viewQ );
  1885. expect( mappings.get( domQULB ) ).to.equal( viewQ.getChild( 0 ).getChild( 0 ).getChild( 1 ) );
  1886. } );
  1887. it( 'should not rerender DOM when typing inside empty inline element', () => {
  1888. const view = parse( '<container:p>Foo Bar<attribute:strong></attribute:strong></container:p>' );
  1889. viewRoot._appendChild( view );
  1890. renderer.markToSync( 'children', viewRoot );
  1891. renderer.render();
  1892. expect( domRoot.innerHTML ).to.equal( '<p>Foo Bar<strong></strong></p>' );
  1893. const viewP = viewRoot.getChild( 0 );
  1894. viewP._removeChildren( 1 );
  1895. viewP._insertChild( 1, parse( '<attribute:strong>a</attribute:strong>' ) );
  1896. const domP = domRoot.childNodes[ 0 ];
  1897. const domText = domP.childNodes[ 0 ];
  1898. const domB = domP.childNodes[ 1 ];
  1899. domB.innerHTML = 'a';
  1900. renderer.markToSync( 'children', viewRoot );
  1901. renderer.markToSync( 'children', viewRoot.getChild( 0 ) );
  1902. renderer.render();
  1903. // Assert content.
  1904. expect( domRoot.innerHTML ).to.equal( '<p>Foo Bar<strong>a</strong></p>' );
  1905. // Assert if DOM elements did not change.
  1906. expect( domRoot.childNodes[ 0 ] ).to.equal( domP );
  1907. expect( domRoot.childNodes[ 0 ].childNodes[ 0 ] ).to.equal( domText );
  1908. expect( domRoot.childNodes[ 0 ].childNodes[ 1 ] ).to.equal( domB );
  1909. // Assert mappings.
  1910. const mappings = renderer.domConverter._domToViewMapping;
  1911. expect( mappings.get( domP ) ).to.equal( viewP );
  1912. expect( mappings.get( domB ) ).to.equal( viewP.getChild( 1 ) );
  1913. } );
  1914. it( 'should handle complex view duplication', () => {
  1915. const content = '' +
  1916. '<container:blockquote>' +
  1917. '<container:ul>' +
  1918. '<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
  1919. '<container:li>Item 2</container:li>' +
  1920. '<container:li>' +
  1921. '<attribute:a href="https://cksource.com">Li<attribute:strong>nk</attribute:strong></attribute:a>' +
  1922. '</container:li>' +
  1923. '</container:ul>' +
  1924. '</container:blockquote>';
  1925. const expected = '' +
  1926. '<blockquote>' +
  1927. '<ul>' +
  1928. '<li>Quoted <strong>item 1</strong></li>' +
  1929. '<li>Item 2</li>' +
  1930. '<li><a href="https://cksource.com">Li<strong>nk</strong></a></li>' +
  1931. '</ul>' +
  1932. '</blockquote>';
  1933. viewRoot._appendChild( parse( content ) );
  1934. renderer.markToSync( 'children', viewRoot );
  1935. renderer.render();
  1936. expect( domRoot.innerHTML ).to.equal( expected );
  1937. viewRoot._removeChildren( 0, viewRoot.childCount );
  1938. viewRoot._appendChild( parse( content + content ) );
  1939. const domBQ = domRoot.childNodes[ 0 ];
  1940. const domUL = domBQ.childNodes[ 0 ];
  1941. const domLI1 = domUL.childNodes[ 0 ];
  1942. const domLI2 = domUL.childNodes[ 1 ];
  1943. const domLI3 = domUL.childNodes[ 2 ];
  1944. renderer.markToSync( 'children', viewRoot );
  1945. renderer.render();
  1946. // Assert content.
  1947. expect( domRoot.innerHTML ).to.equal( expected + expected );
  1948. // Assert if DOM elements did not change.
  1949. expect( domRoot.childNodes[ 0 ] ).to.equal( domBQ );
  1950. expect( domBQ.childNodes[ 0 ] ).to.equal( domUL );
  1951. expect( domUL.childNodes[ 0 ] ).to.equal( domLI1 );
  1952. expect( domUL.childNodes[ 1 ] ).to.equal( domLI2 );
  1953. expect( domUL.childNodes[ 2 ] ).to.equal( domLI3 );
  1954. // Assert mappings.
  1955. const domMappings = renderer.domConverter._domToViewMapping;
  1956. expect( domMappings.get( domBQ ) ).to.equal( viewRoot.getChild( 0 ) );
  1957. expect( domMappings.get( domUL ) ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  1958. expect( domMappings.get( domLI1 ) ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  1959. expect( domMappings.get( domLI2 ) ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ).getChild( 1 ) );
  1960. expect( domMappings.get( domLI3 ) ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ).getChild( 2 ) );
  1961. // Assert if new view elements are bind to new DOM elements.
  1962. const viewMappings = renderer.domConverter._domToViewMapping;
  1963. expect( viewMappings.get( viewRoot.getChild( 1 ) ) ).not.equal( domBQ );
  1964. expect( viewMappings.get( viewRoot.getChild( 1 ).getChild( 0 ) ) ).not.equal( domUL );
  1965. expect( viewMappings.get( viewRoot.getChild( 1 ).getChild( 0 ).getChild( 0 ) ) ).not.equal( domLI1 );
  1966. expect( viewMappings.get( viewRoot.getChild( 1 ).getChild( 0 ).getChild( 1 ) ) ).not.equal( domLI2 );
  1967. expect( viewMappings.get( viewRoot.getChild( 1 ).getChild( 0 ).getChild( 2 ) ) ).not.equal( domLI3 );
  1968. } );
  1969. it( 'should handle complex view replace', () => {
  1970. const content = '' +
  1971. '<container:h2>He' +
  1972. '<attribute:i>ading 1</attribute:i>' +
  1973. '</container:h2>' +
  1974. '<container:p>Ph ' +
  1975. '<attribute:strong>Bold</attribute:strong>' +
  1976. '<attribute:a href="https://ckeditor.com">' +
  1977. '<attribute:strong>Lin<attribute:i>k</attribute:i></attribute:strong>' +
  1978. '</attribute:a>' +
  1979. '</container:p>' +
  1980. '<container:blockquote>' +
  1981. '<container:ul>' +
  1982. '<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
  1983. '<container:li>Item 2</container:li>' +
  1984. '<container:li>' +
  1985. '<attribute:a href="https://cksource.com">Li<attribute:strong>nk</attribute:strong></attribute:a>' +
  1986. '</container:li>' +
  1987. '</container:ul>' +
  1988. '</container:blockquote>';
  1989. const replacement = '' +
  1990. '<container:p>' +
  1991. '1' +
  1992. '<attribute:i>A</attribute:i>' +
  1993. '</container:p>' +
  1994. '<container:p>' +
  1995. '<attribute:a href="https://cksource.com">' +
  1996. 'Li' +
  1997. '<attribute:strong>nk</attribute:strong>' +
  1998. '</attribute:a>' +
  1999. '</container:p>' +
  2000. '<container:h1>' +
  2001. 'Heading ' +
  2002. '<attribute:strong>1</attribute:strong>' +
  2003. '</container:h1>' +
  2004. '<container:h2>' +
  2005. '<attribute:a href="https://ckeditor.com">Heading 2</attribute:a>' +
  2006. '</container:h2>' +
  2007. '<container:h3>' +
  2008. 'Heading' +
  2009. '<attribute:i> 3</attribute:i>' +
  2010. '</container:h3>' +
  2011. '<container:blockquote>' +
  2012. 'Foo Bar Baz' +
  2013. '</container:blockquote>' +
  2014. '<container:ul>' +
  2015. '<container:li>' +
  2016. 'Item ' +
  2017. '<attribute:strong>1</attribute:strong>' +
  2018. '</container:li>' +
  2019. '<container:li>' +
  2020. '<attribute:a href="https://ckeditor.com">Item</attribute:a>' +
  2021. ' 2' +
  2022. '</container:li>' +
  2023. '</container:ul>';
  2024. viewRoot._appendChild( parse( content ) );
  2025. renderer.markToSync( 'children', viewRoot );
  2026. renderer.render();
  2027. expect( domRoot.innerHTML ).to.equal( '' +
  2028. '<h2>He<i>ading 1</i></h2>' +
  2029. '<p>Ph <strong>Bold</strong><a href="https://ckeditor.com"><strong>Lin<i>k</i></strong></a></p>' +
  2030. '<blockquote><ul>' +
  2031. '<li>Quoted <strong>item 1</strong></li>' +
  2032. '<li>Item 2</li><li><a href="https://cksource.com">Li<strong>nk</strong></a></li>' +
  2033. '</ul></blockquote>' );
  2034. viewRoot._removeChildren( 0, viewRoot.childCount );
  2035. viewRoot._appendChild( parse( replacement ) );
  2036. renderer.markToSync( 'children', viewRoot );
  2037. renderer.render();
  2038. // Here we just check if new DOM structure was properly rendered.
  2039. expect( domRoot.innerHTML ).to.equal( '' +
  2040. '<p>1<i>A</i></p>' +
  2041. '<p><a href="https://cksource.com">Li<strong>nk</strong></a></p>' +
  2042. '<h1>Heading <strong>1</strong></h1>' +
  2043. '<h2><a href="https://ckeditor.com">Heading 2</a></h2>' +
  2044. '<h3>Heading<i> 3</i></h3>' +
  2045. '<blockquote>Foo Bar Baz</blockquote>' +
  2046. '<ul><li>Item <strong>1</strong></li><li><a href="https://ckeditor.com">Item</a> 2</li></ul>' );
  2047. } );
  2048. it( 'should handle br elements while refreshing bindings', () => {
  2049. const expected = `<p>Foo Bar</p><p>${ BR_FILLER( document ).outerHTML }</p>`; // eslint-disable-line new-cap
  2050. viewRoot._appendChild( parse( '<container:p>Foo Bar</container:p><container:p></container:p>' ) );
  2051. renderer.markToSync( 'children', viewRoot );
  2052. renderer.render();
  2053. expect( domRoot.innerHTML ).to.equal( expected );
  2054. // There is a case in Safari that during accent panel navigation on macOS our 'BR_FILLER' is replaced with
  2055. // just '<br>' element which breaks accent composition in an empty paragraph. It also throws an error while
  2056. // refreshing mappings in a renderer. Simulate such behaviour (#1354).
  2057. domRoot.childNodes[ 1 ].innerHTML = '<br>';
  2058. viewRoot._removeChildren( 1 );
  2059. viewRoot._insertChild( 1, parse( '<container:p></container:p>' ) );
  2060. renderer.markToSync( 'children', viewRoot );
  2061. renderer.render();
  2062. expect( domRoot.innerHTML ).to.equal( expected );
  2063. } );
  2064. it( 'should handle list to paragraph conversion', () => {
  2065. const view = '' +
  2066. '<container:ol>' +
  2067. '<container:li>Item 1' +
  2068. '<container:ol>' +
  2069. '<container:li>Item 2</container:li>' +
  2070. '</container:ol>' +
  2071. '</container:li>' +
  2072. '</container:ol>' +
  2073. '<container:p>Paragraph</container:p>' +
  2074. '<container:ol>' +
  2075. '<container:li>Item 3' +
  2076. '<container:ol>' +
  2077. '<container:li>Item 4</container:li>' +
  2078. '</container:ol>' +
  2079. '</container:li>' +
  2080. '</container:ol>';
  2081. viewRoot._appendChild( parse( view ) );
  2082. renderer.markToSync( 'children', viewRoot );
  2083. renderer.render();
  2084. expect( domRoot.innerHTML ).to.equal(
  2085. '<ol><li>Item 1<ol><li>Item 2</li></ol></li></ol><p>Paragraph</p><ol><li>Item 3<ol><li>Item 4</li></ol></li></ol>' );
  2086. const viewOL1 = viewRoot.getChild( 0 );
  2087. viewOL1.getChild( 0 )._removeChildren( 1 );
  2088. viewRoot._removeChildren( 2 );
  2089. viewRoot._insertChild( 1, parse( '<container:p>Item 2</container:p>' ) );
  2090. viewRoot._insertChild( 3, parse( '<container:p>Item 3</container:p>' ) );
  2091. viewRoot._insertChild( 4, parse( '<container:ol><container:li>Item 4</container:li></container:ol>' ) );
  2092. const domOL1 = domRoot.childNodes[ 0 ];
  2093. const domOL2 = domRoot.childNodes[ 2 ];
  2094. const domP = domRoot.childNodes[ 1 ];
  2095. renderer.markToSync( 'children', viewRoot );
  2096. renderer.markToSync( 'children', viewOL1.getChild( 0 ) );
  2097. renderer.render();
  2098. expect( domRoot.innerHTML ).to.equal(
  2099. '<ol><li>Item 1</li></ol><p>Item 2</p><p>Paragraph</p><p>Item 3</p><ol><li>Item 4</li></ol>' );
  2100. expect( domRoot.childNodes[ 0 ] ).to.equal( domOL1 );
  2101. expect( domRoot.childNodes[ 2 ] ).to.equal( domP );
  2102. expect( domRoot.childNodes[ 4 ] ).to.equal( domOL2 );
  2103. } );
  2104. it( 'should handle attributes change in replaced elements', () => {
  2105. const view = '' +
  2106. '<container:ol>' +
  2107. '<container:li data-index="1" align="left">Item 1</container:li>' +
  2108. '</container:ol>' +
  2109. '<container:p>Paragraph ' +
  2110. '<attribute:a href="123">Link</attribute:a>' +
  2111. '</container:p>' +
  2112. '<container:p id="p1"><attribute:i>Bar</attribute:i>Baz</container:p>';
  2113. viewRoot._appendChild( parse( view ) );
  2114. renderer.markToSync( 'children', viewRoot );
  2115. renderer.render();
  2116. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( normalizeHtml(
  2117. '<ol><li data-index="1" align="left">Item 1</li></ol>' +
  2118. '<p>Paragraph <a href="123">Link</a></p><p id="p1"><i>Bar</i>Baz</p>' ) );
  2119. const viewOL = viewRoot.getChild( 0 );
  2120. viewOL._removeChildren( 0 );
  2121. viewOL._insertChild( 0, parse( '<container:li data-index="2" data-attr="foo">Item 1</container:li>' ) );
  2122. const viewP1 = viewRoot.getChild( 1 );
  2123. viewP1._removeChildren( 1 );
  2124. viewP1._insertChild( 1, parse( '<attribute:a href="456" class="cke">Foo</attribute:a>' ) );
  2125. viewRoot._removeChildren( 2 );
  2126. viewRoot._insertChild( 2, parse( '<container:p>Bar</container:p>' ) );
  2127. renderer.markToSync( 'children', viewRoot );
  2128. renderer.markToSync( 'children', viewOL );
  2129. renderer.markToSync( 'children', viewP1 );
  2130. renderer.render();
  2131. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( normalizeHtml(
  2132. '<ol><li data-index="2" data-attr="foo">Item 1</li></ol>' +
  2133. '<p>Paragraph <a href="456" class="cke">Foo</a></p><p>Bar</p>' ) );
  2134. } );
  2135. it( 'should handle classes change in replaced elements', () => {
  2136. const view = '' +
  2137. '<container:ol>' +
  2138. '<container:li class="foo1 bar2 baz3">Item 1</container:li>' +
  2139. '</container:ol>' +
  2140. '<container:p><attribute:i class="i1 i2">Bar</attribute:i>Baz</container:p>';
  2141. viewRoot._appendChild( parse( view ) );
  2142. renderer.markToSync( 'children', viewRoot );
  2143. renderer.render();
  2144. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( normalizeHtml(
  2145. '<ol><li class="foo1 bar2 baz3">Item 1</li></ol><p><i class="i1 i2">Bar</i>Baz</p>' ) );
  2146. const viewOL = viewRoot.getChild( 0 );
  2147. const oldViewLI = viewOL.getChild( 0 );
  2148. viewOL._removeChildren( 0 );
  2149. viewOL._insertChild( 0, parse( '<container:li class="bar2 baz4 bax5">Item 1</container:li>' ) );
  2150. const oldViewP = viewRoot.getChild( 1 );
  2151. viewRoot._removeChildren( 1 );
  2152. viewRoot._insertChild( 1, parse( '<container:p class="p1 p2"><attribute:i>Foo</attribute:i></container:p>' ) );
  2153. renderer.markToSync( 'attributes', oldViewLI );
  2154. renderer.markToSync( 'attributes', oldViewP );
  2155. renderer.markToSync( 'children', viewRoot );
  2156. renderer.markToSync( 'children', viewOL );
  2157. renderer.render();
  2158. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( normalizeHtml(
  2159. '<ol><li class="bar2 baz4 bax5">Item 1</li></ol><p class="p1 p2"><i>Foo</i></p>' ) );
  2160. } );
  2161. it( 'should handle styles change in replaced elements', () => {
  2162. const view = '' +
  2163. '<container:ol>' +
  2164. '<container:li style="color:#000;font-weight:bold;">Foo</container:li>' +
  2165. '<container:li>Bar ' +
  2166. '<attribute:i>' +
  2167. '<attribute:b style="color:#00F;background-color:#000;font-size:12px;">Baz</attribute:b>' +
  2168. ' Bax</attribute:i>' +
  2169. '</container:li>' +
  2170. '</container:ol>';
  2171. viewRoot._appendChild( parse( view ) );
  2172. renderer.markToSync( 'children', viewRoot );
  2173. renderer.render();
  2174. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( normalizeHtml(
  2175. '<ol><li style="color:#000;font-weight:bold;">Foo</li>' +
  2176. '<li>Bar <i><b style="color:#00F;background-color:#000;font-size:12px;">Baz</b> Bax</i></li></ol>' ) );
  2177. const viewOL = viewRoot.getChild( 0 );
  2178. const viewLI1 = viewOL.getChild( 0 );
  2179. const viewLI2 = viewOL.getChild( 1 );
  2180. viewLI1._removeStyle( 'font-weight' );
  2181. viewLI1._setStyle( { color: '#FFF' } );
  2182. viewLI2._setStyle( { 'font-weight': 'bold' } );
  2183. viewLI2._removeChildren( 0, viewLI2.childCount );
  2184. viewLI2._insertChild( 0, parse( 'Ba1 <attribute:i style="color:#000;border-width:1px;">Ba3 ' +
  2185. '<attribute:b style="font-size:15px;">Ba2</attribute:b></attribute:i>' ) );
  2186. renderer.markToSync( 'attributes', viewLI1 );
  2187. renderer.markToSync( 'attributes', viewLI2 );
  2188. renderer.markToSync( 'children', viewRoot );
  2189. renderer.markToSync( 'children', viewLI2 );
  2190. renderer.render();
  2191. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( normalizeHtml(
  2192. '<ol><li style="color:#FFF;">Foo</li>' +
  2193. '<li style="font-weight:bold;">Ba1 <i style="color:#000;border-width:1px;">Ba3 ' +
  2194. '<b style="font-size:15px;">Ba2</b></i></li></ol>' ) );
  2195. } );
  2196. } );
  2197. } );
  2198. describe( '#922', () => {
  2199. let view, viewDoc, viewRoot, domRoot, converter;
  2200. beforeEach( () => {
  2201. view = new View();
  2202. viewDoc = view.document;
  2203. domRoot = document.createElement( 'div' );
  2204. document.body.appendChild( domRoot );
  2205. viewRoot = createViewRoot( viewDoc );
  2206. view.attachDomRoot( domRoot );
  2207. converter = view.domConverter;
  2208. } );
  2209. it( 'should properly render unwrapped attributes #1', () => {
  2210. setViewData( view,
  2211. '<container:p>' +
  2212. '[<attribute:italic>' +
  2213. '<attribute:strong>f</attribute:strong>' +
  2214. '</attribute:italic>]' +
  2215. '<attribute:strong>oo</attribute:strong>' +
  2216. '</container:p>'
  2217. );
  2218. // Render it to DOM to create initial DOM <-> view mappings.
  2219. view.render();
  2220. // Unwrap italic attribute element.
  2221. view.change( writer => {
  2222. writer.unwrap( viewDoc.selection.getFirstRange(), new ViewAttributeElement( 'italic' ) );
  2223. } );
  2224. expect( getViewData( view ) ).to.equal( '<p>[<strong>foo</strong>]</p>' );
  2225. // Re-render changes in view to DOM.
  2226. view.render();
  2227. // Check if DOM is rendered correctly.
  2228. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( '<p><strong>foo</strong></p>' );
  2229. expect( checkMappings() ).to.be.true;
  2230. } );
  2231. it( 'should properly render unwrapped attributes #2', () => {
  2232. setViewData( view,
  2233. '<container:p>' +
  2234. '[<attribute:italic>' +
  2235. '<attribute:strong>foo</attribute:strong>' +
  2236. '</attribute:italic>]' +
  2237. '</container:p>' );
  2238. // Render it to DOM to create initial DOM <-> view mappings.
  2239. view.render();
  2240. // Unwrap italic attribute element and change text inside.
  2241. view.change( writer => {
  2242. writer.unwrap( viewDoc.selection.getFirstRange(), new ViewAttributeElement( 'italic' ) );
  2243. } );
  2244. viewRoot.getChild( 0 ).getChild( 0 ).getChild( 0 )._data = 'bar';
  2245. expect( getViewData( view ) ).to.equal( '<p>[<strong>bar</strong>]</p>' );
  2246. // Re-render changes in view to DOM.
  2247. view.render();
  2248. // Check if DOM is rendered correctly.
  2249. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( '<p><strong>bar</strong></p>' );
  2250. expect( checkMappings() ).to.be.true;
  2251. } );
  2252. it( 'should properly render if text is changed and element is inserted into same node #1', () => {
  2253. setViewData( view,
  2254. '<container:p>foo</container:p>'
  2255. );
  2256. // Render it to DOM to create initial DOM <-> view mappings.
  2257. view.render();
  2258. // Change text and insert new element into paragraph.
  2259. const textNode = viewRoot.getChild( 0 ).getChild( 0 );
  2260. textNode._data = 'foobar';
  2261. view.change( writer => {
  2262. writer.insert( ViewPosition.createAfter( textNode ), new ViewAttributeElement( 'img' ) );
  2263. } );
  2264. expect( getViewData( view ) ).to.equal( '<p>foobar<img></img></p>' );
  2265. // Re-render changes in view to DOM.
  2266. view.render();
  2267. // Check if DOM is rendered correctly.
  2268. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( '<p>foobar<img></img></p>' );
  2269. expect( checkMappings() ).to.be.true;
  2270. } );
  2271. it( 'should properly render if text is changed and element is inserted into same node #2', () => {
  2272. setViewData( view,
  2273. '<container:p>foo</container:p>'
  2274. );
  2275. // Render it to DOM to create initial DOM <-> view mappings.
  2276. view.render();
  2277. // Change text and insert new element into paragraph.
  2278. const textNode = viewRoot.getChild( 0 ).getChild( 0 );
  2279. textNode._data = 'foobar';
  2280. view.change( writer => {
  2281. writer.insert( ViewPosition.createBefore( textNode ), new ViewAttributeElement( 'img' ) );
  2282. } );
  2283. expect( getViewData( view ) ).to.equal( '<p><img></img>foobar</p>' );
  2284. // Re-render changes in view to DOM.
  2285. view.render();
  2286. // Check if DOM is rendered correctly.
  2287. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( '<p><img></img>foobar</p>' );
  2288. expect( checkMappings() ).to.be.true;
  2289. } );
  2290. it( 'should not unbind elements that are removed and reinserted to DOM', () => {
  2291. setViewData( view,
  2292. '<container:p>' +
  2293. '<attribute:b></attribute:b>' +
  2294. '<attribute:i></attribute:i>' +
  2295. '<attribute:span></attribute:span>' +
  2296. '</container:p>'
  2297. );
  2298. // Render it to DOM to create initial DOM <-> view mappings.
  2299. view.render();
  2300. // Remove first element and reinsert it at the end.
  2301. const container = viewRoot.getChild( 0 );
  2302. const firstElement = container.getChild( 0 );
  2303. view.change( writer => {
  2304. writer.remove( ViewRange.createOn( firstElement ) );
  2305. writer.insert( new ViewPosition( container, 2 ), firstElement );
  2306. } );
  2307. expect( getViewData( view ) ).to.equal( '<p><i></i><span></span><b></b></p>' );
  2308. // Re-render changes in view to DOM.
  2309. view.render();
  2310. // Check if DOM is rendered correctly.
  2311. expect( normalizeHtml( domRoot.innerHTML ) ).to.equal( '<p><i></i><span></span><b></b></p>' );
  2312. expect( checkMappings() ).to.be.true;
  2313. } );
  2314. // Checks if every node in DOM tree is mapped to the view.
  2315. function checkMappings() {
  2316. const domWalker = document.createTreeWalker( domRoot, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT );
  2317. while ( domWalker.nextNode() ) {
  2318. const node = domWalker.currentNode;
  2319. if ( !converter.mapDomToView( node ) && !converter.findCorrespondingViewText( node ) ) {
  2320. return false;
  2321. }
  2322. }
  2323. return true;
  2324. }
  2325. } );
  2326. describe( '_markDescendantTextToSync', () => {
  2327. let viewRoot;
  2328. beforeEach( () => {
  2329. viewRoot = new ViewElement( 'div' );
  2330. renderer.markedTexts.clear();
  2331. renderer.markedAttributes.clear();
  2332. renderer.markedChildren.clear();
  2333. } );
  2334. it( 'should handle null values', () => {
  2335. // Such situation occurs when renderer encounters inline filler in 'renderer._updateChildren'.
  2336. renderer._markDescendantTextToSync( null );
  2337. expect( renderer.markedChildren.size ).to.equal( 0 );
  2338. expect( renderer.markedAttributes.size ).to.equal( 0 );
  2339. expect( renderer.markedTexts.size ).to.equal( 0 );
  2340. } );
  2341. it( 'should handle element nodes', () => {
  2342. const viewP = parse( '<container:p>foo<attribute:b>bar<attribute:i>baz</attribute:i></attribute:b></container:p>' );
  2343. viewRoot._appendChild( viewP );
  2344. renderer._markDescendantTextToSync( viewP );
  2345. expect( renderer.markedChildren.size ).to.equal( 0 );
  2346. expect( renderer.markedAttributes.size ).to.equal( 0 );
  2347. expect( renderer.markedTexts.size ).to.equal( 3 );
  2348. } );
  2349. it( 'should handle text nodes', () => {
  2350. const viewP = parse( '<container:p><attribute:b>bar<attribute:i>baz</attribute:i></attribute:b></container:p>' );
  2351. viewRoot._appendChild( viewP );
  2352. renderer._markDescendantTextToSync( viewP.getChild( 0 ).getChild( 0 ) );
  2353. expect( renderer.markedChildren.size ).to.equal( 0 );
  2354. expect( renderer.markedAttributes.size ).to.equal( 0 );
  2355. expect( renderer.markedTexts.size ).to.equal( 1 );
  2356. } );
  2357. it( 'should handle document fragment', () => {
  2358. const fragment = new DocumentFragment();
  2359. renderer._markDescendantTextToSync( fragment );
  2360. expect( renderer.markedChildren.size ).to.equal( 0 );
  2361. expect( renderer.markedAttributes.size ).to.equal( 0 );
  2362. expect( renderer.markedTexts.size ).to.equal( 0 );
  2363. } );
  2364. it( 'should handle empty element nodes', () => {
  2365. const viewP = parse( '<container:p></container:p>' );
  2366. viewRoot._appendChild( viewP );
  2367. renderer._markDescendantTextToSync( viewP );
  2368. expect( renderer.markedChildren.size ).to.equal( 0 );
  2369. expect( renderer.markedAttributes.size ).to.equal( 0 );
  2370. expect( renderer.markedTexts.size ).to.equal( 0 );
  2371. } );
  2372. } );
  2373. describe( '_updateText', () => {
  2374. let viewRoot, domRoot;
  2375. beforeEach( () => {
  2376. viewRoot = new ViewElement( 'div' );
  2377. domRoot = document.createElement( 'div' );
  2378. document.body.appendChild( domRoot );
  2379. domConverter.bindElements( domRoot, viewRoot );
  2380. renderer.markedTexts.clear();
  2381. renderer.markedAttributes.clear();
  2382. renderer.markedChildren.clear();
  2383. renderer.isFocused = true;
  2384. } );
  2385. afterEach( () => {
  2386. domRoot.remove();
  2387. } );
  2388. it( 'should update text - change on end', () => {
  2389. const viewText = new ViewText( 'foo' );
  2390. viewRoot._appendChild( viewText );
  2391. renderer.markToSync( 'children', viewRoot );
  2392. renderer.render();
  2393. expect( domRoot.childNodes.length ).to.equal( 1 );
  2394. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  2395. viewText._data = 'fobar';
  2396. renderer._updateText( viewText, {} );
  2397. expect( domRoot.childNodes.length ).to.equal( 1 );
  2398. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'fobar' );
  2399. } );
  2400. it( 'should update text - change on start', () => {
  2401. const viewText = new ViewText( 'foo' );
  2402. viewRoot._appendChild( viewText );
  2403. renderer.markToSync( 'children', viewRoot );
  2404. renderer.render();
  2405. expect( domRoot.childNodes.length ).to.equal( 1 );
  2406. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  2407. viewText._data = 'baro';
  2408. renderer._updateText( viewText, {} );
  2409. expect( domRoot.childNodes.length ).to.equal( 1 );
  2410. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'baro' );
  2411. } );
  2412. it( 'should update text - change in the middle', () => {
  2413. const viewText = new ViewText( 'foobar' );
  2414. viewRoot._appendChild( viewText );
  2415. renderer.markToSync( 'children', viewRoot );
  2416. renderer.render();
  2417. expect( domRoot.childNodes.length ).to.equal( 1 );
  2418. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foobar' );
  2419. viewText._data = 'fobazr';
  2420. renderer._updateText( viewText, {} );
  2421. expect( domRoot.childNodes.length ).to.equal( 1 );
  2422. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'fobazr' );
  2423. } );
  2424. it( 'should update text - empty expected', () => {
  2425. const viewText = new ViewText( 'foo' );
  2426. viewRoot._appendChild( viewText );
  2427. renderer.markToSync( 'children', viewRoot );
  2428. renderer.render();
  2429. expect( domRoot.childNodes.length ).to.equal( 1 );
  2430. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  2431. viewText._data = '';
  2432. renderer._updateText( viewText, {} );
  2433. expect( domRoot.childNodes.length ).to.equal( 1 );
  2434. expect( domRoot.childNodes[ 0 ].data ).to.equal( '' );
  2435. } );
  2436. it( 'should update text - empty actual', () => {
  2437. const viewText = new ViewText( '' );
  2438. viewRoot._appendChild( viewText );
  2439. renderer.markToSync( 'children', viewRoot );
  2440. renderer.render();
  2441. expect( domRoot.childNodes.length ).to.equal( 1 );
  2442. expect( domRoot.childNodes[ 0 ].data ).to.equal( '' );
  2443. viewText._data = 'fobar';
  2444. renderer._updateText( viewText, {} );
  2445. expect( domRoot.childNodes.length ).to.equal( 1 );
  2446. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'fobar' );
  2447. } );
  2448. it( 'should handle filler during text modifications', () => {
  2449. const viewText = new ViewText( 'foo' );
  2450. viewRoot._appendChild( viewText );
  2451. renderer.markToSync( 'children', viewRoot );
  2452. renderer.render();
  2453. expect( domRoot.childNodes.length ).to.equal( 1 );
  2454. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'foo' );
  2455. // 1. Insert filler.
  2456. renderer._updateText( viewText, {
  2457. inlineFillerPosition: {
  2458. parent: viewText.parent,
  2459. offset: 0
  2460. }
  2461. } );
  2462. expect( domRoot.childNodes.length ).to.equal( 1 );
  2463. expect( domRoot.childNodes[ 0 ].data ).to.equal( INLINE_FILLER + 'foo' );
  2464. // 2. Edit text - filler should be preserved.
  2465. viewText._data = 'barfoo';
  2466. renderer._updateText( viewText, {
  2467. inlineFillerPosition: {
  2468. parent: viewText.parent,
  2469. offset: 0
  2470. }
  2471. } );
  2472. expect( domRoot.childNodes.length ).to.equal( 1 );
  2473. expect( domRoot.childNodes[ 0 ].data ).to.equal( INLINE_FILLER + 'barfoo' );
  2474. // 3. Remove filler.
  2475. renderer._updateText( viewText, {} );
  2476. expect( domRoot.childNodes.length ).to.equal( 1 );
  2477. expect( domRoot.childNodes[ 0 ].data ).to.equal( 'barfoo' );
  2478. } );
  2479. it( 'should handle filler during text modifications - empty text', () => {
  2480. const viewText = new ViewText( '' );
  2481. viewRoot._appendChild( viewText );
  2482. renderer.markToSync( 'children', viewRoot );
  2483. renderer.render();
  2484. expect( domRoot.childNodes.length ).to.equal( 1 );
  2485. expect( domRoot.childNodes[ 0 ].data ).to.equal( '' );
  2486. // 1. Insert filler.
  2487. renderer._updateText( viewText, {
  2488. inlineFillerPosition: {
  2489. parent: viewText.parent,
  2490. offset: 0
  2491. }
  2492. } );
  2493. expect( domRoot.childNodes.length ).to.equal( 1 );
  2494. expect( domRoot.childNodes[ 0 ].data ).to.equal( INLINE_FILLER );
  2495. // 2. Edit text - filler should be preserved.
  2496. viewText._data = 'foo';
  2497. renderer._updateText( viewText, {
  2498. inlineFillerPosition: {
  2499. parent: viewText.parent,
  2500. offset: 0
  2501. }
  2502. } );
  2503. expect( domRoot.childNodes.length ).to.equal( 1 );
  2504. expect( domRoot.childNodes[ 0 ].data ).to.equal( INLINE_FILLER + 'foo' );
  2505. // 3. Remove filler.
  2506. viewText._data = '';
  2507. renderer._updateText( viewText, {} );
  2508. expect( domRoot.childNodes.length ).to.equal( 1 );
  2509. expect( domRoot.childNodes[ 0 ].data ).to.equal( '' );
  2510. } );
  2511. it( 'should handle filler during text modifications inside inline element', () => {
  2512. const viewB = new ViewElement( 'b' );
  2513. const viewText = new ViewText( 'foo' );
  2514. viewB._appendChild( viewText );
  2515. viewRoot._appendChild( viewB );
  2516. renderer.markToSync( 'children', viewRoot );
  2517. renderer.render();
  2518. expect( domRoot.childNodes.length ).to.equal( 1 );
  2519. expect( domRoot.childNodes[ 0 ].tagName ).to.equal( 'B' );
  2520. expect( domRoot.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  2521. expect( domRoot.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  2522. // 1. Insert filler.
  2523. renderer._updateText( viewText, {
  2524. inlineFillerPosition: {
  2525. parent: viewText.parent,
  2526. offset: 0
  2527. }
  2528. } );
  2529. expect( domRoot.childNodes.length ).to.equal( 1 );
  2530. expect( domRoot.childNodes[ 0 ].tagName ).to.equal( 'B' );
  2531. expect( domRoot.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  2532. expect( domRoot.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER + 'foo' );
  2533. // 2. Edit text - filler should be preserved.
  2534. viewText._data = 'bar';
  2535. renderer._updateText( viewText, {
  2536. inlineFillerPosition: {
  2537. parent: viewText.parent,
  2538. offset: 0
  2539. }
  2540. } );
  2541. expect( domRoot.childNodes.length ).to.equal( 1 );
  2542. expect( domRoot.childNodes[ 0 ].tagName ).to.equal( 'B' );
  2543. expect( domRoot.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  2544. expect( domRoot.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( INLINE_FILLER + 'bar' );
  2545. // 3. Remove filler.
  2546. viewText._data = 'bar';
  2547. renderer._updateText( viewText, {} );
  2548. expect( domRoot.childNodes.length ).to.equal( 1 );
  2549. expect( domRoot.childNodes[ 0 ].tagName ).to.equal( 'B' );
  2550. expect( domRoot.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  2551. expect( domRoot.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  2552. } );
  2553. } );
  2554. } );
  2555. function renderAndExpectNoChanges( renderer, domRoot ) {
  2556. const config = {
  2557. childList: true,
  2558. characterData: true,
  2559. characterDataOldValue: true,
  2560. subtree: true
  2561. };
  2562. const mutationObserver = new window.MutationObserver( () => {
  2563. throw 'There should be not mutations';
  2564. } );
  2565. mutationObserver.observe( domRoot, config );
  2566. renderer.render();
  2567. const records = mutationObserver.takeRecords();
  2568. mutationObserver.disconnect();
  2569. expect( records.length ).to.equal( 0 );
  2570. }