renderer.js 134 KB

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