downcasthelpers.js 73 KB

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