renderer.js 157 KB

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