downcast-converters.js 55 KB

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