downcasthelpers.js 73 KB

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