8
0

downcasthelpers.js 103 KB

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