downcasthelpers.js 104 KB

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