renderer.js 148 KB

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