downcasthelpers.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import EditingController from '../../src/controller/editingcontroller';
  6. import Conversion from '../../src/conversion/conversion';
  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 log from '@ckeditor/ckeditor5-utils/src/log';
  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. describe( 'DowncastHelpers', () => {
  31. let conversion, model, modelRoot, viewRoot, downcastHelpers, controller;
  32. let modelRootStart;
  33. beforeEach( () => {
  34. model = new Model();
  35. const modelDoc = model.document;
  36. modelRoot = modelDoc.createRoot();
  37. controller = new EditingController( model );
  38. // Set name of view root the same as dom root.
  39. // This is a mock of attaching view root to dom root.
  40. controller.view.document.getRoot()._name = 'div';
  41. viewRoot = controller.view.document.getRoot();
  42. downcastHelpers = new DowncastHelpers( controller.downcastDispatcher );
  43. conversion = new Conversion();
  44. conversion.register( 'downcast', downcastHelpers );
  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, viewWriter ) => viewWriter.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. } );
  90. describe( 'attributeToElement()', () => {
  91. beforeEach( () => {
  92. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  93. } );
  94. it( 'should be chainable', () => {
  95. expect( downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } ) ).to.equal( downcastHelpers );
  96. } );
  97. it( 'config.view is a string', () => {
  98. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  99. model.change( writer => {
  100. writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
  101. } );
  102. expectResult( '<strong>foo</strong>' );
  103. } );
  104. it( 'can be overwritten using converterPriority', () => {
  105. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  106. downcastHelpers.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
  107. model.change( writer => {
  108. writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
  109. } );
  110. expectResult( '<b>foo</b>' );
  111. } );
  112. it( 'config.view is a view element definition', () => {
  113. downcastHelpers.attributeToElement( {
  114. model: 'invert',
  115. view: {
  116. name: 'span',
  117. classes: [ 'font-light', 'bg-dark' ]
  118. }
  119. } );
  120. model.change( writer => {
  121. writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
  122. } );
  123. expectResult( '<span class="bg-dark font-light">foo</span>' );
  124. expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  125. } );
  126. it( 'config.view allows specifying the element\'s priority', () => {
  127. downcastHelpers.attributeToElement( {
  128. model: 'invert',
  129. view: {
  130. name: 'span',
  131. priority: 5
  132. }
  133. } );
  134. model.change( writer => {
  135. writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
  136. } );
  137. expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
  138. } );
  139. it( 'model attribute value is enum', () => {
  140. downcastHelpers.attributeToElement( {
  141. model: {
  142. key: 'fontSize',
  143. values: [ 'big', 'small' ]
  144. },
  145. view: {
  146. big: {
  147. name: 'span',
  148. styles: {
  149. 'font-size': '1.2em'
  150. }
  151. },
  152. small: {
  153. name: 'span',
  154. styles: {
  155. 'font-size': '0.8em'
  156. },
  157. priority: 5
  158. }
  159. }
  160. } );
  161. model.change( writer => {
  162. writer.insertText( 'foo', { fontSize: 'big' }, modelRoot, 0 );
  163. } );
  164. expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  165. expectResult( '<span style="font-size:1.2em">foo</span>' );
  166. model.change( writer => {
  167. writer.setAttribute( 'fontSize', 'small', modelRoot.getChild( 0 ) );
  168. } );
  169. expectResult( '<span style="font-size:0.8em">foo</span>' );
  170. expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
  171. model.change( writer => {
  172. writer.removeAttribute( 'fontSize', modelRoot.getChild( 0 ) );
  173. } );
  174. expectResult( 'foo' );
  175. } );
  176. it( 'config.view is a function', () => {
  177. downcastHelpers.attributeToElement( {
  178. model: 'bold',
  179. view: ( modelAttributeValue, viewWriter ) => {
  180. return viewWriter.createAttributeElement( 'span', { style: 'font-weight:' + modelAttributeValue } );
  181. }
  182. } );
  183. model.change( writer => {
  184. writer.insertText( 'foo', { bold: '500' }, modelRoot, 0 );
  185. } );
  186. expectResult( '<span style="font-weight:500">foo</span>' );
  187. } );
  188. it( 'config.model.name is given', () => {
  189. downcastHelpers.attributeToElement( {
  190. model: {
  191. key: 'color',
  192. name: '$text'
  193. },
  194. view: ( modelAttributeValue, viewWriter ) => {
  195. return viewWriter.createAttributeElement( 'span', { style: 'color:' + modelAttributeValue } );
  196. }
  197. } );
  198. downcastHelpers.elementToElement( {
  199. model: 'smiley',
  200. view: ( modelElement, viewWriter ) => {
  201. return viewWriter.createEmptyElement( 'img', {
  202. src: 'smile.jpg',
  203. class: 'smiley'
  204. } );
  205. }
  206. } );
  207. model.change( writer => {
  208. writer.insertText( 'foo', { color: '#FF0000' }, modelRoot, 0 );
  209. writer.insertElement( 'smiley', { color: '#FF0000' }, modelRoot, 3 );
  210. } );
  211. expectResult( '<span style="color:#FF0000">foo</span><img class="smiley" src="smile.jpg"></img>' );
  212. } );
  213. it( 'should not convert if creator returned null', () => {
  214. downcastHelpers.elementToElement( { model: 'div', view: () => null } );
  215. const modelElement = new ModelElement( 'div' );
  216. model.change( writer => {
  217. writer.insert( modelElement, modelRootStart );
  218. } );
  219. expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
  220. } );
  221. it( 'should convert insert/change/remove of attribute in model into wrapping element in a view', () => {
  222. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
  223. downcastHelpers.attributeToElement( {
  224. model: 'bold',
  225. view: ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' )
  226. } );
  227. model.change( writer => {
  228. writer.insert( modelElement, modelRootStart );
  229. } );
  230. expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
  231. model.change( writer => {
  232. writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
  233. } );
  234. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  235. } );
  236. it( 'should convert insert/remove of attribute in model with wrapping element generating function as a parameter', () => {
  237. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { style: 'bold' } ) );
  238. downcastHelpers.attributeToElement( {
  239. model: 'style',
  240. view: ( modelAttributeValue, viewWriter ) => {
  241. if ( modelAttributeValue == 'bold' ) {
  242. return viewWriter.createAttributeElement( 'b' );
  243. }
  244. }
  245. } );
  246. model.change( writer => {
  247. writer.insert( modelElement, modelRootStart );
  248. } );
  249. expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
  250. model.change( writer => {
  251. writer.removeAttribute( 'style', writer.createRangeIn( modelElement ) );
  252. } );
  253. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  254. } );
  255. it( 'should update range on re-wrapping attribute (#475)', () => {
  256. const modelElement = new ModelElement( 'paragraph', null, [
  257. new ModelText( 'x' ),
  258. new ModelText( 'foo', { link: 'http://foo.com' } ),
  259. new ModelText( 'x' )
  260. ] );
  261. downcastHelpers.attributeToElement( {
  262. model: 'link',
  263. view: ( modelAttributeValue, viewWriter ) => {
  264. return viewWriter.createAttributeElement( 'a', { href: modelAttributeValue } );
  265. }
  266. } );
  267. model.change( writer => {
  268. writer.insert( modelElement, modelRootStart );
  269. } );
  270. expect( viewToString( viewRoot ) ).to.equal( '<div><p>x<a href="http://foo.com">foo</a>x</p></div>' );
  271. // Set new attribute on old link but also on non-linked characters.
  272. model.change( writer => {
  273. writer.setAttribute( 'link', 'http://foobar.com', writer.createRangeIn( modelElement ) );
  274. } );
  275. expect( viewToString( viewRoot ) ).to.equal( '<div><p><a href="http://foobar.com">xfoox</a></p></div>' );
  276. } );
  277. it( 'should support unicode', () => {
  278. const modelElement = new ModelElement( 'paragraph', null, [ 'நி', new ModelText( 'லைக்', { bold: true } ), 'கு' ] );
  279. downcastHelpers.attributeToElement( {
  280. model: 'bold',
  281. view: ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' )
  282. } );
  283. model.change( writer => {
  284. writer.insert( modelElement, modelRootStart );
  285. } );
  286. expect( viewToString( viewRoot ) ).to.equal( '<div><p>நி<b>லைக்</b>கு</p></div>' );
  287. model.change( writer => {
  288. writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
  289. } );
  290. expect( viewToString( viewRoot ) ).to.equal( '<div><p>நிலைக்கு</p></div>' );
  291. } );
  292. it( 'should be possible to override ', () => {
  293. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
  294. downcastHelpers.attributeToElement( {
  295. model: 'bold',
  296. view: ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' )
  297. } );
  298. downcastHelpers.attributeToElement( {
  299. model: 'bold',
  300. view: ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'strong' ),
  301. converterPriority: 'high'
  302. } );
  303. model.change( writer => {
  304. writer.insert( modelElement, modelRootStart );
  305. } );
  306. expect( viewToString( viewRoot ) ).to.equal( '<div><p><strong>foobar</strong></p></div>' );
  307. } );
  308. it( 'should not convert and not consume if creator function returned null', () => {
  309. sinon.spy( controller.downcastDispatcher, 'fire' );
  310. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { italic: true } ) );
  311. downcastHelpers.attributeToElement( {
  312. model: 'italic',
  313. view: () => null
  314. } );
  315. const spy = sinon.spy();
  316. controller.downcastDispatcher.on( 'attribute:italic', spy );
  317. model.change( writer => {
  318. writer.insert( modelElement, modelRootStart );
  319. } );
  320. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  321. expect( controller.downcastDispatcher.fire.calledWith( 'attribute:italic:$text' ) ).to.be.true;
  322. expect( spy.called ).to.be.true;
  323. } );
  324. } );
  325. describe( 'attributeToAttribute()', () => {
  326. testUtils.createSinonSandbox();
  327. beforeEach( () => {
  328. downcastHelpers.elementToElement( { model: 'image', view: 'img' } );
  329. downcastHelpers.elementToElement( {
  330. model: 'paragraph',
  331. view: ( modelItem, viewWriter ) => viewWriter.createContainerElement( 'p' )
  332. } );
  333. downcastHelpers.attributeToAttribute( {
  334. model: 'class',
  335. view: 'class'
  336. } );
  337. } );
  338. it( 'should be chainable', () => {
  339. expect( downcastHelpers.attributeToAttribute( { model: 'source', view: 'src' } ) ).to.equal( downcastHelpers );
  340. } );
  341. it( 'config.view is a string', () => {
  342. downcastHelpers.attributeToAttribute( { model: 'source', view: 'src' } );
  343. model.change( writer => {
  344. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  345. } );
  346. expectResult( '<img src="foo.jpg"></img>' );
  347. model.change( writer => {
  348. writer.removeAttribute( 'source', modelRoot.getChild( 0 ) );
  349. } );
  350. expectResult( '<img></img>' );
  351. } );
  352. it( 'can be overwritten using converterPriority', () => {
  353. downcastHelpers.attributeToAttribute( { model: 'source', view: 'href' } );
  354. downcastHelpers.attributeToAttribute( { model: 'source', view: 'src', converterPriority: 'high' } );
  355. model.change( writer => {
  356. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  357. } );
  358. expectResult( '<img src="foo.jpg"></img>' );
  359. } );
  360. it( 'model element name specified', () => {
  361. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  362. downcastHelpers.attributeToAttribute( {
  363. model: {
  364. name: 'image',
  365. key: 'source'
  366. },
  367. view: 'src'
  368. } );
  369. model.change( writer => {
  370. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  371. } );
  372. expectResult( '<img src="foo.jpg"></img>' );
  373. model.change( writer => {
  374. writer.rename( modelRoot.getChild( 0 ), 'paragraph' );
  375. } );
  376. expectResult( '<p></p>' );
  377. } );
  378. it( 'config.view is an object, model attribute value is enum', () => {
  379. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  380. downcastHelpers.attributeToAttribute( {
  381. model: {
  382. key: 'styled',
  383. values: [ 'dark', 'light' ]
  384. },
  385. view: {
  386. dark: {
  387. key: 'class',
  388. value: [ 'styled', 'styled-dark' ]
  389. },
  390. light: {
  391. key: 'class',
  392. value: [ 'styled', 'styled-light' ]
  393. }
  394. }
  395. } );
  396. model.change( writer => {
  397. writer.insertElement( 'paragraph', { styled: 'dark' }, modelRoot, 0 );
  398. } );
  399. expectResult( '<p class="styled styled-dark"></p>' );
  400. model.change( writer => {
  401. writer.setAttribute( 'styled', 'light', modelRoot.getChild( 0 ) );
  402. } );
  403. expectResult( '<p class="styled styled-light"></p>' );
  404. model.change( writer => {
  405. writer.removeAttribute( 'styled', modelRoot.getChild( 0 ) );
  406. } );
  407. expectResult( '<p></p>' );
  408. } );
  409. it( 'config.view is an object, model attribute value is enum, view has style', () => {
  410. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  411. downcastHelpers.attributeToAttribute( {
  412. model: {
  413. key: 'align',
  414. values: [ 'right', 'center' ]
  415. },
  416. view: {
  417. right: {
  418. key: 'style',
  419. value: {
  420. 'text-align': 'right'
  421. }
  422. },
  423. center: {
  424. key: 'style',
  425. value: {
  426. 'text-align': 'center'
  427. }
  428. }
  429. }
  430. } );
  431. model.change( writer => {
  432. writer.insertElement( 'paragraph', { align: 'right' }, modelRoot, 0 );
  433. } );
  434. expectResult( '<p style="text-align:right"></p>' );
  435. model.change( writer => {
  436. writer.setAttribute( 'align', 'center', modelRoot.getChild( 0 ) );
  437. } );
  438. expectResult( '<p style="text-align:center"></p>' );
  439. model.change( writer => {
  440. writer.removeAttribute( 'align', modelRoot.getChild( 0 ) );
  441. } );
  442. expectResult( '<p></p>' );
  443. } );
  444. it( 'config.view is an object, only name and key are provided', () => {
  445. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  446. downcastHelpers.attributeToAttribute( {
  447. model: {
  448. name: 'paragraph',
  449. key: 'class'
  450. },
  451. view: {
  452. name: 'paragraph',
  453. key: 'class'
  454. }
  455. } );
  456. model.change( writer => {
  457. writer.insertElement( 'paragraph', { class: 'dark' }, modelRoot, 0 );
  458. } );
  459. expectResult( '<p class="dark"></p>' );
  460. model.change( writer => {
  461. writer.setAttribute( 'class', 'light', modelRoot.getChild( 0 ) );
  462. } );
  463. expectResult( '<p class="light"></p>' );
  464. model.change( writer => {
  465. writer.removeAttribute( 'class', modelRoot.getChild( 0 ) );
  466. } );
  467. expectResult( '<p></p>' );
  468. } );
  469. it( 'config.view is a function', () => {
  470. downcastHelpers.attributeToAttribute( {
  471. model: 'styled',
  472. view: attributeValue => ( { key: 'class', value: 'styled-' + attributeValue } )
  473. } );
  474. model.change( writer => {
  475. writer.insertElement( 'image', { styled: 'pull-out' }, modelRoot, 0 );
  476. } );
  477. expectResult( '<img class="styled-pull-out"></img>' );
  478. } );
  479. // #1587
  480. it( 'config.view and config.model as strings in generic conversion (elements only)', () => {
  481. const logSpy = testUtils.sinon.spy( log, 'warn' );
  482. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  483. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  484. model.change( writer => {
  485. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 0 );
  486. writer.insertElement( 'paragraph', { test: '2' }, modelRoot, 1 );
  487. } );
  488. expectResult( '<p test="1"></p><p test="2"></p>' );
  489. expect( logSpy.callCount ).to.equal( 0 );
  490. model.change( writer => {
  491. writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
  492. } );
  493. expectResult( '<p test="1"></p><p></p>' );
  494. } );
  495. // #1587
  496. it( 'config.view and config.model as strings in generic conversion (elements + text)', () => {
  497. const logSpy = testUtils.sinon.spy( log, 'warn' );
  498. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  499. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  500. model.change( writer => {
  501. writer.insertElement( 'paragraph', modelRoot, 0 );
  502. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 1 );
  503. writer.insertText( 'Foo', { test: '2' }, modelRoot.getChild( 0 ), 0 );
  504. writer.insertText( 'Bar', { test: '3' }, modelRoot.getChild( 1 ), 0 );
  505. } );
  506. expectResult( '<p>Foo</p><p test="1">Bar</p>' );
  507. expect( logSpy.callCount ).to.equal( 2 );
  508. expect( logSpy.alwaysCalledWithMatch( 'conversion-attribute-to-attribute-on-text' ) ).to.true;
  509. model.change( writer => {
  510. writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
  511. } );
  512. expectResult( '<p>Foo</p><p>Bar</p>' );
  513. } );
  514. it( 'should convert attribute insert/change/remove on a model node', () => {
  515. const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  516. model.change( writer => {
  517. writer.insert( modelElement, modelRootStart );
  518. } );
  519. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  520. model.change( writer => {
  521. writer.setAttribute( 'class', 'bar', modelElement );
  522. } );
  523. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="bar">foobar</p></div>' );
  524. model.change( writer => {
  525. writer.removeAttribute( 'class', modelElement );
  526. } );
  527. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  528. } );
  529. it( 'should convert insert/change/remove with attribute generating function as a parameter', () => {
  530. downcastHelpers.elementToElement( { model: 'div', view: 'div' } );
  531. downcastHelpers.attributeToAttribute( {
  532. model: 'theme',
  533. view: ( value, data ) => {
  534. if ( data.item instanceof ModelElement && data.item.childCount > 0 ) {
  535. value += ' fix-content';
  536. }
  537. return { key: 'class', value };
  538. }
  539. } );
  540. const modelParagraph = new ModelElement( 'paragraph', { theme: 'nice' }, new ModelText( 'foobar' ) );
  541. const modelDiv = new ModelElement( 'div', { theme: 'nice' } );
  542. model.change( writer => {
  543. writer.insert( [ modelParagraph, modelDiv ], modelRootStart );
  544. } );
  545. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="nice fix-content">foobar</p><div class="nice"></div></div>' );
  546. model.change( writer => {
  547. writer.setAttribute( 'theme', 'awesome', modelParagraph );
  548. } );
  549. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="awesome fix-content">foobar</p><div class="nice"></div></div>' );
  550. model.change( writer => {
  551. writer.removeAttribute( 'theme', modelParagraph );
  552. } );
  553. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p><div class="nice"></div></div>' );
  554. } );
  555. it( 'should be possible to override setAttribute', () => {
  556. downcastHelpers.attributeToAttribute( {
  557. model: 'class',
  558. view: ( evt, data, conversionApi ) => {
  559. conversionApi.consumable.consume( data.item, 'attribute:class' );
  560. },
  561. converterPriority: 'high'
  562. } );
  563. model.change( writer => {
  564. const modelElement = new ModelElement( 'paragraph', { classes: 'foo' }, new ModelText( 'foobar' ) );
  565. writer.insert( modelElement, modelRootStart );
  566. } );
  567. // No attribute set.
  568. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  569. } );
  570. it( 'should not convert or consume if element creator returned null', () => {
  571. const callback = sinon.stub().returns( null );
  572. downcastHelpers.attributeToAttribute( {
  573. model: 'class',
  574. view: callback,
  575. converterPriority: 'high'
  576. } );
  577. const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  578. model.change( writer => {
  579. writer.insert( modelElement, modelRootStart );
  580. } );
  581. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  582. sinon.assert.called( callback );
  583. } );
  584. } );
  585. describe( 'markerToElement()', () => {
  586. let modelText, modelElement, range;
  587. it( 'should be chainable', () => {
  588. expect( downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } ) ).to.equal( downcastHelpers );
  589. } );
  590. it( 'config.view is a string', () => {
  591. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  592. model.change( writer => {
  593. writer.insertText( 'foo', modelRoot, 0 );
  594. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  595. writer.addMarker( 'search', { range, usingOperation: false } );
  596. } );
  597. expectResult( 'f<marker-search></marker-search>o<marker-search></marker-search>o' );
  598. } );
  599. it( 'can be overwritten using converterPriority', () => {
  600. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  601. downcastHelpers.markerToElement( { model: 'search', view: 'search', converterPriority: 'high' } );
  602. model.change( writer => {
  603. writer.insertText( 'foo', modelRoot, 0 );
  604. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  605. writer.addMarker( 'search', { range, usingOperation: false } );
  606. } );
  607. expectResult( 'f<search></search>o<search></search>o' );
  608. } );
  609. it( 'config.view is a view element definition', () => {
  610. downcastHelpers.markerToElement( {
  611. model: 'search',
  612. view: {
  613. name: 'span',
  614. attributes: {
  615. 'data-marker': 'search'
  616. }
  617. }
  618. } );
  619. model.change( writer => {
  620. writer.insertText( 'foo', modelRoot, 0 );
  621. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  622. writer.addMarker( 'search', { range, usingOperation: false } );
  623. } );
  624. expectResult( 'f<span data-marker="search"></span>o<span data-marker="search"></span>o' );
  625. } );
  626. it( 'config.view is a function', () => {
  627. downcastHelpers.markerToElement( {
  628. model: 'search',
  629. view: ( data, viewWriter ) => {
  630. return viewWriter.createUIElement( 'span', { 'data-marker': 'search', 'data-start': data.isOpening } );
  631. }
  632. } );
  633. model.change( writer => {
  634. writer.insertText( 'foo', modelRoot, 0 );
  635. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  636. writer.addMarker( 'search', { range, usingOperation: false } );
  637. } );
  638. expectResult( 'f<span data-marker="search" data-start="true"></span>o<span data-marker="search" data-start="false"></span>o' );
  639. } );
  640. describe( 'collapsed range', () => {
  641. beforeEach( () => {
  642. modelText = new ModelText( 'foobar' );
  643. modelElement = new ModelElement( 'paragraph', null, modelText );
  644. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  645. model.change( writer => {
  646. writer.insert( modelElement, modelRootStart );
  647. } );
  648. range = model.createRange( model.createPositionAt( modelElement, 3 ), model.createPositionAt( modelElement, 3 ) );
  649. } );
  650. it( 'should insert and remove ui element', () => {
  651. downcastHelpers.markerToElement( {
  652. model: 'marker',
  653. view: ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } )
  654. } );
  655. model.change( writer => {
  656. writer.addMarker( 'marker', { range, usingOperation: false } );
  657. } );
  658. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo<span class="marker"></span>bar</p></div>' );
  659. model.change( writer => {
  660. writer.removeMarker( 'marker' );
  661. } );
  662. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  663. } );
  664. it( 'should not convert if consumable was consumed', () => {
  665. sinon.spy( controller.downcastDispatcher, 'fire' );
  666. downcastHelpers.markerToElement( {
  667. model: 'marker',
  668. view: ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } )
  669. } );
  670. controller.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  671. conversionApi.consumable.consume( data.markerRange, 'addMarker:marker' );
  672. }, { priority: 'high' } );
  673. model.change( writer => {
  674. writer.addMarker( 'marker', { range, usingOperation: false } );
  675. } );
  676. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  677. expect( controller.downcastDispatcher.fire.calledWith( 'addMarker:marker' ) );
  678. } );
  679. it( 'should not convert if creator returned null', () => {
  680. downcastHelpers.markerToElement( {
  681. model: 'marker',
  682. view: () => null
  683. } );
  684. model.change( writer => {
  685. writer.addMarker( 'marker', { range, usingOperation: false } );
  686. } );
  687. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  688. model.change( writer => {
  689. writer.removeMarker( 'marker' );
  690. } );
  691. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  692. } );
  693. } );
  694. describe( 'non-collapsed range', () => {
  695. beforeEach( () => {
  696. modelText = new ModelText( 'foobar' );
  697. modelElement = new ModelElement( 'paragraph', null, modelText );
  698. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  699. model.change( writer => {
  700. writer.insert( modelElement, modelRootStart );
  701. } );
  702. range = model.createRange( model.createPositionAt( modelElement, 2 ), model.createPositionAt( modelElement, 5 ) );
  703. } );
  704. it( 'should insert and remove ui element - element as a creator', () => {
  705. downcastHelpers.markerToElement( {
  706. model: 'marker',
  707. view: ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } )
  708. } );
  709. model.change( writer => {
  710. writer.addMarker( 'marker', { range, usingOperation: false } );
  711. } );
  712. expect( viewToString( viewRoot ) )
  713. .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  714. model.change( writer => {
  715. writer.removeMarker( 'marker' );
  716. } );
  717. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  718. } );
  719. it( 'should insert and remove ui element - function as a creator', () => {
  720. downcastHelpers.markerToElement( {
  721. model: 'marker',
  722. view: ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': data.markerName } )
  723. } );
  724. model.change( writer => {
  725. writer.addMarker( 'marker', { range, usingOperation: false } );
  726. } );
  727. expect( viewToString( viewRoot ) )
  728. .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  729. model.change( writer => {
  730. writer.removeMarker( 'marker' );
  731. } );
  732. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  733. } );
  734. it( 'should insert and remove different opening and ending element', () => {
  735. downcastHelpers.markerToElement( {
  736. model: 'marker',
  737. view: ( data, viewWriter ) => {
  738. if ( data.isOpening ) {
  739. return viewWriter.createUIElement( 'span', { 'class': data.markerName, 'data-start': true } );
  740. }
  741. return viewWriter.createUIElement( 'span', { 'class': data.markerName, 'data-end': true } );
  742. }
  743. } );
  744. model.change( writer => {
  745. writer.addMarker( 'marker', { range, usingOperation: false } );
  746. } );
  747. expect( viewToString( viewRoot ) ).to.equal(
  748. '<div><p>fo<span class="marker" data-start="true"></span>oba<span class="marker" data-end="true"></span>r</p></div>'
  749. );
  750. model.change( writer => {
  751. writer.removeMarker( 'marker' );
  752. } );
  753. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  754. } );
  755. it( 'should not convert if consumable was consumed', () => {
  756. sinon.spy( controller.downcastDispatcher, 'fire' );
  757. downcastHelpers.markerToElement( {
  758. model: 'marker',
  759. view: ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } )
  760. } );
  761. controller.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  762. conversionApi.consumable.consume( data.item, 'addMarker:marker' );
  763. }, { priority: 'high' } );
  764. model.change( writer => {
  765. writer.addMarker( 'marker', { range, usingOperation: false } );
  766. } );
  767. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  768. expect( controller.downcastDispatcher.fire.calledWith( 'addMarker:marker' ) );
  769. } );
  770. } );
  771. } );
  772. describe( 'markerToHighlight()', () => {
  773. it( 'should be chainable', () => {
  774. expect( downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } ) ).to.equal( downcastHelpers );
  775. } );
  776. it( 'config.view is a highlight descriptor', () => {
  777. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  778. model.change( writer => {
  779. writer.insertText( 'foo', modelRoot, 0 );
  780. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  781. writer.addMarker( 'comment', { range, usingOperation: false } );
  782. } );
  783. expectResult( '<span class="comment">foo</span>' );
  784. } );
  785. it( 'can be overwritten using converterPriority', () => {
  786. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  787. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'new-comment' }, converterPriority: 'high' } );
  788. model.change( writer => {
  789. writer.insertText( 'foo', modelRoot, 0 );
  790. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  791. writer.addMarker( 'comment', { range, usingOperation: false } );
  792. } );
  793. expectResult( '<span class="new-comment">foo</span>' );
  794. } );
  795. it( 'config.view is a function', () => {
  796. downcastHelpers.markerToHighlight( {
  797. model: 'comment',
  798. view: data => {
  799. const commentType = data.markerName.split( ':' )[ 1 ];
  800. return {
  801. classes: [ 'comment', 'comment-' + commentType ]
  802. };
  803. }
  804. } );
  805. model.change( writer => {
  806. writer.insertText( 'foo', modelRoot, 0 );
  807. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  808. writer.addMarker( 'comment:abc', { range, usingOperation: false } );
  809. } );
  810. expectResult( '<span class="comment comment-abc">foo</span>' );
  811. } );
  812. describe( 'highlight', () => {
  813. const highlightConfig = {
  814. model: 'marker',
  815. view: {
  816. classes: 'highlight-class',
  817. attributes: { title: 'title' }
  818. },
  819. converterPriority: 7
  820. };
  821. describe( 'on text', () => {
  822. let markerRange;
  823. beforeEach( () => {
  824. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  825. const modelElement1 = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  826. const modelElement2 = new ModelElement( 'paragraph', null, new ModelText( 'bar' ) );
  827. model.change( writer => {
  828. writer.insert( [ modelElement1, modelElement2 ], modelRootStart );
  829. } );
  830. markerRange = model.createRangeIn( modelRoot );
  831. } );
  832. it( 'should wrap and unwrap text nodes', () => {
  833. downcastHelpers.markerToHighlight( highlightConfig );
  834. model.change( writer => {
  835. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  836. } );
  837. expect( viewToString( viewRoot ) ).to.equal(
  838. '<div>' +
  839. '<p>' +
  840. '<span class="highlight-class" title="title">foo</span>' +
  841. '</p>' +
  842. '<p>' +
  843. '<span class="highlight-class" title="title">bar</span>' +
  844. '</p>' +
  845. '</div>'
  846. );
  847. model.change( writer => {
  848. writer.removeMarker( 'marker' );
  849. } );
  850. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  851. } );
  852. it( 'should be possible to overwrite', () => {
  853. downcastHelpers.markerToHighlight( highlightConfig );
  854. downcastHelpers.markerToHighlight( {
  855. model: 'marker',
  856. view: { classes: 'override-class' },
  857. converterPriority: 'high'
  858. } );
  859. model.change( writer => {
  860. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  861. } );
  862. expect( viewToString( viewRoot ) ).to.equal(
  863. '<div>' +
  864. '<p>' +
  865. '<span class="override-class">foo</span>' +
  866. '</p>' +
  867. '<p>' +
  868. '<span class="override-class">bar</span>' +
  869. '</p>' +
  870. '</div>'
  871. );
  872. model.change( writer => {
  873. writer.removeMarker( 'marker' );
  874. } );
  875. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  876. } );
  877. it( 'should do nothing if descriptor is not provided or generating function returns null', () => {
  878. downcastHelpers.markerToHighlight( {
  879. model: 'marker',
  880. view: () => null,
  881. converterPriority: 'high'
  882. } );
  883. model.change( writer => {
  884. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  885. } );
  886. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  887. model.change( writer => {
  888. writer.removeMarker( 'marker' );
  889. } );
  890. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  891. } );
  892. it( 'should do nothing if collapsed marker is converted', () => {
  893. downcastHelpers.markerToHighlight( {
  894. model: 'marker',
  895. view: { classes: 'foo' },
  896. converterPriority: 'high'
  897. } );
  898. markerRange = model.createRange( model.createPositionAt( modelRoot, 0 ), model.createPositionAt( modelRoot, 0 ) );
  899. model.change( writer => {
  900. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  901. } );
  902. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  903. model.change( () => {
  904. model.markers._remove( 'marker' );
  905. } );
  906. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  907. } );
  908. it( 'should correctly wrap and unwrap multiple, intersecting markers', () => {
  909. downcastHelpers.markerToHighlight( {
  910. model: 'markerFoo',
  911. view: { classes: 'foo' }
  912. } );
  913. downcastHelpers.markerToHighlight( {
  914. model: 'markerBar',
  915. view: { classes: 'bar' }
  916. } );
  917. downcastHelpers.markerToHighlight( {
  918. model: 'markerXyz',
  919. view: { classes: 'xyz' }
  920. } );
  921. const p1 = modelRoot.getChild( 0 );
  922. const p2 = modelRoot.getChild( 1 );
  923. model.change( writer => {
  924. const range = writer.createRange( writer.createPositionAt( p1, 0 ), writer.createPositionAt( p1, 3 ) );
  925. writer.addMarker( 'markerFoo', { range, usingOperation: false } );
  926. } );
  927. expect( viewToString( viewRoot ) ).to.equal(
  928. '<div>' +
  929. '<p>' +
  930. '<span class="foo">foo</span>' +
  931. '</p>' +
  932. '<p>bar</p>' +
  933. '</div>'
  934. );
  935. model.change( writer => {
  936. const range = writer.createRange( writer.createPositionAt( p1, 1 ), writer.createPositionAt( p2, 2 ) );
  937. writer.addMarker( 'markerBar', { range, usingOperation: false } );
  938. } );
  939. expect( viewToString( viewRoot ) ).to.equal(
  940. '<div>' +
  941. '<p>' +
  942. '<span class="foo">f</span>' +
  943. '<span class="bar">' +
  944. '<span class="foo">oo</span>' +
  945. '</span>' +
  946. '</p>' +
  947. '<p>' +
  948. '<span class="bar">ba</span>' +
  949. 'r' +
  950. '</p>' +
  951. '</div>'
  952. );
  953. model.change( writer => {
  954. const range = writer.createRange( writer.createPositionAt( p1, 2 ), writer.createPositionAt( p2, 3 ) );
  955. writer.addMarker( 'markerXyz', { range, usingOperation: false } );
  956. } );
  957. expect( viewToString( viewRoot ) ).to.equal(
  958. '<div>' +
  959. '<p>' +
  960. '<span class="foo">f</span>' +
  961. '<span class="bar">' +
  962. '<span class="foo">' +
  963. 'o' +
  964. '<span class="xyz">o</span>' +
  965. '</span>' +
  966. '</span>' +
  967. '</p>' +
  968. '<p>' +
  969. '<span class="bar">' +
  970. '<span class="xyz">ba</span>' +
  971. '</span>' +
  972. '<span class="xyz">r</span>' +
  973. '</p>' +
  974. '</div>'
  975. );
  976. model.change( writer => {
  977. writer.removeMarker( 'markerBar' );
  978. } );
  979. expect( viewToString( viewRoot ) ).to.equal(
  980. '<div>' +
  981. '<p>' +
  982. '<span class="foo">' +
  983. 'fo' +
  984. '<span class="xyz">o</span>' +
  985. '</span>' +
  986. '</p>' +
  987. '<p>' +
  988. '<span class="xyz">bar</span>' +
  989. '</p>' +
  990. '</div>'
  991. );
  992. model.change( writer => {
  993. writer.removeMarker( 'markerFoo' );
  994. } );
  995. expect( viewToString( viewRoot ) ).to.equal(
  996. '<div>' +
  997. '<p>' +
  998. 'fo' +
  999. '<span class="xyz">o</span>' +
  1000. '</p>' +
  1001. '<p>' +
  1002. '<span class="xyz">bar</span>' +
  1003. '</p>' +
  1004. '</div>'
  1005. );
  1006. model.change( writer => {
  1007. writer.removeMarker( 'markerXyz' );
  1008. } );
  1009. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1010. } );
  1011. it( 'should do nothing if marker is applied and removed on empty-ish range', () => {
  1012. downcastHelpers.markerToHighlight( highlightConfig );
  1013. const p1 = modelRoot.getChild( 0 );
  1014. const p2 = modelRoot.getChild( 1 );
  1015. const markerRange = model.createRange( model.createPositionAt( p1, 3 ), model.createPositionAt( p2, 0 ) );
  1016. model.change( writer => {
  1017. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1018. } );
  1019. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1020. model.change( writer => {
  1021. writer.removeMarker( 'marker', { range: markerRange, usingOperation: false } );
  1022. } );
  1023. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1024. } );
  1025. } );
  1026. describe( 'on element', () => {
  1027. const highlightConfig = {
  1028. model: 'marker',
  1029. view: {
  1030. classes: 'highlight-class',
  1031. attributes: { title: 'title' },
  1032. id: 'customId'
  1033. },
  1034. converterPriority: 7
  1035. };
  1036. let markerRange;
  1037. beforeEach( () => {
  1038. downcastHelpers.elementToElement( {
  1039. model: 'div',
  1040. view: () => {
  1041. const viewContainer = new ViewContainerElement( 'div' );
  1042. viewContainer._setCustomProperty( 'addHighlight', ( element, descriptor, writer ) => {
  1043. writer.addClass( descriptor.classes, element );
  1044. } );
  1045. viewContainer._setCustomProperty( 'removeHighlight', ( element, id, writer ) => {
  1046. writer.setAttribute( 'class', '', element );
  1047. } );
  1048. return viewContainer;
  1049. }
  1050. } );
  1051. const modelElement = new ModelElement( 'div', null, new ModelText( 'foo' ) );
  1052. model.change( writer => {
  1053. writer.insert( modelElement, modelRootStart );
  1054. } );
  1055. markerRange = model.createRangeOn( modelElement );
  1056. downcastHelpers.markerToHighlight( highlightConfig );
  1057. } );
  1058. it( 'should use addHighlight and removeHighlight on elements and not convert children nodes', () => {
  1059. model.change( writer => {
  1060. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1061. } );
  1062. expect( viewToString( viewRoot ) ).to.equal(
  1063. '<div>' +
  1064. '<div class="highlight-class">' +
  1065. 'foo' +
  1066. '</div>' +
  1067. '</div>'
  1068. );
  1069. model.change( writer => {
  1070. writer.removeMarker( 'marker' );
  1071. } );
  1072. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1073. } );
  1074. it( 'should be possible to override', () => {
  1075. downcastHelpers.markerToHighlight( {
  1076. model: 'marker',
  1077. view: { classes: 'override-class' },
  1078. converterPriority: 'high'
  1079. } );
  1080. model.change( writer => {
  1081. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1082. } );
  1083. expect( viewToString( viewRoot ) ).to.equal(
  1084. '<div>' +
  1085. '<div class="override-class">' +
  1086. 'foo' +
  1087. '</div>' +
  1088. '</div>'
  1089. );
  1090. model.change( writer => {
  1091. writer.removeMarker( 'marker' );
  1092. } );
  1093. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1094. } );
  1095. it( 'should use default priority and id if not provided', () => {
  1096. const viewDiv = viewRoot.getChild( 0 );
  1097. downcastHelpers.markerToHighlight( {
  1098. model: 'marker2',
  1099. view: () => null,
  1100. converterPriority: 'high'
  1101. } );
  1102. viewDiv._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
  1103. expect( descriptor.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  1104. expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
  1105. } );
  1106. viewDiv._setCustomProperty( 'removeHighlight', ( element, id ) => {
  1107. expect( id ).to.equal( 'marker:foo-bar-baz' );
  1108. } );
  1109. model.change( writer => {
  1110. writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  1111. } );
  1112. } );
  1113. it( 'should do nothing if descriptor is not provided', () => {
  1114. downcastHelpers.markerToHighlight( {
  1115. model: 'marker2',
  1116. view: () => null
  1117. } );
  1118. model.change( writer => {
  1119. writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  1120. } );
  1121. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1122. model.change( writer => {
  1123. writer.removeMarker( 'marker2' );
  1124. } );
  1125. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1126. } );
  1127. } );
  1128. } );
  1129. } );
  1130. function expectResult( string ) {
  1131. expect( stringifyView( viewRoot, null, { ignoreRoot: true } ) ).to.equal( string );
  1132. }
  1133. } );
  1134. describe( 'downcast converters', () => {
  1135. let dispatcher, modelDoc, modelRoot, viewRoot, controller, modelRootStart, model;
  1136. beforeEach( () => {
  1137. model = new Model();
  1138. modelDoc = model.document;
  1139. modelRoot = modelDoc.createRoot();
  1140. controller = new EditingController( model );
  1141. viewRoot = controller.view.document.getRoot();
  1142. // Set name of view root the same as dom root.
  1143. // This is a mock of attaching view root to dom root.
  1144. controller.view.document.getRoot()._name = 'div';
  1145. dispatcher = controller.downcastDispatcher;
  1146. const downcastHelpers = new DowncastHelpers( dispatcher );
  1147. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1148. modelRootStart = model.createPositionAt( modelRoot, 0 );
  1149. } );
  1150. describe( 'insertText()', () => {
  1151. it( 'should downcast text', () => {
  1152. model.change( writer => {
  1153. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  1154. } );
  1155. expect( viewToString( viewRoot ) ).to.equal( '<div>foobar</div>' );
  1156. } );
  1157. it( 'should support unicode', () => {
  1158. model.change( writer => {
  1159. writer.insert( new ModelText( 'நிலைக்கு' ), modelRootStart );
  1160. } );
  1161. expect( viewToString( viewRoot ) ).to.equal( '<div>நிலைக்கு</div>' );
  1162. } );
  1163. it( 'should be possible to override it', () => {
  1164. dispatcher.on( 'insert:$text', ( evt, data, conversionApi ) => {
  1165. conversionApi.consumable.consume( data.item, 'insert' );
  1166. }, { converterPriority: 'high' } );
  1167. model.change( writer => {
  1168. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  1169. } );
  1170. expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
  1171. } );
  1172. } );
  1173. // Remove converter is by default already added in `EditingController` instance.
  1174. describe( 'remove()', () => {
  1175. it( 'should remove items from view accordingly to changes in model #1', () => {
  1176. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  1177. model.change( writer => {
  1178. writer.insert( modelElement, modelRootStart );
  1179. } );
  1180. model.change( writer => {
  1181. writer.remove(
  1182. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  1183. );
  1184. } );
  1185. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foar</p></div>' );
  1186. } );
  1187. it( 'should be possible to overwrite', () => {
  1188. dispatcher.on( 'remove', evt => evt.stop(), { priority: 'high' } );
  1189. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  1190. model.change( writer => {
  1191. writer.insert( modelElement, modelRootStart );
  1192. } );
  1193. model.change( writer => {
  1194. writer.remove(
  1195. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  1196. );
  1197. } );
  1198. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1199. } );
  1200. it( 'should support unicode', () => {
  1201. const modelElement = new ModelElement( 'paragraph', null, 'நிலைக்கு' );
  1202. model.change( writer => {
  1203. writer.insert( modelElement, modelRootStart );
  1204. } );
  1205. model.change( writer => {
  1206. writer.remove(
  1207. writer.createRange( writer.createPositionAt( modelElement, 0 ), writer.createPositionAt( modelElement, 6 ) )
  1208. );
  1209. } );
  1210. expect( viewToString( viewRoot ) ).to.equal( '<div><p>கு</p></div>' );
  1211. } );
  1212. it( 'should not remove view ui elements that are placed next to removed content', () => {
  1213. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  1214. viewRoot._appendChild( [
  1215. new ViewText( 'foz' ),
  1216. new ViewUIElement( 'span' ),
  1217. new ViewText( 'bar' )
  1218. ] );
  1219. // Remove 'b'.
  1220. model.change( writer => {
  1221. writer.remove(
  1222. writer.createRange( writer.createPositionAt( modelRoot, 3 ), writer.createPositionAt( modelRoot, 4 ) )
  1223. );
  1224. } );
  1225. expect( viewToString( viewRoot ) ).to.equal( '<div>foz<span></span>ar</div>' );
  1226. // Remove 'z'.
  1227. model.change( writer => {
  1228. writer.remove(
  1229. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 3 ) )
  1230. );
  1231. } );
  1232. expect( viewToString( viewRoot ) ).to.equal( '<div>fo<span></span>ar</div>' );
  1233. } );
  1234. it( 'should remove correct amount of text when it is split by view ui element', () => {
  1235. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  1236. viewRoot._appendChild( [
  1237. new ViewText( 'foz' ),
  1238. new ViewUIElement( 'span' ),
  1239. new ViewText( 'bar' )
  1240. ] );
  1241. // Remove 'z<span></span>b'.
  1242. model.change( writer => {
  1243. writer.remove(
  1244. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 4 ) )
  1245. );
  1246. } );
  1247. expect( viewToString( viewRoot ) ).to.equal( '<div>foar</div>' );
  1248. } );
  1249. it( 'should unbind elements', () => {
  1250. const modelElement = new ModelElement( 'paragraph' );
  1251. model.change( writer => {
  1252. writer.insert( modelElement, modelRootStart );
  1253. } );
  1254. const viewElement = controller.mapper.toViewElement( modelElement );
  1255. expect( viewElement ).not.to.be.undefined;
  1256. expect( controller.mapper.toModelElement( viewElement ) ).to.equal( modelElement );
  1257. model.change( writer => {
  1258. writer.remove( modelElement );
  1259. } );
  1260. expect( controller.mapper.toViewElement( modelElement ) ).to.be.undefined;
  1261. expect( controller.mapper.toModelElement( viewElement ) ).to.be.undefined;
  1262. } );
  1263. it( 'should not break when remove() is used as part of unwrapping', () => {
  1264. const modelP = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  1265. const modelWidget = new ModelElement( 'widget', null, modelP );
  1266. const downcastHelpers = new DowncastHelpers( dispatcher );
  1267. downcastHelpers.elementToElement( { model: 'widget', view: 'widget' } );
  1268. model.change( writer => {
  1269. writer.insert( modelWidget, modelRootStart );
  1270. } );
  1271. expect( viewToString( viewRoot ) ).to.equal( '<div><widget><p>foo</p></widget></div>' );
  1272. const viewP = controller.mapper.toViewElement( modelP );
  1273. expect( viewP ).not.to.be.undefined;
  1274. model.change( writer => {
  1275. writer.unwrap( modelWidget );
  1276. } );
  1277. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p></div>' );
  1278. // `modelP` is now bound with newly created view element.
  1279. expect( controller.mapper.toViewElement( modelP ) ).not.to.equal( viewP );
  1280. // `viewP` is no longer bound with model element.
  1281. expect( controller.mapper.toModelElement( viewP ) ).to.be.undefined;
  1282. // View element from view root is bound to `modelP`.
  1283. expect( controller.mapper.toModelElement( viewRoot.getChild( 0 ) ) ).to.equal( modelP );
  1284. } );
  1285. it( 'should work correctly if container element after ui element is removed', () => {
  1286. // Prepare a model and view structure.
  1287. // This is done outside of conversion to put view ui elements inside easily.
  1288. const modelP1 = new ModelElement( 'paragraph' );
  1289. const modelP2 = new ModelElement( 'paragraph' );
  1290. const viewP1 = new ViewContainerElement( 'p' );
  1291. const viewUi1 = new ViewUIElement( 'span' );
  1292. const viewUi2 = new ViewUIElement( 'span' );
  1293. const viewP2 = new ViewContainerElement( 'p' );
  1294. modelRoot._appendChild( [ modelP1, modelP2 ] );
  1295. viewRoot._appendChild( [ viewP1, viewUi1, viewUi2, viewP2 ] );
  1296. controller.mapper.bindElements( modelP1, viewP1 );
  1297. controller.mapper.bindElements( modelP2, viewP2 );
  1298. // Remove second paragraph element.
  1299. model.change( writer => {
  1300. writer.remove( writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) ) );
  1301. } );
  1302. expect( viewToString( viewRoot ) ).to.equal( '<div><p></p><span></span><span></span></div>' );
  1303. } );
  1304. it( 'should work correctly if container element after text node is removed', () => {
  1305. const modelText = new ModelText( 'foo' );
  1306. const modelP = new ModelElement( 'paragraph' );
  1307. model.change( writer => {
  1308. writer.insert( [ modelText, modelP ], modelRootStart );
  1309. } );
  1310. model.change( writer => {
  1311. writer.remove( modelP );
  1312. } );
  1313. expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
  1314. } );
  1315. } );
  1316. describe( 'createViewElementFromHighlightDescriptor()', () => {
  1317. it( 'should return attribute element from descriptor object', () => {
  1318. const descriptor = {
  1319. classes: 'foo-class',
  1320. attributes: { one: '1', two: '2' },
  1321. priority: 7
  1322. };
  1323. const element = createViewElementFromHighlightDescriptor( descriptor );
  1324. expect( element.is( 'attributeElement' ) ).to.be.true;
  1325. expect( element.name ).to.equal( 'span' );
  1326. expect( element.priority ).to.equal( 7 );
  1327. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1328. for ( const key of Object.keys( descriptor.attributes ) ) {
  1329. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1330. }
  1331. } );
  1332. it( 'should return attribute element from descriptor object - array with classes', () => {
  1333. const descriptor = {
  1334. classes: [ 'foo-class', 'bar-class' ],
  1335. attributes: { one: '1', two: '2' },
  1336. priority: 7
  1337. };
  1338. const element = createViewElementFromHighlightDescriptor( descriptor );
  1339. expect( element.is( 'attributeElement' ) ).to.be.true;
  1340. expect( element.name ).to.equal( 'span' );
  1341. expect( element.priority ).to.equal( 7 );
  1342. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1343. expect( element.hasClass( 'bar-class' ) ).to.be.true;
  1344. for ( const key of Object.keys( descriptor.attributes ) ) {
  1345. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1346. }
  1347. } );
  1348. it( 'should create element without class', () => {
  1349. const descriptor = {
  1350. attributes: { one: '1', two: '2' },
  1351. priority: 7
  1352. };
  1353. const element = createViewElementFromHighlightDescriptor( descriptor );
  1354. expect( element.is( 'attributeElement' ) ).to.be.true;
  1355. expect( element.name ).to.equal( 'span' );
  1356. expect( element.priority ).to.equal( 7 );
  1357. for ( const key of Object.keys( descriptor.attributes ) ) {
  1358. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1359. }
  1360. } );
  1361. it( 'should create element without priority', () => {
  1362. const descriptor = {
  1363. classes: 'foo-class',
  1364. attributes: { one: '1', two: '2' }
  1365. };
  1366. const element = createViewElementFromHighlightDescriptor( descriptor );
  1367. expect( element.is( 'attributeElement' ) ).to.be.true;
  1368. expect( element.name ).to.equal( 'span' );
  1369. expect( element.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  1370. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1371. for ( const key of Object.keys( descriptor.attributes ) ) {
  1372. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1373. }
  1374. } );
  1375. it( 'should create element without attributes', () => {
  1376. const descriptor = {
  1377. classes: 'foo-class',
  1378. priority: 7
  1379. };
  1380. const element = createViewElementFromHighlightDescriptor( descriptor );
  1381. expect( element.is( 'attributeElement' ) ).to.be.true;
  1382. expect( element.name ).to.equal( 'span' );
  1383. expect( element.priority ).to.equal( 7 );
  1384. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1385. } );
  1386. } );
  1387. } );
  1388. describe( 'downcast selection converters', () => {
  1389. let dispatcher, mapper, model, view, modelDoc, modelRoot, docSelection, viewDoc, viewRoot, viewSelection, downcastHelpers;
  1390. beforeEach( () => {
  1391. model = new Model();
  1392. modelDoc = model.document;
  1393. modelRoot = modelDoc.createRoot();
  1394. docSelection = modelDoc.selection;
  1395. model.schema.extend( '$text', { allowIn: '$root' } );
  1396. view = new View();
  1397. viewDoc = view.document;
  1398. viewRoot = createViewRoot( viewDoc );
  1399. viewSelection = viewDoc.selection;
  1400. mapper = new Mapper();
  1401. mapper.bindElements( modelRoot, viewRoot );
  1402. dispatcher = new DowncastDispatcher( { mapper, viewSelection } );
  1403. dispatcher.on( 'insert:$text', insertText() );
  1404. downcastHelpers = new DowncastHelpers( dispatcher );
  1405. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  1406. downcastHelpers.markerToHighlight( { model: 'marker', view: { classes: 'marker' }, converterPriority: 1 } );
  1407. // Default selection converters.
  1408. dispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  1409. dispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  1410. dispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  1411. } );
  1412. afterEach( () => {
  1413. view.destroy();
  1414. } );
  1415. describe( 'default converters', () => {
  1416. describe( 'range selection', () => {
  1417. it( 'in same container', () => {
  1418. test(
  1419. [ 1, 4 ],
  1420. 'foobar',
  1421. 'f{oob}ar'
  1422. );
  1423. } );
  1424. it( 'in same container with unicode characters', () => {
  1425. test(
  1426. [ 2, 6 ],
  1427. 'நிலைக்கு',
  1428. 'நி{லைக்}கு'
  1429. );
  1430. } );
  1431. it( 'in same container, over attribute', () => {
  1432. test(
  1433. [ 1, 5 ],
  1434. 'fo<$text bold="true">ob</$text>ar',
  1435. 'f{o<strong>ob</strong>a}r'
  1436. );
  1437. } );
  1438. it( 'in same container, next to attribute', () => {
  1439. test(
  1440. [ 1, 2 ],
  1441. 'fo<$text bold="true">ob</$text>ar',
  1442. 'f{o}<strong>ob</strong>ar'
  1443. );
  1444. } );
  1445. it( 'in same attribute', () => {
  1446. test(
  1447. [ 2, 4 ],
  1448. 'f<$text bold="true">ooba</$text>r',
  1449. 'f<strong>o{ob}a</strong>r'
  1450. );
  1451. } );
  1452. it( 'in same attribute, selection same as attribute', () => {
  1453. test(
  1454. [ 2, 4 ],
  1455. 'fo<$text bold="true">ob</$text>ar',
  1456. 'fo{<strong>ob</strong>}ar'
  1457. );
  1458. } );
  1459. it( 'starts in text node, ends in attribute #1', () => {
  1460. test(
  1461. [ 1, 3 ],
  1462. 'fo<$text bold="true">ob</$text>ar',
  1463. 'f{o<strong>o}b</strong>ar'
  1464. );
  1465. } );
  1466. it( 'starts in text node, ends in attribute #2', () => {
  1467. test(
  1468. [ 1, 4 ],
  1469. 'fo<$text bold="true">ob</$text>ar',
  1470. 'f{o<strong>ob</strong>}ar'
  1471. );
  1472. } );
  1473. it( 'starts in attribute, ends in text node', () => {
  1474. test(
  1475. [ 3, 5 ],
  1476. 'fo<$text bold="true">ob</$text>ar',
  1477. 'fo<strong>o{b</strong>a}r'
  1478. );
  1479. } );
  1480. it( 'consumes consumable values properly', () => {
  1481. // Add callback that will fire before default ones.
  1482. // This should prevent default callback doing anything.
  1483. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  1484. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  1485. }, { priority: 'high' } );
  1486. // Similar test case as the first in this suite.
  1487. test(
  1488. [ 1, 4 ],
  1489. 'foobar',
  1490. 'foobar' // No selection in view.
  1491. );
  1492. } );
  1493. it( 'should convert backward selection', () => {
  1494. test(
  1495. [ 1, 3, 'backward' ],
  1496. 'foobar',
  1497. 'f{oo}bar'
  1498. );
  1499. expect( viewSelection.focus.offset ).to.equal( 1 );
  1500. } );
  1501. } );
  1502. describe( 'collapsed selection', () => {
  1503. let marker;
  1504. it( 'in container', () => {
  1505. test(
  1506. [ 1, 1 ],
  1507. 'foobar',
  1508. 'f{}oobar'
  1509. );
  1510. } );
  1511. it( 'in attribute', () => {
  1512. test(
  1513. [ 3, 3 ],
  1514. 'f<$text bold="true">ooba</$text>r',
  1515. 'f<strong>oo{}ba</strong>r'
  1516. );
  1517. } );
  1518. it( 'in attribute and marker', () => {
  1519. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  1520. model.change( writer => {
  1521. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1522. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  1523. writer.setSelection( modelRoot, 3 );
  1524. } );
  1525. // Remove view children manually (without firing additional conversion).
  1526. viewRoot._removeChildren( 0, viewRoot.childCount );
  1527. // Convert model to view.
  1528. view.change( writer => {
  1529. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1530. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1531. dispatcher.convertSelection( docSelection, model.markers, writer );
  1532. } );
  1533. // Stringify view and check if it is same as expected.
  1534. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal(
  1535. '<div>f<span class="marker">o<strong>o{}b</strong>a</span>r</div>'
  1536. );
  1537. } );
  1538. it( 'in attribute and marker - no attribute', () => {
  1539. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  1540. model.change( writer => {
  1541. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1542. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  1543. writer.setSelection( modelRoot, 3 );
  1544. writer.removeSelectionAttribute( 'bold' );
  1545. } );
  1546. // Remove view children manually (without firing additional conversion).
  1547. viewRoot._removeChildren( 0, viewRoot.childCount );
  1548. // Convert model to view.
  1549. view.change( writer => {
  1550. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1551. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1552. dispatcher.convertSelection( docSelection, model.markers, writer );
  1553. } );
  1554. // Stringify view and check if it is same as expected.
  1555. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1556. .to.equal( '<div>f<span class="marker">o<strong>o</strong>[]<strong>b</strong>a</span>r</div>' );
  1557. } );
  1558. it( 'in marker - using highlight descriptor creator', () => {
  1559. downcastHelpers.markerToHighlight( {
  1560. model: 'marker2',
  1561. view: data => ( { classes: data.markerName } )
  1562. } );
  1563. setModelData( model, 'foobar' );
  1564. model.change( writer => {
  1565. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1566. marker = writer.addMarker( 'marker2', { range, usingOperation: false } );
  1567. writer.setSelection( modelRoot, 3 );
  1568. } );
  1569. // Remove view children manually (without firing additional conversion).
  1570. viewRoot._removeChildren( 0, viewRoot.childCount );
  1571. // Convert model to view.
  1572. view.change( writer => {
  1573. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1574. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1575. dispatcher.convertSelection( docSelection, model.markers, writer );
  1576. } );
  1577. // Stringify view and check if it is same as expected.
  1578. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1579. .to.equal( '<div>f<span class="marker2">oo{}ba</span>r</div>' );
  1580. } );
  1581. it( 'should do nothing if creator return null', () => {
  1582. downcastHelpers.markerToHighlight( {
  1583. model: 'marker3',
  1584. view: () => null
  1585. } );
  1586. setModelData( model, 'foobar' );
  1587. model.change( writer => {
  1588. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1589. marker = writer.addMarker( 'marker3', { range, usingOperation: false } );
  1590. writer.setSelection( modelRoot, 3 );
  1591. } );
  1592. // Remove view children manually (without firing additional conversion).
  1593. viewRoot._removeChildren( 0, viewRoot.childCount );
  1594. // Convert model to view.
  1595. view.change( writer => {
  1596. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1597. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1598. dispatcher.convertSelection( docSelection, model.markers, writer );
  1599. } );
  1600. // Stringify view and check if it is same as expected.
  1601. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1602. .to.equal( '<div>foo{}bar</div>' );
  1603. } );
  1604. // #1072 - if the container has only ui elements, collapsed selection attribute should be rendered after those ui elements.
  1605. it( 'selection with attribute before ui element - no non-ui children', () => {
  1606. setModelData( model, '' );
  1607. // Add two ui elements to view.
  1608. viewRoot._appendChild( [
  1609. new ViewUIElement( 'span' ),
  1610. new ViewUIElement( 'span' )
  1611. ] );
  1612. model.change( writer => {
  1613. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 0 ] ) ) );
  1614. writer.setSelectionAttribute( 'bold', true );
  1615. } );
  1616. // Convert model to view.
  1617. view.change( writer => {
  1618. dispatcher.convertSelection( docSelection, model.markers, writer );
  1619. } );
  1620. // Stringify view and check if it is same as expected.
  1621. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1622. .to.equal( '<div><span></span><span></span><strong>[]</strong></div>' );
  1623. } );
  1624. // #1072.
  1625. it( 'selection with attribute before ui element - has non-ui children #1', () => {
  1626. setModelData( model, 'x' );
  1627. model.change( writer => {
  1628. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  1629. writer.setSelectionAttribute( 'bold', true );
  1630. } );
  1631. // Convert model to view.
  1632. view.change( writer => {
  1633. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1634. // Add ui element to view.
  1635. const uiElement = new ViewUIElement( 'span' );
  1636. viewRoot._insertChild( 1, uiElement );
  1637. dispatcher.convertSelection( docSelection, model.markers, writer );
  1638. } );
  1639. // Stringify view and check if it is same as expected.
  1640. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1641. .to.equal( '<div>x<strong>[]</strong><span></span></div>' );
  1642. } );
  1643. // #1072.
  1644. it( 'selection with attribute before ui element - has non-ui children #2', () => {
  1645. setModelData( model, '<$text bold="true">x</$text>y' );
  1646. model.change( writer => {
  1647. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  1648. writer.setSelectionAttribute( 'bold', true );
  1649. } );
  1650. // Convert model to view.
  1651. view.change( writer => {
  1652. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1653. // Add ui element to view.
  1654. const uiElement = new ViewUIElement( 'span' );
  1655. viewRoot._insertChild( 1, uiElement, writer );
  1656. dispatcher.convertSelection( docSelection, model.markers, writer );
  1657. } );
  1658. // Stringify view and check if it is same as expected.
  1659. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1660. .to.equal( '<div><strong>x{}</strong><span></span>y</div>' );
  1661. } );
  1662. it( 'consumes consumable values properly', () => {
  1663. // Add callbacks that will fire before default ones.
  1664. // This should prevent default callbacks doing anything.
  1665. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  1666. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  1667. }, { priority: 'high' } );
  1668. dispatcher.on( 'attribute:bold', ( evt, data, conversionApi ) => {
  1669. expect( conversionApi.consumable.consume( data.item, 'attribute:bold' ) ).to.be.true;
  1670. }, { priority: 'high' } );
  1671. // Similar test case as above.
  1672. test(
  1673. [ 3, 3 ],
  1674. 'f<$text bold="true">ooba</$text>r',
  1675. 'foobar' // No selection in view and no attribute.
  1676. );
  1677. } );
  1678. } );
  1679. } );
  1680. describe( 'clean-up', () => {
  1681. describe( 'convertRangeSelection', () => {
  1682. it( 'should remove all ranges before adding new range', () => {
  1683. test(
  1684. [ 0, 2 ],
  1685. 'foobar',
  1686. '{fo}obar'
  1687. );
  1688. test(
  1689. [ 3, 5 ],
  1690. 'foobar',
  1691. 'foo{ba}r'
  1692. );
  1693. expect( viewSelection.rangeCount ).to.equal( 1 );
  1694. } );
  1695. } );
  1696. describe( 'convertCollapsedSelection', () => {
  1697. it( 'should remove all ranges before adding new range', () => {
  1698. test(
  1699. [ 2, 2 ],
  1700. 'foobar',
  1701. 'fo{}obar'
  1702. );
  1703. test(
  1704. [ 3, 3 ],
  1705. 'foobar',
  1706. 'foo{}bar'
  1707. );
  1708. expect( viewSelection.rangeCount ).to.equal( 1 );
  1709. } );
  1710. } );
  1711. describe( 'clearAttributes', () => {
  1712. it( 'should remove all ranges before adding new range', () => {
  1713. test(
  1714. [ 3, 3 ],
  1715. 'foobar',
  1716. 'foo<strong>[]</strong>bar',
  1717. { bold: 'true' }
  1718. );
  1719. view.change( writer => {
  1720. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  1721. model.change( writer => {
  1722. writer.setSelection( modelRange );
  1723. } );
  1724. dispatcher.convertSelection( modelDoc.selection, model.markers, writer );
  1725. } );
  1726. expect( viewSelection.rangeCount ).to.equal( 1 );
  1727. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  1728. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  1729. } );
  1730. it( 'should do nothing if the attribute element had been already removed', () => {
  1731. test(
  1732. [ 3, 3 ],
  1733. 'foobar',
  1734. 'foo<strong>[]</strong>bar',
  1735. { bold: 'true' }
  1736. );
  1737. view.change( writer => {
  1738. // Remove <strong></strong> manually.
  1739. writer.mergeAttributes( viewSelection.getFirstPosition() );
  1740. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  1741. model.change( writer => {
  1742. writer.setSelection( modelRange );
  1743. } );
  1744. dispatcher.convertSelection( modelDoc.selection, model.markers, writer );
  1745. } );
  1746. expect( viewSelection.rangeCount ).to.equal( 1 );
  1747. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  1748. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  1749. } );
  1750. it( 'should clear fake selection', () => {
  1751. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  1752. view.change( writer => {
  1753. writer.setSelection( modelRange, { fake: true } );
  1754. dispatcher.convertSelection( docSelection, model.markers, writer );
  1755. } );
  1756. expect( viewSelection.isFake ).to.be.false;
  1757. } );
  1758. } );
  1759. } );
  1760. describe( 'table cell selection converter', () => {
  1761. beforeEach( () => {
  1762. model.schema.register( 'table', { isLimit: true } );
  1763. model.schema.register( 'tr', { isLimit: true } );
  1764. model.schema.register( 'td', { isLimit: true } );
  1765. model.schema.extend( 'table', { allowIn: '$root' } );
  1766. model.schema.extend( 'tr', { allowIn: 'table' } );
  1767. model.schema.extend( 'td', { allowIn: 'tr' } );
  1768. model.schema.extend( '$text', { allowIn: 'td' } );
  1769. const downcastHelpers = new DowncastHelpers( dispatcher );
  1770. // "Universal" converter to convert table structure.
  1771. downcastHelpers.elementToElement( { model: 'table', view: 'table' } );
  1772. downcastHelpers.elementToElement( { model: 'tr', view: 'tr' } );
  1773. downcastHelpers.elementToElement( { model: 'td', view: 'td' } );
  1774. // Special converter for table cells.
  1775. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  1776. const selection = data.selection;
  1777. if ( !conversionApi.consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  1778. return;
  1779. }
  1780. for ( const range of selection.getRanges() ) {
  1781. const node = range.start.parent;
  1782. if ( !!node && node.is( 'td' ) ) {
  1783. conversionApi.consumable.consume( selection, 'selection' );
  1784. const viewNode = conversionApi.mapper.toViewElement( node );
  1785. conversionApi.writer.addClass( 'selected', viewNode );
  1786. }
  1787. }
  1788. }, { priority: 'high' } );
  1789. } );
  1790. it( 'should not be used to convert selection that is not on table cell', () => {
  1791. test(
  1792. [ 1, 5 ],
  1793. 'f{o<$text bold="true">ob</$text>a}r',
  1794. 'f{o<strong>ob</strong>a}r'
  1795. );
  1796. } );
  1797. it( 'should add a class to the selected table cell', () => {
  1798. test(
  1799. // table tr#0 td#0 [foo, table tr#0 td#0 bar]
  1800. [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 3 ] ],
  1801. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  1802. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  1803. );
  1804. } );
  1805. it( 'should not be used if selection contains more than just a table cell', () => {
  1806. test(
  1807. // table tr td#1 f{oo bar, table tr#2 bar]
  1808. [ [ 0, 0, 0, 1 ], [ 0, 0, 1, 3 ] ],
  1809. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  1810. '[<table><tr><td>foo</td><td>bar</td></tr></table>]'
  1811. );
  1812. } );
  1813. } );
  1814. // Tests if the selection got correctly converted.
  1815. // Because `setData` might use selection converters itself to set the selection, we can't use it
  1816. // to set the selection (because then we would test converters using converters).
  1817. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  1818. // that are offsets or paths of selection positions in root element.
  1819. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  1820. // Parse passed `modelInput` string and set it as current model.
  1821. setModelData( model, modelInput );
  1822. // Manually set selection ranges using passed `selectionPaths`.
  1823. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  1824. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  1825. const startPos = model.createPositionFromPath( modelRoot, startPath );
  1826. const endPos = model.createPositionFromPath( modelRoot, endPath );
  1827. const isBackward = selectionPaths[ 2 ] === 'backward';
  1828. model.change( writer => {
  1829. writer.setSelection( writer.createRange( startPos, endPos ), { backward: isBackward } );
  1830. // And add or remove passed attributes.
  1831. for ( const key in selectionAttributes ) {
  1832. const value = selectionAttributes[ key ];
  1833. if ( value ) {
  1834. writer.setSelectionAttribute( key, value );
  1835. } else {
  1836. writer.removeSelectionAttribute( key );
  1837. }
  1838. }
  1839. } );
  1840. // Remove view children manually (without firing additional conversion).
  1841. viewRoot._removeChildren( 0, viewRoot.childCount );
  1842. // Convert model to view.
  1843. view.change( writer => {
  1844. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1845. dispatcher.convertSelection( docSelection, model.markers, writer );
  1846. } );
  1847. // Stringify view and check if it is same as expected.
  1848. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  1849. }
  1850. } );
  1851. function viewToString( item ) {
  1852. let result = '';
  1853. if ( item instanceof ViewText ) {
  1854. result = item.data;
  1855. } else {
  1856. // ViewElement or ViewDocumentFragment.
  1857. for ( const child of item.getChildren() ) {
  1858. result += viewToString( child );
  1859. }
  1860. if ( item instanceof ViewElement ) {
  1861. result = '<' + item.name + viewAttributesToString( item ) + '>' + result + '</' + item.name + '>';
  1862. }
  1863. }
  1864. return result;
  1865. }
  1866. function viewAttributesToString( item ) {
  1867. let result = '';
  1868. for ( const key of item.getAttributeKeys() ) {
  1869. const value = item.getAttribute( key );
  1870. if ( value ) {
  1871. result += ' ' + key + '="' + value + '"';
  1872. }
  1873. }
  1874. return result;
  1875. }