downcasthelpers.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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. insertElement, wrap, createViewElementFromHighlightDescriptor
  19. } from '../../src/conversion/downcasthelpers';
  20. import { stringify } from '../../src/dev-utils/view';
  21. describe( 'DowncastHelpers', () => {
  22. let conversion, model, modelRoot, viewRoot, downcastHelpers;
  23. beforeEach( () => {
  24. model = new Model();
  25. const modelDoc = model.document;
  26. modelRoot = modelDoc.createRoot();
  27. const controller = new EditingController( model );
  28. // Set name of view root the same as dom root.
  29. // This is a mock of attaching view root to dom root.
  30. controller.view.document.getRoot()._name = 'div';
  31. viewRoot = controller.view.document.getRoot();
  32. downcastHelpers = new DowncastHelpers( controller.downcastDispatcher );
  33. conversion = new Conversion();
  34. conversion.register( 'downcast', downcastHelpers );
  35. } );
  36. describe( 'elementToElement()', () => {
  37. it( 'should be chainable', () => {
  38. expect( downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ) ).to.equal( downcastHelpers );
  39. } );
  40. it( 'config.view is a string', () => {
  41. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  42. model.change( writer => {
  43. writer.insertElement( 'paragraph', modelRoot, 0 );
  44. } );
  45. expectResult( '<p></p>' );
  46. } );
  47. it( 'can be overwritten using converterPriority', () => {
  48. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  49. downcastHelpers.elementToElement( { model: 'paragraph', view: 'foo', converterPriority: 'high' } );
  50. model.change( writer => {
  51. writer.insertElement( 'paragraph', modelRoot, 0 );
  52. } );
  53. expectResult( '<foo></foo>' );
  54. } );
  55. it( 'config.view is a view element definition', () => {
  56. downcastHelpers.elementToElement( {
  57. model: 'fancyParagraph',
  58. view: {
  59. name: 'p',
  60. classes: 'fancy'
  61. }
  62. } );
  63. model.change( writer => {
  64. writer.insertElement( 'fancyParagraph', modelRoot, 0 );
  65. } );
  66. expectResult( '<p class="fancy"></p>' );
  67. } );
  68. it( 'config.view is a function', () => {
  69. downcastHelpers.elementToElement( {
  70. model: 'heading',
  71. view: ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'h' + modelElement.getAttribute( 'level' ) )
  72. } );
  73. model.change( writer => {
  74. writer.insertElement( 'heading', { level: 2 }, modelRoot, 0 );
  75. } );
  76. expectResult( '<h2></h2>' );
  77. } );
  78. } );
  79. describe( 'attributeToElement()', () => {
  80. it( 'should be chainable', () => {
  81. expect( downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } ) ).to.equal( downcastHelpers );
  82. } );
  83. it( 'config.view is a string', () => {
  84. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  85. model.change( writer => {
  86. writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
  87. } );
  88. expectResult( '<strong>foo</strong>' );
  89. } );
  90. it( 'can be overwritten using converterPriority', () => {
  91. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  92. downcastHelpers.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
  93. model.change( writer => {
  94. writer.insertText( 'foo', { bold: true }, modelRoot, 0 );
  95. } );
  96. expectResult( '<b>foo</b>' );
  97. } );
  98. it( 'config.view is a view element definition', () => {
  99. downcastHelpers.attributeToElement( {
  100. model: 'invert',
  101. view: {
  102. name: 'span',
  103. classes: [ 'font-light', 'bg-dark' ]
  104. }
  105. } );
  106. model.change( writer => {
  107. writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
  108. } );
  109. expectResult( '<span class="bg-dark font-light">foo</span>' );
  110. expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  111. } );
  112. it( 'config.view allows specifying the element\'s priority', () => {
  113. downcastHelpers.attributeToElement( {
  114. model: 'invert',
  115. view: {
  116. name: 'span',
  117. priority: 5
  118. }
  119. } );
  120. model.change( writer => {
  121. writer.insertText( 'foo', { invert: true }, modelRoot, 0 );
  122. } );
  123. expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
  124. } );
  125. it( 'model attribute value is enum', () => {
  126. downcastHelpers.attributeToElement( {
  127. model: {
  128. key: 'fontSize',
  129. values: [ 'big', 'small' ]
  130. },
  131. view: {
  132. big: {
  133. name: 'span',
  134. styles: {
  135. 'font-size': '1.2em'
  136. }
  137. },
  138. small: {
  139. name: 'span',
  140. styles: {
  141. 'font-size': '0.8em'
  142. },
  143. priority: 5
  144. }
  145. }
  146. } );
  147. model.change( writer => {
  148. writer.insertText( 'foo', { fontSize: 'big' }, modelRoot, 0 );
  149. } );
  150. expect( viewRoot.getChild( 0 ).priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  151. expectResult( '<span style="font-size:1.2em">foo</span>' );
  152. model.change( writer => {
  153. writer.setAttribute( 'fontSize', 'small', modelRoot.getChild( 0 ) );
  154. } );
  155. expectResult( '<span style="font-size:0.8em">foo</span>' );
  156. expect( viewRoot.getChild( 0 ).priority ).to.equal( 5 );
  157. model.change( writer => {
  158. writer.removeAttribute( 'fontSize', modelRoot.getChild( 0 ) );
  159. } );
  160. expectResult( 'foo' );
  161. } );
  162. it( 'config.view is a function', () => {
  163. downcastHelpers.attributeToElement( {
  164. model: 'bold',
  165. view: ( modelAttributeValue, viewWriter ) => {
  166. return viewWriter.createAttributeElement( 'span', { style: 'font-weight:' + modelAttributeValue } );
  167. }
  168. } );
  169. model.change( writer => {
  170. writer.insertText( 'foo', { bold: '500' }, modelRoot, 0 );
  171. } );
  172. expectResult( '<span style="font-weight:500">foo</span>' );
  173. } );
  174. it( 'config.model.name is given', () => {
  175. downcastHelpers.attributeToElement( {
  176. model: {
  177. key: 'color',
  178. name: '$text'
  179. },
  180. view: ( modelAttributeValue, viewWriter ) => {
  181. return viewWriter.createAttributeElement( 'span', { style: 'color:' + modelAttributeValue } );
  182. }
  183. } );
  184. downcastHelpers.elementToElement( {
  185. model: 'smiley',
  186. view: ( modelElement, viewWriter ) => {
  187. return viewWriter.createEmptyElement( 'img', {
  188. src: 'smile.jpg',
  189. class: 'smiley'
  190. } );
  191. }
  192. } );
  193. model.change( writer => {
  194. writer.insertText( 'foo', { color: '#FF0000' }, modelRoot, 0 );
  195. writer.insertElement( 'smiley', { color: '#FF0000' }, modelRoot, 3 );
  196. } );
  197. expectResult( '<span style="color:#FF0000">foo</span><img class="smiley" src="smile.jpg"></img>' );
  198. } );
  199. } );
  200. describe( 'attributeToAttribute()', () => {
  201. testUtils.createSinonSandbox();
  202. beforeEach( () => {
  203. downcastHelpers.elementToElement( { model: 'image', view: 'img' } );
  204. } );
  205. it( 'should be chainable', () => {
  206. expect( downcastHelpers.attributeToAttribute( { model: 'source', view: 'src' } ) ).to.equal( downcastHelpers );
  207. } );
  208. it( 'config.view is a string', () => {
  209. downcastHelpers.attributeToAttribute( { model: 'source', view: 'src' } );
  210. model.change( writer => {
  211. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  212. } );
  213. expectResult( '<img src="foo.jpg"></img>' );
  214. model.change( writer => {
  215. writer.removeAttribute( 'source', modelRoot.getChild( 0 ) );
  216. } );
  217. expectResult( '<img></img>' );
  218. } );
  219. it( 'can be overwritten using converterPriority', () => {
  220. downcastHelpers.attributeToAttribute( { model: 'source', view: 'href' } );
  221. downcastHelpers.attributeToAttribute( { model: 'source', view: 'src', converterPriority: 'high' } );
  222. model.change( writer => {
  223. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  224. } );
  225. expectResult( '<img src="foo.jpg"></img>' );
  226. } );
  227. it( 'model element name specified', () => {
  228. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  229. downcastHelpers.attributeToAttribute( {
  230. model: {
  231. name: 'image',
  232. key: 'source'
  233. },
  234. view: 'src'
  235. } );
  236. model.change( writer => {
  237. writer.insertElement( 'image', { source: 'foo.jpg' }, modelRoot, 0 );
  238. } );
  239. expectResult( '<img src="foo.jpg"></img>' );
  240. model.change( writer => {
  241. writer.rename( modelRoot.getChild( 0 ), 'paragraph' );
  242. } );
  243. expectResult( '<p></p>' );
  244. } );
  245. it( 'config.view is an object, model attribute value is enum', () => {
  246. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  247. downcastHelpers.attributeToAttribute( {
  248. model: {
  249. key: 'styled',
  250. values: [ 'dark', 'light' ]
  251. },
  252. view: {
  253. dark: {
  254. key: 'class',
  255. value: [ 'styled', 'styled-dark' ]
  256. },
  257. light: {
  258. key: 'class',
  259. value: [ 'styled', 'styled-light' ]
  260. }
  261. }
  262. } );
  263. model.change( writer => {
  264. writer.insertElement( 'paragraph', { styled: 'dark' }, modelRoot, 0 );
  265. } );
  266. expectResult( '<p class="styled styled-dark"></p>' );
  267. model.change( writer => {
  268. writer.setAttribute( 'styled', 'light', modelRoot.getChild( 0 ) );
  269. } );
  270. expectResult( '<p class="styled styled-light"></p>' );
  271. model.change( writer => {
  272. writer.removeAttribute( 'styled', modelRoot.getChild( 0 ) );
  273. } );
  274. expectResult( '<p></p>' );
  275. } );
  276. it( 'config.view is an object, model attribute value is enum, view has style', () => {
  277. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  278. downcastHelpers.attributeToAttribute( {
  279. model: {
  280. key: 'align',
  281. values: [ 'right', 'center' ]
  282. },
  283. view: {
  284. right: {
  285. key: 'style',
  286. value: {
  287. 'text-align': 'right'
  288. }
  289. },
  290. center: {
  291. key: 'style',
  292. value: {
  293. 'text-align': 'center'
  294. }
  295. }
  296. }
  297. } );
  298. model.change( writer => {
  299. writer.insertElement( 'paragraph', { align: 'right' }, modelRoot, 0 );
  300. } );
  301. expectResult( '<p style="text-align:right"></p>' );
  302. model.change( writer => {
  303. writer.setAttribute( 'align', 'center', modelRoot.getChild( 0 ) );
  304. } );
  305. expectResult( '<p style="text-align:center"></p>' );
  306. model.change( writer => {
  307. writer.removeAttribute( 'align', modelRoot.getChild( 0 ) );
  308. } );
  309. expectResult( '<p></p>' );
  310. } );
  311. it( 'config.view is an object, only name and key are provided', () => {
  312. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  313. downcastHelpers.attributeToAttribute( {
  314. model: {
  315. name: 'paragraph',
  316. key: 'class'
  317. },
  318. view: {
  319. name: 'paragraph',
  320. key: 'class'
  321. }
  322. } );
  323. model.change( writer => {
  324. writer.insertElement( 'paragraph', { class: 'dark' }, modelRoot, 0 );
  325. } );
  326. expectResult( '<p class="dark"></p>' );
  327. model.change( writer => {
  328. writer.setAttribute( 'class', 'light', modelRoot.getChild( 0 ) );
  329. } );
  330. expectResult( '<p class="light"></p>' );
  331. model.change( writer => {
  332. writer.removeAttribute( 'class', modelRoot.getChild( 0 ) );
  333. } );
  334. expectResult( '<p></p>' );
  335. } );
  336. it( 'config.view is a function', () => {
  337. downcastHelpers.attributeToAttribute( {
  338. model: 'styled',
  339. view: attributeValue => ( { key: 'class', value: 'styled-' + attributeValue } )
  340. } );
  341. model.change( writer => {
  342. writer.insertElement( 'image', { styled: 'pull-out' }, modelRoot, 0 );
  343. } );
  344. expectResult( '<img class="styled-pull-out"></img>' );
  345. } );
  346. // #1587
  347. it( 'config.view and config.model as strings in generic conversion (elements only)', () => {
  348. const logSpy = testUtils.sinon.spy( log, 'warn' );
  349. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  350. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  351. model.change( writer => {
  352. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 0 );
  353. writer.insertElement( 'paragraph', { test: '2' }, modelRoot, 1 );
  354. } );
  355. expectResult( '<p test="1"></p><p test="2"></p>' );
  356. expect( logSpy.callCount ).to.equal( 0 );
  357. model.change( writer => {
  358. writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
  359. } );
  360. expectResult( '<p test="1"></p><p></p>' );
  361. } );
  362. // #1587
  363. it( 'config.view and config.model as strings in generic conversion (elements + text)', () => {
  364. const logSpy = testUtils.sinon.spy( log, 'warn' );
  365. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  366. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  367. model.change( writer => {
  368. writer.insertElement( 'paragraph', modelRoot, 0 );
  369. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 1 );
  370. writer.insertText( 'Foo', { test: '2' }, modelRoot.getChild( 0 ), 0 );
  371. writer.insertText( 'Bar', { test: '3' }, modelRoot.getChild( 1 ), 0 );
  372. } );
  373. expectResult( '<p>Foo</p><p test="1">Bar</p>' );
  374. expect( logSpy.callCount ).to.equal( 2 );
  375. expect( logSpy.alwaysCalledWithMatch( 'conversion-attribute-to-attribute-on-text' ) ).to.true;
  376. model.change( writer => {
  377. writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
  378. } );
  379. expectResult( '<p>Foo</p><p>Bar</p>' );
  380. } );
  381. } );
  382. describe( 'markerToElement()', () => {
  383. it( 'should be chainable', () => {
  384. expect( downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } ) ).to.equal( downcastHelpers );
  385. } );
  386. it( 'config.view is a string', () => {
  387. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  388. model.change( writer => {
  389. writer.insertText( 'foo', modelRoot, 0 );
  390. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  391. writer.addMarker( 'search', { range, usingOperation: false } );
  392. } );
  393. expectResult( 'f<marker-search></marker-search>o<marker-search></marker-search>o' );
  394. } );
  395. it( 'can be overwritten using converterPriority', () => {
  396. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  397. downcastHelpers.markerToElement( { model: 'search', view: 'search', converterPriority: 'high' } );
  398. model.change( writer => {
  399. writer.insertText( 'foo', modelRoot, 0 );
  400. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  401. writer.addMarker( 'search', { range, usingOperation: false } );
  402. } );
  403. expectResult( 'f<search></search>o<search></search>o' );
  404. } );
  405. it( 'config.view is a view element definition', () => {
  406. downcastHelpers.markerToElement( {
  407. model: 'search',
  408. view: {
  409. name: 'span',
  410. attributes: {
  411. 'data-marker': 'search'
  412. }
  413. }
  414. } );
  415. model.change( writer => {
  416. writer.insertText( 'foo', modelRoot, 0 );
  417. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  418. writer.addMarker( 'search', { range, usingOperation: false } );
  419. } );
  420. expectResult( 'f<span data-marker="search"></span>o<span data-marker="search"></span>o' );
  421. } );
  422. it( 'config.view is a function', () => {
  423. downcastHelpers.markerToElement( {
  424. model: 'search',
  425. view: ( data, viewWriter ) => {
  426. return viewWriter.createUIElement( 'span', { 'data-marker': 'search', 'data-start': data.isOpening } );
  427. }
  428. } );
  429. model.change( writer => {
  430. writer.insertText( 'foo', modelRoot, 0 );
  431. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  432. writer.addMarker( 'search', { range, usingOperation: false } );
  433. } );
  434. expectResult( 'f<span data-marker="search" data-start="true"></span>o<span data-marker="search" data-start="false"></span>o' );
  435. } );
  436. } );
  437. describe( 'markerToHighlight()', () => {
  438. it( 'should be chainable', () => {
  439. expect( downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } ) ).to.equal( downcastHelpers );
  440. } );
  441. it( 'config.view is a highlight descriptor', () => {
  442. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  443. model.change( writer => {
  444. writer.insertText( 'foo', modelRoot, 0 );
  445. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  446. writer.addMarker( 'comment', { range, usingOperation: false } );
  447. } );
  448. expectResult( '<span class="comment">foo</span>' );
  449. } );
  450. it( 'can be overwritten using converterPriority', () => {
  451. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  452. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'new-comment' }, converterPriority: 'high' } );
  453. model.change( writer => {
  454. writer.insertText( 'foo', modelRoot, 0 );
  455. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  456. writer.addMarker( 'comment', { range, usingOperation: false } );
  457. } );
  458. expectResult( '<span class="new-comment">foo</span>' );
  459. } );
  460. it( 'config.view is a function', () => {
  461. downcastHelpers.markerToHighlight( {
  462. model: 'comment',
  463. view: data => {
  464. const commentType = data.markerName.split( ':' )[ 1 ];
  465. return {
  466. classes: [ 'comment', 'comment-' + commentType ]
  467. };
  468. }
  469. } );
  470. model.change( writer => {
  471. writer.insertText( 'foo', modelRoot, 0 );
  472. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  473. writer.addMarker( 'comment:abc', { range, usingOperation: false } );
  474. } );
  475. expectResult( '<span class="comment comment-abc">foo</span>' );
  476. } );
  477. } );
  478. function expectResult( string ) {
  479. expect( stringify( viewRoot, null, { ignoreRoot: true } ) ).to.equal( string );
  480. }
  481. } );
  482. describe( 'downcast-converters', () => {
  483. let dispatcher, modelDoc, modelRoot, viewRoot, controller, modelRootStart, model;
  484. beforeEach( () => {
  485. model = new Model();
  486. modelDoc = model.document;
  487. modelRoot = modelDoc.createRoot();
  488. controller = new EditingController( model );
  489. viewRoot = controller.view.document.getRoot();
  490. // Set name of view root the same as dom root.
  491. // This is a mock of attaching view root to dom root.
  492. controller.view.document.getRoot()._name = 'div';
  493. dispatcher = controller.downcastDispatcher;
  494. dispatcher.on(
  495. 'insert:paragraph',
  496. insertElement(
  497. ( modelItem, viewWriter ) => viewWriter.createContainerElement( 'p' )
  498. )
  499. );
  500. // dispatcher.on( 'attribute:class', changeAttribute() );
  501. modelRootStart = model.createPositionAt( modelRoot, 0 );
  502. } );
  503. function viewAttributesToString( item ) {
  504. let result = '';
  505. for ( const key of item.getAttributeKeys() ) {
  506. const value = item.getAttribute( key );
  507. if ( value ) {
  508. result += ' ' + key + '="' + value + '"';
  509. }
  510. }
  511. return result;
  512. }
  513. function viewToString( item ) {
  514. let result = '';
  515. if ( item instanceof ViewText ) {
  516. result = item.data;
  517. } else {
  518. // ViewElement or ViewDocumentFragment.
  519. for ( const child of item.getChildren() ) {
  520. result += viewToString( child );
  521. }
  522. if ( item instanceof ViewElement ) {
  523. result = '<' + item.name + viewAttributesToString( item ) + '>' + result + '</' + item.name + '>';
  524. }
  525. }
  526. return result;
  527. }
  528. describe( 'insertText', () => {
  529. it( 'should downcast text', () => {
  530. model.change( writer => {
  531. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  532. } );
  533. expect( viewToString( viewRoot ) ).to.equal( '<div>foobar</div>' );
  534. } );
  535. it( 'should support unicode', () => {
  536. model.change( writer => {
  537. writer.insert( new ModelText( 'நிலைக்கு' ), modelRootStart );
  538. } );
  539. expect( viewToString( viewRoot ) ).to.equal( '<div>நிலைக்கு</div>' );
  540. } );
  541. it( 'should be possible to override it', () => {
  542. dispatcher.on( 'insert:$text', ( evt, data, conversionApi ) => {
  543. conversionApi.consumable.consume( data.item, 'insert' );
  544. }, { converterPriority: 'high' } );
  545. model.change( writer => {
  546. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  547. } );
  548. expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
  549. } );
  550. } );
  551. describe( 'insertElement', () => {
  552. it( 'should convert element insertion in model', () => {
  553. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  554. model.change( writer => {
  555. writer.insert( modelElement, modelRootStart );
  556. } );
  557. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  558. } );
  559. it( 'should not convert if creator returned null', () => {
  560. dispatcher.on( 'insert:div', insertElement( () => null ) );
  561. const modelElement = new ModelElement( 'div' );
  562. model.change( writer => {
  563. writer.insert( modelElement, modelRootStart );
  564. } );
  565. expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
  566. } );
  567. } );
  568. //
  569. // describe( 'changeAttribute', () => {
  570. // it( 'should convert attribute insert/change/remove on a model node', () => {
  571. // const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  572. //
  573. // model.change( writer => {
  574. // writer.insert( modelElement, modelRootStart );
  575. // } );
  576. //
  577. // expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  578. //
  579. // model.change( writer => {
  580. // writer.setAttribute( 'class', 'bar', modelElement );
  581. // } );
  582. //
  583. // expect( viewToString( viewRoot ) ).to.equal( '<div><p class="bar">foobar</p></div>' );
  584. //
  585. // model.change( writer => {
  586. // writer.removeAttribute( 'class', modelElement );
  587. // } );
  588. //
  589. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  590. // } );
  591. //
  592. // it( 'should convert insert/change/remove with attribute generating function as a parameter', () => {
  593. // const themeConverter = ( value, data ) => {
  594. // if ( data.item instanceof ModelElement && data.item.childCount > 0 ) {
  595. // value += ' fix-content';
  596. // }
  597. //
  598. // return { key: 'class', value };
  599. // };
  600. //
  601. // dispatcher.on( 'insert:div', insertElement( ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'div' ) ) );
  602. // dispatcher.on( 'attribute:theme', changeAttribute( themeConverter ) );
  603. //
  604. // const modelParagraph = new ModelElement( 'paragraph', { theme: 'nice' }, new ModelText( 'foobar' ) );
  605. // const modelDiv = new ModelElement( 'div', { theme: 'nice' } );
  606. //
  607. // model.change( writer => {
  608. // writer.insert( [ modelParagraph, modelDiv ], modelRootStart );
  609. // } );
  610. //
  611. // expect( viewToString( viewRoot ) ).to.equal( '<div><p class="nice fix-content">foobar</p><div class="nice"></div></div>' );
  612. //
  613. // model.change( writer => {
  614. // writer.setAttribute( 'theme', 'awesome', modelParagraph );
  615. // } );
  616. //
  617. // expect( viewToString( viewRoot ) ).to.equal( '<div><p class="awesome fix-content">foobar</p><div class="nice"></div></div>' );
  618. //
  619. // model.change( writer => {
  620. // writer.removeAttribute( 'theme', modelParagraph );
  621. // } );
  622. //
  623. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p><div class="nice"></div></div>' );
  624. // } );
  625. //
  626. // it( 'should be possible to override setAttribute', () => {
  627. // const modelElement = new ModelElement( 'paragraph', { classes: 'foo' }, new ModelText( 'foobar' ) );
  628. //
  629. // dispatcher.on( 'attribute:class', ( evt, data, conversionApi ) => {
  630. // conversionApi.consumable.consume( data.item, 'attribute:class' );
  631. // }, { converterPriority: 'high' } );
  632. //
  633. // model.change( writer => {
  634. // writer.insert( modelElement, modelRootStart );
  635. // } );
  636. //
  637. // // No attribute set.
  638. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  639. // } );
  640. //
  641. // it( 'should not convert or consume if element creator returned null', () => {
  642. // const callback = sinon.stub().returns( null );
  643. //
  644. // dispatcher.on( 'attribute:class', changeAttribute( callback ) );
  645. //
  646. // const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  647. //
  648. // model.change( writer => {
  649. // writer.insert( modelElement, modelRootStart );
  650. // } );
  651. //
  652. // expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  653. //
  654. // sinon.assert.called( callback );
  655. // } );
  656. // } );
  657. describe( 'wrap', () => {
  658. it( 'should convert insert/change/remove of attribute in model into wrapping element in a view', () => {
  659. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
  660. const creator = ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' );
  661. dispatcher.on( 'attribute:bold', wrap( creator ) );
  662. model.change( writer => {
  663. writer.insert( modelElement, modelRootStart );
  664. } );
  665. expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
  666. model.change( writer => {
  667. writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
  668. } );
  669. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  670. } );
  671. it( 'should convert insert/remove of attribute in model with wrapping element generating function as a parameter', () => {
  672. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { style: 'bold' } ) );
  673. const elementGenerator = ( modelAttributeValue, viewWriter ) => {
  674. if ( modelAttributeValue == 'bold' ) {
  675. return viewWriter.createAttributeElement( 'b' );
  676. }
  677. };
  678. dispatcher.on( 'attribute:style', wrap( elementGenerator ) );
  679. model.change( writer => {
  680. writer.insert( modelElement, modelRootStart );
  681. } );
  682. expect( viewToString( viewRoot ) ).to.equal( '<div><p><b>foobar</b></p></div>' );
  683. model.change( writer => {
  684. writer.removeAttribute( 'style', writer.createRangeIn( modelElement ) );
  685. } );
  686. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  687. } );
  688. it( 'should update range on re-wrapping attribute (#475)', () => {
  689. const modelElement = new ModelElement( 'paragraph', null, [
  690. new ModelText( 'x' ),
  691. new ModelText( 'foo', { link: 'http://foo.com' } ),
  692. new ModelText( 'x' )
  693. ] );
  694. const elementGenerator = ( modelAttributeValue, viewWriter ) => {
  695. return viewWriter.createAttributeElement( 'a', { href: modelAttributeValue } );
  696. };
  697. dispatcher.on( 'attribute:link', wrap( elementGenerator ) );
  698. model.change( writer => {
  699. writer.insert( modelElement, modelRootStart );
  700. } );
  701. expect( viewToString( viewRoot ) ).to.equal( '<div><p>x<a href="http://foo.com">foo</a>x</p></div>' );
  702. // Set new attribute on old link but also on non-linked characters.
  703. model.change( writer => {
  704. writer.setAttribute( 'link', 'http://foobar.com', writer.createRangeIn( modelElement ) );
  705. } );
  706. expect( viewToString( viewRoot ) ).to.equal( '<div><p><a href="http://foobar.com">xfoox</a></p></div>' );
  707. } );
  708. it( 'should support unicode', () => {
  709. const modelElement = new ModelElement( 'paragraph', null, [ 'நி', new ModelText( 'லைக்', { bold: true } ), 'கு' ] );
  710. const creator = ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' );
  711. dispatcher.on( 'attribute:bold', wrap( creator ) );
  712. model.change( writer => {
  713. writer.insert( modelElement, modelRootStart );
  714. } );
  715. expect( viewToString( viewRoot ) ).to.equal( '<div><p>நி<b>லைக்</b>கு</p></div>' );
  716. model.change( writer => {
  717. writer.removeAttribute( 'bold', writer.createRangeIn( modelElement ) );
  718. } );
  719. expect( viewToString( viewRoot ) ).to.equal( '<div><p>நிலைக்கு</p></div>' );
  720. } );
  721. it( 'should be possible to override wrap', () => {
  722. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { bold: true } ) );
  723. dispatcher.on( 'attribute:bold', wrap( ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'b' ) ) );
  724. dispatcher.on(
  725. 'attribute:bold',
  726. wrap( ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'strong' ) ),
  727. { priority: 'high' }
  728. );
  729. model.change( writer => {
  730. writer.insert( modelElement, modelRootStart );
  731. } );
  732. expect( viewToString( viewRoot ) ).to.equal( '<div><p><strong>foobar</strong></p></div>' );
  733. } );
  734. it( 'should not convert and not consume if creator function returned null', () => {
  735. const elementGenerator = () => null;
  736. sinon.spy( dispatcher, 'fire' );
  737. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar', { italic: true } ) );
  738. dispatcher.on( 'attribute:italic', wrap( elementGenerator ) );
  739. const spy = sinon.spy();
  740. dispatcher.on( 'attribute:italic', spy );
  741. model.change( writer => {
  742. writer.insert( modelElement, modelRootStart );
  743. } );
  744. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  745. expect( dispatcher.fire.calledWith( 'attribute:italic:$text' ) ).to.be.true;
  746. expect( spy.called ).to.be.true;
  747. } );
  748. } );
  749. // describe( 'insertUIElement/removeUIElement', () => {
  750. // let modelText, modelElement, range;
  751. //
  752. // beforeEach( () => {
  753. // modelText = new ModelText( 'foobar' );
  754. // modelElement = new ModelElement( 'paragraph', null, modelText );
  755. //
  756. // model.change( writer => {
  757. // writer.insert( modelElement, modelRootStart );
  758. // } );
  759. // } );
  760. //
  761. // describe( 'collapsed range', () => {
  762. // beforeEach( () => {
  763. // range = model.createRange( model.createPositionAt( modelElement, 3 ), model.createPositionAt( modelElement, 3 ) );
  764. // } );
  765. //
  766. // it( 'should insert and remove ui element', () => {
  767. // const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } );
  768. //
  769. // dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
  770. // dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
  771. //
  772. // model.change( writer => {
  773. // writer.addMarker( 'marker', { range, usingOperation: false } );
  774. // } );
  775. //
  776. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo<span class="marker"></span>bar</p></div>' );
  777. //
  778. // model.change( writer => {
  779. // writer.removeMarker( 'marker' );
  780. // } );
  781. //
  782. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  783. // } );
  784. //
  785. // it( 'should not convert if consumable was consumed', () => {
  786. // sinon.spy( dispatcher, 'fire' );
  787. //
  788. // dispatcher.on( 'addMarker:marker', insertUIElement(
  789. // ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } ) )
  790. // );
  791. //
  792. // dispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  793. // conversionApi.consumable.consume( data.markerRange, 'addMarker:marker' );
  794. // }, { priority: 'high' } );
  795. //
  796. // model.change( writer => {
  797. // writer.addMarker( 'marker', { range, usingOperation: false } );
  798. // } );
  799. //
  800. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  801. // expect( dispatcher.fire.calledWith( 'addMarker:marker' ) );
  802. // } );
  803. //
  804. // it( 'should not convert if creator returned null', () => {
  805. // dispatcher.on( 'addMarker:marker', insertUIElement( () => null ) );
  806. // dispatcher.on( 'removeMarker:marker', removeUIElement( () => null ) );
  807. //
  808. // model.change( writer => {
  809. // writer.addMarker( 'marker', { range, usingOperation: false } );
  810. // } );
  811. //
  812. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  813. //
  814. // model.change( writer => {
  815. // writer.removeMarker( 'marker' );
  816. // } );
  817. //
  818. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  819. // } );
  820. // } );
  821. //
  822. // describe( 'non-collapsed range', () => {
  823. // beforeEach( () => {
  824. // range = model.createRange( model.createPositionAt( modelElement, 2 ), model.createPositionAt( modelElement, 5 ) );
  825. // } );
  826. //
  827. // it( 'should insert and remove ui element - element as a creator', () => {
  828. // const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } );
  829. //
  830. // dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
  831. // dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
  832. //
  833. // model.change( writer => {
  834. // writer.addMarker( 'marker', { range, usingOperation: false } );
  835. // } );
  836. //
  837. // expect( viewToString( viewRoot ) )
  838. // .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  839. //
  840. // model.change( writer => {
  841. // writer.removeMarker( 'marker' );
  842. // } );
  843. //
  844. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  845. // } );
  846. //
  847. // it( 'should insert and remove ui element - function as a creator', () => {
  848. // const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': data.markerName } );
  849. //
  850. // dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
  851. // dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
  852. //
  853. // model.change( writer => {
  854. // writer.addMarker( 'marker', { range, usingOperation: false } );
  855. // } );
  856. //
  857. // expect( viewToString( viewRoot ) )
  858. // .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  859. //
  860. // model.change( writer => {
  861. // writer.removeMarker( 'marker' );
  862. // } );
  863. //
  864. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  865. // } );
  866. //
  867. // it( 'should insert and remove different opening and ending element', () => {
  868. // function creator( data, viewWriter ) {
  869. // if ( data.isOpening ) {
  870. // return viewWriter.createUIElement( 'span', { 'class': data.markerName, 'data-start': true } );
  871. // }
  872. //
  873. // return viewWriter.createUIElement( 'span', { 'class': data.markerName, 'data-end': true } );
  874. // }
  875. //
  876. // dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
  877. // dispatcher.on( 'removeMarker:marker', removeUIElement( creator ) );
  878. //
  879. // model.change( writer => {
  880. // writer.addMarker( 'marker', { range, usingOperation: false } );
  881. // } );
  882. //
  883. // expect( viewToString( viewRoot ) ).to.equal(
  884. // '<div><p>fo<span class="marker" data-start="true"></span>oba<span class="marker" data-end="true"></span>r</p></div>'
  885. // );
  886. //
  887. // model.change( writer => {
  888. // writer.removeMarker( 'marker' );
  889. // } );
  890. //
  891. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  892. // } );
  893. //
  894. // it( 'should not convert if consumable was consumed', () => {
  895. // const creator = ( data, viewWriter ) => viewWriter.createUIElement( 'span', { 'class': 'marker' } );
  896. //
  897. // sinon.spy( dispatcher, 'fire' );
  898. //
  899. // dispatcher.on( 'addMarker:marker', insertUIElement( creator ) );
  900. // dispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  901. // conversionApi.consumable.consume( data.item, 'addMarker:marker' );
  902. // }, { priority: 'high' } );
  903. //
  904. // model.change( writer => {
  905. // writer.addMarker( 'marker', { range, usingOperation: false } );
  906. // } );
  907. //
  908. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  909. // expect( dispatcher.fire.calledWith( 'addMarker:marker' ) );
  910. // } );
  911. // } );
  912. // } );
  913. // Remove converter is by default already added in `EditingController` instance.
  914. describe( 'remove', () => {
  915. it( 'should remove items from view accordingly to changes in model #1', () => {
  916. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  917. model.change( writer => {
  918. writer.insert( modelElement, modelRootStart );
  919. } );
  920. model.change( writer => {
  921. writer.remove(
  922. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  923. );
  924. } );
  925. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foar</p></div>' );
  926. } );
  927. it( 'should be possible to overwrite', () => {
  928. dispatcher.on( 'remove', evt => evt.stop(), { priority: 'high' } );
  929. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  930. model.change( writer => {
  931. writer.insert( modelElement, modelRootStart );
  932. } );
  933. model.change( writer => {
  934. writer.remove(
  935. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  936. );
  937. } );
  938. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  939. } );
  940. it( 'should support unicode', () => {
  941. const modelElement = new ModelElement( 'paragraph', null, 'நிலைக்கு' );
  942. model.change( writer => {
  943. writer.insert( modelElement, modelRootStart );
  944. } );
  945. model.change( writer => {
  946. writer.remove(
  947. writer.createRange( writer.createPositionAt( modelElement, 0 ), writer.createPositionAt( modelElement, 6 ) )
  948. );
  949. } );
  950. expect( viewToString( viewRoot ) ).to.equal( '<div><p>கு</p></div>' );
  951. } );
  952. it( 'should not remove view ui elements that are placed next to removed content', () => {
  953. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  954. viewRoot._appendChild( [
  955. new ViewText( 'foz' ),
  956. new ViewUIElement( 'span' ),
  957. new ViewText( 'bar' )
  958. ] );
  959. // Remove 'b'.
  960. model.change( writer => {
  961. writer.remove(
  962. writer.createRange( writer.createPositionAt( modelRoot, 3 ), writer.createPositionAt( modelRoot, 4 ) )
  963. );
  964. } );
  965. expect( viewToString( viewRoot ) ).to.equal( '<div>foz<span></span>ar</div>' );
  966. // Remove 'z'.
  967. model.change( writer => {
  968. writer.remove(
  969. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 3 ) )
  970. );
  971. } );
  972. expect( viewToString( viewRoot ) ).to.equal( '<div>fo<span></span>ar</div>' );
  973. } );
  974. it( 'should remove correct amount of text when it is split by view ui element', () => {
  975. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  976. viewRoot._appendChild( [
  977. new ViewText( 'foz' ),
  978. new ViewUIElement( 'span' ),
  979. new ViewText( 'bar' )
  980. ] );
  981. // Remove 'z<span></span>b'.
  982. model.change( writer => {
  983. writer.remove(
  984. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 4 ) )
  985. );
  986. } );
  987. expect( viewToString( viewRoot ) ).to.equal( '<div>foar</div>' );
  988. } );
  989. it( 'should unbind elements', () => {
  990. const modelElement = new ModelElement( 'paragraph' );
  991. model.change( writer => {
  992. writer.insert( modelElement, modelRootStart );
  993. } );
  994. const viewElement = controller.mapper.toViewElement( modelElement );
  995. expect( viewElement ).not.to.be.undefined;
  996. expect( controller.mapper.toModelElement( viewElement ) ).to.equal( modelElement );
  997. model.change( writer => {
  998. writer.remove( modelElement );
  999. } );
  1000. expect( controller.mapper.toViewElement( modelElement ) ).to.be.undefined;
  1001. expect( controller.mapper.toModelElement( viewElement ) ).to.be.undefined;
  1002. } );
  1003. it( 'should not break when remove() is used as part of unwrapping', () => {
  1004. const modelP = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  1005. const modelWidget = new ModelElement( 'widget', null, modelP );
  1006. dispatcher.on( 'insert:widget', insertElement(
  1007. ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'widget' ) )
  1008. );
  1009. model.change( writer => {
  1010. writer.insert( modelWidget, modelRootStart );
  1011. } );
  1012. expect( viewToString( viewRoot ) ).to.equal( '<div><widget><p>foo</p></widget></div>' );
  1013. const viewP = controller.mapper.toViewElement( modelP );
  1014. expect( viewP ).not.to.be.undefined;
  1015. model.change( writer => {
  1016. writer.unwrap( modelWidget );
  1017. } );
  1018. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p></div>' );
  1019. // `modelP` is now bound with newly created view element.
  1020. expect( controller.mapper.toViewElement( modelP ) ).not.to.equal( viewP );
  1021. // `viewP` is no longer bound with model element.
  1022. expect( controller.mapper.toModelElement( viewP ) ).to.be.undefined;
  1023. // View element from view root is bound to `modelP`.
  1024. expect( controller.mapper.toModelElement( viewRoot.getChild( 0 ) ) ).to.equal( modelP );
  1025. } );
  1026. it( 'should work correctly if container element after ui element is removed', () => {
  1027. // Prepare a model and view structure.
  1028. // This is done outside of conversion to put view ui elements inside easily.
  1029. const modelP1 = new ModelElement( 'paragraph' );
  1030. const modelP2 = new ModelElement( 'paragraph' );
  1031. const viewP1 = new ViewContainerElement( 'p' );
  1032. const viewUi1 = new ViewUIElement( 'span' );
  1033. const viewUi2 = new ViewUIElement( 'span' );
  1034. const viewP2 = new ViewContainerElement( 'p' );
  1035. modelRoot._appendChild( [ modelP1, modelP2 ] );
  1036. viewRoot._appendChild( [ viewP1, viewUi1, viewUi2, viewP2 ] );
  1037. controller.mapper.bindElements( modelP1, viewP1 );
  1038. controller.mapper.bindElements( modelP2, viewP2 );
  1039. // Remove second paragraph element.
  1040. model.change( writer => {
  1041. writer.remove( writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) ) );
  1042. } );
  1043. expect( viewToString( viewRoot ) ).to.equal( '<div><p></p><span></span><span></span></div>' );
  1044. } );
  1045. it( 'should work correctly if container element after text node is removed', () => {
  1046. const modelText = new ModelText( 'foo' );
  1047. const modelP = new ModelElement( 'paragraph' );
  1048. model.change( writer => {
  1049. writer.insert( [ modelText, modelP ], modelRootStart );
  1050. } );
  1051. model.change( writer => {
  1052. writer.remove( modelP );
  1053. } );
  1054. expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
  1055. } );
  1056. } );
  1057. // describe( 'highlight', () => {
  1058. // describe( 'on text', () => {
  1059. // const highlightDescriptor = {
  1060. // classes: 'highlight-class',
  1061. // priority: 7,
  1062. // attributes: { title: 'title' }
  1063. // };
  1064. //
  1065. // let markerRange;
  1066. //
  1067. // beforeEach( () => {
  1068. // const modelElement1 = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  1069. // const modelElement2 = new ModelElement( 'paragraph', null, new ModelText( 'bar' ) );
  1070. //
  1071. // model.change( writer => {
  1072. // writer.insert( [ modelElement1, modelElement2 ], modelRootStart );
  1073. // } );
  1074. //
  1075. // markerRange = model.createRangeIn( modelRoot );
  1076. // } );
  1077. //
  1078. // it( 'should wrap and unwrap text nodes', () => {
  1079. // dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  1080. // dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
  1081. // dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
  1082. //
  1083. // model.change( writer => {
  1084. // writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1085. // } );
  1086. //
  1087. // expect( viewToString( viewRoot ) ).to.equal(
  1088. // '<div>' +
  1089. // '<p>' +
  1090. // '<span class="highlight-class" title="title">foo</span>' +
  1091. // '</p>' +
  1092. // '<p>' +
  1093. // '<span class="highlight-class" title="title">bar</span>' +
  1094. // '</p>' +
  1095. // '</div>'
  1096. // );
  1097. //
  1098. // model.change( writer => {
  1099. // writer.removeMarker( 'marker' );
  1100. // } );
  1101. //
  1102. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1103. // } );
  1104. //
  1105. // it( 'should be possible to overwrite', () => {
  1106. // dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  1107. // dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
  1108. // dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
  1109. //
  1110. // const newDescriptor = { classes: 'override-class' };
  1111. //
  1112. // dispatcher.on( 'addMarker:marker', highlightText( newDescriptor ), { priority: 'high' } );
  1113. // dispatcher.on( 'addMarker:marker', highlightElement( newDescriptor ), { priority: 'high' } );
  1114. // dispatcher.on( 'removeMarker:marker', removeHighlight( newDescriptor ), { priority: 'high' } );
  1115. //
  1116. // model.change( writer => {
  1117. // writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1118. // } );
  1119. //
  1120. // expect( viewToString( viewRoot ) ).to.equal(
  1121. // '<div>' +
  1122. // '<p>' +
  1123. // '<span class="override-class">foo</span>' +
  1124. // '</p>' +
  1125. // '<p>' +
  1126. // '<span class="override-class">bar</span>' +
  1127. // '</p>' +
  1128. // '</div>'
  1129. // );
  1130. //
  1131. // model.change( writer => {
  1132. // writer.removeMarker( 'marker' );
  1133. // } );
  1134. //
  1135. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1136. // } );
  1137. //
  1138. // it( 'should do nothing if descriptor is not provided or generating function returns null', () => {
  1139. // dispatcher.on( 'addMarker:marker', highlightText( () => null ), { priority: 'high' } );
  1140. // dispatcher.on( 'addMarker:marker', highlightElement( () => null ), { priority: 'high' } );
  1141. // dispatcher.on( 'removeMarker:marker', removeHighlight( () => null ), { priority: 'high' } );
  1142. //
  1143. // model.change( writer => {
  1144. // writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1145. // } );
  1146. //
  1147. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1148. //
  1149. // model.change( writer => {
  1150. // writer.removeMarker( 'marker' );
  1151. // } );
  1152. //
  1153. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1154. // } );
  1155. //
  1156. // it( 'should do nothing if collapsed marker is converted', () => {
  1157. // const descriptor = { classes: 'foo' };
  1158. //
  1159. // dispatcher.on( 'addMarker:marker', highlightText( descriptor ), { priority: 'high' } );
  1160. // dispatcher.on( 'addMarker:marker', highlightElement( descriptor ), { priority: 'high' } );
  1161. // dispatcher.on( 'removeMarker:marker', removeHighlight( descriptor ), { priority: 'high' } );
  1162. //
  1163. // markerRange = model.createRange( model.createPositionAt( modelRoot, 0 ), model.createPositionAt( modelRoot, 0 ) );
  1164. //
  1165. // model.change( writer => {
  1166. // writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1167. // } );
  1168. //
  1169. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1170. //
  1171. // model.change( () => {
  1172. // model.markers._remove( 'marker' );
  1173. // } );
  1174. //
  1175. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1176. // } );
  1177. //
  1178. // it( 'should correctly wrap and unwrap multiple, intersecting markers', () => {
  1179. // const descriptorFoo = { classes: 'foo' };
  1180. // const descriptorBar = { classes: 'bar' };
  1181. // const descriptorXyz = { classes: 'xyz' };
  1182. //
  1183. // dispatcher.on( 'addMarker:markerFoo', highlightText( descriptorFoo ) );
  1184. // dispatcher.on( 'addMarker:markerBar', highlightText( descriptorBar ) );
  1185. // dispatcher.on( 'addMarker:markerXyz', highlightText( descriptorXyz ) );
  1186. //
  1187. // dispatcher.on( 'removeMarker:markerFoo', removeHighlight( descriptorFoo ) );
  1188. // dispatcher.on( 'removeMarker:markerBar', removeHighlight( descriptorBar ) );
  1189. // dispatcher.on( 'removeMarker:markerXyz', removeHighlight( descriptorXyz ) );
  1190. //
  1191. // const p1 = modelRoot.getChild( 0 );
  1192. // const p2 = modelRoot.getChild( 1 );
  1193. //
  1194. // model.change( writer => {
  1195. // const range = writer.createRange( writer.createPositionAt( p1, 0 ), writer.createPositionAt( p1, 3 ) );
  1196. // writer.addMarker( 'markerFoo', { range, usingOperation: false } );
  1197. // } );
  1198. //
  1199. // expect( viewToString( viewRoot ) ).to.equal(
  1200. // '<div>' +
  1201. // '<p>' +
  1202. // '<span class="foo">foo</span>' +
  1203. // '</p>' +
  1204. // '<p>bar</p>' +
  1205. // '</div>'
  1206. // );
  1207. //
  1208. // model.change( writer => {
  1209. // const range = writer.createRange( writer.createPositionAt( p1, 1 ), writer.createPositionAt( p2, 2 ) );
  1210. // writer.addMarker( 'markerBar', { range, usingOperation: false } );
  1211. // } );
  1212. //
  1213. // expect( viewToString( viewRoot ) ).to.equal(
  1214. // '<div>' +
  1215. // '<p>' +
  1216. // '<span class="foo">f</span>' +
  1217. // '<span class="bar">' +
  1218. // '<span class="foo">oo</span>' +
  1219. // '</span>' +
  1220. // '</p>' +
  1221. // '<p>' +
  1222. // '<span class="bar">ba</span>' +
  1223. // 'r' +
  1224. // '</p>' +
  1225. // '</div>'
  1226. // );
  1227. //
  1228. // model.change( writer => {
  1229. // const range = writer.createRange( writer.createPositionAt( p1, 2 ), writer.createPositionAt( p2, 3 ) );
  1230. // writer.addMarker( 'markerXyz', { range, usingOperation: false } );
  1231. // } );
  1232. //
  1233. // expect( viewToString( viewRoot ) ).to.equal(
  1234. // '<div>' +
  1235. // '<p>' +
  1236. // '<span class="foo">f</span>' +
  1237. // '<span class="bar">' +
  1238. // '<span class="foo">' +
  1239. // 'o' +
  1240. // '<span class="xyz">o</span>' +
  1241. // '</span>' +
  1242. // '</span>' +
  1243. // '</p>' +
  1244. // '<p>' +
  1245. // '<span class="bar">' +
  1246. // '<span class="xyz">ba</span>' +
  1247. // '</span>' +
  1248. // '<span class="xyz">r</span>' +
  1249. // '</p>' +
  1250. // '</div>'
  1251. // );
  1252. //
  1253. // model.change( writer => {
  1254. // writer.removeMarker( 'markerBar' );
  1255. // } );
  1256. //
  1257. // expect( viewToString( viewRoot ) ).to.equal(
  1258. // '<div>' +
  1259. // '<p>' +
  1260. // '<span class="foo">' +
  1261. // 'fo' +
  1262. // '<span class="xyz">o</span>' +
  1263. // '</span>' +
  1264. // '</p>' +
  1265. // '<p>' +
  1266. // '<span class="xyz">bar</span>' +
  1267. // '</p>' +
  1268. // '</div>'
  1269. // );
  1270. //
  1271. // model.change( writer => {
  1272. // writer.removeMarker( 'markerFoo' );
  1273. // } );
  1274. //
  1275. // expect( viewToString( viewRoot ) ).to.equal(
  1276. // '<div>' +
  1277. // '<p>' +
  1278. // 'fo' +
  1279. // '<span class="xyz">o</span>' +
  1280. // '</p>' +
  1281. // '<p>' +
  1282. // '<span class="xyz">bar</span>' +
  1283. // '</p>' +
  1284. // '</div>'
  1285. // );
  1286. //
  1287. // model.change( writer => {
  1288. // writer.removeMarker( 'markerXyz' );
  1289. // } );
  1290. //
  1291. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1292. // } );
  1293. //
  1294. // it( 'should do nothing if marker is applied and removed on empty-ish range', () => {
  1295. // dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  1296. // dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
  1297. //
  1298. // const p1 = modelRoot.getChild( 0 );
  1299. // const p2 = modelRoot.getChild( 1 );
  1300. //
  1301. // const markerRange = model.createRange( model.createPositionAt( p1, 3 ), model.createPositionAt( p2, 0 ) );
  1302. //
  1303. // model.change( writer => {
  1304. // writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1305. // } );
  1306. //
  1307. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1308. //
  1309. // model.change( writer => {
  1310. // writer.removeMarker( 'marker', { range: markerRange, usingOperation: false } );
  1311. // } );
  1312. //
  1313. // expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1314. // } );
  1315. // } );
  1316. //
  1317. // describe( 'on element', () => {
  1318. // const highlightDescriptor = {
  1319. // classes: 'highlight-class',
  1320. // priority: 7,
  1321. // attributes: { title: 'title' },
  1322. // id: 'customId'
  1323. // };
  1324. //
  1325. // let markerRange;
  1326. //
  1327. // beforeEach( () => {
  1328. // // Provide converter for div element. View div element will have custom highlight handling.
  1329. // dispatcher.on( 'insert:div', insertElement( () => {
  1330. // const viewContainer = new ViewContainerElement( 'div' );
  1331. //
  1332. // viewContainer._setCustomProperty( 'addHighlight', ( element, descriptor, writer ) => {
  1333. // writer.addClass( descriptor.classes, element );
  1334. // } );
  1335. //
  1336. // viewContainer._setCustomProperty( 'removeHighlight', ( element, id, writer ) => {
  1337. // writer.setAttribute( 'class', '', element );
  1338. // } );
  1339. //
  1340. // return viewContainer;
  1341. // } ) );
  1342. //
  1343. // const modelElement = new ModelElement( 'div', null, new ModelText( 'foo' ) );
  1344. //
  1345. // model.change( writer => {
  1346. // writer.insert( modelElement, modelRootStart );
  1347. // } );
  1348. //
  1349. // markerRange = model.createRangeOn( modelElement );
  1350. //
  1351. // dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  1352. // dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
  1353. // dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
  1354. // } );
  1355. //
  1356. // it( 'should use addHighlight and removeHighlight on elements and not convert children nodes', () => {
  1357. // model.change( writer => {
  1358. // writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1359. // } );
  1360. //
  1361. // expect( viewToString( viewRoot ) ).to.equal(
  1362. // '<div>' +
  1363. // '<div class="highlight-class">' +
  1364. // 'foo' +
  1365. // '</div>' +
  1366. // '</div>'
  1367. // );
  1368. //
  1369. // model.change( writer => {
  1370. // writer.removeMarker( 'marker' );
  1371. // } );
  1372. //
  1373. // expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1374. // } );
  1375. //
  1376. // it( 'should be possible to override', () => {
  1377. // const newDescriptor = { classes: 'override-class' };
  1378. //
  1379. // dispatcher.on( 'addMarker:marker', highlightText( newDescriptor ), { priority: 'high' } );
  1380. // dispatcher.on( 'addMarker:marker', highlightElement( newDescriptor ), { priority: 'high' } );
  1381. // dispatcher.on( 'removeMarker:marker', removeHighlight( newDescriptor ), { priority: 'high' } );
  1382. //
  1383. // model.change( writer => {
  1384. // writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1385. // } );
  1386. //
  1387. // expect( viewToString( viewRoot ) ).to.equal(
  1388. // '<div>' +
  1389. // '<div class="override-class">' +
  1390. // 'foo' +
  1391. // '</div>' +
  1392. // '</div>'
  1393. // );
  1394. //
  1395. // model.change( writer => {
  1396. // writer.removeMarker( 'marker' );
  1397. // } );
  1398. //
  1399. // expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1400. // } );
  1401. //
  1402. // it( 'should use default priority and id if not provided', () => {
  1403. // const viewDiv = viewRoot.getChild( 0 );
  1404. //
  1405. // dispatcher.on( 'addMarker:marker2', highlightText( () => null ) );
  1406. // dispatcher.on( 'addMarker:marker2', highlightElement( () => null ) );
  1407. // dispatcher.on( 'removeMarker:marker2', removeHighlight( () => null ) );
  1408. //
  1409. // viewDiv._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
  1410. // expect( descriptor.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  1411. // expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
  1412. // } );
  1413. //
  1414. // viewDiv._setCustomProperty( 'removeHighlight', ( element, id ) => {
  1415. // expect( id ).to.equal( 'marker:foo-bar-baz' );
  1416. // } );
  1417. //
  1418. // model.change( writer => {
  1419. // writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  1420. // } );
  1421. // } );
  1422. //
  1423. // it( 'should do nothing if descriptor is not provided', () => {
  1424. // dispatcher.on( 'addMarker:marker2', highlightText( () => null ) );
  1425. // dispatcher.on( 'addMarker:marker2', highlightElement( () => null ) );
  1426. // dispatcher.on( 'removeMarker:marker2', removeHighlight( () => null ) );
  1427. //
  1428. // model.change( writer => {
  1429. // writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  1430. // } );
  1431. //
  1432. // expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1433. //
  1434. // model.change( writer => {
  1435. // writer.removeMarker( 'marker2' );
  1436. // } );
  1437. //
  1438. // expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1439. // } );
  1440. // } );
  1441. // } );
  1442. describe( 'createViewElementFromHighlightDescriptor()', () => {
  1443. it( 'should return attribute element from descriptor object', () => {
  1444. const descriptor = {
  1445. classes: 'foo-class',
  1446. attributes: { one: '1', two: '2' },
  1447. priority: 7
  1448. };
  1449. const element = createViewElementFromHighlightDescriptor( descriptor );
  1450. expect( element.is( 'attributeElement' ) ).to.be.true;
  1451. expect( element.name ).to.equal( 'span' );
  1452. expect( element.priority ).to.equal( 7 );
  1453. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1454. for ( const key of Object.keys( descriptor.attributes ) ) {
  1455. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1456. }
  1457. } );
  1458. it( 'should return attribute element from descriptor object - array with classes', () => {
  1459. const descriptor = {
  1460. classes: [ 'foo-class', 'bar-class' ],
  1461. attributes: { one: '1', two: '2' },
  1462. priority: 7
  1463. };
  1464. const element = createViewElementFromHighlightDescriptor( descriptor );
  1465. expect( element.is( 'attributeElement' ) ).to.be.true;
  1466. expect( element.name ).to.equal( 'span' );
  1467. expect( element.priority ).to.equal( 7 );
  1468. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1469. expect( element.hasClass( 'bar-class' ) ).to.be.true;
  1470. for ( const key of Object.keys( descriptor.attributes ) ) {
  1471. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1472. }
  1473. } );
  1474. it( 'should create element without class', () => {
  1475. const descriptor = {
  1476. attributes: { one: '1', two: '2' },
  1477. priority: 7
  1478. };
  1479. const element = createViewElementFromHighlightDescriptor( descriptor );
  1480. expect( element.is( 'attributeElement' ) ).to.be.true;
  1481. expect( element.name ).to.equal( 'span' );
  1482. expect( element.priority ).to.equal( 7 );
  1483. for ( const key of Object.keys( descriptor.attributes ) ) {
  1484. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1485. }
  1486. } );
  1487. it( 'should create element without priority', () => {
  1488. const descriptor = {
  1489. classes: 'foo-class',
  1490. attributes: { one: '1', two: '2' }
  1491. };
  1492. const element = createViewElementFromHighlightDescriptor( descriptor );
  1493. expect( element.is( 'attributeElement' ) ).to.be.true;
  1494. expect( element.name ).to.equal( 'span' );
  1495. expect( element.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  1496. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1497. for ( const key of Object.keys( descriptor.attributes ) ) {
  1498. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1499. }
  1500. } );
  1501. it( 'should create element without attributes', () => {
  1502. const descriptor = {
  1503. classes: 'foo-class',
  1504. priority: 7
  1505. };
  1506. const element = createViewElementFromHighlightDescriptor( descriptor );
  1507. expect( element.is( 'attributeElement' ) ).to.be.true;
  1508. expect( element.name ).to.equal( 'span' );
  1509. expect( element.priority ).to.equal( 7 );
  1510. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1511. } );
  1512. } );
  1513. } );