8
0

downcasthelpers.js 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851
  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, { writer } ) => writer.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, { writer } ) => {
  180. return writer.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, { writer } ) => {
  195. return writer.createAttributeElement( 'span', { style: 'color:' + modelAttributeValue } );
  196. }
  197. } );
  198. downcastHelpers.elementToElement( {
  199. model: 'smiley',
  200. view: ( modelElement, { writer } ) => {
  201. return writer.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, { writer } ) => writer.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, { writer } ) => {
  241. if ( modelAttributeValue == 'bold' ) {
  242. return writer.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, { writer } ) => {
  264. return writer.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, { writer } ) => writer.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, { writer } ) => writer.createAttributeElement( 'b' )
  297. } );
  298. downcastHelpers.attributeToElement( {
  299. model: 'bold',
  300. view: ( modelAttributeValue, { writer } ) => writer.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, { writer } ) => writer.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, conversionApi ) => {
  473. // To ensure conversion API is provided.
  474. expect( conversionApi.writer ).to.instanceof( DowncastWriter );
  475. return { key: 'class', value: 'styled-' + attributeValue };
  476. }
  477. } );
  478. model.change( writer => {
  479. writer.insertElement( 'image', { styled: 'pull-out' }, modelRoot, 0 );
  480. } );
  481. expectResult( '<img class="styled-pull-out"></img>' );
  482. } );
  483. // #1587
  484. it( 'config.view and config.model as strings in generic conversion (elements only)', () => {
  485. const consoleWarnStub = testUtils.sinon.stub( console, 'warn' );
  486. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  487. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  488. model.change( writer => {
  489. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 0 );
  490. writer.insertElement( 'paragraph', { test: '2' }, modelRoot, 1 );
  491. } );
  492. expectResult( '<p test="1"></p><p test="2"></p>' );
  493. expect( consoleWarnStub.callCount ).to.equal( 0 );
  494. model.change( writer => {
  495. writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
  496. } );
  497. expectResult( '<p test="1"></p><p></p>' );
  498. } );
  499. // #1587
  500. it( 'config.view and config.model as strings in generic conversion (elements + text)', () => {
  501. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  502. downcastHelpers.attributeToAttribute( { model: 'test', view: 'test' } );
  503. expectToThrowCKEditorError( () => {
  504. model.change( writer => {
  505. writer.insertElement( 'paragraph', modelRoot, 0 );
  506. writer.insertElement( 'paragraph', { test: '1' }, modelRoot, 1 );
  507. writer.insertText( 'Foo', { test: '2' }, modelRoot.getChild( 0 ), 0 );
  508. writer.insertText( 'Bar', { test: '3' }, modelRoot.getChild( 1 ), 0 );
  509. } );
  510. }, /^conversion-attribute-to-attribute-on-text/ );
  511. } );
  512. it( 'should convert attribute insert/change/remove on a model node', () => {
  513. const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  514. model.change( writer => {
  515. writer.insert( modelElement, modelRootStart );
  516. } );
  517. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  518. model.change( writer => {
  519. writer.setAttribute( 'class', 'bar', modelElement );
  520. } );
  521. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="bar">foobar</p></div>' );
  522. model.change( writer => {
  523. writer.removeAttribute( 'class', modelElement );
  524. } );
  525. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  526. } );
  527. it( 'should be possible to override setAttribute', () => {
  528. downcastHelpers.attributeToAttribute( {
  529. model: 'class',
  530. view: ( evt, data, conversionApi ) => {
  531. conversionApi.consumable.consume( data.item, 'attribute:class' );
  532. },
  533. converterPriority: 'high'
  534. } );
  535. model.change( writer => {
  536. const modelElement = new ModelElement( 'paragraph', { classes: 'foo' }, new ModelText( 'foobar' ) );
  537. writer.insert( modelElement, modelRootStart );
  538. } );
  539. // No attribute set.
  540. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  541. } );
  542. it( 'should not convert or consume if element creator returned null', () => {
  543. const callback = sinon.stub().returns( null );
  544. downcastHelpers.attributeToAttribute( {
  545. model: 'class',
  546. view: callback,
  547. converterPriority: 'high'
  548. } );
  549. const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );
  550. model.change( writer => {
  551. writer.insert( modelElement, modelRootStart );
  552. } );
  553. expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );
  554. sinon.assert.called( callback );
  555. } );
  556. } );
  557. describe( 'markerToElement()', () => {
  558. let modelText, modelElement, range;
  559. it( 'should be chainable', () => {
  560. expect( downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } ) ).to.equal( downcastHelpers );
  561. } );
  562. it( 'config.view is a string', () => {
  563. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  564. model.change( writer => {
  565. writer.insertText( 'foo', modelRoot, 0 );
  566. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  567. writer.addMarker( 'search', { range, usingOperation: false } );
  568. } );
  569. expectResult( 'f<marker-search></marker-search>o<marker-search></marker-search>o' );
  570. } );
  571. it( 'can be overwritten using converterPriority', () => {
  572. downcastHelpers.markerToElement( { model: 'search', view: 'marker-search' } );
  573. downcastHelpers.markerToElement( { model: 'search', view: 'search', converterPriority: 'high' } );
  574. model.change( writer => {
  575. writer.insertText( 'foo', modelRoot, 0 );
  576. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  577. writer.addMarker( 'search', { range, usingOperation: false } );
  578. } );
  579. expectResult( 'f<search></search>o<search></search>o' );
  580. } );
  581. it( 'config.view is a view element definition', () => {
  582. downcastHelpers.markerToElement( {
  583. model: 'search',
  584. view: {
  585. name: 'span',
  586. attributes: {
  587. 'data-marker': 'search'
  588. }
  589. }
  590. } );
  591. model.change( writer => {
  592. writer.insertText( 'foo', modelRoot, 0 );
  593. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  594. writer.addMarker( 'search', { range, usingOperation: false } );
  595. } );
  596. expectResult( 'f<span data-marker="search"></span>o<span data-marker="search"></span>o' );
  597. } );
  598. it( 'config.view is a function', () => {
  599. downcastHelpers.markerToElement( {
  600. model: 'search',
  601. view: ( data, { writer } ) => {
  602. return writer.createUIElement( 'span', { 'data-marker': 'search', 'data-start': data.isOpening } );
  603. }
  604. } );
  605. model.change( writer => {
  606. writer.insertText( 'foo', modelRoot, 0 );
  607. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) );
  608. writer.addMarker( 'search', { range, usingOperation: false } );
  609. } );
  610. expectResult( 'f<span data-marker="search" data-start="true"></span>o<span data-marker="search" data-start="false"></span>o' );
  611. } );
  612. describe( 'collapsed range', () => {
  613. beforeEach( () => {
  614. modelText = new ModelText( 'foobar' );
  615. modelElement = new ModelElement( 'paragraph', null, modelText );
  616. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  617. model.change( writer => {
  618. writer.insert( modelElement, modelRootStart );
  619. } );
  620. range = model.createRange( model.createPositionAt( modelElement, 3 ), model.createPositionAt( modelElement, 3 ) );
  621. } );
  622. it( 'should insert and remove ui element', () => {
  623. downcastHelpers.markerToElement( {
  624. model: 'marker',
  625. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  626. } );
  627. model.change( writer => {
  628. writer.addMarker( 'marker', { range, usingOperation: false } );
  629. } );
  630. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo<span class="marker"></span>bar</p></div>' );
  631. model.change( writer => {
  632. writer.removeMarker( 'marker' );
  633. } );
  634. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  635. } );
  636. it( 'should not convert if consumable was consumed', () => {
  637. sinon.spy( controller.downcastDispatcher, 'fire' );
  638. downcastHelpers.markerToElement( {
  639. model: 'marker',
  640. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  641. } );
  642. controller.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  643. conversionApi.consumable.consume( data.markerRange, 'addMarker:marker' );
  644. }, { priority: 'high' } );
  645. model.change( writer => {
  646. writer.addMarker( 'marker', { range, usingOperation: false } );
  647. } );
  648. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  649. expect( controller.downcastDispatcher.fire.calledWith( 'addMarker:marker' ) );
  650. } );
  651. it( 'should not convert if creator returned null', () => {
  652. downcastHelpers.markerToElement( {
  653. model: 'marker',
  654. view: () => null
  655. } );
  656. model.change( writer => {
  657. writer.addMarker( 'marker', { range, usingOperation: false } );
  658. } );
  659. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  660. model.change( writer => {
  661. writer.removeMarker( 'marker' );
  662. } );
  663. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  664. } );
  665. } );
  666. describe( 'non-collapsed range', () => {
  667. beforeEach( () => {
  668. modelText = new ModelText( 'foobar' );
  669. modelElement = new ModelElement( 'paragraph', null, modelText );
  670. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  671. model.change( writer => {
  672. writer.insert( modelElement, modelRootStart );
  673. } );
  674. range = model.createRange( model.createPositionAt( modelElement, 2 ), model.createPositionAt( modelElement, 5 ) );
  675. } );
  676. it( 'should insert and remove ui element - element as a creator', () => {
  677. downcastHelpers.markerToElement( {
  678. model: 'marker',
  679. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  680. } );
  681. model.change( writer => {
  682. writer.addMarker( 'marker', { range, usingOperation: false } );
  683. } );
  684. expect( viewToString( viewRoot ) )
  685. .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  686. model.change( writer => {
  687. writer.removeMarker( 'marker' );
  688. } );
  689. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  690. } );
  691. it( 'should insert and remove ui element - function as a creator', () => {
  692. downcastHelpers.markerToElement( {
  693. model: 'marker',
  694. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': data.markerName } )
  695. } );
  696. model.change( writer => {
  697. writer.addMarker( 'marker', { range, usingOperation: false } );
  698. } );
  699. expect( viewToString( viewRoot ) )
  700. .to.equal( '<div><p>fo<span class="marker"></span>oba<span class="marker"></span>r</p></div>' );
  701. model.change( writer => {
  702. writer.removeMarker( 'marker' );
  703. } );
  704. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  705. } );
  706. it( 'should insert and remove different opening and ending element', () => {
  707. downcastHelpers.markerToElement( {
  708. model: 'marker',
  709. view: ( data, { writer } ) => {
  710. if ( data.isOpening ) {
  711. return writer.createUIElement( 'span', { 'class': data.markerName, 'data-start': true } );
  712. }
  713. return writer.createUIElement( 'span', { 'class': data.markerName, 'data-end': true } );
  714. }
  715. } );
  716. model.change( writer => {
  717. writer.addMarker( 'marker', { range, usingOperation: false } );
  718. } );
  719. expect( viewToString( viewRoot ) ).to.equal(
  720. '<div><p>fo<span class="marker" data-start="true"></span>oba<span class="marker" data-end="true"></span>r</p></div>'
  721. );
  722. model.change( writer => {
  723. writer.removeMarker( 'marker' );
  724. } );
  725. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  726. } );
  727. it( 'should not convert if consumable was consumed', () => {
  728. sinon.spy( controller.downcastDispatcher, 'fire' );
  729. downcastHelpers.markerToElement( {
  730. model: 'marker',
  731. view: ( data, { writer } ) => writer.createUIElement( 'span', { 'class': 'marker' } )
  732. } );
  733. controller.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  734. conversionApi.consumable.consume( data.item, 'addMarker:marker' );
  735. }, { priority: 'high' } );
  736. model.change( writer => {
  737. writer.addMarker( 'marker', { range, usingOperation: false } );
  738. } );
  739. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  740. expect( controller.downcastDispatcher.fire.calledWith( 'addMarker:marker' ) );
  741. } );
  742. } );
  743. } );
  744. describe( 'markerToData()', () => {
  745. let root;
  746. beforeEach( () => {
  747. root = model.document.getRoot();
  748. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  749. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  750. } );
  751. it( 'should be chainable', () => {
  752. expect( downcastHelpers.markerToData( { model: 'search' } ) ).to.equal( downcastHelpers );
  753. } );
  754. it( 'default conversion, inside text, non-collapsed, no name', () => {
  755. downcastHelpers.markerToData( { model: 'search' } );
  756. setModelData( model, '<paragraph>Fo[ob]ar</paragraph>' );
  757. model.change( writer => {
  758. writer.addMarker( 'search', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  759. } );
  760. expectResult( '<p>Fo<search-start></search-start>ob<search-end></search-end>ar</p>' );
  761. model.change( writer => {
  762. writer.removeMarker( 'search' );
  763. } );
  764. expectResult( '<p>Foobar</p>' );
  765. } );
  766. it( 'default conversion, inside text, non-collapsed, name', () => {
  767. downcastHelpers.markerToData( { model: 'group' } );
  768. setModelData( model, '<paragraph>Fo[ob]ar</paragraph>' );
  769. model.change( writer => {
  770. writer.addMarker( 'group:foo:bar:baz', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  771. } );
  772. expectResult( '<p>Fo<group-start name="foo:bar:baz"></group-start>ob<group-end name="foo:bar:baz"></group-end>ar</p>' );
  773. model.change( writer => {
  774. writer.removeMarker( 'group:foo:bar:baz' );
  775. } );
  776. expectResult( '<p>Foobar</p>' );
  777. } );
  778. it( 'default conversion, inside text, collapsed, no name', () => {
  779. downcastHelpers.markerToData( { model: 'search' } );
  780. setModelData( model, '<paragraph>Foo[]bar</paragraph>' );
  781. model.change( writer => {
  782. writer.addMarker( 'search', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  783. } );
  784. expectResult( '<p>Foo<search-start></search-start><search-end></search-end>bar</p>' );
  785. model.change( writer => {
  786. writer.removeMarker( 'search' );
  787. } );
  788. expectResult( '<p>Foobar</p>' );
  789. } );
  790. it( 'default conversion, inside text, collapsed, multiple markers, no name', () => {
  791. downcastHelpers.markerToData( { model: 'group' } );
  792. setModelData( model, '<paragraph>Foo[]bar</paragraph>' );
  793. model.change( writer => {
  794. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  795. writer.addMarker( 'group:abc', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  796. } );
  797. expectResult(
  798. '<p>' +
  799. 'Foo' +
  800. '<group-start name="abc"></group-start><group-end name="abc"></group-end>' +
  801. '<group-start name="foo"></group-start><group-end name="foo"></group-end>' +
  802. 'bar' +
  803. '</p>'
  804. );
  805. model.change( writer => {
  806. writer.removeMarker( 'group:foo' );
  807. writer.removeMarker( 'group:abc' );
  808. } );
  809. expectResult( '<p>Foobar</p>' );
  810. } );
  811. it( 'default conversion, on two elements, no name', () => {
  812. downcastHelpers.markerToData( { model: 'search' } );
  813. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  814. model.change( writer => {
  815. const range = writer.createRangeIn( root );
  816. writer.addMarker( 'search', { range, usingOperation: false } );
  817. } );
  818. expectResult( '<p data-search-start-before="">Foo</p><p data-search-end-after="">Bar</p>' );
  819. model.change( writer => {
  820. writer.removeMarker( 'search' );
  821. } );
  822. expectResult( '<p>Foo</p><p>Bar</p>' );
  823. } );
  824. it( 'default conversion, on two elements, name', () => {
  825. downcastHelpers.markerToData( { model: 'group' } );
  826. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  827. model.change( writer => {
  828. const range = writer.createRangeIn( root );
  829. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  830. } );
  831. expectResult( '<p data-group-start-before="foo:bar:baz">Foo</p><p data-group-end-after="foo:bar:baz">Bar</p>' );
  832. model.change( writer => {
  833. writer.removeMarker( 'group:foo:bar:baz' );
  834. } );
  835. expectResult( '<p>Foo</p><p>Bar</p>' );
  836. } );
  837. it( 'default conversion, on one element, name', () => {
  838. downcastHelpers.markerToData( { model: 'group' } );
  839. setModelData( model, '<paragraph>Foobar</paragraph>' );
  840. model.change( writer => {
  841. const range = writer.createRangeIn( root );
  842. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  843. } );
  844. expectResult( '<p data-group-end-after="foo:bar:baz" data-group-start-before="foo:bar:baz">Foobar</p>' );
  845. model.change( writer => {
  846. writer.removeMarker( 'group:foo:bar:baz' );
  847. } );
  848. expectResult( '<p>Foobar</p>' );
  849. } );
  850. it( 'default conversion, collapsed before element, name', () => {
  851. downcastHelpers.markerToData( { model: 'group' } );
  852. setModelData( model, '<paragraph>Foobar</paragraph>' );
  853. model.change( writer => {
  854. // Collapsed before <paragraph>.
  855. const range = writer.createRange(
  856. writer.createPositionFromPath( root, [ 0 ] )
  857. );
  858. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  859. } );
  860. expectResult( '<p data-group-end-before="foo:bar:baz" data-group-start-before="foo:bar:baz">Foobar</p>' );
  861. model.change( writer => {
  862. writer.removeMarker( 'group:foo:bar:baz' );
  863. } );
  864. expectResult( '<p>Foobar</p>' );
  865. } );
  866. it( 'default conversion, collapsed after element, name', () => {
  867. downcastHelpers.markerToData( { model: 'group' } );
  868. setModelData( model, '<paragraph>Foobar</paragraph>' );
  869. model.change( writer => {
  870. // Collapsed before <paragraph>.
  871. const range = writer.createRange(
  872. writer.createPositionFromPath( root, [ 1 ] )
  873. );
  874. writer.addMarker( 'group:foo:bar:baz', { range, usingOperation: false } );
  875. } );
  876. expectResult( '<p data-group-end-after="foo:bar:baz" data-group-start-after="foo:bar:baz">Foobar</p>' );
  877. model.change( writer => {
  878. writer.removeMarker( 'group:foo:bar:baz' );
  879. } );
  880. expectResult( '<p>Foobar</p>' );
  881. } );
  882. it( 'default conversion, mixed, multiple markers, name', () => {
  883. downcastHelpers.markerToData( { model: 'group' } );
  884. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  885. model.change( writer => {
  886. const range = writer.createRange(
  887. writer.createPositionFromPath( root, [ 0 ] ),
  888. writer.createPositionFromPath( root, [ 1, 2 ] )
  889. );
  890. writer.addMarker( 'group:foo:bar', { range, usingOperation: false } );
  891. writer.addMarker( 'group:abc:xyz', { range, usingOperation: false } );
  892. } );
  893. expectResult(
  894. '<p data-group-start-before="abc:xyz,foo:bar">Foo</p>' +
  895. '<p>Ba<group-end name="abc:xyz"></group-end><group-end name="foo:bar"></group-end>r</p>'
  896. );
  897. model.change( writer => {
  898. writer.removeMarker( 'group:foo:bar' );
  899. writer.removeMarker( 'group:abc:xyz' );
  900. } );
  901. expectResult( '<p>Foo</p><p>Bar</p>' );
  902. } );
  903. it( 'default conversion, mixed #2, multiple markers, name', () => {
  904. downcastHelpers.markerToData( { model: 'group' } );
  905. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  906. model.change( writer => {
  907. const range = writer.createRange(
  908. writer.createPositionFromPath( root, [ 0, 1 ] ),
  909. writer.createPositionFromPath( root, [ 2 ] )
  910. );
  911. writer.addMarker( 'group:foo:bar', { range, usingOperation: false } );
  912. writer.addMarker( 'group:abc:xyz', { range, usingOperation: false } );
  913. } );
  914. expectResult(
  915. '<p>F<group-start name="abc:xyz"></group-start><group-start name="foo:bar"></group-start>oo</p>' +
  916. '<p data-group-end-after="abc:xyz,foo:bar">Bar</p>'
  917. );
  918. model.change( writer => {
  919. writer.removeMarker( 'group:foo:bar' );
  920. writer.removeMarker( 'group:abc:xyz' );
  921. } );
  922. expectResult( '<p>Foo</p><p>Bar</p>' );
  923. } );
  924. it( 'default conversion, mixed #3, multiple markers, name', () => {
  925. downcastHelpers.markerToData( { model: 'group' } );
  926. setModelData( model, '<paragraph>Foo</paragraph>' );
  927. model.change( writer => {
  928. const range = writer.createRange(
  929. writer.createPositionFromPath( root, [ 0 ] ),
  930. writer.createPositionFromPath( root, [ 0, 2 ] )
  931. );
  932. writer.addMarker( 'group:foo:bar', { range, usingOperation: false } );
  933. writer.addMarker( 'group:abc:xyz', { range, usingOperation: false } );
  934. } );
  935. expectResult(
  936. '<p data-group-start-before="abc:xyz,foo:bar">' +
  937. 'Fo<group-end name="abc:xyz"></group-end><group-end name="foo:bar"></group-end>o' +
  938. '</p>'
  939. );
  940. model.change( writer => {
  941. writer.removeMarker( 'group:foo:bar' );
  942. writer.removeMarker( 'group:abc:xyz' );
  943. } );
  944. expectResult( '<p>Foo</p>' );
  945. } );
  946. it( 'default conversion, mixed #4, 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, 2 ] ),
  952. writer.createPositionFromPath( root, [ 1 ] )
  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-end-after="abc:xyz,foo:bar">' +
  959. 'Fo<group-start name="abc:xyz"></group-start><group-start name="foo:bar"></group-start>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( 'conversion callback, mixed, multiple markers, name', () => {
  969. const customData = {
  970. foo: 'bar',
  971. abc: 'xyz'
  972. };
  973. downcastHelpers.markerToData( {
  974. model: 'group',
  975. view: ( markerName, conversionApi ) => {
  976. const namePart = markerName.split( ':' )[ 1 ];
  977. // To ensure conversion API is provided.
  978. expect( conversionApi.writer ).to.instanceof( DowncastWriter );
  979. return {
  980. group: 'g',
  981. name: namePart + '_' + customData[ namePart ]
  982. };
  983. }
  984. } );
  985. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  986. model.change( writer => {
  987. const range = writer.createRange(
  988. writer.createPositionFromPath( root, [ 0 ] ),
  989. writer.createPositionFromPath( root, [ 1, 2 ] )
  990. );
  991. writer.addMarker( 'group:foo', { range, usingOperation: false } );
  992. writer.addMarker( 'group:abc', { range, usingOperation: false } );
  993. } );
  994. expectResult(
  995. '<p data-g-start-before="abc_xyz,foo_bar">Foo</p>' +
  996. '<p>Ba<g-end name="abc_xyz"></g-end><g-end name="foo_bar"></g-end>r</p>'
  997. );
  998. model.change( writer => {
  999. writer.removeMarker( 'group:foo' );
  1000. writer.removeMarker( 'group:abc' );
  1001. } );
  1002. expectResult( '<p>Foo</p><p>Bar</p>' );
  1003. } );
  1004. it( 'conversion callback, mixed #2, multiple markers, name', () => {
  1005. const customData = {
  1006. foo: 'bar',
  1007. abc: 'xyz'
  1008. };
  1009. downcastHelpers.markerToData( {
  1010. model: 'group',
  1011. view: markerName => {
  1012. const namePart = markerName.split( ':' )[ 1 ];
  1013. return {
  1014. group: 'g',
  1015. name: namePart + '_' + customData[ namePart ]
  1016. };
  1017. }
  1018. } );
  1019. setModelData( model, '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>' );
  1020. model.change( writer => {
  1021. const range = writer.createRange(
  1022. writer.createPositionFromPath( root, [ 0, 1 ] ),
  1023. writer.createPositionFromPath( root, [ 2 ] )
  1024. );
  1025. writer.addMarker( 'group:foo', { range, usingOperation: false } );
  1026. writer.addMarker( 'group:abc', { range, usingOperation: false } );
  1027. } );
  1028. expectResult(
  1029. '<p>F<g-start name="abc_xyz"></g-start><g-start name="foo_bar"></g-start>oo</p>' +
  1030. '<p data-g-end-after="abc_xyz,foo_bar">Bar</p>'
  1031. );
  1032. model.change( writer => {
  1033. writer.removeMarker( 'group:foo' );
  1034. writer.removeMarker( 'group:abc' );
  1035. } );
  1036. expectResult( '<p>Foo</p><p>Bar</p>' );
  1037. } );
  1038. it( 'can be overwritten using converterPriority', () => {
  1039. downcastHelpers.markerToData( {
  1040. model: 'group'
  1041. } );
  1042. downcastHelpers.markerToData( {
  1043. model: 'group',
  1044. view: markerName => {
  1045. const name = markerName.split( ':' )[ 1 ];
  1046. return {
  1047. group: 'g',
  1048. name
  1049. };
  1050. },
  1051. converterPriority: 'high'
  1052. } );
  1053. setModelData( model, '<paragraph>F[ooba]r</paragraph>' );
  1054. model.change( writer => {
  1055. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1056. } );
  1057. expectResult(
  1058. '<p>F<g-start name="foo"></g-start>ooba<g-end name="foo"></g-end>r</p>'
  1059. );
  1060. model.change( writer => {
  1061. writer.removeMarker( 'group:foo' );
  1062. } );
  1063. expectResult( '<p>Foobar</p>' );
  1064. } );
  1065. it( 'can be overwritten by custom callback', () => {
  1066. downcastHelpers.markerToData( {
  1067. model: 'group'
  1068. } );
  1069. downcastHelpers.add( dispatcher => {
  1070. dispatcher.on( 'addMarker:group', ( evt, data, conversionApi ) => {
  1071. conversionApi.consumable.consume( data.markerRange, evt.name );
  1072. }, { priority: 'high' } );
  1073. } );
  1074. setModelData( model, '<paragraph>Foo[]bar</paragraph>' );
  1075. model.change( writer => {
  1076. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1077. } );
  1078. expectResult( '<p>Foobar</p>' );
  1079. model.change( writer => {
  1080. writer.removeMarker( 'group:foo' );
  1081. } );
  1082. expectResult( '<p>Foobar</p>' );
  1083. } );
  1084. it( 'should not perform conversion if the callback returned falsy value', () => {
  1085. downcastHelpers.markerToData( {
  1086. model: 'group',
  1087. view: () => false
  1088. } );
  1089. setModelData( model, '<paragraph>F[ooba]r</paragraph>' );
  1090. model.change( writer => {
  1091. writer.addMarker( 'group:foo', { range: model.document.selection.getFirstRange(), usingOperation: false } );
  1092. } );
  1093. expectResult( '<p>Foobar</p>' );
  1094. model.change( writer => {
  1095. writer.removeMarker( 'group:foo' );
  1096. } );
  1097. expectResult( '<p>Foobar</p>' );
  1098. } );
  1099. } );
  1100. describe( 'markerToHighlight()', () => {
  1101. it( 'should be chainable', () => {
  1102. expect( downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } ) ).to.equal( downcastHelpers );
  1103. } );
  1104. it( 'config.view is a highlight descriptor', () => {
  1105. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  1106. model.change( writer => {
  1107. writer.insertText( 'foo', modelRoot, 0 );
  1108. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  1109. writer.addMarker( 'comment', { range, usingOperation: false } );
  1110. } );
  1111. expectResult( '<span class="comment">foo</span>' );
  1112. } );
  1113. it( 'can be overwritten using converterPriority', () => {
  1114. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  1115. downcastHelpers.markerToHighlight( { model: 'comment', view: { classes: 'new-comment' }, converterPriority: 'high' } );
  1116. model.change( writer => {
  1117. writer.insertText( 'foo', modelRoot, 0 );
  1118. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  1119. writer.addMarker( 'comment', { range, usingOperation: false } );
  1120. } );
  1121. expectResult( '<span class="new-comment">foo</span>' );
  1122. } );
  1123. it( 'config.view is a function', () => {
  1124. downcastHelpers.markerToHighlight( {
  1125. model: 'comment',
  1126. view: ( data, conversionApi ) => {
  1127. const commentType = data.markerName.split( ':' )[ 1 ];
  1128. // To ensure conversion API is provided.
  1129. expect( conversionApi.writer ).to.instanceof( DowncastWriter );
  1130. return {
  1131. classes: [ 'comment', 'comment-' + commentType ]
  1132. };
  1133. }
  1134. } );
  1135. model.change( writer => {
  1136. writer.insertText( 'foo', modelRoot, 0 );
  1137. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 3 ) );
  1138. writer.addMarker( 'comment:abc', { range, usingOperation: false } );
  1139. } );
  1140. expectResult( '<span class="comment comment-abc">foo</span>' );
  1141. } );
  1142. describe( 'highlight', () => {
  1143. const highlightConfig = {
  1144. model: 'marker',
  1145. view: {
  1146. classes: 'highlight-class',
  1147. attributes: { title: 'title' }
  1148. },
  1149. converterPriority: 7
  1150. };
  1151. describe( 'on text', () => {
  1152. let markerRange;
  1153. beforeEach( () => {
  1154. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1155. const modelElement1 = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  1156. const modelElement2 = new ModelElement( 'paragraph', null, new ModelText( 'bar' ) );
  1157. model.change( writer => {
  1158. writer.insert( [ modelElement1, modelElement2 ], modelRootStart );
  1159. } );
  1160. markerRange = model.createRangeIn( modelRoot );
  1161. } );
  1162. it( 'should wrap and unwrap text nodes', () => {
  1163. downcastHelpers.markerToHighlight( highlightConfig );
  1164. model.change( writer => {
  1165. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1166. } );
  1167. expect( viewToString( viewRoot ) ).to.equal(
  1168. '<div>' +
  1169. '<p>' +
  1170. '<span class="highlight-class" title="title">foo</span>' +
  1171. '</p>' +
  1172. '<p>' +
  1173. '<span class="highlight-class" title="title">bar</span>' +
  1174. '</p>' +
  1175. '</div>'
  1176. );
  1177. model.change( writer => {
  1178. writer.removeMarker( 'marker' );
  1179. } );
  1180. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1181. } );
  1182. it( 'should be possible to overwrite', () => {
  1183. downcastHelpers.markerToHighlight( highlightConfig );
  1184. downcastHelpers.markerToHighlight( {
  1185. model: 'marker',
  1186. view: { classes: 'override-class' },
  1187. converterPriority: 'high'
  1188. } );
  1189. model.change( writer => {
  1190. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1191. } );
  1192. expect( viewToString( viewRoot ) ).to.equal(
  1193. '<div>' +
  1194. '<p>' +
  1195. '<span class="override-class">foo</span>' +
  1196. '</p>' +
  1197. '<p>' +
  1198. '<span class="override-class">bar</span>' +
  1199. '</p>' +
  1200. '</div>'
  1201. );
  1202. model.change( writer => {
  1203. writer.removeMarker( 'marker' );
  1204. } );
  1205. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1206. } );
  1207. it( 'should do nothing if descriptor is not provided or generating function returns null', () => {
  1208. downcastHelpers.markerToHighlight( {
  1209. model: 'marker',
  1210. view: () => null,
  1211. converterPriority: 'high'
  1212. } );
  1213. model.change( writer => {
  1214. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1215. } );
  1216. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1217. model.change( writer => {
  1218. writer.removeMarker( 'marker' );
  1219. } );
  1220. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1221. } );
  1222. it( 'should do nothing if collapsed marker is converted', () => {
  1223. downcastHelpers.markerToHighlight( {
  1224. model: 'marker',
  1225. view: { classes: 'foo' },
  1226. converterPriority: 'high'
  1227. } );
  1228. markerRange = model.createRange( model.createPositionAt( modelRoot, 0 ), model.createPositionAt( modelRoot, 0 ) );
  1229. model.change( writer => {
  1230. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1231. } );
  1232. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1233. model.change( () => {
  1234. model.markers._remove( 'marker' );
  1235. } );
  1236. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1237. } );
  1238. it( 'should correctly wrap and unwrap multiple, intersecting markers', () => {
  1239. downcastHelpers.markerToHighlight( {
  1240. model: 'markerFoo',
  1241. view: { classes: 'foo' }
  1242. } );
  1243. downcastHelpers.markerToHighlight( {
  1244. model: 'markerBar',
  1245. view: { classes: 'bar' }
  1246. } );
  1247. downcastHelpers.markerToHighlight( {
  1248. model: 'markerXyz',
  1249. view: { classes: 'xyz' }
  1250. } );
  1251. const p1 = modelRoot.getChild( 0 );
  1252. const p2 = modelRoot.getChild( 1 );
  1253. model.change( writer => {
  1254. const range = writer.createRange( writer.createPositionAt( p1, 0 ), writer.createPositionAt( p1, 3 ) );
  1255. writer.addMarker( 'markerFoo', { range, usingOperation: false } );
  1256. } );
  1257. expect( viewToString( viewRoot ) ).to.equal(
  1258. '<div>' +
  1259. '<p>' +
  1260. '<span class="foo">foo</span>' +
  1261. '</p>' +
  1262. '<p>bar</p>' +
  1263. '</div>'
  1264. );
  1265. model.change( writer => {
  1266. const range = writer.createRange( writer.createPositionAt( p1, 1 ), writer.createPositionAt( p2, 2 ) );
  1267. writer.addMarker( 'markerBar', { range, usingOperation: false } );
  1268. } );
  1269. expect( viewToString( viewRoot ) ).to.equal(
  1270. '<div>' +
  1271. '<p>' +
  1272. '<span class="foo">f</span>' +
  1273. '<span class="bar">' +
  1274. '<span class="foo">oo</span>' +
  1275. '</span>' +
  1276. '</p>' +
  1277. '<p>' +
  1278. '<span class="bar">ba</span>' +
  1279. 'r' +
  1280. '</p>' +
  1281. '</div>'
  1282. );
  1283. model.change( writer => {
  1284. const range = writer.createRange( writer.createPositionAt( p1, 2 ), writer.createPositionAt( p2, 3 ) );
  1285. writer.addMarker( 'markerXyz', { 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">' +
  1293. 'o' +
  1294. '<span class="xyz">o</span>' +
  1295. '</span>' +
  1296. '</span>' +
  1297. '</p>' +
  1298. '<p>' +
  1299. '<span class="bar">' +
  1300. '<span class="xyz">ba</span>' +
  1301. '</span>' +
  1302. '<span class="xyz">r</span>' +
  1303. '</p>' +
  1304. '</div>'
  1305. );
  1306. model.change( writer => {
  1307. writer.removeMarker( 'markerBar' );
  1308. } );
  1309. expect( viewToString( viewRoot ) ).to.equal(
  1310. '<div>' +
  1311. '<p>' +
  1312. '<span class="foo">' +
  1313. 'fo' +
  1314. '<span class="xyz">o</span>' +
  1315. '</span>' +
  1316. '</p>' +
  1317. '<p>' +
  1318. '<span class="xyz">bar</span>' +
  1319. '</p>' +
  1320. '</div>'
  1321. );
  1322. model.change( writer => {
  1323. writer.removeMarker( 'markerFoo' );
  1324. } );
  1325. expect( viewToString( viewRoot ) ).to.equal(
  1326. '<div>' +
  1327. '<p>' +
  1328. 'fo' +
  1329. '<span class="xyz">o</span>' +
  1330. '</p>' +
  1331. '<p>' +
  1332. '<span class="xyz">bar</span>' +
  1333. '</p>' +
  1334. '</div>'
  1335. );
  1336. model.change( writer => {
  1337. writer.removeMarker( 'markerXyz' );
  1338. } );
  1339. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1340. } );
  1341. it( 'should do nothing if marker is applied and removed on empty-ish range', () => {
  1342. downcastHelpers.markerToHighlight( highlightConfig );
  1343. const p1 = modelRoot.getChild( 0 );
  1344. const p2 = modelRoot.getChild( 1 );
  1345. const markerRange = model.createRange( model.createPositionAt( p1, 3 ), model.createPositionAt( p2, 0 ) );
  1346. model.change( writer => {
  1347. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1348. } );
  1349. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1350. model.change( writer => {
  1351. writer.removeMarker( 'marker', { range: markerRange, usingOperation: false } );
  1352. } );
  1353. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
  1354. } );
  1355. } );
  1356. describe( 'on element', () => {
  1357. const highlightConfig = {
  1358. model: 'marker',
  1359. view: {
  1360. classes: 'highlight-class',
  1361. attributes: { title: 'title' },
  1362. id: 'customId'
  1363. },
  1364. converterPriority: 7
  1365. };
  1366. let markerRange, viewDocument;
  1367. beforeEach( () => {
  1368. viewDocument = new ViewDocument( new StylesProcessor() );
  1369. downcastHelpers.elementToElement( {
  1370. model: 'div',
  1371. view: () => {
  1372. const viewContainer = new ViewContainerElement( viewDocument, 'div' );
  1373. viewContainer._setCustomProperty( 'addHighlight', ( element, descriptor, writer ) => {
  1374. writer.addClass( descriptor.classes, element );
  1375. } );
  1376. viewContainer._setCustomProperty( 'removeHighlight', ( element, id, writer ) => {
  1377. writer.setAttribute( 'class', '', element );
  1378. } );
  1379. return viewContainer;
  1380. }
  1381. } );
  1382. const modelElement = new ModelElement( 'div', null, new ModelText( 'foo' ) );
  1383. model.change( writer => {
  1384. writer.insert( modelElement, modelRootStart );
  1385. } );
  1386. markerRange = model.createRangeOn( modelElement );
  1387. downcastHelpers.markerToHighlight( highlightConfig );
  1388. } );
  1389. it( 'should use addHighlight and removeHighlight on elements and not convert children nodes', () => {
  1390. model.change( writer => {
  1391. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1392. } );
  1393. expect( viewToString( viewRoot ) ).to.equal(
  1394. '<div>' +
  1395. '<div class="highlight-class">' +
  1396. 'foo' +
  1397. '</div>' +
  1398. '</div>'
  1399. );
  1400. model.change( writer => {
  1401. writer.removeMarker( 'marker' );
  1402. } );
  1403. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1404. } );
  1405. it( 'should be possible to override', () => {
  1406. downcastHelpers.markerToHighlight( {
  1407. model: 'marker',
  1408. view: { classes: 'override-class' },
  1409. converterPriority: 'high'
  1410. } );
  1411. model.change( writer => {
  1412. writer.addMarker( 'marker', { range: markerRange, usingOperation: false } );
  1413. } );
  1414. expect( viewToString( viewRoot ) ).to.equal(
  1415. '<div>' +
  1416. '<div class="override-class">' +
  1417. 'foo' +
  1418. '</div>' +
  1419. '</div>'
  1420. );
  1421. model.change( writer => {
  1422. writer.removeMarker( 'marker' );
  1423. } );
  1424. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1425. } );
  1426. it( 'should use default priority and id if not provided', () => {
  1427. const viewDiv = viewRoot.getChild( 0 );
  1428. downcastHelpers.markerToHighlight( {
  1429. model: 'marker2',
  1430. view: () => null,
  1431. converterPriority: 'high'
  1432. } );
  1433. viewDiv._setCustomProperty( 'addHighlight', ( element, descriptor ) => {
  1434. expect( descriptor.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  1435. expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
  1436. } );
  1437. viewDiv._setCustomProperty( 'removeHighlight', ( element, id ) => {
  1438. expect( id ).to.equal( 'marker:foo-bar-baz' );
  1439. } );
  1440. model.change( writer => {
  1441. writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  1442. } );
  1443. } );
  1444. it( 'should do nothing if descriptor is not provided', () => {
  1445. downcastHelpers.markerToHighlight( {
  1446. model: 'marker2',
  1447. view: () => null
  1448. } );
  1449. model.change( writer => {
  1450. writer.addMarker( 'marker2', { range: markerRange, usingOperation: false } );
  1451. } );
  1452. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1453. model.change( writer => {
  1454. writer.removeMarker( 'marker2' );
  1455. } );
  1456. expect( viewToString( viewRoot ) ).to.equal( '<div><div>foo</div></div>' );
  1457. } );
  1458. } );
  1459. } );
  1460. } );
  1461. function expectResult( string ) {
  1462. expect( stringifyView( viewRoot, null, { ignoreRoot: true } ) ).to.equal( string );
  1463. }
  1464. } );
  1465. describe( 'downcast converters', () => {
  1466. let dispatcher, modelDoc, modelRoot, viewRoot, controller, modelRootStart, model;
  1467. beforeEach( () => {
  1468. model = new Model();
  1469. modelDoc = model.document;
  1470. modelRoot = modelDoc.createRoot();
  1471. controller = new EditingController( model, new StylesProcessor() );
  1472. viewRoot = controller.view.document.getRoot();
  1473. // Set name of view root the same as dom root.
  1474. // This is a mock of attaching view root to dom root.
  1475. controller.view.document.getRoot()._name = 'div';
  1476. dispatcher = controller.downcastDispatcher;
  1477. const downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  1478. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  1479. modelRootStart = model.createPositionAt( modelRoot, 0 );
  1480. } );
  1481. describe( 'insertText()', () => {
  1482. it( 'should downcast text', () => {
  1483. model.change( writer => {
  1484. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  1485. } );
  1486. expect( viewToString( viewRoot ) ).to.equal( '<div>foobar</div>' );
  1487. } );
  1488. it( 'should support unicode', () => {
  1489. model.change( writer => {
  1490. writer.insert( new ModelText( 'நிலைக்கு' ), modelRootStart );
  1491. } );
  1492. expect( viewToString( viewRoot ) ).to.equal( '<div>நிலைக்கு</div>' );
  1493. } );
  1494. it( 'should be possible to override it', () => {
  1495. dispatcher.on( 'insert:$text', ( evt, data, conversionApi ) => {
  1496. conversionApi.consumable.consume( data.item, 'insert' );
  1497. }, { converterPriority: 'high' } );
  1498. model.change( writer => {
  1499. writer.insert( new ModelText( 'foobar' ), modelRootStart );
  1500. } );
  1501. expect( viewToString( viewRoot ) ).to.equal( '<div></div>' );
  1502. } );
  1503. } );
  1504. // Remove converter is by default already added in `EditingController` instance.
  1505. describe( 'remove()', () => {
  1506. let viewDocument;
  1507. beforeEach( () => {
  1508. viewDocument = new ViewDocument( new StylesProcessor() );
  1509. } );
  1510. it( 'should remove items from view accordingly to changes in model #1', () => {
  1511. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  1512. model.change( writer => {
  1513. writer.insert( modelElement, modelRootStart );
  1514. } );
  1515. model.change( writer => {
  1516. writer.remove(
  1517. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  1518. );
  1519. } );
  1520. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foar</p></div>' );
  1521. } );
  1522. it( 'should be possible to overwrite', () => {
  1523. dispatcher.on( 'remove', evt => evt.stop(), { priority: 'high' } );
  1524. const modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foobar' ) );
  1525. model.change( writer => {
  1526. writer.insert( modelElement, modelRootStart );
  1527. } );
  1528. model.change( writer => {
  1529. writer.remove(
  1530. writer.createRange( writer.createPositionAt( modelElement, 2 ), writer.createPositionAt( modelElement, 4 ) )
  1531. );
  1532. } );
  1533. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
  1534. } );
  1535. it( 'should support unicode', () => {
  1536. const modelElement = new ModelElement( 'paragraph', null, 'நிலைக்கு' );
  1537. model.change( writer => {
  1538. writer.insert( modelElement, modelRootStart );
  1539. } );
  1540. model.change( writer => {
  1541. writer.remove(
  1542. writer.createRange( writer.createPositionAt( modelElement, 0 ), writer.createPositionAt( modelElement, 6 ) )
  1543. );
  1544. } );
  1545. expect( viewToString( viewRoot ) ).to.equal( '<div><p>கு</p></div>' );
  1546. } );
  1547. it( 'should not remove view ui elements that are placed next to removed content', () => {
  1548. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  1549. viewRoot._appendChild( [
  1550. new ViewText( viewDocument, 'foz' ),
  1551. new ViewUIElement( viewDocument, 'span' ),
  1552. new ViewText( viewDocument, 'bar' )
  1553. ] );
  1554. // Remove 'b'.
  1555. model.change( writer => {
  1556. writer.remove(
  1557. writer.createRange( writer.createPositionAt( modelRoot, 3 ), writer.createPositionAt( modelRoot, 4 ) )
  1558. );
  1559. } );
  1560. expect( viewToString( viewRoot ) ).to.equal( '<div>foz<span></span>ar</div>' );
  1561. // Remove 'z'.
  1562. model.change( writer => {
  1563. writer.remove(
  1564. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 3 ) )
  1565. );
  1566. } );
  1567. expect( viewToString( viewRoot ) ).to.equal( '<div>fo<span></span>ar</div>' );
  1568. } );
  1569. it( 'should remove correct amount of text when it is split by view ui element', () => {
  1570. modelRoot._appendChild( new ModelText( 'fozbar' ) );
  1571. viewRoot._appendChild( [
  1572. new ViewText( viewDocument, 'foz' ),
  1573. new ViewUIElement( viewDocument, 'span' ),
  1574. new ViewText( viewDocument, 'bar' )
  1575. ] );
  1576. // Remove 'z<span></span>b'.
  1577. model.change( writer => {
  1578. writer.remove(
  1579. writer.createRange( writer.createPositionAt( modelRoot, 2 ), writer.createPositionAt( modelRoot, 4 ) )
  1580. );
  1581. } );
  1582. expect( viewToString( viewRoot ) ).to.equal( '<div>foar</div>' );
  1583. } );
  1584. it( 'should unbind elements', () => {
  1585. const modelElement = new ModelElement( 'paragraph' );
  1586. model.change( writer => {
  1587. writer.insert( modelElement, modelRootStart );
  1588. } );
  1589. const viewElement = controller.mapper.toViewElement( modelElement );
  1590. expect( viewElement ).not.to.be.undefined;
  1591. expect( controller.mapper.toModelElement( viewElement ) ).to.equal( modelElement );
  1592. model.change( writer => {
  1593. writer.remove( modelElement );
  1594. } );
  1595. expect( controller.mapper.toViewElement( modelElement ) ).to.be.undefined;
  1596. expect( controller.mapper.toModelElement( viewElement ) ).to.be.undefined;
  1597. } );
  1598. it( 'should not break when remove() is used as part of unwrapping', () => {
  1599. const modelP = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  1600. const modelWidget = new ModelElement( 'widget', null, modelP );
  1601. const downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  1602. downcastHelpers.elementToElement( { model: 'widget', view: 'widget' } );
  1603. model.change( writer => {
  1604. writer.insert( modelWidget, modelRootStart );
  1605. } );
  1606. expect( viewToString( viewRoot ) ).to.equal( '<div><widget><p>foo</p></widget></div>' );
  1607. const viewP = controller.mapper.toViewElement( modelP );
  1608. expect( viewP ).not.to.be.undefined;
  1609. model.change( writer => {
  1610. writer.unwrap( modelWidget );
  1611. } );
  1612. expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p></div>' );
  1613. // `modelP` is now bound with newly created view element.
  1614. expect( controller.mapper.toViewElement( modelP ) ).not.to.equal( viewP );
  1615. // `viewP` is no longer bound with model element.
  1616. expect( controller.mapper.toModelElement( viewP ) ).to.be.undefined;
  1617. // View element from view root is bound to `modelP`.
  1618. expect( controller.mapper.toModelElement( viewRoot.getChild( 0 ) ) ).to.equal( modelP );
  1619. } );
  1620. it( 'should work correctly if container element after ui element is removed', () => {
  1621. // Prepare a model and view structure.
  1622. // This is done outside of conversion to put view ui elements inside easily.
  1623. const modelP1 = new ModelElement( 'paragraph' );
  1624. const modelP2 = new ModelElement( 'paragraph' );
  1625. const viewP1 = new ViewContainerElement( viewDocument, 'p' );
  1626. const viewUi1 = new ViewUIElement( viewDocument, 'span' );
  1627. const viewUi2 = new ViewUIElement( viewDocument, 'span' );
  1628. const viewP2 = new ViewContainerElement( viewDocument, 'p' );
  1629. modelRoot._appendChild( [ modelP1, modelP2 ] );
  1630. viewRoot._appendChild( [ viewP1, viewUi1, viewUi2, viewP2 ] );
  1631. controller.mapper.bindElements( modelP1, viewP1 );
  1632. controller.mapper.bindElements( modelP2, viewP2 );
  1633. // Remove second paragraph element.
  1634. model.change( writer => {
  1635. writer.remove( writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 2 ) ) );
  1636. } );
  1637. expect( viewToString( viewRoot ) ).to.equal( '<div><p></p><span></span><span></span></div>' );
  1638. } );
  1639. it( 'should work correctly if container element after text node is removed', () => {
  1640. const modelText = new ModelText( 'foo' );
  1641. const modelP = new ModelElement( 'paragraph' );
  1642. model.change( writer => {
  1643. writer.insert( [ modelText, modelP ], modelRootStart );
  1644. } );
  1645. model.change( writer => {
  1646. writer.remove( modelP );
  1647. } );
  1648. expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
  1649. } );
  1650. } );
  1651. describe( 'createViewElementFromHighlightDescriptor()', () => {
  1652. let viewWriter;
  1653. beforeEach( () => {
  1654. viewWriter = new DowncastWriter( controller.view.document );
  1655. } );
  1656. it( 'should return attribute element from descriptor object', () => {
  1657. const descriptor = {
  1658. classes: 'foo-class',
  1659. attributes: { one: '1', two: '2' },
  1660. priority: 7
  1661. };
  1662. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  1663. expect( element.is( 'attributeElement' ) ).to.be.true;
  1664. expect( element.name ).to.equal( 'span' );
  1665. expect( element.priority ).to.equal( 7 );
  1666. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1667. for ( const key of Object.keys( descriptor.attributes ) ) {
  1668. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1669. }
  1670. } );
  1671. it( 'should return attribute element from descriptor object - array with classes', () => {
  1672. const descriptor = {
  1673. classes: [ 'foo-class', 'bar-class' ],
  1674. attributes: { one: '1', two: '2' },
  1675. priority: 7
  1676. };
  1677. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  1678. expect( element.is( 'attributeElement' ) ).to.be.true;
  1679. expect( element.name ).to.equal( 'span' );
  1680. expect( element.priority ).to.equal( 7 );
  1681. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1682. expect( element.hasClass( 'bar-class' ) ).to.be.true;
  1683. for ( const key of Object.keys( descriptor.attributes ) ) {
  1684. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1685. }
  1686. } );
  1687. it( 'should create element without class', () => {
  1688. const descriptor = {
  1689. attributes: { one: '1', two: '2' },
  1690. priority: 7
  1691. };
  1692. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  1693. expect( element.is( 'attributeElement' ) ).to.be.true;
  1694. expect( element.name ).to.equal( 'span' );
  1695. expect( element.priority ).to.equal( 7 );
  1696. for ( const key of Object.keys( descriptor.attributes ) ) {
  1697. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1698. }
  1699. } );
  1700. it( 'should create element without priority', () => {
  1701. const descriptor = {
  1702. classes: 'foo-class',
  1703. attributes: { one: '1', two: '2' }
  1704. };
  1705. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  1706. expect( element.is( 'attributeElement' ) ).to.be.true;
  1707. expect( element.name ).to.equal( 'span' );
  1708. expect( element.priority ).to.equal( ViewAttributeElement.DEFAULT_PRIORITY );
  1709. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1710. for ( const key of Object.keys( descriptor.attributes ) ) {
  1711. expect( element.getAttribute( key ) ).to.equal( descriptor.attributes[ key ] );
  1712. }
  1713. } );
  1714. it( 'should create element without attributes', () => {
  1715. const descriptor = {
  1716. classes: 'foo-class',
  1717. priority: 7
  1718. };
  1719. const element = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  1720. expect( element.is( 'attributeElement' ) ).to.be.true;
  1721. expect( element.name ).to.equal( 'span' );
  1722. expect( element.priority ).to.equal( 7 );
  1723. expect( element.hasClass( 'foo-class' ) ).to.be.true;
  1724. } );
  1725. } );
  1726. } );
  1727. describe( 'downcast selection converters', () => {
  1728. let dispatcher, mapper, model, view, modelDoc, modelRoot, docSelection, viewDoc, viewRoot, viewSelection, downcastHelpers;
  1729. beforeEach( () => {
  1730. model = new Model();
  1731. modelDoc = model.document;
  1732. modelRoot = modelDoc.createRoot();
  1733. docSelection = modelDoc.selection;
  1734. model.schema.extend( '$text', { allowIn: '$root' } );
  1735. view = new View( new StylesProcessor() );
  1736. viewDoc = view.document;
  1737. viewRoot = createViewRoot( viewDoc );
  1738. viewSelection = viewDoc.selection;
  1739. mapper = new Mapper();
  1740. mapper.bindElements( modelRoot, viewRoot );
  1741. dispatcher = new DowncastDispatcher( { mapper, viewSelection } );
  1742. dispatcher.on( 'insert:$text', insertText() );
  1743. downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  1744. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  1745. downcastHelpers.markerToHighlight( { model: 'marker', view: { classes: 'marker' }, converterPriority: 1 } );
  1746. // Default selection converters.
  1747. dispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  1748. dispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  1749. dispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  1750. } );
  1751. afterEach( () => {
  1752. view.destroy();
  1753. } );
  1754. describe( 'default converters', () => {
  1755. describe( 'range selection', () => {
  1756. it( 'in same container', () => {
  1757. testSelection(
  1758. [ 1, 4 ],
  1759. 'foobar',
  1760. 'f{oob}ar'
  1761. );
  1762. } );
  1763. it( 'in same container with unicode characters', () => {
  1764. testSelection(
  1765. [ 2, 6 ],
  1766. 'நிலைக்கு',
  1767. 'நி{லைக்}கு'
  1768. );
  1769. } );
  1770. it( 'in same container, over attribute', () => {
  1771. testSelection(
  1772. [ 1, 5 ],
  1773. 'fo<$text bold="true">ob</$text>ar',
  1774. 'f{o<strong>ob</strong>a}r'
  1775. );
  1776. } );
  1777. it( 'in same container, next to attribute', () => {
  1778. testSelection(
  1779. [ 1, 2 ],
  1780. 'fo<$text bold="true">ob</$text>ar',
  1781. 'f{o}<strong>ob</strong>ar'
  1782. );
  1783. } );
  1784. it( 'in same attribute', () => {
  1785. testSelection(
  1786. [ 2, 4 ],
  1787. 'f<$text bold="true">ooba</$text>r',
  1788. 'f<strong>o{ob}a</strong>r'
  1789. );
  1790. } );
  1791. it( 'in same attribute, selection same as attribute', () => {
  1792. testSelection(
  1793. [ 2, 4 ],
  1794. 'fo<$text bold="true">ob</$text>ar',
  1795. 'fo{<strong>ob</strong>}ar'
  1796. );
  1797. } );
  1798. it( 'starts in text node, ends in attribute #1', () => {
  1799. testSelection(
  1800. [ 1, 3 ],
  1801. 'fo<$text bold="true">ob</$text>ar',
  1802. 'f{o<strong>o}b</strong>ar'
  1803. );
  1804. } );
  1805. it( 'starts in text node, ends in attribute #2', () => {
  1806. testSelection(
  1807. [ 1, 4 ],
  1808. 'fo<$text bold="true">ob</$text>ar',
  1809. 'f{o<strong>ob</strong>}ar'
  1810. );
  1811. } );
  1812. it( 'starts in attribute, ends in text node', () => {
  1813. testSelection(
  1814. [ 3, 5 ],
  1815. 'fo<$text bold="true">ob</$text>ar',
  1816. 'fo<strong>o{b</strong>a}r'
  1817. );
  1818. } );
  1819. it( 'consumes consumable values properly', () => {
  1820. // Add callback that will fire before default ones.
  1821. // This should prevent default callback doing anything.
  1822. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  1823. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  1824. }, { priority: 'high' } );
  1825. // Similar test case as the first in this suite.
  1826. testSelection(
  1827. [ 1, 4 ],
  1828. 'foobar',
  1829. 'foobar' // No selection in view.
  1830. );
  1831. } );
  1832. it( 'should convert backward selection', () => {
  1833. testSelection(
  1834. [ 1, 3, 'backward' ],
  1835. 'foobar',
  1836. 'f{oo}bar'
  1837. );
  1838. expect( viewSelection.focus.offset ).to.equal( 1 );
  1839. } );
  1840. } );
  1841. describe( 'collapsed selection', () => {
  1842. let marker, viewDocument;
  1843. beforeEach( () => {
  1844. viewDocument = new ViewDocument( new StylesProcessor() );
  1845. } );
  1846. it( 'in container', () => {
  1847. testSelection(
  1848. [ 1, 1 ],
  1849. 'foobar',
  1850. 'f{}oobar'
  1851. );
  1852. } );
  1853. it( 'in attribute', () => {
  1854. testSelection(
  1855. [ 3, 3 ],
  1856. 'f<$text bold="true">ooba</$text>r',
  1857. 'f<strong>oo{}ba</strong>r'
  1858. );
  1859. } );
  1860. it( 'in attribute and marker', () => {
  1861. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  1862. model.change( writer => {
  1863. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1864. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  1865. writer.setSelection( modelRoot, 3 );
  1866. } );
  1867. // Remove view children manually (without firing additional conversion).
  1868. viewRoot._removeChildren( 0, viewRoot.childCount );
  1869. // Convert model to view.
  1870. view.change( writer => {
  1871. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1872. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1873. dispatcher.convertSelection( docSelection, model.markers, writer );
  1874. } );
  1875. // Stringify view and check if it is same as expected.
  1876. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal(
  1877. '<div>f<span class="marker">o<strong>o{}b</strong>a</span>r</div>'
  1878. );
  1879. } );
  1880. it( 'in attribute and marker - no attribute', () => {
  1881. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  1882. model.change( writer => {
  1883. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1884. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  1885. writer.setSelection( modelRoot, 3 );
  1886. writer.removeSelectionAttribute( 'bold' );
  1887. } );
  1888. // Remove view children manually (without firing additional conversion).
  1889. viewRoot._removeChildren( 0, viewRoot.childCount );
  1890. // Convert model to view.
  1891. view.change( writer => {
  1892. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1893. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1894. dispatcher.convertSelection( docSelection, model.markers, writer );
  1895. } );
  1896. // Stringify view and check if it is same as expected.
  1897. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1898. .to.equal( '<div>f<span class="marker">o<strong>o</strong>[]<strong>b</strong>a</span>r</div>' );
  1899. } );
  1900. it( 'in marker - using highlight descriptor creator', () => {
  1901. downcastHelpers.markerToHighlight( {
  1902. model: 'marker2',
  1903. view: data => ( { classes: data.markerName } )
  1904. } );
  1905. setModelData( model, 'foobar' );
  1906. model.change( writer => {
  1907. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1908. marker = writer.addMarker( 'marker2', { range, usingOperation: false } );
  1909. writer.setSelection( modelRoot, 3 );
  1910. } );
  1911. // Remove view children manually (without firing additional conversion).
  1912. viewRoot._removeChildren( 0, viewRoot.childCount );
  1913. // Convert model to view.
  1914. view.change( writer => {
  1915. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1916. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1917. dispatcher.convertSelection( docSelection, model.markers, writer );
  1918. } );
  1919. // Stringify view and check if it is same as expected.
  1920. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1921. .to.equal( '<div>f<span class="marker2">oo{}ba</span>r</div>' );
  1922. } );
  1923. it( 'should do nothing if creator return null', () => {
  1924. downcastHelpers.markerToHighlight( {
  1925. model: 'marker3',
  1926. view: () => null
  1927. } );
  1928. setModelData( model, 'foobar' );
  1929. model.change( writer => {
  1930. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  1931. marker = writer.addMarker( 'marker3', { range, usingOperation: false } );
  1932. writer.setSelection( modelRoot, 3 );
  1933. } );
  1934. // Remove view children manually (without firing additional conversion).
  1935. viewRoot._removeChildren( 0, viewRoot.childCount );
  1936. // Convert model to view.
  1937. view.change( writer => {
  1938. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1939. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  1940. dispatcher.convertSelection( docSelection, model.markers, writer );
  1941. } );
  1942. // Stringify view and check if it is same as expected.
  1943. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1944. .to.equal( '<div>foo{}bar</div>' );
  1945. } );
  1946. // #1072 - if the container has only ui elements, collapsed selection attribute should be rendered after those ui elements.
  1947. it( 'selection with attribute before ui element - no non-ui children', () => {
  1948. setModelData( model, '' );
  1949. // Add two ui elements to view.
  1950. viewRoot._appendChild( [
  1951. new ViewUIElement( viewDocument, 'span' ),
  1952. new ViewUIElement( viewDocument, 'span' )
  1953. ] );
  1954. model.change( writer => {
  1955. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 0 ] ) ) );
  1956. writer.setSelectionAttribute( 'bold', true );
  1957. } );
  1958. // Convert model to view.
  1959. view.change( writer => {
  1960. dispatcher.convertSelection( docSelection, model.markers, writer );
  1961. } );
  1962. // Stringify view and check if it is same as expected.
  1963. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1964. .to.equal( '<div><span></span><span></span><strong>[]</strong></div>' );
  1965. } );
  1966. // #1072.
  1967. it( 'selection with attribute before ui element - has non-ui children #1', () => {
  1968. setModelData( model, 'x' );
  1969. model.change( writer => {
  1970. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  1971. writer.setSelectionAttribute( 'bold', true );
  1972. } );
  1973. // Convert model to view.
  1974. view.change( writer => {
  1975. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1976. // Add ui element to view.
  1977. const uiElement = new ViewUIElement( viewDocument, 'span' );
  1978. viewRoot._insertChild( 1, uiElement );
  1979. dispatcher.convertSelection( docSelection, model.markers, writer );
  1980. } );
  1981. // Stringify view and check if it is same as expected.
  1982. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  1983. .to.equal( '<div>x<strong>[]</strong><span></span></div>' );
  1984. } );
  1985. // #1072.
  1986. it( 'selection with attribute before ui element - has non-ui children #2', () => {
  1987. setModelData( model, '<$text bold="true">x</$text>y' );
  1988. model.change( writer => {
  1989. writer.setSelection( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  1990. writer.setSelectionAttribute( 'bold', true );
  1991. } );
  1992. // Convert model to view.
  1993. view.change( writer => {
  1994. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  1995. // Add ui element to view.
  1996. const uiElement = new ViewUIElement( viewDocument, 'span' );
  1997. viewRoot._insertChild( 1, uiElement, writer );
  1998. dispatcher.convertSelection( docSelection, model.markers, writer );
  1999. } );
  2000. // Stringify view and check if it is same as expected.
  2001. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  2002. .to.equal( '<div><strong>x{}</strong><span></span>y</div>' );
  2003. } );
  2004. it( 'consumes consumable values properly', () => {
  2005. // Add callbacks that will fire before default ones.
  2006. // This should prevent default callbacks doing anything.
  2007. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  2008. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  2009. }, { priority: 'high' } );
  2010. dispatcher.on( 'attribute:bold', ( evt, data, conversionApi ) => {
  2011. expect( conversionApi.consumable.consume( data.item, 'attribute:bold' ) ).to.be.true;
  2012. }, { priority: 'high' } );
  2013. // Similar test case as above.
  2014. testSelection(
  2015. [ 3, 3 ],
  2016. 'f<$text bold="true">ooba</$text>r',
  2017. 'foobar' // No selection in view and no attribute.
  2018. );
  2019. } );
  2020. } );
  2021. } );
  2022. describe( 'clean-up', () => {
  2023. describe( 'convertRangeSelection', () => {
  2024. it( 'should remove all ranges before adding new range', () => {
  2025. testSelection(
  2026. [ 0, 2 ],
  2027. 'foobar',
  2028. '{fo}obar'
  2029. );
  2030. testSelection(
  2031. [ 3, 5 ],
  2032. 'foobar',
  2033. 'foo{ba}r'
  2034. );
  2035. expect( viewSelection.rangeCount ).to.equal( 1 );
  2036. } );
  2037. } );
  2038. describe( 'convertCollapsedSelection', () => {
  2039. it( 'should remove all ranges before adding new range', () => {
  2040. testSelection(
  2041. [ 2, 2 ],
  2042. 'foobar',
  2043. 'fo{}obar'
  2044. );
  2045. testSelection(
  2046. [ 3, 3 ],
  2047. 'foobar',
  2048. 'foo{}bar'
  2049. );
  2050. expect( viewSelection.rangeCount ).to.equal( 1 );
  2051. } );
  2052. } );
  2053. describe( 'clearAttributes', () => {
  2054. it( 'should remove all ranges before adding new range', () => {
  2055. testSelection(
  2056. [ 3, 3 ],
  2057. 'foobar',
  2058. 'foo<strong>[]</strong>bar',
  2059. { bold: 'true' }
  2060. );
  2061. view.change( writer => {
  2062. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  2063. model.change( writer => {
  2064. writer.setSelection( modelRange );
  2065. } );
  2066. dispatcher.convertSelection( modelDoc.selection, model.markers, writer );
  2067. } );
  2068. expect( viewSelection.rangeCount ).to.equal( 1 );
  2069. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  2070. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  2071. } );
  2072. it( 'should do nothing if the attribute element had been already removed', () => {
  2073. testSelection(
  2074. [ 3, 3 ],
  2075. 'foobar',
  2076. 'foo<strong>[]</strong>bar',
  2077. { bold: 'true' }
  2078. );
  2079. view.change( writer => {
  2080. // Remove <strong></strong> manually.
  2081. writer.mergeAttributes( viewSelection.getFirstPosition() );
  2082. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  2083. model.change( writer => {
  2084. writer.setSelection( modelRange );
  2085. } );
  2086. dispatcher.convertSelection( modelDoc.selection, model.markers, writer );
  2087. } );
  2088. expect( viewSelection.rangeCount ).to.equal( 1 );
  2089. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  2090. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  2091. } );
  2092. it( 'should clear fake selection', () => {
  2093. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  2094. view.change( writer => {
  2095. writer.setSelection( modelRange, { fake: true } );
  2096. dispatcher.convertSelection( docSelection, model.markers, writer );
  2097. } );
  2098. expect( viewSelection.isFake ).to.be.false;
  2099. } );
  2100. } );
  2101. } );
  2102. describe( 'table cell selection converter', () => {
  2103. beforeEach( () => {
  2104. model.schema.register( 'table', { isLimit: true } );
  2105. model.schema.register( 'tr', { isLimit: true } );
  2106. model.schema.register( 'td', { isLimit: true } );
  2107. model.schema.extend( 'table', { allowIn: '$root' } );
  2108. model.schema.extend( 'tr', { allowIn: 'table' } );
  2109. model.schema.extend( 'td', { allowIn: 'tr' } );
  2110. model.schema.extend( '$text', { allowIn: 'td' } );
  2111. const downcastHelpers = new DowncastHelpers( [ dispatcher ] );
  2112. // "Universal" converter to convert table structure.
  2113. downcastHelpers.elementToElement( { model: 'table', view: 'table' } );
  2114. downcastHelpers.elementToElement( { model: 'tr', view: 'tr' } );
  2115. downcastHelpers.elementToElement( { model: 'td', view: 'td' } );
  2116. // Special converter for table cells.
  2117. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  2118. const selection = data.selection;
  2119. if ( !conversionApi.consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  2120. return;
  2121. }
  2122. for ( const range of selection.getRanges() ) {
  2123. const node = range.start.parent;
  2124. if ( !!node && node.is( 'element', 'td' ) ) {
  2125. conversionApi.consumable.consume( selection, 'selection' );
  2126. const viewNode = conversionApi.mapper.toViewElement( node );
  2127. conversionApi.writer.addClass( 'selected', viewNode );
  2128. }
  2129. }
  2130. }, { priority: 'high' } );
  2131. } );
  2132. it( 'should not be used to convert selection that is not on table cell', () => {
  2133. testSelection(
  2134. [ 1, 5 ],
  2135. 'f{o<$text bold="true">ob</$text>a}r',
  2136. 'f{o<strong>ob</strong>a}r'
  2137. );
  2138. } );
  2139. it( 'should add a class to the selected table cell', () => {
  2140. testSelection(
  2141. // table tr#0 td#0 [foo, table tr#0 td#0 bar]
  2142. [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 3 ] ],
  2143. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  2144. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  2145. );
  2146. } );
  2147. it( 'should not be used if selection contains more than just a table cell', () => {
  2148. testSelection(
  2149. // table tr td#1 f{oo bar, table tr#2 bar]
  2150. [ [ 0, 0, 0, 1 ], [ 0, 0, 1, 3 ] ],
  2151. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  2152. '[<table><tr><td>foo</td><td>bar</td></tr></table>]'
  2153. );
  2154. } );
  2155. } );
  2156. // Tests if the selection got correctly converted.
  2157. // Because `setData` might use selection converters itself to set the selection, we can't use it
  2158. // to set the selection (because then we would test converters using converters).
  2159. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  2160. // that are offsets or paths of selection positions in root element.
  2161. function testSelection( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  2162. // Parse passed `modelInput` string and set it as current model.
  2163. setModelData( model, modelInput );
  2164. // Manually set selection ranges using passed `selectionPaths`.
  2165. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  2166. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  2167. const startPos = model.createPositionFromPath( modelRoot, startPath );
  2168. const endPos = model.createPositionFromPath( modelRoot, endPath );
  2169. const isBackward = selectionPaths[ 2 ] === 'backward';
  2170. model.change( writer => {
  2171. writer.setSelection( writer.createRange( startPos, endPos ), { backward: isBackward } );
  2172. // And add or remove passed attributes.
  2173. for ( const key in selectionAttributes ) {
  2174. const value = selectionAttributes[ key ];
  2175. if ( value ) {
  2176. writer.setSelectionAttribute( key, value );
  2177. } else {
  2178. writer.removeSelectionAttribute( key );
  2179. }
  2180. }
  2181. } );
  2182. // Remove view children manually (without firing additional conversion).
  2183. viewRoot._removeChildren( 0, viewRoot.childCount );
  2184. // Convert model to view.
  2185. view.change( writer => {
  2186. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  2187. dispatcher.convertSelection( docSelection, model.markers, writer );
  2188. } );
  2189. // Stringify view and check if it is same as expected.
  2190. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  2191. }
  2192. } );
  2193. function viewToString( item ) {
  2194. let result = '';
  2195. if ( item instanceof ViewText ) {
  2196. result = item.data;
  2197. } else {
  2198. // ViewElement or ViewDocumentFragment.
  2199. for ( const child of item.getChildren() ) {
  2200. result += viewToString( child );
  2201. }
  2202. if ( item instanceof ViewElement ) {
  2203. result = '<' + item.name + viewAttributesToString( item ) + '>' + result + '</' + item.name + '>';
  2204. }
  2205. }
  2206. return result;
  2207. }
  2208. function viewAttributesToString( item ) {
  2209. let result = '';
  2210. for ( const key of item.getAttributeKeys() ) {
  2211. const value = item.getAttribute( key );
  2212. if ( value ) {
  2213. result += ' ' + key + '="' + value + '"';
  2214. }
  2215. }
  2216. return result;
  2217. }