downcasthelpers.js 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678
  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 console */
  6. import EditingController from '../../src/controller/editingcontroller';
  7. import Model from '../../src/model/model';
  8. import ModelElement from '../../src/model/element';
  9. import ModelText from '../../src/model/text';
  10. import ViewElement from '../../src/view/element';
  11. import ViewAttributeElement from '../../src/view/attributeelement';
  12. import ViewContainerElement from '../../src/view/containerelement';
  13. import ViewUIElement from '../../src/view/uielement';
  14. import ViewText from '../../src/view/text';
  15. import ViewDocument from '../../src/view/document';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. import DowncastHelpers, {
  18. clearAttributes,
  19. convertCollapsedSelection,
  20. convertRangeSelection,
  21. createViewElementFromHighlightDescriptor,
  22. insertText
  23. } from '../../src/conversion/downcasthelpers';
  24. import Mapper from '../../src/conversion/mapper';
  25. import DowncastDispatcher from '../../src/conversion/downcastdispatcher';
  26. import { stringify as stringifyView } from '../../src/dev-utils/view';
  27. import View from '../../src/view/view';
  28. import createViewRoot from '../view/_utils/createroot';
  29. import { setData as setModelData } from '../../src/dev-utils/model';
  30. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  31. import { StylesProcessor } from '../../src/view/stylesmap';
  32. import DowncastWriter from '../../src/view/downcastwriter';
  33. function insertBazSlot( writer, modelRoot ) {
  34. const slot = writer.createElement( 'slot' );
  35. const paragraph = writer.createElement( 'paragraph' );
  36. writer.insertText( 'baz', paragraph, 0 );
  37. writer.insert( paragraph, slot, 0 );
  38. writer.insert( slot, modelRoot.getChild( 0 ), 'end' );
  39. }
  40. describe( 'DowncastHelpers', () => {
  41. let model, modelRoot, viewRoot, downcastHelpers, controller, modelRootStart;
  42. beforeEach( () => {
  43. model = new Model();
  44. const modelDoc = model.document;
  45. modelRoot = modelDoc.createRoot();
  46. controller = new EditingController( model, new StylesProcessor() );
  47. // Set name of view root the same as dom root.
  48. // This is a mock of attaching view root to dom root.
  49. controller.view.document.getRoot()._name = 'div';
  50. viewRoot = controller.view.document.getRoot();
  51. downcastHelpers = new DowncastHelpers( [ controller.downcastDispatcher ] );
  52. modelRootStart = model.createPositionAt( modelRoot, 0 );
  53. } );
  54. describe( 'elementToElement()', () => {
  55. it( 'should be chainable', () => {
  56. expect( downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ) ).to.equal( downcastHelpers );
  57. } );
  58. it( 'config.view is a string', () => {
  59. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  60. model.change( writer => {
  61. writer.insertElement( 'paragraph', modelRoot, 0 );
  62. } );
  63. expectResult( '<p></p>' );
  64. } );
  65. it( 'can be overwritten using converterPriority', () => {
  66. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  67. downcastHelpers.elementToElement( { model: 'paragraph', view: 'foo', converterPriority: 'high' } );
  68. model.change( writer => {
  69. writer.insertElement( 'paragraph', modelRoot, 0 );
  70. } );
  71. expectResult( '<foo></foo>' );
  72. } );
  73. it( 'config.view is a view element definition', () => {
  74. downcastHelpers.elementToElement( {
  75. model: 'fancyParagraph',
  76. view: {
  77. name: 'p',
  78. classes: 'fancy'
  79. }
  80. } );
  81. model.change( writer => {
  82. writer.insertElement( 'fancyParagraph', modelRoot, 0 );
  83. } );
  84. expectResult( '<p class="fancy"></p>' );
  85. } );
  86. it( 'config.view is a function', () => {
  87. downcastHelpers.elementToElement( {
  88. model: 'heading',
  89. view: ( modelElement, { writer } ) => writer.createContainerElement( 'h' + modelElement.getAttribute( 'level' ) )
  90. } );
  91. model.change( writer => {
  92. writer.insertElement( 'heading', { level: 2 }, modelRoot, 0 );
  93. } );
  94. expectResult( '<h2></h2>' );
  95. } );
  96. describe( 'config.triggerBy', () => {
  97. describe( 'with simple block view structure', () => {
  98. beforeEach( () => {
  99. model.schema.register( 'simpleBlock', {
  100. allowIn: '$root',
  101. allowAttributes: [ 'toStyle', 'toClass' ]
  102. } );
  103. downcastHelpers.elementToElement( {
  104. model: 'simpleBlock',
  105. view: ( modelElement, { writer } ) => {
  106. return writer.createContainerElement( 'div', getViewAttributes( modelElement ) );
  107. },
  108. triggerBy: [
  109. 'attribute:toStyle:simpleBlock',
  110. 'attribute:toClass:simpleBlock'
  111. ]
  112. } );
  113. } );
  114. it( 'should convert on insert', () => {
  115. model.change( writer => {
  116. writer.insertElement( 'simpleBlock', modelRoot, 0 );
  117. } );
  118. expectResult( '<div></div>' );
  119. } );
  120. it( 'should convert on attribute set', () => {
  121. setModelData( model, '<simpleBlock></simpleBlock>' );
  122. model.change( writer => {
  123. writer.setAttribute( 'toStyle', 'display:block', modelRoot.getChild( 0 ) );
  124. } );
  125. expectResult( '<div style="display:block"></div>' );
  126. } );
  127. it( 'should convert on attribute change', () => {
  128. setModelData( model, '<simpleBlock toStyle="display:block"></simpleBlock>' );
  129. model.change( writer => {
  130. writer.setAttribute( 'toStyle', 'display:inline', modelRoot.getChild( 0 ) );
  131. } );
  132. expectResult( '<div style="display:inline"></div>' );
  133. } );
  134. it( 'should convert on attribute remove', () => {
  135. setModelData( model, '<simpleBlock toStyle="display:block"></simpleBlock>' );
  136. model.change( writer => {
  137. writer.removeAttribute( 'toStyle', modelRoot.getChild( 0 ) );
  138. } );
  139. expectResult( '<div></div>' );
  140. } );
  141. it( 'should convert on one attribute add and other remove', () => {
  142. setModelData( model, '<simpleBlock toStyle="display:block"></simpleBlock>' );
  143. model.change( writer => {
  144. writer.removeAttribute( 'toStyle', modelRoot.getChild( 0 ) );
  145. writer.setAttribute( 'toClass', true, modelRoot.getChild( 0 ) );
  146. } );
  147. expectResult( '<div class="is-classy"></div>' );
  148. } );
  149. it( 'should do nothing if non-triggerBy attribute has changed', () => {
  150. setModelData( model, '<simpleBlock></simpleBlock>' );
  151. model.change( writer => {
  152. writer.setAttribute( 'notTriggered', true, modelRoot.getChild( 0 ) );
  153. } );
  154. expectResult( '<div></div>' );
  155. } );
  156. } );
  157. describe( 'with complex view structure - no children allowed', () => {
  158. beforeEach( () => {
  159. model.schema.register( 'complex', {
  160. allowIn: '$root',
  161. allowAttributes: [ 'toStyle', 'toClass' ]
  162. } );
  163. downcastHelpers.elementToElement( {
  164. model: 'complex',
  165. view: ( modelElement, { writer } ) => {
  166. const outer = writer.createContainerElement( 'div', { class: 'complex-outer' } );
  167. const inner = writer.createContainerElement( 'div', getViewAttributes( modelElement ) );
  168. writer.insert( writer.createPositionAt( outer, 0 ), inner );
  169. return outer;
  170. },
  171. triggerBy: [
  172. 'attribute:toStyle:complex',
  173. 'attribute:toClass:complex'
  174. ]
  175. } );
  176. } );
  177. it( 'should convert on insert', () => {
  178. model.change( writer => {
  179. writer.insertElement( 'complex', modelRoot, 0 );
  180. } );
  181. expectResult( '<div class="complex-outer"><div></div></div>' );
  182. } );
  183. it( 'should convert on attribute set', () => {
  184. setModelData( model, '<complex></complex>' );
  185. model.change( writer => {
  186. writer.setAttribute( 'toStyle', 'display:block', modelRoot.getChild( 0 ) );
  187. } );
  188. expectResult( '<div class="complex-outer"><div style="display:block"></div></div>' );
  189. } );
  190. it( 'should convert on attribute change', () => {
  191. setModelData( model, '<complex toStyle="display:block"></complex>' );
  192. model.change( writer => {
  193. writer.setAttribute( 'toStyle', 'display:inline', modelRoot.getChild( 0 ) );
  194. } );
  195. expectResult( '<div class="complex-outer"><div style="display:inline"></div></div>' );
  196. } );
  197. it( 'should convert on attribute remove', () => {
  198. setModelData( model, '<complex toStyle="display:block"></complex>' );
  199. model.change( writer => {
  200. writer.removeAttribute( 'toStyle', modelRoot.getChild( 0 ) );
  201. } );
  202. expectResult( '<div class="complex-outer"><div></div></div>' );
  203. } );
  204. it( 'should convert on one attribute add and other remove', () => {
  205. setModelData( model, '<complex toStyle="display:block"></complex>' );
  206. model.change( writer => {
  207. writer.removeAttribute( 'toStyle', modelRoot.getChild( 0 ) );
  208. writer.setAttribute( 'toClass', true, modelRoot.getChild( 0 ) );
  209. } );
  210. expectResult( '<div class="complex-outer"><div class="is-classy"></div></div>' );
  211. } );
  212. it( 'should do nothing if non-triggerBy attribute has changed', () => {
  213. setModelData( model, '<complex></complex>' );
  214. model.change( writer => {
  215. writer.setAttribute( 'notTriggered', true, modelRoot.getChild( 0 ) );
  216. } );
  217. expectResult( '<div class="complex-outer"><div></div></div>' );
  218. } );
  219. } );
  220. describe( 'with complex view structure (without slots)', () => {
  221. beforeEach( () => {
  222. model.schema.register( 'complex', {
  223. allowIn: '$root',
  224. allowAttributes: [ 'toStyle', 'toClass' ]
  225. } );
  226. downcastHelpers.elementToElement( {
  227. model: 'complex',
  228. view: ( modelElement, { writer, mapper } ) => {
  229. const outer = writer.createContainerElement( 'c-outer' );
  230. const inner = writer.createContainerElement( 'c-inner', getViewAttributes( modelElement ) );
  231. writer.insert( writer.createPositionAt( outer, 0 ), inner );
  232. mapper.bindElements( modelElement, outer ); // Need for nested mapping
  233. mapper.bindElements( modelElement, inner );
  234. return outer;
  235. },
  236. triggerBy: [
  237. 'attribute:toStyle:complex',
  238. 'attribute:toClass:complex'
  239. ]
  240. } );
  241. model.schema.register( 'paragraph', {
  242. inheritAllFrom: '$block',
  243. allowIn: 'complex'
  244. } );
  245. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  246. } );
  247. it( 'should convert on insert', () => {
  248. model.change( writer => {
  249. writer.insertElement( 'complex', modelRoot, 0 );
  250. } );
  251. expectResult( '<c-outer><c-inner></c-inner></c-outer>' );
  252. } );
  253. it( 'should convert on attribute set', () => {
  254. setModelData( model, '<complex></complex>' );
  255. model.change( writer => {
  256. writer.setAttribute( 'toStyle', 'display:block', modelRoot.getChild( 0 ) );
  257. } );
  258. expectResult( '<c-outer><c-inner style="display:block"></c-inner></c-outer>' );
  259. } );
  260. it( 'should convert on attribute remove', () => {
  261. setModelData( model, '<complex toStyle="display:block"></complex>' );
  262. model.change( writer => {
  263. writer.removeAttribute( 'toStyle', modelRoot.getChild( 0 ) );
  264. } );
  265. expectResult( '<c-outer><c-inner></c-inner></c-outer>' );
  266. } );
  267. it( 'should convert on one attribute add and other remove', () => {
  268. setModelData( model, '<complex toStyle="display:block"></complex>' );
  269. model.change( writer => {
  270. writer.removeAttribute( 'toStyle', modelRoot.getChild( 0 ) );
  271. writer.setAttribute( 'toClass', true, modelRoot.getChild( 0 ) );
  272. } );
  273. expectResult( '<c-outer><c-inner class="is-classy"></c-inner></c-outer>' );
  274. } );
  275. it( 'should do nothing if non-triggerBy attribute has changed', () => {
  276. setModelData( model, '<complex></complex>' );
  277. model.change( writer => {
  278. writer.setAttribute( 'notTriggered', true, modelRoot.getChild( 0 ) );
  279. } );
  280. expectResult( '<c-outer><c-inner></c-inner></c-outer>' );
  281. } );
  282. describe( 'memoization', () => {
  283. it( 'should create new element on re-converting element', () => {
  284. setModelData( model, '<complex></complex>' );
  285. const renderedView = viewRoot.getChild( 0 );
  286. model.change( writer => {
  287. writer.setAttribute( 'toStyle', 'display:block', modelRoot.getChild( 0 ) );
  288. } );
  289. const viewAfterReRender = viewRoot.getChild( 0 );
  290. expect( viewAfterReRender ).to.not.equal( renderedView );
  291. } );
  292. // Skipped, as it would require two-level mapping. See https://github.com/ckeditor/ckeditor5/issues/1589.
  293. // Doable as a similar case works in table scenario for table cells (table is refreshed).
  294. it.skip( 'should not re-create child elements on re-converting element', () => {
  295. setModelData( model, '<complex><paragraph>Foo bar baz</paragraph></complex>' );
  296. expectResult( '<c-outer><c-inner><p>Foo bar baz</p></c-inner></c-outer>' );
  297. const renderedViewView = viewRoot.getChild( 0 ).getChild( 0 );
  298. model.change( writer => {
  299. writer.setAttribute( 'toStyle', 'display:block', modelRoot.getChild( 0 ) );
  300. } );
  301. const viewAfterReRender = viewRoot.getChild( 0 ).getChild( 0 );
  302. expect( viewAfterReRender ).to.equal( renderedViewView );
  303. } );
  304. } );
  305. } );
  306. describe( 'with complex view structure (slot conversion)', () => {
  307. beforeEach( () => {
  308. model.schema.register( 'complex', {
  309. allowIn: '$root',
  310. allowAttributes: [ 'classForMain', 'classForWrap', 'attributeToElement' ]
  311. } );
  312. downcastHelpers.elementToElement( {
  313. model: 'complex',
  314. view: ( modelElement, { writer, mapper } ) => {
  315. const classForMain = !!modelElement.getAttribute( 'classForMain' );
  316. const classForWrap = !!modelElement.getAttribute( 'classForWrap' );
  317. const attributeToElement = !!modelElement.getAttribute( 'attributeToElement' );
  318. const outer = writer.createContainerElement( 'div', {
  319. class: `complex-slots${ classForMain ? ' with-class' : '' }`
  320. } );
  321. const inner = writer.createContainerElement( 'div', {
  322. class: `slots${ classForWrap ? ' with-class' : '' }`
  323. } );
  324. if ( attributeToElement ) {
  325. const optional = writer.createEmptyElement( 'div', { class: 'optional' } );
  326. writer.insert( writer.createPositionAt( outer, 0 ), optional );
  327. }
  328. writer.insert( writer.createPositionAt( outer, 'end' ), inner );
  329. mapper.bindElements( modelElement, inner );
  330. for ( const slot of modelElement.getChildren() ) {
  331. const viewSlot = writer.createContainerElement( 'div', { class: 'slot' } );
  332. writer.insert( writer.createPositionAt( inner, slot.index ), viewSlot );
  333. mapper.bindSlotElements( slot, viewSlot );
  334. }
  335. return outer;
  336. },
  337. triggerBy: [
  338. 'attribute:classForMain:complex',
  339. 'attribute:classForWrap:complex',
  340. 'attribute:attributeToElement:complex',
  341. 'insert:slot',
  342. 'remove:slot'
  343. ]
  344. } );
  345. model.schema.register( 'slot', {
  346. allowIn: 'complex'
  347. } );
  348. model.schema.register( 'paragraph', {
  349. inheritAllFrom: '$block',
  350. allowIn: 'slot'
  351. } );
  352. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  353. } );
  354. it( 'should convert on insert', () => {
  355. model.change( writer => {
  356. writer.insertElement( 'complex', modelRoot, 0 );
  357. } );
  358. expectResult( '<div class="complex-slots"><div class="slots"></div></div>' );
  359. } );
  360. it( 'should convert on attribute set (main element)', () => {
  361. setModelData( model, '<complex></complex>' );
  362. model.change( writer => {
  363. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  364. } );
  365. expectResult( '<div class="complex-slots with-class"><div class="slots"></div></div>' );
  366. } );
  367. it( 'should convert on attribute set (other element)', () => {
  368. setModelData( model, '<complex></complex>' );
  369. model.change( writer => {
  370. writer.setAttribute( 'classForWrap', true, modelRoot.getChild( 0 ) );
  371. } );
  372. expectResult( '<div class="complex-slots"><div class="slots with-class"></div></div>' );
  373. } );
  374. it( 'should convert on attribute set (insert new view element)', () => {
  375. setModelData( model, '<complex></complex>' );
  376. model.change( writer => {
  377. writer.setAttribute( 'attributeToElement', true, modelRoot.getChild( 0 ) );
  378. } );
  379. expectResult( '<div class="complex-slots"><div class="optional"></div><div class="slots"></div></div>' );
  380. } );
  381. it( 'should convert element with slots', () => {
  382. setModelData( model,
  383. '<complex>' +
  384. '<slot><paragraph>foo</paragraph></slot>' +
  385. '<slot><paragraph>bar</paragraph></slot>' +
  386. '</complex>' );
  387. expectResult(
  388. '<div class="complex-slots">' +
  389. '<div class="slots">' +
  390. '<div class="slot"><p>foo</p></div>' +
  391. '<div class="slot"><p>bar</p></div>' +
  392. '</div>' +
  393. '</div>'
  394. );
  395. } );
  396. it( 'should convert element on adding slot', () => {
  397. setModelData( model,
  398. '<complex>' +
  399. '<slot><paragraph>foo</paragraph></slot>' +
  400. '<slot><paragraph>bar</paragraph></slot>' +
  401. '</complex>' );
  402. model.change( writer => {
  403. insertBazSlot( writer, modelRoot );
  404. } );
  405. expectResult(
  406. '<div class="complex-slots">' +
  407. '<div class="slots">' +
  408. '<div class="slot"><p>foo</p></div>' +
  409. '<div class="slot"><p>bar</p></div>' +
  410. '<div class="slot"><p>baz</p></div>' +
  411. '</div>' +
  412. '</div>'
  413. );
  414. } );
  415. it( 'should convert element on removing slot', () => {
  416. setModelData( model,
  417. '<complex>' +
  418. '<slot><paragraph>foo</paragraph></slot>' +
  419. '<slot><paragraph>bar</paragraph></slot>' +
  420. '</complex>' );
  421. model.change( writer => {
  422. writer.remove( modelRoot.getChild( 0 ).getChild( 0 ) );
  423. } );
  424. expectResult(
  425. '<div class="complex-slots">' +
  426. '<div class="slots">' +
  427. '<div class="slot"><p>bar</p></div>' +
  428. '</div>' +
  429. '</div>'
  430. );
  431. } );
  432. it( 'should convert element on multiple triggers (remove + insert)', () => {
  433. setModelData( model,
  434. '<complex>' +
  435. '<slot><paragraph>foo</paragraph></slot>' +
  436. '<slot><paragraph>bar</paragraph></slot>' +
  437. '</complex>' );
  438. model.change( writer => {
  439. writer.remove( modelRoot.getChild( 0 ).getChild( 0 ) );
  440. insertBazSlot( writer, modelRoot );
  441. } );
  442. expectResult(
  443. '<div class="complex-slots">' +
  444. '<div class="slots">' +
  445. '<div class="slot"><p>bar</p></div>' +
  446. '<div class="slot"><p>baz</p></div>' +
  447. '</div>' +
  448. '</div>'
  449. );
  450. } );
  451. it( 'should convert element on multiple triggers (remove + attribute)', () => {
  452. setModelData( model,
  453. '<complex>' +
  454. '<slot><paragraph>foo</paragraph></slot>' +
  455. '<slot><paragraph>bar</paragraph></slot>' +
  456. '</complex>' );
  457. model.change( writer => {
  458. writer.remove( modelRoot.getChild( 0 ).getChild( 0 ) );
  459. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  460. } );
  461. expectResult(
  462. '<div class="complex-slots with-class">' +
  463. '<div class="slots">' +
  464. '<div class="slot"><p>bar</p></div>' +
  465. '</div>' +
  466. '</div>'
  467. );
  468. } );
  469. it( 'should convert element on multiple triggers (insert + attribute)', () => {
  470. setModelData( model,
  471. '<complex>' +
  472. '<slot><paragraph>foo</paragraph></slot>' +
  473. '<slot><paragraph>bar</paragraph></slot>' +
  474. '</complex>' );
  475. model.change( writer => {
  476. insertBazSlot( writer, modelRoot );
  477. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  478. } );
  479. expectResult(
  480. '<div class="complex-slots with-class">' +
  481. '<div class="slots">' +
  482. '<div class="slot"><p>foo</p></div>' +
  483. '<div class="slot"><p>bar</p></div>' +
  484. '<div class="slot"><p>baz</p></div>' +
  485. '</div>' +
  486. '</div>'
  487. );
  488. } );
  489. describe( 'memoization', () => {
  490. it( 'should create new element on re-converting element', () => {
  491. setModelData( model, '<complex>' +
  492. '<slot><paragraph>foo</paragraph></slot>' +
  493. '<slot><paragraph>bar</paragraph></slot>' +
  494. '</complex>'
  495. );
  496. const complexView = viewRoot.getChild( 0 );
  497. model.change( writer => {
  498. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  499. } );
  500. const viewAfterReRender = viewRoot.getChild( 0 );
  501. expect( viewAfterReRender ).to.not.equal( complexView );
  502. } );
  503. it( 'should not re-create slot\'s child elements on re-converting main element (attribute changed)', () => {
  504. setModelData( model, '<complex>' +
  505. '<slot><paragraph>foo</paragraph></slot>' +
  506. '<slot><paragraph>bar</paragraph></slot>' +
  507. '</complex>'
  508. );
  509. const [ main, slotOne, slotOneChild, slotTwo, slotTwoChild ] = getNodes();
  510. model.change( writer => {
  511. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  512. } );
  513. const [ mainAfter, slotOneAfter, slotOneChildAfter, slotTwoAfter, slotTwoChildAfter ] = getNodes();
  514. expect( mainAfter, 'main view' ).to.not.equal( main );
  515. expect( slotOneAfter, 'first slot view' ).to.not.equal( slotOne );
  516. expect( slotTwoAfter, 'second slot view' ).to.not.equal( slotTwo );
  517. expect( slotOneChildAfter, 'first slot paragraph view' ).to.equal( slotOneChild );
  518. expect( slotTwoChildAfter, 'second slot paragraph view' ).to.equal( slotTwoChild );
  519. } );
  520. it( 'should not re-create slot\'s child elements on re-converting main element (slot added)', () => {
  521. setModelData( model, '<complex>' +
  522. '<slot><paragraph>foo</paragraph></slot>' +
  523. '<slot><paragraph>bar</paragraph></slot>' +
  524. '</complex>'
  525. );
  526. const [ main, slotOne, slotOneChild, slotTwo, slotTwoChild ] = getNodes();
  527. model.change( writer => {
  528. const slot = writer.createElement( 'slot' );
  529. const paragraph = writer.createElement( 'paragraph' );
  530. writer.insertText( 'baz', paragraph, 0 );
  531. writer.insert( paragraph, slot, 0 );
  532. writer.insert( slot, modelRoot.getChild( 0 ), 'end' );
  533. } );
  534. const [
  535. mainAfter,
  536. slotOneAfter, slotOneChildAfter,
  537. slotTwoAfter, slotTwoChildAfter,
  538. slot3, slot3Child
  539. ] = getNodes();
  540. expect( mainAfter, 'main view' ).to.not.equal( main );
  541. expect( slotOneAfter, 'first slot view' ).to.not.equal( slotOne );
  542. expect( slotTwoAfter, 'second slot view' ).to.not.equal( slotTwo );
  543. expect( slotOneChildAfter, 'first slot paragraph view' ).to.equal( slotOneChild );
  544. expect( slotTwoChildAfter, 'second slot paragraph view' ).to.equal( slotTwoChild );
  545. expect( slot3, 'third slot view' ).to.not.be.undefined;
  546. expect( slot3Child, 'third slot paragraph view' ).to.not.be.undefined;
  547. } );
  548. /**
  549. * Returns a generator that yields elements as [ mainView, slot1, childOfSlot1, slot2, childOfSlot2, ... ].
  550. */
  551. function* getNodes() {
  552. const main = viewRoot.getChild( 0 );
  553. yield main;
  554. const slotWrap = main.getChild( 0 );
  555. for ( const slot of slotWrap.getChildren() ) {
  556. const slotOneChild = slot.getChild( 0 );
  557. yield slot;
  558. yield slotOneChild;
  559. }
  560. }
  561. } );
  562. } );
  563. // Skipped, as it would require two-level mapping. See https://github.com/ckeditor/ckeditor5/issues/1589.
  564. describe.skip( 'with complex view structure (slot conversion atomic converters for some changes)', () => {
  565. beforeEach( () => {
  566. model.schema.register( 'complex', {
  567. allowIn: '$root',
  568. allowAttributes: [ 'classForMain', 'classForWrap', 'attributeToElement' ]
  569. } );
  570. function createViewSlot( slot, { writer, mapper } ) {
  571. const viewSlot = writer.createContainerElement( 'div', { class: 'slot' } );
  572. mapper.bindSlotElements( slot, viewSlot );
  573. return viewSlot;
  574. }
  575. downcastHelpers.elementToElement( {
  576. model: 'complex',
  577. view: ( modelElement, { writer, mapper, consumable } ) => {
  578. const classForMain = !!modelElement.getAttribute( 'classForMain' );
  579. const classForWrap = !!modelElement.getAttribute( 'classForWrap' );
  580. const attributeToElement = !!modelElement.getAttribute( 'attributeToElement' );
  581. const outer = writer.createContainerElement( 'div', {
  582. class: `complex-slots${ classForMain ? ' with-class' : '' }`
  583. } );
  584. const inner = writer.createContainerElement( 'div', {
  585. class: `slots${ classForWrap ? ' with-class' : '' }`
  586. } );
  587. if ( attributeToElement ) {
  588. const optional = writer.createEmptyElement( 'div', { class: 'optional' } );
  589. writer.insert( writer.createPositionAt( outer, 0 ), optional );
  590. }
  591. writer.insert( writer.createPositionAt( outer, 'end' ), inner );
  592. mapper.bindElements( modelElement, outer );
  593. mapper.bindElements( modelElement, inner );
  594. for ( const slot of modelElement.getChildren() ) {
  595. const viewSlot = createViewSlot( slot, { writer, mapper } );
  596. writer.insert( writer.createPositionAt( inner, slot.index ), viewSlot );
  597. consumable.consume( slot, 'insert' );
  598. }
  599. return outer;
  600. },
  601. triggerBy: [
  602. 'attribute:classForMain:complex',
  603. 'attribute:classForWrap:complex',
  604. 'attribute:attributeToElement:complex'
  605. // Contrary to the previous test - do not act on slot insert/remove: 'insert:slot', 'remove:slot'.
  606. ]
  607. } );
  608. downcastHelpers.elementToElement( {
  609. model: 'slot',
  610. view: createViewSlot
  611. } );
  612. model.schema.register( 'slot', {
  613. allowIn: 'complex'
  614. } );
  615. model.schema.register( 'paragraph', {
  616. inheritAllFrom: '$block',
  617. allowIn: 'slot'
  618. } );
  619. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  620. } );
  621. it( 'should convert on insert', () => {
  622. model.change( writer => {
  623. writer.insertElement( 'complex', modelRoot, 0 );
  624. } );
  625. expectResult( '<div class="complex-slots"><div class="slots"></div></div>' );
  626. } );
  627. // TODO: add memoization check - as this is need.
  628. it( 'should convert on attribute set (main element)', () => {
  629. setModelData( model, '<complex></complex>' );
  630. model.change( writer => {
  631. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  632. } );
  633. expectResult( '<div class="complex-slots with-class"><div class="slots"></div></div>' );
  634. } );
  635. it( 'should convert on attribute set (other element)', () => {
  636. setModelData( model, '<complex></complex>' );
  637. model.change( writer => {
  638. writer.setAttribute( 'classForWrap', true, modelRoot.getChild( 0 ) );
  639. } );
  640. expectResult( '<div class="complex-slots"><div class="slots with-class"></div></div>' );
  641. } );
  642. it( 'should convert on attribute set (insert new view element)', () => {
  643. setModelData( model, '<complex></complex>' );
  644. model.change( writer => {
  645. writer.setAttribute( 'attributeToElement', true, modelRoot.getChild( 0 ) );
  646. } );
  647. expectResult( '<div class="complex-slots"><div class="optional"></div><div class="slots"></div></div>' );
  648. } );
  649. it( 'should convert element with slots', () => {
  650. setModelData( model,
  651. '<complex>' +
  652. '<slot><paragraph>foo</paragraph></slot>' +
  653. '<slot><paragraph>bar</paragraph></slot>' +
  654. '</complex>' );
  655. expectResult(
  656. '<div class="complex-slots">' +
  657. '<div class="slots">' +
  658. '<div class="slot"><p>foo</p></div>' +
  659. '<div class="slot"><p>bar</p></div>' +
  660. '</div>' +
  661. '</div>'
  662. );
  663. } );
  664. it( 'should not convert element on adding slot', () => {
  665. setModelData( model,
  666. '<complex>' +
  667. '<slot><paragraph>foo</paragraph></slot>' +
  668. '<slot><paragraph>bar</paragraph></slot>' +
  669. '</complex>' );
  670. model.change( writer => {
  671. const slot = writer.createElement( 'slot' );
  672. const paragraph = writer.createElement( 'paragraph' );
  673. writer.insertText( 'baz', paragraph, 0 );
  674. writer.insert( paragraph, slot, 0 );
  675. writer.insert( slot, modelRoot.getChild( 0 ), 'end' );
  676. } );
  677. expectResult(
  678. '<div class="complex-slots">' +
  679. '<div class="slots">' +
  680. '<div class="slot"><p>foo</p></div>' +
  681. '<div class="slot"><p>bar</p></div>' +
  682. '<div class="slot"><p>baz</p></div>' +
  683. '</div>' +
  684. '</div>'
  685. );
  686. } );
  687. it( 'should not convert element on removing slot', () => {
  688. setModelData( model,
  689. '<complex>' +
  690. '<slot><paragraph>foo</paragraph></slot>' +
  691. '<slot><paragraph>bar</paragraph></slot>' +
  692. '</complex>' );
  693. model.change( writer => {
  694. writer.remove( modelRoot.getChild( 0 ).getChild( 0 ) );
  695. } );
  696. expectResult(
  697. '<div class="complex-slots">' +
  698. '<div class="slots">' +
  699. '<div class="slot"><p>bar</p></div>' +
  700. '</div>' +
  701. '</div>'
  702. );
  703. } );
  704. it( 'should convert element on a trigger and block atomic converters (remove + attribute)', () => {
  705. setModelData( model,
  706. '<complex>' +
  707. '<slot><paragraph>foo</paragraph></slot>' +
  708. '<slot><paragraph>bar</paragraph></slot>' +
  709. '</complex>' );
  710. model.change( writer => {
  711. writer.remove( modelRoot.getChild( 0 ).getChild( 0 ) );
  712. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  713. } );
  714. expectResult(
  715. '<div class="complex-slots with-class">' +
  716. '<div class="slots">' +
  717. '<div class="slot"><p>bar</p></div>' +
  718. '</div>' +
  719. '</div>'
  720. );
  721. } );
  722. it( 'should convert element on a trigger and block atomic converters (insert + attribute)', () => {
  723. setModelData( model,
  724. '<complex>' +
  725. '<slot><paragraph>foo</paragraph></slot>' +
  726. '<slot><paragraph>bar</paragraph></slot>' +
  727. '</complex>' );
  728. model.change( writer => {
  729. writer.insert( modelRoot.getChild( 0 ).getChild( 0 ) );
  730. writer.setAttribute( 'classForMain', true, modelRoot.getChild( 0 ) );
  731. } );
  732. expectResult(
  733. '<div class="complex-slots with-class">' +
  734. '<div class="slots">' +
  735. '<div class="slot"><p>foo</p></div>' +
  736. '<div class="slot"><p>bar</p></div>' +
  737. '<div class="slot"><p>baz</p></div>' +
  738. '</div>' +
  739. '</div>'
  740. );
  741. } );
  742. } );
  743. function getViewAttributes( modelElement ) {
  744. // TODO decide whether below is readable:
  745. const toStyle = modelElement.hasAttribute( 'toStyle' ) && { style: modelElement.getAttribute( 'toStyle' ) };
  746. const toClass = modelElement.hasAttribute( 'toClass' ) && { class: 'is-classy' };
  747. const attributes = {
  748. ...toStyle,
  749. ...toClass
  750. };
  751. return attributes;
  752. }
  753. } );
  754. } );
  755. describe( 'attributeToElement()', () => {
  756. beforeEach( () => {
  757. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  758. } );
  759. it( 'should be chainable', () => {
  760. expect( downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } ) ).to.equal( downcastHelpers );
  761. } );
  762. it( 'config.view is a string', () => {
  763. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  764. model.change( writer => {
  765. writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
  766. } );
  767. expectResult( '<strong>foo</strong>' );
  768. } );
  769. it( 'can be overwritten using converterPriority', () => {
  770. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  771. downcastHelpers.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
  772. model.change( writer => {
  773. writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
  774. } );
  775. expectResult( '<b>foo</b>' );
  776. } );
  777. it( 'config.view is a view element definition', () => {
  778. downcastHelpers.attributeToElement( {
  779. model: 'invert',
  780. view: {
  781. name: 'span',
  782. classes: [ 'font-light', 'bg-dark' ]
  783. }
  784. } );
  785. model.change( writer => {
  786. writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
  787. } );
  788. expectResult( '<span class="bg-dark font-light">foo</span>' );
  789. expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  790. } );
  791. it( 'config.view allows specifying the element\'s priority', () => {
  792. downcastHelpers.attributeToElement( {
  793. model: 'invert',
  794. view: {
  795. name: 'span',
  796. priority: 5
  797. }
  798. } );
  799. model.change( writer => {
  800. writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
  801. } );
  802. expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
  803. } );
  804. it( 'model attribute value is enum', () => {
  805. downcastHelpers.attributeToElement( {
  806. model: {
  807. key: 'fontSize',
  808. values: [ 'big', 'small' ]
  809. },
  810. view: {
  811. big: {
  812. name: 'span',
  813. styles: {
  814. 'font-size': '1.2em'
  815. }
  816. },
  817. small: {
  818. name: 'span',
  819. styles: {
  820. 'font-size': '0.8em'
  821. },
  822. priority: 5
  823. }
  824. }
  825. } );
  826. model.change( writer => {
  827. writer.insertText( 'foo', { fontSize: 'big' }, modelRoot, 0 );
  828. } );
  829. expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  830. expectResult( '<span style="font-size:1.2em">foo</span>' );
  831. model.change( writer => {
  832. writer.setAttribute( 'fontSize', 'small', modelRoot.getChild( 0 ) );
  833. } );
  834. expectResult( '<span style="font-size:0.8em">foo</span>' );
  835. expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
  836. model.change( writer => {
  837. writer.removeAttribute( 'fontSize', modelRoot.getChild( 0 ) );
  838. } );
  839. expectResult( 'foo' );
  840. } );
  841. it( 'config.view is a function', () => {
  842. downcastHelpers.attributeToElement( {
  843. model: 'bold',
  844. view: ( modelAttributeValue, { writer } ) => {
  845. return writer.createAttributeElement( 'span', { style: 'font-weight:' + modelAttributeValue } );
  846. }
  847. } );
  848. model.change( writer => {
  849. writer.insertText( 'foo', { bold: '500' }, modelRoot, 0 );
  850. } );
  851. expectResult( '<span style="font-weight:500">foo</span>' );
  852. } );
  853. it( 'config.model.name is given', () => {
  854. downcastHelpers.attributeToElement( {
  855. model: {
  856. key: 'color',
  857. name: '$text'
  858. },
  859. view: ( modelAttributeValue, { writer } ) => {
  860. return writer.createAttributeElement( 'span', { style: 'color:' + modelAttributeValue } );
  861. }
  862. } );
  863. downcastHelpers.elementToElement( {
  864. model: 'smiley',
  865. view: ( modelElement, { writer } ) => {
  866. return writer.createEmptyElement( 'img', {
  867. src: 'smile.jpg',
  868. class: 'smiley'
  869. } );
  870. }
  871. } );
  872. model.change( writer => {
  873. writer.insertText( 'foo', { color: '#FF0000' }, modelRoot, 0 );
  874. writer.insertElement( 'smiley', { color: '#FF0000' }, modelRoot, 3 );
  875. } );
  876. expectResult( '<span style="color:#FF0000">foo</span><img class="smiley" src="smile.jpg"></img>' );
  877. } );
  878. it( 'should not convert if creator returned null', () => {
  879. downcastHelpers.elementToElement( { model: 'div', view: () => null } );
  880. const modelElement = new ModelElement( 'div' );
  881. model.change( writer => {
  882. writer.insert( modelElement, modelRootStart );
  883. } );
  884. expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
  885. } );
  886. it( 'should convert insert/change/remove of attribute in model into wrapping element in a view', () => {
  887. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
  888. downcastHelpers.attributeToElement( {
  889. model: 'bold',
  890. view: ( modelAttributeValue, { writer } ) => writer.createAttributeElement( 'b' )
  891. } );
  892. model.change( writer => {
  893. writer.insert( modelElement, modelRootStart );
  894. } );
  895. expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
  896. model.change( writer => {
  897. writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
  898. } );
  899. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  900. } );
  901. it( 'should convert insert/remove of attribute in model with wrapping element generating function as a parameter', () => {
  902. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { style: 'bold' } ) );
  903. downcastHelpers.attributeToElement( {
  904. model: 'style',
  905. view: ( modelAttributeValue, { writer } ) => {
  906. if ( modelAttributeValue == 'bold' ) {
  907. return writer.createAttributeElement( 'b' );
  908. }
  909. }
  910. } );
  911. model.change( writer => {
  912. writer.insert( modelElement, modelRootStart );
  913. } );
  914. expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
  915. model.change( writer => {
  916. writer.removeAttribute( 'style', writer.createRangeIn( modelElement ) );
  917. } );
  918. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  919. } );
  920. it( 'should update range on re-wrapping attribute (#475)', () => {
  921. const modelElement = new ModelElement( 'paragraph', null, [
  922. new ModelText( 'x' ),
  923. new ModelText( 'foo', { link: 'http://foo.com' } ),
  924. new ModelText( 'x' )
  925. ] );
  926. downcastHelpers.attributeToElement( {
  927. model: 'link',
  928. view: ( modelAttributeValue, { writer } ) => {
  929. return writer.createAttributeElement( 'a', { href: modelAttributeValue } );
  930. }
  931. } );
  932. model.change( writer => {
  933. writer.insert( modelElement, modelRootStart );
  934. } );
  935. expect( viewToString( viewRoot ) ).to.equal( '<div><p>x<a href="http://foo.com">foo</a>x</p></div>' );
  936. // Set new attribute on old link but also on non-linked characters.
  937. model.change( writer => {
  938. writer.setAttribute( 'link', 'http://foobar.com', writer.createRangeIn( modelElement ) );
  939. } );
  940. expect( viewToString( viewRoot ) ).to.equal( '<div><p><a href="http://foobar.com">xfoox</a></p></div>' );
  941. } );
  942. it( 'should support unicode', () => {
  943. const modelElement = new ModelElement( 'paragraph', null, [ 'நி', new ModelText( 'லைக்', { bold: true } ), 'கு' ] );
  944. downcastHelpers.attributeToElement( {
  945. model: 'bold',
  946. view: ( modelAttributeValue, { writer } ) => writer.createAttributeElement( 'b' )
  947. } );
  948. model.change( writer => {
  949. writer.insert( modelElement, modelRootStart );
  950. } );
  951. expect( viewToString( viewRoot ) ).to.equal( '<div><p>நி<b>லைக்</b>கு</p></div>' );
  952. model.change( writer => {
  953. writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
  954. } );
  955. expect( viewToString( viewRoot ) ).to.equal( '<div><p>நிலைக்கு</p></div>' );
  956. } );
  957. it( 'should be possible to override ', () => {
  958. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
  959. downcastHelpers.attributeToElement( {
  960. model: 'bold',
  961. view: ( modelAttributeValue, { writer } ) => writer.createAttributeElement( 'b' )
  962. } );
  963. downcastHelpers.attributeToElement( {
  964. model: 'bold',
  965. view: ( modelAttributeValue, { writer } ) => writer.createAttributeElement( 'strong' ),
  966. converterPriority: 'high'
  967. } );
  968. model.change( writer => {
  969. writer.insert( modelElement, modelRootStart );
  970. } );
  971. expect( viewToString( viewRoot ) ).to.equal( '<div><p><strong>foobar</strong></p></div>' );
  972. } );
  973. it( 'should not convert and not consume if creator function returned null', () => {
  974. sinon.spy( controller.downcastDispatcher, 'fire' );
  975. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { italic: true } ) );
  976. downcastHelpers.attributeToElement( {
  977. model: 'italic',
  978. view: () => null
  979. } );
  980. const spy = sinon.spy();
  981. controller.downcastDispatcher.on( 'attribute:italic', spy );
  982. model.change( writer => {
  983. writer.insert( modelElement, modelRootStart );
  984. } );
  985. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  986. expect( controller.downcastDispatcher.fire.calledWith( 'attribute:italic:$text' ) ).to.be.true;
  987. expect( spy.called ).to.be.true;
  988. } );
  989. } );
  990. describe( 'attributeToAttribute()', () => {
  991. testUtils.createSinonSandbox();
  992. beforeEach( () => {
  993. downcastHelpers.elementToElement( { model: 'image', view: 'img' } );
  994. downcastHelpers.elementToElement( {
  995. model: 'paragraph',
  996. view: ( modelItem, { writer } ) => writer.createContainerElement( 'p' )
  997. } );
  998. downcastHelpers.attributeToAttribute( {
  999. model: 'class',
  1000. view: 'class'
  1001. } );
  1002. } );
  1003. it( 'should be chainable', () => {
  1004. expect( downcastHelpers.attributeToAttribute( { model: 'source', view: 'src' } ) ).to.equal( downcastHelpers );
  1005. } );
  1006. it( 'config.view is a string', () => {
  1007. downcastHelpers.attributeToAttribute( { model: 'source', view: 'src' } );
  1008. model.change( writer => {
  1009. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  1010. } );
  1011. expectResult( '<img src="foo.jpg"></img>' );
  1012. model.change( writer => {
  1013. writer.removeAttribute( 'source', modelRoot.getChild( 0 ) );
  1014. } );
  1015. expectResult( '<img></img>' );
  1016. } );
  1017. it( 'can be overwritten using converterPriority', () => {
  1018. downcastHelpers.attributeToAttribute( { model: 'source', view: 'href' } );
  1019. downcastHelpers.attributeToAttribute( { model: 'source', view: 'src', converterPriority: 'high' } );
  1020. model.change( writer => {
  1021. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  1022. } );
  1023. expectResult( '<img src="foo.jpg"></img>' );
  1024. } );
  1025. it( 'model element name specified', () => {
  1026. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1027. downcastHelpers.attributeToAttribute( {
  1028. model: {
  1029. name: 'image',
  1030. key: 'source'
  1031. },
  1032. view: 'src'
  1033. } );
  1034. model.change( writer => {
  1035. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  1036. } );
  1037. expectResult( '<img src="foo.jpg"></img>' );
  1038. model.change( writer => {
  1039. writer.rename( modelRoot.getChild( 0 ), 'paragraph' );
  1040. } );
  1041. expectResult( '<p></p>' );
  1042. } );
  1043. it( 'config.view is an object, model attribute value is enum', () => {
  1044. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1045. downcastHelpers.attributeToAttribute( {
  1046. model: {
  1047. key: 'styled',
  1048. values: [ 'dark', 'light' ]
  1049. },
  1050. view: {
  1051. dark: {
  1052. key: 'class',
  1053. value: [ 'styled', 'styled-dark' ]
  1054. },
  1055. light: {
  1056. key: 'class',
  1057. value: [ 'styled', 'styled-light' ]
  1058. }
  1059. }
  1060. } );
  1061. model.change( writer => {
  1062. writer.insertElement( 'paragraph', { styled: 'dark' }, modelRoot, 0 );
  1063. } );
  1064. expectResult( '<p class="styled styled-dark"></p>' );
  1065. model.change( writer => {
  1066. writer.setAttribute( 'styled', 'light', modelRoot.getChild( 0 ) );
  1067. } );
  1068. expectResult( '<p class="styled styled-light"></p>' );
  1069. model.change( writer => {
  1070. writer.removeAttribute( 'styled', modelRoot.getChild( 0 ) );
  1071. } );
  1072. expectResult( '<p></p>' );
  1073. } );
  1074. it( 'config.view is an object, model attribute value is enum, view has style', () => {
  1075. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1076. downcastHelpers.attributeToAttribute( {
  1077. model: {
  1078. key: 'align',
  1079. values: [ 'right', 'center' ]
  1080. },
  1081. view: {
  1082. right: {
  1083. key: 'style',
  1084. value: {
  1085. 'text-align': 'right'
  1086. }
  1087. },
  1088. center: {
  1089. key: 'style',
  1090. value: {
  1091. 'text-align': 'center'
  1092. }
  1093. }
  1094. }
  1095. } );
  1096. model.change( writer => {
  1097. writer.insertElement( 'paragraph', { align: 'right' }, modelRoot, 0 );
  1098. } );
  1099. expectResult( '<p style="text-align:right"></p>' );
  1100. model.change( writer => {
  1101. writer.setAttribute( 'align', 'center', modelRoot.getChild( 0 ) );
  1102. } );
  1103. expectResult( '<p style="text-align:center"></p>' );
  1104. model.change( writer => {
  1105. writer.removeAttribute( 'align', modelRoot.getChild( 0 ) );
  1106. } );
  1107. expectResult( '<p></p>' );
  1108. } );
  1109. it( 'config.view is an object, only name and key are provided', () => {
  1110. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1111. downcastHelpers.attributeToAttribute( {
  1112. model: {
  1113. name: 'paragraph',
  1114. key: 'class'
  1115. },
  1116. view: {
  1117. name: 'paragraph',
  1118. key: 'class'
  1119. }
  1120. } );
  1121. model.change( writer => {
  1122. writer.insertElement( 'paragraph', { class: 'dark' }, modelRoot, 0 );
  1123. } );
  1124. expectResult( '<p class="dark"></p>' );
  1125. model.change( writer => {
  1126. writer.setAttribute( 'class', 'light', modelRoot.getChild( 0 ) );
  1127. } );
  1128. expectResult( '<p class="light"></p>' );
  1129. model.change( writer => {
  1130. writer.removeAttribute( 'class', modelRoot.getChild( 0 ) );
  1131. } );
  1132. expectResult( '<p></p>' );
  1133. } );
  1134. it( 'config.view is a function', () => {
  1135. downcastHelpers.attributeToAttribute( {
  1136. model: 'styled',
  1137. view: ( attributeValue, conversionApi ) => {
  1138. // To ensure conversion API is provided.
  1139. expect( conversionApi.writer ).to.instanceof( DowncastWriter );
  1140. return { key: 'class', value: 'styled-' + attributeValue };
  1141. }
  1142. } );
  1143. model.change( writer => {
  1144. writer.insertElement( 'image', { styled: 'pull-out' }, modelRoot, 0 );
  1145. } );
  1146. expectResult( '<img class="styled-pull-out"></img>' );
  1147. } );
  1148. // #1587
  1149. it( 'config.view and config.model as strings in generic conversion (elements only)', () => {
  1150. const consoleWarnStub = testUtils.sinon.stub( console, 'warn' );
  1151. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1152. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  1153. model.change( writer => {
  1154. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 0 );
  1155. writer.insertElement( 'paragraph', { test: '2' }, modelRoot, 1 );
  1156. } );
  1157. expectResult( '<p test="1"></p><p test="2"></p>' );
  1158. expect( consoleWarnStub.callCount ).to.equal( 0 );
  1159. model.change( writer => {
  1160. writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
  1161. } );
  1162. expectResult( '<p test="1"></p><p></p>' );
  1163. } );
  1164. // #1587
  1165. it( 'config.view and config.model as strings in generic conversion (elements + text)', () => {
  1166. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1167. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  1168. expectToThrowCKEditorError( () => {
  1169. model.change( writer => {
  1170. writer.insertElement( 'paragraph', modelRoot, 0 );
  1171. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 1 );
  1172. writer.insertText( 'Foo', { test: '2' }, modelRoot.getChild( 0 ), 0 );
  1173. writer.insertText( 'Bar', { test: '3' }, modelRoot.getChild( 1 ), 0 );
  1174. } );
  1175. }, /^conversion-attribute-to-attribute-on-text/ );
  1176. } );
  1177. it( 'should convert attribute insert/change/remove on a model node', () => {
  1178. const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  1179. model.change( writer => {
  1180. writer.insert( modelElement, modelRootStart );
  1181. } );
  1182. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  1183. model.change( writer => {
  1184. writer.setAttribute( 'class', 'bar', modelElement );
  1185. } );
  1186. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="bar">foobar</p></div>' );
  1187. model.change( writer => {
  1188. writer.removeAttribute( 'class', modelElement );
  1189. } );
  1190. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1191. } );
  1192. it( 'should be possible to override setAttribute', () => {
  1193. downcastHelpers.attributeToAttribute( {
  1194. model: 'class',
  1195. view: ( evt, data, conversionApi ) => {
  1196. conversionApi.consumable.consume( data.item, 'attribute:class' );
  1197. },
  1198. converterPriority: 'high'
  1199. } );
  1200. model.change( writer => {
  1201. const modelElement = new ModelElement( 'paragraph', { classes: 'foo' }, new ModelText( 'foobar' ) );
  1202. writer.insert( modelElement, modelRootStart );
  1203. } );
  1204. // No attribute set.
  1205. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1206. } );
  1207. it( 'should not convert or consume if element creator returned null', () => {
  1208. const callback = sinon.stub().returns( null );
  1209. downcastHelpers.attributeToAttribute( {
  1210. model: 'class',
  1211. view: callback,
  1212. converterPriority: 'high'
  1213. } );
  1214. const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  1215. model.change( writer => {
  1216. writer.insert( modelElement, modelRootStart );
  1217. } );
  1218. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  1219. sinon.assert.called( callback );
  1220. } );
  1221. } );
  1222. describe( 'markerToElement()', () => {
  1223. let modelText, modelElement, range;
  1224. it( 'should be chainable', () => {
  1225. expect( downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } ) ).to.equal( downcastHelpers );
  1226. } );
  1227. it( 'config.view is a string', () => {
  1228. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  1229. model.change( writer => {
  1230. writer.insertText( 'foo', modelRoot, 0 );
  1231. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  1232. writer.addMarker( 'search', { range, usingOperation: false } );
  1233. } );
  1234. expectResult( 'f<marker-search></marker-search>o<marker-search></marker-search>o' );
  1235. } );
  1236. it( 'can be overwritten using converterPriority', () => {
  1237. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  1238. downcastHelpers.markerToElement( { model: 'search', view: 'search', converterPriority: 'high' } );
  1239. model.change( writer => {
  1240. writer.insertText( 'foo', modelRoot, 0 );
  1241. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  1242. writer.addMarker( 'search', { range, usingOperation: false } );
  1243. } );
  1244. expectResult( 'f<search></search>o<search></search>o' );
  1245. } );
  1246. it( 'config.view is a view element definition', () => {
  1247. downcastHelpers.markerToElement( {
  1248. model: 'search',
  1249. view: {
  1250. name: 'span',
  1251. attributes: {
  1252. 'data-marker': 'search'
  1253. }
  1254. }
  1255. } );
  1256. model.change( writer => {
  1257. writer.insertText( 'foo', modelRoot, 0 );
  1258. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  1259. writer.addMarker( 'search', { range, usingOperation: false } );
  1260. } );
  1261. expectResult( 'f<span data-marker="search"></span>o<span data-marker="search"></span>o' );
  1262. } );
  1263. it( 'config.view is a function', () => {
  1264. downcastHelpers.markerToElement( {
  1265. model: 'search',
  1266. view: ( data, { writer } ) => {
  1267. return writer.createUIElement( 'span', { 'data-marker': 'search', 'data-start': data.isOpening } );
  1268. }
  1269. } );
  1270. model.change( writer => {
  1271. writer.insertText( 'foo', modelRoot, 0 );
  1272. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  1273. writer.addMarker( 'search', { range, usingOperation: false } );
  1274. } );
  1275. expectResult( 'f<span data-marker="search" data-start="true"></span>o<span data-marker="search" data-start="false"></span>o' );
  1276. } );
  1277. describe( 'collapsed range', () => {
  1278. beforeEach( () => {
  1279. modelText = new ModelText( 'foobar' );
  1280. modelElement = new ModelElement( 'paragraph', null, modelText );
  1281. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1282. model.change( writer => {
  1283. writer.insert( modelElement, modelRootStart );
  1284. } );
  1285. range = model.createRange( model.createPositionAt( modelElement, 3 ), model.createPositionAt( modelElement, 3 ) );
  1286. } );
  1287. it( 'should insert and remove ui element', () => {
  1288. downcastHelpers.markerToElement( {
  1289. model: 'marker',
  1290. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  1291. } );
  1292. model.change( writer => {
  1293. writer.addMarker( 'marker', { range, usingOperation: false } );
  1294. } );
  1295. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo<span class="marker"></span>bar</p></div>' );
  1296. model.change( writer => {
  1297. writer.removeMarker( 'marker' );
  1298. } );
  1299. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1300. } );
  1301. it( 'should not convert if consumable was consumed', () => {
  1302. sinon.spy( controller.downcastDispatcher, 'fire' );
  1303. downcastHelpers.markerToElement( {
  1304. model: 'marker',
  1305. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  1306. } );
  1307. controller.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  1308. conversionApi.consumable.consume( data.markerRange, 'addMarker:marker' );
  1309. }, { priority: 'high' } );
  1310. model.change( writer => {
  1311. writer.addMarker( 'marker', { range, usingOperation: false } );
  1312. } );
  1313. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1314. expect( controller.downcastDispatcher.fire.calledWith( 'addMarker:marker' ) );
  1315. } );
  1316. it( 'should not convert if creator returned null', () => {
  1317. downcastHelpers.markerToElement( {
  1318. model: 'marker',
  1319. view: () => null
  1320. } );
  1321. model.change( writer => {
  1322. writer.addMarker( 'marker', { range, usingOperation: false } );
  1323. } );
  1324. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1325. model.change( writer => {
  1326. writer.removeMarker( 'marker' );
  1327. } );
  1328. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1329. } );
  1330. } );
  1331. describe( 'non-collapsed range', () => {
  1332. beforeEach( () => {
  1333. modelText = new ModelText( 'foobar' );
  1334. modelElement = new ModelElement( 'paragraph', null, modelText );
  1335. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1336. model.change( writer => {
  1337. writer.insert( modelElement, modelRootStart );
  1338. } );
  1339. range = model.createRange( model.createPositionAt( modelElement, 2 ), model.createPositionAt( modelElement, 5 ) );
  1340. } );
  1341. it( 'should insert and remove ui element - element as a creator', () => {
  1342. downcastHelpers.markerToElement( {
  1343. model: 'marker',
  1344. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  1345. } );
  1346. model.change( writer => {
  1347. writer.addMarker( 'marker', { range, usingOperation: false } );
  1348. } );
  1349. expect( viewToString( viewRoot ) )
  1350. .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  1351. model.change( writer => {
  1352. writer.removeMarker( 'marker' );
  1353. } );
  1354. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1355. } );
  1356. it( 'should insert and remove ui element - function as a creator', () => {
  1357. downcastHelpers.markerToElement( {
  1358. model: 'marker',
  1359. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': data.markerName } )
  1360. } );
  1361. model.change( writer => {
  1362. writer.addMarker( 'marker', { range, usingOperation: false } );
  1363. } );
  1364. expect( viewToString( viewRoot ) )
  1365. .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  1366. model.change( writer => {
  1367. writer.removeMarker( 'marker' );
  1368. } );
  1369. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1370. } );
  1371. it( 'should insert and remove different opening and ending element', () => {
  1372. downcastHelpers.markerToElement( {
  1373. model: 'marker',
  1374. view: ( data, { writer } ) => {
  1375. if ( data.isOpening ) {
  1376. return writer.createUIElement( 'span', { 'class': data.markerName, 'data-start': true } );
  1377. }
  1378. return writer.createUIElement( 'span', { 'class': data.markerName, 'data-end': true } );
  1379. }
  1380. } );
  1381. model.change( writer => {
  1382. writer.addMarker( 'marker', { range, usingOperation: false } );
  1383. } );
  1384. expect( viewToString( viewRoot ) ).to.equal(
  1385. '<div><p>fo<span class="marker" data-start="true"></span>oba<span class="marker" data-end="true"></span>r</p></div>'
  1386. );
  1387. model.change( writer => {
  1388. writer.removeMarker( 'marker' );
  1389. } );
  1390. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1391. } );
  1392. it( 'should not convert if consumable was consumed', () => {
  1393. sinon.spy( controller.downcastDispatcher, 'fire' );
  1394. downcastHelpers.markerToElement( {
  1395. model: 'marker',
  1396. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  1397. } );
  1398. controller.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  1399. conversionApi.consumable.consume( data.item, 'addMarker:marker' );
  1400. }, { priority: 'high' } );
  1401. model.change( writer => {
  1402. writer.addMarker( 'marker', { range, usingOperation: false } );
  1403. } );
  1404. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1405. expect( controller.downcastDispatcher.fire.calledWith( 'addMarker:marker' ) );
  1406. } );
  1407. } );
  1408. } );
  1409. describe( 'markerToData()', () => {
  1410. let root;
  1411. beforeEach( () => {
  1412. root = model.document.getRoot();
  1413. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  1414. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1415. } );
  1416. it( 'should be chainable', () => {
  1417. expect( downcastHelpers.markerToData( { model: 'search' } ) ).to.equal( downcastHelpers );
  1418. } );
  1419. it( 'default conversion, inside text, non-collapsed, no name', () => {
  1420. downcastHelpers.markerToData( { model: 'search' } );
  1421. setModelData( model, '<paragraph>Fo[ob]ar</paragraph>' );
  1422. model.change( writer => {
  1423. writer.addMarker( 'search', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1424. } );
  1425. expectResult( '<p>Fo<search-start></search-start>ob<search-end></search-end>ar</p>' );
  1426. model.change( writer => {
  1427. writer.removeMarker( 'search' );
  1428. } );
  1429. expectResult( '<p>Foobar</p>' );
  1430. } );
  1431. it( 'default conversion, inside text, non-collapsed, name', () => {
  1432. downcastHelpers.markerToData( { model: 'group' } );
  1433. setModelData( model, '<paragraph>Fo[ob]ar</paragraph>' );
  1434. model.change( writer => {
  1435. writer.addMarker( 'group:foo:bar:baz', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1436. } );
  1437. expectResult( '<p>Fo<group-start name="foo:bar:baz"></group-start>ob<group-end name="foo:bar:baz"></group-end>ar</p>' );
  1438. model.change( writer => {
  1439. writer.removeMarker( 'group:foo:bar:baz' );
  1440. } );
  1441. expectResult( '<p>Foobar</p>' );
  1442. } );
  1443. it( 'default conversion, inside text, collapsed, no name', () => {
  1444. downcastHelpers.markerToData( { model: 'search' } );
  1445. setModelData( model, '<paragraph>Foo[]bar</paragraph>' );
  1446. model.change( writer => {
  1447. writer.addMarker( 'search', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1448. } );
  1449. expectResult( '<p>Foo<search-start></search-start><search-end></search-end>bar</p>' );
  1450. model.change( writer => {
  1451. writer.removeMarker( 'search' );
  1452. } );
  1453. expectResult( '<p>Foobar</p>' );
  1454. } );
  1455. it( 'default conversion, inside text, collapsed, multiple markers, no name', () => {
  1456. downcastHelpers.markerToData( { model: 'group' } );
  1457. setModelData( model, '<paragraph>Foo[]bar</paragraph>' );
  1458. model.change( writer => {
  1459. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1460. writer.addMarker( 'group:abc', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1461. } );
  1462. expectResult(
  1463. '<p>' +
  1464. 'Foo' +
  1465. '<group-start name="abc"></group-start><group-end name="abc"></group-end>' +
  1466. '<group-start name="foo"></group-start><group-end name="foo"></group-end>' +
  1467. 'bar' +
  1468. '</p>'
  1469. );
  1470. model.change( writer => {
  1471. writer.removeMarker( 'group:foo' );
  1472. writer.removeMarker( 'group:abc' );
  1473. } );
  1474. expectResult( '<p>Foobar</p>' );
  1475. } );
  1476. it( 'default conversion, on two elements, no name', () => {
  1477. downcastHelpers.markerToData( { model: 'search' } );
  1478. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  1479. model.change( writer => {
  1480. const range = writer.createRangeIn( root );
  1481. writer.addMarker( 'search', { range, usingOperation: false } );
  1482. } );
  1483. expectResult( '<p data-search-start-before="">Foo</p><p data-search-end-after="">Bar</p>' );
  1484. model.change( writer => {
  1485. writer.removeMarker( 'search' );
  1486. } );
  1487. expectResult( '<p>Foo</p><p>Bar</p>' );
  1488. } );
  1489. it( 'default conversion, on two elements, name', () => {
  1490. downcastHelpers.markerToData( { model: 'group' } );
  1491. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  1492. model.change( writer => {
  1493. const range = writer.createRangeIn( root );
  1494. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  1495. } );
  1496. expectResult( '<p data-group-start-before="foo:bar:baz">Foo</p><p data-group-end-after="foo:bar:baz">Bar</p>' );
  1497. model.change( writer => {
  1498. writer.removeMarker( 'group:foo:bar:baz' );
  1499. } );
  1500. expectResult( '<p>Foo</p><p>Bar</p>' );
  1501. } );
  1502. it( 'default conversion, on one element, name', () => {
  1503. downcastHelpers.markerToData( { model: 'group' } );
  1504. setModelData( model, '<paragraph>Foobar</paragraph>' );
  1505. model.change( writer => {
  1506. const range = writer.createRangeIn( root );
  1507. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  1508. } );
  1509. expectResult( '<p data-group-end-after="foo:bar:baz" data-group-start-before="foo:bar:baz">Foobar</p>' );
  1510. model.change( writer => {
  1511. writer.removeMarker( 'group:foo:bar:baz' );
  1512. } );
  1513. expectResult( '<p>Foobar</p>' );
  1514. } );
  1515. it( 'default conversion, collapsed before element, name', () => {
  1516. downcastHelpers.markerToData( { model: 'group' } );
  1517. setModelData( model, '<paragraph>Foobar</paragraph>' );
  1518. model.change( writer => {
  1519. // Collapsed before <paragraph>.
  1520. const range = writer.createRange(
  1521. writer.createPositionFromPath( root, [ 0 ] )
  1522. );
  1523. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  1524. } );
  1525. expectResult( '<p data-group-end-before="foo:bar:baz" data-group-start-before="foo:bar:baz">Foobar</p>' );
  1526. model.change( writer => {
  1527. writer.removeMarker( 'group:foo:bar:baz' );
  1528. } );
  1529. expectResult( '<p>Foobar</p>' );
  1530. } );
  1531. it( 'default conversion, collapsed after element, name', () => {
  1532. downcastHelpers.markerToData( { model: 'group' } );
  1533. setModelData( model, '<paragraph>Foobar</paragraph>' );
  1534. model.change( writer => {
  1535. // Collapsed before <paragraph>.
  1536. const range = writer.createRange(
  1537. writer.createPositionFromPath( root, [ 1 ] )
  1538. );
  1539. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  1540. } );
  1541. expectResult( '<p data-group-end-after="foo:bar:baz" data-group-start-after="foo:bar:baz">Foobar</p>' );
  1542. model.change( writer => {
  1543. writer.removeMarker( 'group:foo:bar:baz' );
  1544. } );
  1545. expectResult( '<p>Foobar</p>' );
  1546. } );
  1547. it( 'default conversion, mixed, multiple markers, name', () => {
  1548. downcastHelpers.markerToData( { model: 'group' } );
  1549. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  1550. model.change( writer => {
  1551. const range = writer.createRange(
  1552. writer.createPositionFromPath( root, [ 0 ] ),
  1553. writer.createPositionFromPath( root, [ 1, 2 ] )
  1554. );
  1555. writer.addMarker( 'group:foo:bar', { range, usingOperation: false } );
  1556. writer.addMarker( 'group:abc:xyz', { range, usingOperation: false } );
  1557. } );
  1558. expectResult(
  1559. '<p data-group-start-before="abc:xyz,foo:bar">Foo</p>' +
  1560. '<p>Ba<group-end name="abc:xyz"></group-end><group-end name="foo:bar"></group-end>r</p>'
  1561. );
  1562. model.change( writer => {
  1563. writer.removeMarker( 'group:foo:bar' );
  1564. writer.removeMarker( 'group:abc:xyz' );
  1565. } );
  1566. expectResult( '<p>Foo</p><p>Bar</p>' );
  1567. } );
  1568. it( 'default conversion, mixed #2, multiple markers, name', () => {
  1569. downcastHelpers.markerToData( { model: 'group' } );
  1570. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  1571. model.change( writer => {
  1572. const range = writer.createRange(
  1573. writer.createPositionFromPath( root, [ 0, 1 ] ),
  1574. writer.createPositionFromPath( root, [ 2 ] )
  1575. );
  1576. writer.addMarker( 'group:foo:bar', { range, usingOperation: false } );
  1577. writer.addMarker( 'group:abc:xyz', { range, usingOperation: false } );
  1578. } );
  1579. expectResult(
  1580. '<p>F<group-start name="abc:xyz"></group-start><group-start name="foo:bar"></group-start>oo</p>' +
  1581. '<p data-group-end-after="abc:xyz,foo:bar">Bar</p>'
  1582. );
  1583. model.change( writer => {
  1584. writer.removeMarker( 'group:foo:bar' );
  1585. writer.removeMarker( 'group:abc:xyz' );
  1586. } );
  1587. expectResult( '<p>Foo</p><p>Bar</p>' );
  1588. } );
  1589. it( 'default conversion, mixed #3, multiple markers, name', () => {
  1590. downcastHelpers.markerToData( { model: 'group' } );
  1591. setModelData( model, '<paragraph>Foo</paragraph>' );
  1592. model.change( writer => {
  1593. const range = writer.createRange(
  1594. writer.createPositionFromPath( root, [ 0 ] ),
  1595. writer.createPositionFromPath( root, [ 0, 2 ] )
  1596. );
  1597. writer.addMarker( 'group:foo:bar', { range, usingOperation: false } );
  1598. writer.addMarker( 'group:abc:xyz', { range, usingOperation: false } );
  1599. } );
  1600. expectResult(
  1601. '<p data-group-start-before="abc:xyz,foo:bar">' +
  1602. 'Fo<group-end name="abc:xyz"></group-end><group-end name="foo:bar"></group-end>o' +
  1603. '</p>'
  1604. );
  1605. model.change( writer => {
  1606. writer.removeMarker( 'group:foo:bar' );
  1607. writer.removeMarker( 'group:abc:xyz' );
  1608. } );
  1609. expectResult( '<p>Foo</p>' );
  1610. } );
  1611. it( 'default conversion, mixed #4, multiple markers, name', () => {
  1612. downcastHelpers.markerToData( { model: 'group' } );
  1613. setModelData( model, '<paragraph>Foo</paragraph>' );
  1614. model.change( writer => {
  1615. const range = writer.createRange(
  1616. writer.createPositionFromPath( root, [ 0, 2 ] ),
  1617. writer.createPositionFromPath( root, [ 1 ] )
  1618. );
  1619. writer.addMarker( 'group:foo:bar', { range, usingOperation: false } );
  1620. writer.addMarker( 'group:abc:xyz', { range, usingOperation: false } );
  1621. } );
  1622. expectResult(
  1623. '<p data-group-end-after="abc:xyz,foo:bar">' +
  1624. 'Fo<group-start name="abc:xyz"></group-start><group-start name="foo:bar"></group-start>o' +
  1625. '</p>'
  1626. );
  1627. model.change( writer => {
  1628. writer.removeMarker( 'group:foo:bar' );
  1629. writer.removeMarker( 'group:abc:xyz' );
  1630. } );
  1631. expectResult( '<p>Foo</p>' );
  1632. } );
  1633. it( 'conversion callback, mixed, multiple markers, name', () => {
  1634. const customData = {
  1635. foo: 'bar',
  1636. abc: 'xyz'
  1637. };
  1638. downcastHelpers.markerToData( {
  1639. model: 'group',
  1640. view: ( markerName, conversionApi ) => {
  1641. const namePart = markerName.split( ':' )[ 1 ];
  1642. // To ensure conversion API is provided.
  1643. expect( conversionApi.writer ).to.instanceof( DowncastWriter );
  1644. return {
  1645. group: 'g',
  1646. name: namePart + '_' + customData[ namePart ]
  1647. };
  1648. }
  1649. } );
  1650. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  1651. model.change( writer => {
  1652. const range = writer.createRange(
  1653. writer.createPositionFromPath( root, [ 0 ] ),
  1654. writer.createPositionFromPath( root, [ 1, 2 ] )
  1655. );
  1656. writer.addMarker( 'group:foo', { range, usingOperation: false } );
  1657. writer.addMarker( 'group:abc', { range, usingOperation: false } );
  1658. } );
  1659. expectResult(
  1660. '<p data-g-start-before="abc_xyz,foo_bar">Foo</p>' +
  1661. '<p>Ba<g-end name="abc_xyz"></g-end><g-end name="foo_bar"></g-end>r</p>'
  1662. );
  1663. model.change( writer => {
  1664. writer.removeMarker( 'group:foo' );
  1665. writer.removeMarker( 'group:abc' );
  1666. } );
  1667. expectResult( '<p>Foo</p><p>Bar</p>' );
  1668. } );
  1669. it( 'conversion callback, mixed #2, multiple markers, name', () => {
  1670. const customData = {
  1671. foo: 'bar',
  1672. abc: 'xyz'
  1673. };
  1674. downcastHelpers.markerToData( {
  1675. model: 'group',
  1676. view: markerName => {
  1677. const namePart = markerName.split( ':' )[ 1 ];
  1678. return {
  1679. group: 'g',
  1680. name: namePart + '_' + customData[ namePart ]
  1681. };
  1682. }
  1683. } );
  1684. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  1685. model.change( writer => {
  1686. const range = writer.createRange(
  1687. writer.createPositionFromPath( root, [ 0, 1 ] ),
  1688. writer.createPositionFromPath( root, [ 2 ] )
  1689. );
  1690. writer.addMarker( 'group:foo', { range, usingOperation: false } );
  1691. writer.addMarker( 'group:abc', { range, usingOperation: false } );
  1692. } );
  1693. expectResult(
  1694. '<p>F<g-start name="abc_xyz"></g-start><g-start name="foo_bar"></g-start>oo</p>' +
  1695. '<p data-g-end-after="abc_xyz,foo_bar">Bar</p>'
  1696. );
  1697. model.change( writer => {
  1698. writer.removeMarker( 'group:foo' );
  1699. writer.removeMarker( 'group:abc' );
  1700. } );
  1701. expectResult( '<p>Foo</p><p>Bar</p>' );
  1702. } );
  1703. it( 'can be overwritten using converterPriority', () => {
  1704. downcastHelpers.markerToData( {
  1705. model: 'group'
  1706. } );
  1707. downcastHelpers.markerToData( {
  1708. model: 'group',
  1709. view: markerName => {
  1710. const name = markerName.split( ':' )[ 1 ];
  1711. return {
  1712. group: 'g',
  1713. name
  1714. };
  1715. },
  1716. converterPriority: 'high'
  1717. } );
  1718. setModelData( model, '<paragraph>F[ooba]r</paragraph>' );
  1719. model.change( writer => {
  1720. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1721. } );
  1722. expectResult(
  1723. '<p>F<g-start name="foo"></g-start>ooba<g-end name="foo"></g-end>r</p>'
  1724. );
  1725. model.change( writer => {
  1726. writer.removeMarker( 'group:foo' );
  1727. } );
  1728. expectResult( '<p>Foobar</p>' );
  1729. } );
  1730. it( 'can be overwritten by custom callback', () => {
  1731. downcastHelpers.markerToData( {
  1732. model: 'group'
  1733. } );
  1734. downcastHelpers.add( dispatcher => {
  1735. dispatcher.on( 'addMarker:group', ( evt, data, conversionApi ) => {
  1736. conversionApi.consumable.consume( data.markerRange, evt.name );
  1737. }, { priority: 'high' } );
  1738. } );
  1739. setModelData( model, '<paragraph>Foo[]bar</paragraph>' );
  1740. model.change( writer => {
  1741. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1742. } );
  1743. expectResult( '<p>Foobar</p>' );
  1744. model.change( writer => {
  1745. writer.removeMarker( 'group:foo' );
  1746. } );
  1747. expectResult( '<p>Foobar</p>' );
  1748. } );
  1749. it( 'should not perform conversion if the callback returned falsy value', () => {
  1750. downcastHelpers.markerToData( {
  1751. model: 'group',
  1752. view: () => false
  1753. } );
  1754. setModelData( model, '<paragraph>F[ooba]r</paragraph>' );
  1755. model.change( writer => {
  1756. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1757. } );
  1758. expectResult( '<p>Foobar</p>' );
  1759. model.change( writer => {
  1760. writer.removeMarker( 'group:foo' );
  1761. } );
  1762. expectResult( '<p>Foobar</p>' );
  1763. } );
  1764. } );
  1765. describe( 'markerToHighlight()', () => {
  1766. it( 'should be chainable', () => {
  1767. expect( downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } ) ).to.equal( downcastHelpers );
  1768. } );
  1769. it( 'config.view is a highlight descriptor', () => {
  1770. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  1771. model.change( writer => {
  1772. writer.insertText( 'foo', modelRoot, 0 );
  1773. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  1774. writer.addMarker( 'comment', { range, usingOperation: false } );
  1775. } );
  1776. expectResult( '<span class="comment">foo</span>' );
  1777. } );
  1778. it( 'can be overwritten using converterPriority', () => {
  1779. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  1780. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'new-comment' }, converterPriority: 'high' } );
  1781. model.change( writer => {
  1782. writer.insertText( 'foo', modelRoot, 0 );
  1783. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  1784. writer.addMarker( 'comment', { range, usingOperation: false } );
  1785. } );
  1786. expectResult( '<span class="new-comment">foo</span>' );
  1787. } );
  1788. it( 'config.view is a function', () => {
  1789. downcastHelpers.markerToHighlight( {
  1790. model: 'comment',
  1791. view: ( data, conversionApi ) => {
  1792. const commentType = data.markerName.split( ':' )[ 1 ];
  1793. // To ensure conversion API is provided.
  1794. expect( conversionApi.writer ).to.instanceof( DowncastWriter );
  1795. return {
  1796. classes: [ 'comment', 'comment-' + commentType ]
  1797. };
  1798. }
  1799. } );
  1800. model.change( writer => {
  1801. writer.insertText( 'foo', modelRoot, 0 );
  1802. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  1803. writer.addMarker( 'comment:abc', { range, usingOperation: false } );
  1804. } );
  1805. expectResult( '<span class="comment comment-abc">foo</span>' );
  1806. } );
  1807. describe( 'highlight', () => {
  1808. const highlightConfig = {
  1809. model: 'marker',
  1810. view: {
  1811. classes: 'highlight-class',
  1812. attributes: { title: 'title' }
  1813. },
  1814. converterPriority: 7
  1815. };
  1816. describe( 'on text', () => {
  1817. let markerRange;
  1818. beforeEach( () => {
  1819. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1820. const modelElement1 = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  1821. const modelElement2 = new ModelElement( 'paragraph', null, new ModelText( 'bar' ) );
  1822. model.change( writer => {
  1823. writer.insert( [ modelElement1, modelElement2 ], modelRootStart );
  1824. } );
  1825. markerRange = model.createRangeIn( modelRoot );
  1826. } );
  1827. it( 'should wrap and unwrap text nodes', () => {
  1828. downcastHelpers.markerToHighlight( highlightConfig );
  1829. model.change( writer => {
  1830. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1831. } );
  1832. expect( viewToString( viewRoot ) ).to.equal(
  1833. '<div>' +
  1834. '<p>' +
  1835. '<span class="highlight-class" title="title">foo</span>' +
  1836. '</p>' +
  1837. '<p>' +
  1838. '<span class="highlight-class" title="title">bar</span>' +
  1839. '</p>' +
  1840. '</div>'
  1841. );
  1842. model.change( writer => {
  1843. writer.removeMarker( 'marker' );
  1844. } );
  1845. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1846. } );
  1847. it( 'should be possible to overwrite', () => {
  1848. downcastHelpers.markerToHighlight( highlightConfig );
  1849. downcastHelpers.markerToHighlight( {
  1850. model: 'marker',
  1851. view: { classes: 'override-class' },
  1852. converterPriority: 'high'
  1853. } );
  1854. model.change( writer => {
  1855. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1856. } );
  1857. expect( viewToString( viewRoot ) ).to.equal(
  1858. '<div>' +
  1859. '<p>' +
  1860. '<span class="override-class">foo</span>' +
  1861. '</p>' +
  1862. '<p>' +
  1863. '<span class="override-class">bar</span>' +
  1864. '</p>' +
  1865. '</div>'
  1866. );
  1867. model.change( writer => {
  1868. writer.removeMarker( 'marker' );
  1869. } );
  1870. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1871. } );
  1872. it( 'should do nothing if descriptor is not provided or generating function returns null', () => {
  1873. downcastHelpers.markerToHighlight( {
  1874. model: 'marker',
  1875. view: () => null,
  1876. converterPriority: 'high'
  1877. } );
  1878. model.change( writer => {
  1879. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1880. } );
  1881. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1882. model.change( writer => {
  1883. writer.removeMarker( 'marker' );
  1884. } );
  1885. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1886. } );
  1887. it( 'should do nothing if collapsed marker is converted', () => {
  1888. downcastHelpers.markerToHighlight( {
  1889. model: 'marker',
  1890. view: { classes: 'foo' },
  1891. converterPriority: 'high'
  1892. } );
  1893. markerRange = model.createRange( model.createPositionAt( modelRoot, 0 ), model.createPositionAt( modelRoot, 0 ) );
  1894. model.change( writer => {
  1895. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1896. } );
  1897. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1898. model.change( () => {
  1899. model.markers._remove( 'marker' );
  1900. } );
  1901. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1902. } );
  1903. it( 'should correctly wrap and unwrap multiple, intersecting markers', () => {
  1904. downcastHelpers.markerToHighlight( {
  1905. model: 'markerFoo',
  1906. view: { classes: 'foo' }
  1907. } );
  1908. downcastHelpers.markerToHighlight( {
  1909. model: 'markerBar',
  1910. view: { classes: 'bar' }
  1911. } );
  1912. downcastHelpers.markerToHighlight( {
  1913. model: 'markerXyz',
  1914. view: { classes: 'xyz' }
  1915. } );
  1916. const p1 = modelRoot.getChild( 0 );
  1917. const p2 = modelRoot.getChild( 1 );
  1918. model.change( writer => {
  1919. const range = writer.createRange( writer.createPositionAt( p1, 0 ), writer.createPositionAt( p1, 3 ) );
  1920. writer.addMarker( 'markerFoo', { range, usingOperation: false } );
  1921. } );
  1922. expect( viewToString( viewRoot ) ).to.equal(
  1923. '<div>' +
  1924. '<p>' +
  1925. '<span class="foo">foo</span>' +
  1926. '</p>' +
  1927. '<p>bar</p>' +
  1928. '</div>'
  1929. );
  1930. model.change( writer => {
  1931. const range = writer.createRange( writer.createPositionAt( p1, 1 ), writer.createPositionAt( p2, 2 ) );
  1932. writer.addMarker( 'markerBar', { range, usingOperation: false } );
  1933. } );
  1934. expect( viewToString( viewRoot ) ).to.equal(
  1935. '<div>' +
  1936. '<p>' +
  1937. '<span class="foo">f</span>' +
  1938. '<span class="bar">' +
  1939. '<span class="foo">oo</span>' +
  1940. '</span>' +
  1941. '</p>' +
  1942. '<p>' +
  1943. '<span class="bar">ba</span>' +
  1944. 'r' +
  1945. '</p>' +
  1946. '</div>'
  1947. );
  1948. model.change( writer => {
  1949. const range = writer.createRange( writer.createPositionAt( p1, 2 ), writer.createPositionAt( p2, 3 ) );
  1950. writer.addMarker( 'markerXyz', { range, usingOperation: false } );
  1951. } );
  1952. expect( viewToString( viewRoot ) ).to.equal(
  1953. '<div>' +
  1954. '<p>' +
  1955. '<span class="foo">f</span>' +
  1956. '<span class="bar">' +
  1957. '<span class="foo">' +
  1958. 'o' +
  1959. '<span class="xyz">o</span>' +
  1960. '</span>' +
  1961. '</span>' +
  1962. '</p>' +
  1963. '<p>' +
  1964. '<span class="bar">' +
  1965. '<span class="xyz">ba</span>' +
  1966. '</span>' +
  1967. '<span class="xyz">r</span>' +
  1968. '</p>' +
  1969. '</div>'
  1970. );
  1971. model.change( writer => {
  1972. writer.removeMarker( 'markerBar' );
  1973. } );
  1974. expect( viewToString( viewRoot ) ).to.equal(
  1975. '<div>' +
  1976. '<p>' +
  1977. '<span class="foo">' +
  1978. 'fo' +
  1979. '<span class="xyz">o</span>' +
  1980. '</span>' +
  1981. '</p>' +
  1982. '<p>' +
  1983. '<span class="xyz">bar</span>' +
  1984. '</p>' +
  1985. '</div>'
  1986. );
  1987. model.change( writer => {
  1988. writer.removeMarker( 'markerFoo' );
  1989. } );
  1990. expect( viewToString( viewRoot ) ).to.equal(
  1991. '<div>' +
  1992. '<p>' +
  1993. 'fo' +
  1994. '<span class="xyz">o</span>' +
  1995. '</p>' +
  1996. '<p>' +
  1997. '<span class="xyz">bar</span>' +
  1998. '</p>' +
  1999. '</div>'
  2000. );
  2001. model.change( writer => {
  2002. writer.removeMarker( 'markerXyz' );
  2003. } );
  2004. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  2005. } );
  2006. it( 'should do nothing if marker is applied and removed on empty-ish range', () => {
  2007. downcastHelpers.markerToHighlight( highlightConfig );
  2008. const p1 = modelRoot.getChild( 0 );
  2009. const p2 = modelRoot.getChild( 1 );
  2010. const markerRange = model.createRange( model.createPositionAt( p1, 3 ), model.createPositionAt( p2, 0 ) );
  2011. model.change( writer => {
  2012. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  2013. } );
  2014. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  2015. model.change( writer => {
  2016. writer.removeMarker( 'marker', { range: markerRange, usingOperation: false } );
  2017. } );
  2018. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  2019. } );
  2020. } );
  2021. describe( 'on element', () => {
  2022. const highlightConfig = {
  2023. model: 'marker',
  2024. view: {
  2025. classes: 'highlight-class',
  2026. attributes: { title: 'title' },
  2027. id: 'customId'
  2028. },
  2029. converterPriority: 7
  2030. };
  2031. let markerRange, viewDocument;
  2032. beforeEach( () => {
  2033. viewDocument = new ViewDocument( new StylesProcessor() );
  2034. downcastHelpers.elementToElement( {
  2035. model: 'div',
  2036. view: () => {
  2037. const viewContainer = new ViewContainerElement( viewDocument, 'div' );
  2038. viewContainer._setCustomProperty( 'addHighlight', ( element, descriptor, writer ) => {
  2039. writer.addClass( descriptor.classes, element );
  2040. } );
  2041. viewContainer._setCustomProperty( 'removeHighlight', ( element, id, writer ) => {
  2042. writer.setAttribute( 'class', '', element );
  2043. } );
  2044. return viewContainer;
  2045. }
  2046. } );
  2047. const modelElement = new ModelElement( 'div', null, new ModelText( 'foo' ) );
  2048. model.change( writer => {
  2049. writer.insert( modelElement, modelRootStart );
  2050. } );
  2051. markerRange = model.createRangeOn( modelElement );
  2052. downcastHelpers.markerToHighlight( highlightConfig );
  2053. } );
  2054. it( 'should use addHighlight and removeHighlight on elements and not convert children nodes', () => {
  2055. model.change( writer => {
  2056. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  2057. } );
  2058. expect( viewToString( viewRoot ) ).to.equal(
  2059. '<div>' +
  2060. '<div class="highlight-class">' +
  2061. 'foo' +
  2062. '</div>' +
  2063. '</div>'
  2064. );
  2065. model.change( writer => {
  2066. writer.removeMarker( 'marker' );
  2067. } );
  2068. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  2069. } );
  2070. it( 'should be possible to override', () => {
  2071. downcastHelpers.markerToHighlight( {
  2072. model: 'marker',
  2073. view: { classes: 'override-class' },
  2074. converterPriority: 'high'
  2075. } );
  2076. model.change( writer => {
  2077. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  2078. } );
  2079. expect( viewToString( viewRoot ) ).to.equal(
  2080. '<div>' +
  2081. '<div class="override-class">' +
  2082. 'foo' +
  2083. '</div>' +
  2084. '</div>'
  2085. );
  2086. model.change( writer => {
  2087. writer.removeMarker( 'marker' );
  2088. } );
  2089. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  2090. } );
  2091. it( 'should use default priority and id if not provided', () => {
  2092. const viewDiv = viewRoot.getChild( 0 );
  2093. downcastHelpers.markerToHighlight( {
  2094. model: 'marker2',
  2095. view: () => null,
  2096. converterPriority: 'high'
  2097. } );
  2098. viewDiv._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
  2099. expect( descriptor.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  2100. expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
  2101. } );
  2102. viewDiv._setCustomProperty( 'removeHighlight', ( element, id ) => {
  2103. expect( id ).to.equal( 'marker:foo-bar-baz' );
  2104. } );
  2105. model.change( writer => {
  2106. writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  2107. } );
  2108. } );
  2109. it( 'should do nothing if descriptor is not provided', () => {
  2110. downcastHelpers.markerToHighlight( {
  2111. model: 'marker2',
  2112. view: () => null
  2113. } );
  2114. model.change( writer => {
  2115. writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  2116. } );
  2117. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  2118. model.change( writer => {
  2119. writer.removeMarker( 'marker2' );
  2120. } );
  2121. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  2122. } );
  2123. } );
  2124. } );
  2125. } );
  2126. function expectResult( string ) {
  2127. expect( stringifyView( viewRoot, null, { ignoreRoot: true } ) ).to.equal( string );
  2128. }
  2129. } );
  2130. describe( 'downcast converters', () => {
  2131. let dispatcher, modelDoc, modelRoot, viewRoot, controller, modelRootStart, model;
  2132. beforeEach( () => {
  2133. model = new Model();
  2134. modelDoc = model.document;
  2135. modelRoot = modelDoc.createRoot();
  2136. controller = new EditingController( model, new StylesProcessor() );
  2137. viewRoot = controller.view.document.getRoot();
  2138. // Set name of view root the same as dom root.
  2139. // This is a mock of attaching view root to dom root.
  2140. controller.view.document.getRoot()._name = 'div';
  2141. dispatcher = controller.downcastDispatcher;
  2142. const downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  2143. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  2144. modelRootStart = model.createPositionAt( modelRoot, 0 );
  2145. } );
  2146. describe( 'insertText()', () => {
  2147. it( 'should downcast text', () => {
  2148. model.change( writer => {
  2149. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  2150. } );
  2151. expect( viewToString( viewRoot ) ).to.equal( '<div>foobar</div>' );
  2152. } );
  2153. it( 'should support unicode', () => {
  2154. model.change( writer => {
  2155. writer.insert( new ModelText( 'நிலைக்கு' ), modelRootStart );
  2156. } );
  2157. expect( viewToString( viewRoot ) ).to.equal( '<div>நிலைக்கு</div>' );
  2158. } );
  2159. it( 'should be possible to override it', () => {
  2160. dispatcher.on( 'insert:$text', ( evt, data, conversionApi ) => {
  2161. conversionApi.consumable.consume( data.item, 'insert' );
  2162. }, { converterPriority: 'high' } );
  2163. model.change( writer => {
  2164. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  2165. } );
  2166. expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
  2167. } );
  2168. } );
  2169. // Remove converter is by default already added in `EditingController` instance.
  2170. describe( 'remove()', () => {
  2171. let viewDocument;
  2172. beforeEach( () => {
  2173. viewDocument = new ViewDocument( new StylesProcessor() );
  2174. } );
  2175. it( 'should remove items from view accordingly to changes in model #1', () => {
  2176. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  2177. model.change( writer => {
  2178. writer.insert( modelElement, modelRootStart );
  2179. } );
  2180. model.change( writer => {
  2181. writer.remove(
  2182. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  2183. );
  2184. } );
  2185. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foar</p></div>' );
  2186. } );
  2187. it( 'should be possible to overwrite', () => {
  2188. dispatcher.on( 'remove', evt => evt.stop(), { priority: 'high' } );
  2189. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  2190. model.change( writer => {
  2191. writer.insert( modelElement, modelRootStart );
  2192. } );
  2193. model.change( writer => {
  2194. writer.remove(
  2195. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  2196. );
  2197. } );
  2198. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  2199. } );
  2200. it( 'should support unicode', () => {
  2201. const modelElement = new ModelElement( 'paragraph', null, 'நிலைக்கு' );
  2202. model.change( writer => {
  2203. writer.insert( modelElement, modelRootStart );
  2204. } );
  2205. model.change( writer => {
  2206. writer.remove(
  2207. writer.createRange( writer.createPositionAt( modelElement, 0 ), writer.createPositionAt( modelElement, 6 ) )
  2208. );
  2209. } );
  2210. expect( viewToString( viewRoot ) ).to.equal( '<div><p>கு</p></div>' );
  2211. } );
  2212. it( 'should not remove view ui elements that are placed next to removed content', () => {
  2213. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  2214. viewRoot._appendChild( [
  2215. new ViewText( viewDocument, 'foz' ),
  2216. new ViewUIElement( viewDocument, 'span' ),
  2217. new ViewText( viewDocument, 'bar' )
  2218. ] );
  2219. // Remove 'b'.
  2220. model.change( writer => {
  2221. writer.remove(
  2222. writer.createRange( writer.createPositionAt( modelRoot, 3 ), writer.createPositionAt( modelRoot, 4 ) )
  2223. );
  2224. } );
  2225. expect( viewToString( viewRoot ) ).to.equal( '<div>foz<span></span>ar</div>' );
  2226. // Remove 'z'.
  2227. model.change( writer => {
  2228. writer.remove(
  2229. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 3 ) )
  2230. );
  2231. } );
  2232. expect( viewToString( viewRoot ) ).to.equal( '<div>fo<span></span>ar</div>' );
  2233. } );
  2234. it( 'should remove correct amount of text when it is split by view ui element', () => {
  2235. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  2236. viewRoot._appendChild( [
  2237. new ViewText( viewDocument, 'foz' ),
  2238. new ViewUIElement( viewDocument, 'span' ),
  2239. new ViewText( viewDocument, 'bar' )
  2240. ] );
  2241. // Remove 'z<span></span>b'.
  2242. model.change( writer => {
  2243. writer.remove(
  2244. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 4 ) )
  2245. );
  2246. } );
  2247. expect( viewToString( viewRoot ) ).to.equal( '<div>foar</div>' );
  2248. } );
  2249. it( 'should unbind elements', () => {
  2250. const modelElement = new ModelElement( 'paragraph' );
  2251. model.change( writer => {
  2252. writer.insert( modelElement, modelRootStart );
  2253. } );
  2254. const viewElement = controller.mapper.toViewElement( modelElement );
  2255. expect( viewElement ).not.to.be.undefined;
  2256. expect( controller.mapper.toModelElement( viewElement ) ).to.equal( modelElement );
  2257. model.change( writer => {
  2258. writer.remove( modelElement );
  2259. } );
  2260. expect( controller.mapper.toViewElement( modelElement ) ).to.be.undefined;
  2261. expect( controller.mapper.toModelElement( viewElement ) ).to.be.undefined;
  2262. } );
  2263. it( 'should not break when remove() is used as part of unwrapping', () => {
  2264. const modelP = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  2265. const modelWidget = new ModelElement( 'widget', null, modelP );
  2266. const downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  2267. downcastHelpers.elementToElement( { model: 'widget', view: 'widget' } );
  2268. model.change( writer => {
  2269. writer.insert( modelWidget, modelRootStart );
  2270. } );
  2271. expect( viewToString( viewRoot ) ).to.equal( '<div><widget><p>foo</p></widget></div>' );
  2272. const viewP = controller.mapper.toViewElement( modelP );
  2273. expect( viewP ).not.to.be.undefined;
  2274. model.change( writer => {
  2275. writer.unwrap( modelWidget );
  2276. } );
  2277. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p></div>' );
  2278. // `modelP` is now bound with newly created view element.
  2279. expect( controller.mapper.toViewElement( modelP ) ).not.to.equal( viewP );
  2280. // `viewP` is no longer bound with model element.
  2281. expect( controller.mapper.toModelElement( viewP ) ).to.be.undefined;
  2282. // View element from view root is bound to `modelP`.
  2283. expect( controller.mapper.toModelElement( viewRoot.getChild( 0 ) ) ).to.equal( modelP );
  2284. } );
  2285. it( 'should work correctly if container element after ui element is removed', () => {
  2286. // Prepare a model and view structure.
  2287. // This is done outside of conversion to put view ui elements inside easily.
  2288. const modelP1 = new ModelElement( 'paragraph' );
  2289. const modelP2 = new ModelElement( 'paragraph' );
  2290. const viewP1 = new ViewContainerElement( viewDocument, 'p' );
  2291. const viewUi1 = new ViewUIElement( viewDocument, 'span' );
  2292. const viewUi2 = new ViewUIElement( viewDocument, 'span' );
  2293. const viewP2 = new ViewContainerElement( viewDocument, 'p' );
  2294. modelRoot._appendChild( [ modelP1, modelP2 ] );
  2295. viewRoot._appendChild( [ viewP1, viewUi1, viewUi2, viewP2 ] );
  2296. controller.mapper.bindElements( modelP1, viewP1 );
  2297. controller.mapper.bindElements( modelP2, viewP2 );
  2298. // Remove second paragraph element.
  2299. model.change( writer => {
  2300. writer.remove( writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) ) );
  2301. } );
  2302. expect( viewToString( viewRoot ) ).to.equal( '<div><p></p><span></span><span></span></div>' );
  2303. } );
  2304. it( 'should work correctly if container element after text node is removed', () => {
  2305. const modelText = new ModelText( 'foo' );
  2306. const modelP = new ModelElement( 'paragraph' );
  2307. model.change( writer => {
  2308. writer.insert( [ modelText, modelP ], modelRootStart );
  2309. } );
  2310. model.change( writer => {
  2311. writer.remove( modelP );
  2312. } );
  2313. expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
  2314. } );
  2315. } );
  2316. describe( 'createViewElementFromHighlightDescriptor()', () => {
  2317. let viewWriter;
  2318. beforeEach( () => {
  2319. viewWriter = new DowncastWriter( controller.view.document );
  2320. } );
  2321. it( 'should return attribute element from descriptor object', () => {
  2322. const descriptor = {
  2323. classes: 'foo-class',
  2324. attributes: { one: '1', two: '2' },
  2325. priority: 7
  2326. };
  2327. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  2328. expect( element.is( 'attributeElement' ) ).to.be.true;
  2329. expect( element.name ).to.equal( 'span' );
  2330. expect( element.priority ).to.equal( 7 );
  2331. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  2332. for ( const key of Object.keys( descriptor.attributes ) ) {
  2333. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  2334. }
  2335. } );
  2336. it( 'should return attribute element from descriptor object - array with classes', () => {
  2337. const descriptor = {
  2338. classes: [ 'foo-class', 'bar-class' ],
  2339. attributes: { one: '1', two: '2' },
  2340. priority: 7
  2341. };
  2342. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  2343. expect( element.is( 'attributeElement' ) ).to.be.true;
  2344. expect( element.name ).to.equal( 'span' );
  2345. expect( element.priority ).to.equal( 7 );
  2346. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  2347. expect( element.hasClass( 'bar-class' ) ).to.be.true;
  2348. for ( const key of Object.keys( descriptor.attributes ) ) {
  2349. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  2350. }
  2351. } );
  2352. it( 'should create element without class', () => {
  2353. const descriptor = {
  2354. attributes: { one: '1', two: '2' },
  2355. priority: 7
  2356. };
  2357. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  2358. expect( element.is( 'attributeElement' ) ).to.be.true;
  2359. expect( element.name ).to.equal( 'span' );
  2360. expect( element.priority ).to.equal( 7 );
  2361. for ( const key of Object.keys( descriptor.attributes ) ) {
  2362. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  2363. }
  2364. } );
  2365. it( 'should create element without priority', () => {
  2366. const descriptor = {
  2367. classes: 'foo-class',
  2368. attributes: { one: '1', two: '2' }
  2369. };
  2370. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  2371. expect( element.is( 'attributeElement' ) ).to.be.true;
  2372. expect( element.name ).to.equal( 'span' );
  2373. expect( element.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  2374. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  2375. for ( const key of Object.keys( descriptor.attributes ) ) {
  2376. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  2377. }
  2378. } );
  2379. it( 'should create element without attributes', () => {
  2380. const descriptor = {
  2381. classes: 'foo-class',
  2382. priority: 7
  2383. };
  2384. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  2385. expect( element.is( 'attributeElement' ) ).to.be.true;
  2386. expect( element.name ).to.equal( 'span' );
  2387. expect( element.priority ).to.equal( 7 );
  2388. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  2389. } );
  2390. } );
  2391. } );
  2392. describe( 'downcast selection converters', () => {
  2393. let dispatcher, mapper, model, view, modelDoc, modelRoot, docSelection, viewDoc, viewRoot, viewSelection, downcastHelpers;
  2394. beforeEach( () => {
  2395. model = new Model();
  2396. modelDoc = model.document;
  2397. modelRoot = modelDoc.createRoot();
  2398. docSelection = modelDoc.selection;
  2399. model.schema.extend( '$text', { allowIn: '$root' } );
  2400. view = new View( new StylesProcessor() );
  2401. viewDoc = view.document;
  2402. viewRoot = createViewRoot( viewDoc );
  2403. viewSelection = viewDoc.selection;
  2404. mapper = new Mapper();
  2405. mapper.bindElements( modelRoot, viewRoot );
  2406. dispatcher = new DowncastDispatcher( { mapper, viewSelection } );
  2407. dispatcher.on( 'insert:$text', insertText() );
  2408. downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  2409. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  2410. downcastHelpers.markerToHighlight( { model: 'marker', view: { classes: 'marker' }, converterPriority: 1 } );
  2411. // Default selection converters.
  2412. dispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  2413. dispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  2414. dispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  2415. } );
  2416. afterEach( () => {
  2417. view.destroy();
  2418. } );
  2419. describe( 'default converters', () => {
  2420. describe( 'range selection', () => {
  2421. it( 'in same container', () => {
  2422. testSelection(
  2423. [ 1, 4 ],
  2424. 'foobar',
  2425. 'f{oob}ar'
  2426. );
  2427. } );
  2428. it( 'in same container with unicode characters', () => {
  2429. testSelection(
  2430. [ 2, 6 ],
  2431. 'நிலைக்கு',
  2432. 'நி{லைக்}கு'
  2433. );
  2434. } );
  2435. it( 'in same container, over attribute', () => {
  2436. testSelection(
  2437. [ 1, 5 ],
  2438. 'fo<$text bold="true">ob</$text>ar',
  2439. 'f{o<strong>ob</strong>a}r'
  2440. );
  2441. } );
  2442. it( 'in same container, next to attribute', () => {
  2443. testSelection(
  2444. [ 1, 2 ],
  2445. 'fo<$text bold="true">ob</$text>ar',
  2446. 'f{o}<strong>ob</strong>ar'
  2447. );
  2448. } );
  2449. it( 'in same attribute', () => {
  2450. testSelection(
  2451. [ 2, 4 ],
  2452. 'f<$text bold="true">ooba</$text>r',
  2453. 'f<strong>o{ob}a</strong>r'
  2454. );
  2455. } );
  2456. it( 'in same attribute, selection same as attribute', () => {
  2457. testSelection(
  2458. [ 2, 4 ],
  2459. 'fo<$text bold="true">ob</$text>ar',
  2460. 'fo{<strong>ob</strong>}ar'
  2461. );
  2462. } );
  2463. it( 'starts in text node, ends in attribute #1', () => {
  2464. testSelection(
  2465. [ 1, 3 ],
  2466. 'fo<$text bold="true">ob</$text>ar',
  2467. 'f{o<strong>o}b</strong>ar'
  2468. );
  2469. } );
  2470. it( 'starts in text node, ends in attribute #2', () => {
  2471. testSelection(
  2472. [ 1, 4 ],
  2473. 'fo<$text bold="true">ob</$text>ar',
  2474. 'f{o<strong>ob</strong>}ar'
  2475. );
  2476. } );
  2477. it( 'starts in attribute, ends in text node', () => {
  2478. testSelection(
  2479. [ 3, 5 ],
  2480. 'fo<$text bold="true">ob</$text>ar',
  2481. 'fo<strong>o{b</strong>a}r'
  2482. );
  2483. } );
  2484. it( 'consumes consumable values properly', () => {
  2485. // Add callback that will fire before default ones.
  2486. // This should prevent default callback doing anything.
  2487. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  2488. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  2489. }, { priority: 'high' } );
  2490. // Similar test case as the first in this suite.
  2491. testSelection(
  2492. [ 1, 4 ],
  2493. 'foobar',
  2494. 'foobar' // No selection in view.
  2495. );
  2496. } );
  2497. it( 'should convert backward selection', () => {
  2498. testSelection(
  2499. [ 1, 3, 'backward' ],
  2500. 'foobar',
  2501. 'f{oo}bar'
  2502. );
  2503. expect( viewSelection.focus.offset ).to.equal( 1 );
  2504. } );
  2505. } );
  2506. describe( 'collapsed selection', () => {
  2507. let marker, viewDocument;
  2508. beforeEach( () => {
  2509. viewDocument = new ViewDocument( new StylesProcessor() );
  2510. } );
  2511. it( 'in container', () => {
  2512. testSelection(
  2513. [ 1, 1 ],
  2514. 'foobar',
  2515. 'f{}oobar'
  2516. );
  2517. } );
  2518. it( 'in attribute', () => {
  2519. testSelection(
  2520. [ 3, 3 ],
  2521. 'f<$text bold="true">ooba</$text>r',
  2522. 'f<strong>oo{}ba</strong>r'
  2523. );
  2524. } );
  2525. it( 'in attribute and marker', () => {
  2526. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  2527. model.change( writer => {
  2528. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  2529. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  2530. writer.setSelection( modelRoot, 3 );
  2531. } );
  2532. // Remove view children manually (without firing additional conversion).
  2533. viewRoot._removeChildren( 0, viewRoot.childCount );
  2534. // Convert model to view.
  2535. view.change( writer => {
  2536. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2537. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  2538. dispatcher.convertSelection( docSelection, model.markers, writer );
  2539. } );
  2540. // Stringify view and check if it is same as expected.
  2541. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal(
  2542. '<div>f<span class="marker">o<strong>o{}b</strong>a</span>r</div>'
  2543. );
  2544. } );
  2545. it( 'in attribute and marker - no attribute', () => {
  2546. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  2547. model.change( writer => {
  2548. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  2549. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  2550. writer.setSelection( modelRoot, 3 );
  2551. writer.removeSelectionAttribute( 'bold' );
  2552. } );
  2553. // Remove view children manually (without firing additional conversion).
  2554. viewRoot._removeChildren( 0, viewRoot.childCount );
  2555. // Convert model to view.
  2556. view.change( writer => {
  2557. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2558. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  2559. dispatcher.convertSelection( docSelection, model.markers, writer );
  2560. } );
  2561. // Stringify view and check if it is same as expected.
  2562. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  2563. .to.equal( '<div>f<span class="marker">o<strong>o</strong>[]<strong>b</strong>a</span>r</div>' );
  2564. } );
  2565. it( 'in marker - using highlight descriptor creator', () => {
  2566. downcastHelpers.markerToHighlight( {
  2567. model: 'marker2',
  2568. view: data => ( { classes: data.markerName } )
  2569. } );
  2570. setModelData( model, 'foobar' );
  2571. model.change( writer => {
  2572. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  2573. marker = writer.addMarker( 'marker2', { range, usingOperation: false } );
  2574. writer.setSelection( modelRoot, 3 );
  2575. } );
  2576. // Remove view children manually (without firing additional conversion).
  2577. viewRoot._removeChildren( 0, viewRoot.childCount );
  2578. // Convert model to view.
  2579. view.change( writer => {
  2580. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2581. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  2582. dispatcher.convertSelection( docSelection, model.markers, writer );
  2583. } );
  2584. // Stringify view and check if it is same as expected.
  2585. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  2586. .to.equal( '<div>f<span class="marker2">oo{}ba</span>r</div>' );
  2587. } );
  2588. it( 'should do nothing if creator return null', () => {
  2589. downcastHelpers.markerToHighlight( {
  2590. model: 'marker3',
  2591. view: () => null
  2592. } );
  2593. setModelData( model, 'foobar' );
  2594. model.change( writer => {
  2595. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  2596. marker = writer.addMarker( 'marker3', { range, usingOperation: false } );
  2597. writer.setSelection( modelRoot, 3 );
  2598. } );
  2599. // Remove view children manually (without firing additional conversion).
  2600. viewRoot._removeChildren( 0, viewRoot.childCount );
  2601. // Convert model to view.
  2602. view.change( writer => {
  2603. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2604. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  2605. dispatcher.convertSelection( docSelection, model.markers, writer );
  2606. } );
  2607. // Stringify view and check if it is same as expected.
  2608. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  2609. .to.equal( '<div>foo{}bar</div>' );
  2610. } );
  2611. // #1072 - if the container has only ui elements, collapsed selection attribute should be rendered after those ui elements.
  2612. it( 'selection with attribute before ui element - no non-ui children', () => {
  2613. setModelData( model, '' );
  2614. // Add two ui elements to view.
  2615. viewRoot._appendChild( [
  2616. new ViewUIElement( viewDocument, 'span' ),
  2617. new ViewUIElement( viewDocument, 'span' )
  2618. ] );
  2619. model.change( writer => {
  2620. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 0 ] ) ) );
  2621. writer.setSelectionAttribute( 'bold', true );
  2622. } );
  2623. // Convert model to view.
  2624. view.change( writer => {
  2625. dispatcher.convertSelection( docSelection, model.markers, writer );
  2626. } );
  2627. // Stringify view and check if it is same as expected.
  2628. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  2629. .to.equal( '<div><span></span><span></span><strong>[]</strong></div>' );
  2630. } );
  2631. // #1072.
  2632. it( 'selection with attribute before ui element - has non-ui children #1', () => {
  2633. setModelData( model, 'x' );
  2634. model.change( writer => {
  2635. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  2636. writer.setSelectionAttribute( 'bold', true );
  2637. } );
  2638. // Convert model to view.
  2639. view.change( writer => {
  2640. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2641. // Add ui element to view.
  2642. const uiElement = new ViewUIElement( viewDocument, 'span' );
  2643. viewRoot._insertChild( 1, uiElement );
  2644. dispatcher.convertSelection( docSelection, model.markers, writer );
  2645. } );
  2646. // Stringify view and check if it is same as expected.
  2647. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  2648. .to.equal( '<div>x<strong>[]</strong><span></span></div>' );
  2649. } );
  2650. // #1072.
  2651. it( 'selection with attribute before ui element - has non-ui children #2', () => {
  2652. setModelData( model, '<$text bold="true">x</$text>y' );
  2653. model.change( writer => {
  2654. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  2655. writer.setSelectionAttribute( 'bold', true );
  2656. } );
  2657. // Convert model to view.
  2658. view.change( writer => {
  2659. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2660. // Add ui element to view.
  2661. const uiElement = new ViewUIElement( viewDocument, 'span' );
  2662. viewRoot._insertChild( 1, uiElement, writer );
  2663. dispatcher.convertSelection( docSelection, model.markers, writer );
  2664. } );
  2665. // Stringify view and check if it is same as expected.
  2666. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  2667. .to.equal( '<div><strong>x{}</strong><span></span>y</div>' );
  2668. } );
  2669. it( 'consumes consumable values properly', () => {
  2670. // Add callbacks that will fire before default ones.
  2671. // This should prevent default callbacks doing anything.
  2672. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  2673. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  2674. }, { priority: 'high' } );
  2675. dispatcher.on( 'attribute:bold', ( evt, data, conversionApi ) => {
  2676. expect( conversionApi.consumable.consume( data.item, 'attribute:bold' ) ).to.be.true;
  2677. }, { priority: 'high' } );
  2678. // Similar test case as above.
  2679. testSelection(
  2680. [ 3, 3 ],
  2681. 'f<$text bold="true">ooba</$text>r',
  2682. 'foobar' // No selection in view and no attribute.
  2683. );
  2684. } );
  2685. } );
  2686. } );
  2687. describe( 'clean-up', () => {
  2688. describe( 'convertRangeSelection', () => {
  2689. it( 'should remove all ranges before adding new range', () => {
  2690. testSelection(
  2691. [ 0, 2 ],
  2692. 'foobar',
  2693. '{fo}obar'
  2694. );
  2695. testSelection(
  2696. [ 3, 5 ],
  2697. 'foobar',
  2698. 'foo{ba}r'
  2699. );
  2700. expect( viewSelection.rangeCount ).to.equal( 1 );
  2701. } );
  2702. } );
  2703. describe( 'convertCollapsedSelection', () => {
  2704. it( 'should remove all ranges before adding new range', () => {
  2705. testSelection(
  2706. [ 2, 2 ],
  2707. 'foobar',
  2708. 'fo{}obar'
  2709. );
  2710. testSelection(
  2711. [ 3, 3 ],
  2712. 'foobar',
  2713. 'foo{}bar'
  2714. );
  2715. expect( viewSelection.rangeCount ).to.equal( 1 );
  2716. } );
  2717. } );
  2718. describe( 'clearAttributes', () => {
  2719. it( 'should remove all ranges before adding new range', () => {
  2720. testSelection(
  2721. [ 3, 3 ],
  2722. 'foobar',
  2723. 'foo<strong>[]</strong>bar',
  2724. { bold: 'true' }
  2725. );
  2726. view.change( writer => {
  2727. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  2728. model.change( writer => {
  2729. writer.setSelection( modelRange );
  2730. } );
  2731. dispatcher.convertSelection( modelDoc.selection, model.markers, writer );
  2732. } );
  2733. expect( viewSelection.rangeCount ).to.equal( 1 );
  2734. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  2735. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  2736. } );
  2737. it( 'should do nothing if the attribute element had been already removed', () => {
  2738. testSelection(
  2739. [ 3, 3 ],
  2740. 'foobar',
  2741. 'foo<strong>[]</strong>bar',
  2742. { bold: 'true' }
  2743. );
  2744. view.change( writer => {
  2745. // Remove <strong></strong> manually.
  2746. writer.mergeAttributes( viewSelection.getFirstPosition() );
  2747. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  2748. model.change( writer => {
  2749. writer.setSelection( modelRange );
  2750. } );
  2751. dispatcher.convertSelection( modelDoc.selection, model.markers, writer );
  2752. } );
  2753. expect( viewSelection.rangeCount ).to.equal( 1 );
  2754. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  2755. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  2756. } );
  2757. it( 'should clear fake selection', () => {
  2758. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  2759. view.change( writer => {
  2760. writer.setSelection( modelRange, { fake: true } );
  2761. dispatcher.convertSelection( docSelection, model.markers, writer );
  2762. } );
  2763. expect( viewSelection.isFake ).to.be.false;
  2764. } );
  2765. } );
  2766. } );
  2767. describe( 'table cell selection converter', () => {
  2768. beforeEach( () => {
  2769. model.schema.register( 'table', { isLimit: true } );
  2770. model.schema.register( 'tr', { isLimit: true } );
  2771. model.schema.register( 'td', { isLimit: true } );
  2772. model.schema.extend( 'table', { allowIn: '$root' } );
  2773. model.schema.extend( 'tr', { allowIn: 'table' } );
  2774. model.schema.extend( 'td', { allowIn: 'tr' } );
  2775. model.schema.extend( '$text', { allowIn: 'td' } );
  2776. const downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  2777. // "Universal" converter to convert table structure.
  2778. downcastHelpers.elementToElement( { model: 'table', view: 'table' } );
  2779. downcastHelpers.elementToElement( { model: 'tr', view: 'tr' } );
  2780. downcastHelpers.elementToElement( { model: 'td', view: 'td' } );
  2781. // Special converter for table cells.
  2782. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  2783. const selection = data.selection;
  2784. if ( !conversionApi.consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  2785. return;
  2786. }
  2787. for ( const range of selection.getRanges() ) {
  2788. const node = range.start.parent;
  2789. if ( !!node && node.is( 'element', 'td' ) ) {
  2790. conversionApi.consumable.consume( selection, 'selection' );
  2791. const viewNode = conversionApi.mapper.toViewElement( node );
  2792. conversionApi.writer.addClass( 'selected', viewNode );
  2793. }
  2794. }
  2795. }, { priority: 'high' } );
  2796. } );
  2797. it( 'should not be used to convert selection that is not on table cell', () => {
  2798. testSelection(
  2799. [ 1, 5 ],
  2800. 'f{o<$text bold="true">ob</$text>a}r',
  2801. 'f{o<strong>ob</strong>a}r'
  2802. );
  2803. } );
  2804. it( 'should add a class to the selected table cell', () => {
  2805. testSelection(
  2806. // table tr#0 td#0 [foo, table tr#0 td#0 bar]
  2807. [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 3 ] ],
  2808. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  2809. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  2810. );
  2811. } );
  2812. it( 'should not be used if selection contains more than just a table cell', () => {
  2813. testSelection(
  2814. // table tr td#1 f{oo bar, table tr#2 bar]
  2815. [ [ 0, 0, 0, 1 ], [ 0, 0, 1, 3 ] ],
  2816. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  2817. '[<table><tr><td>foo</td><td>bar</td></tr></table>]'
  2818. );
  2819. } );
  2820. } );
  2821. // Tests if the selection got correctly converted.
  2822. // Because `setData` might use selection converters itself to set the selection, we can't use it
  2823. // to set the selection (because then we would test converters using converters).
  2824. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  2825. // that are offsets or paths of selection positions in root element.
  2826. function testSelection( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  2827. // Parse passed `modelInput` string and set it as current model.
  2828. setModelData( model, modelInput );
  2829. // Manually set selection ranges using passed `selectionPaths`.
  2830. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  2831. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  2832. const startPos = model.createPositionFromPath( modelRoot, startPath );
  2833. const endPos = model.createPositionFromPath( modelRoot, endPath );
  2834. const isBackward = selectionPaths[ 2 ] === 'backward';
  2835. model.change( writer => {
  2836. writer.setSelection( writer.createRange( startPos, endPos ), { backward: isBackward } );
  2837. // And add or remove passed attributes.
  2838. for ( const key in selectionAttributes ) {
  2839. const value = selectionAttributes[ key ];
  2840. if ( value ) {
  2841. writer.setSelectionAttribute( key, value );
  2842. } else {
  2843. writer.removeSelectionAttribute( key );
  2844. }
  2845. }
  2846. } );
  2847. // Remove view children manually (without firing additional conversion).
  2848. viewRoot._removeChildren( 0, viewRoot.childCount );
  2849. // Convert model to view.
  2850. view.change( writer => {
  2851. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2852. dispatcher.convertSelection( docSelection, model.markers, writer );
  2853. } );
  2854. // Stringify view and check if it is same as expected.
  2855. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  2856. }
  2857. } );
  2858. function viewToString( item ) {
  2859. let result = '';
  2860. if ( item instanceof ViewText ) {
  2861. result = item.data;
  2862. } else {
  2863. // ViewElement or ViewDocumentFragment.
  2864. for ( const child of item.getChildren() ) {
  2865. result += viewToString( child );
  2866. }
  2867. if ( item instanceof ViewElement ) {
  2868. result = '<' + item.name + viewAttributesToString( item ) + '>' + result + '</' + item.name + '>';
  2869. }
  2870. }
  2871. return result;
  2872. }
  2873. function viewAttributesToString( item ) {
  2874. let result = '';
  2875. for ( const key of item.getAttributeKeys() ) {
  2876. const value = item.getAttribute( key );
  2877. if ( value ) {
  2878. result += ' ' + key + '="' + value + '"';
  2879. }
  2880. }
  2881. return result;
  2882. }