downcasthelpers.js 112 KB

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