downcasthelpers.js 87 KB

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