renderer.js 145 KB

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