8
0

downcasthelpers.js 104 KB

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