upcasthelpers.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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. import UpcastDispatcher from '../../src/conversion/upcastdispatcher';
  6. import ViewContainerElement from '../../src/view/containerelement';
  7. import ViewDocumentFragment from '../../src/view/documentfragment';
  8. import ViewText from '../../src/view/text';
  9. import ViewUIElement from '../../src/view/uielement';
  10. import ViewAttributeElement from '../../src/view/attributeelement';
  11. import ViewDocument from '../../src/view/document';
  12. import Model from '../../src/model/model';
  13. import ModelDocumentFragment from '../../src/model/documentfragment';
  14. import ModelElement from '../../src/model/element';
  15. import ModelText from '../../src/model/text';
  16. import ModelRange from '../../src/model/range';
  17. import ModelPosition from '../../src/model/position';
  18. import UpcastHelpers, { convertToModelFragment, convertText, convertSelectionChange } from '../../src/conversion/upcasthelpers';
  19. import { getData as modelGetData, setData as modelSetData, stringify } from '../../src/dev-utils/model';
  20. import View from '../../src/view/view';
  21. import createViewRoot from '../view/_utils/createroot';
  22. import { setData as viewSetData, parse as viewParse } from '../../src/dev-utils/view';
  23. import Mapper from '../../src/conversion/mapper';
  24. import ViewSelection from '../../src/view/selection';
  25. import ViewRange from '../../src/view/range';
  26. import { StylesProcessor } from '../../src/view/stylesmap';
  27. import Writer from '../../src/model/writer';
  28. /* globals console */
  29. describe( 'UpcastHelpers', () => {
  30. let upcastDispatcher, model, schema, upcastHelpers, viewDocument;
  31. beforeEach( () => {
  32. model = new Model();
  33. viewDocument = new ViewDocument( new StylesProcessor() );
  34. schema = model.schema;
  35. schema.extend( '$text', {
  36. allowIn: '$root',
  37. allowAttributes: [ 'bold', 'attribA', 'attribB' ]
  38. } );
  39. schema.register( 'paragraph', {
  40. inheritAllFrom: '$block'
  41. } );
  42. upcastDispatcher = new UpcastDispatcher( { schema } );
  43. upcastDispatcher.on( 'text', convertText() );
  44. upcastDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  45. upcastDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
  46. upcastHelpers = new UpcastHelpers( [ upcastDispatcher ] );
  47. } );
  48. describe( 'elementToElement()', () => {
  49. it( 'should be chainable', () => {
  50. expect( upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } ) ).to.equal( upcastHelpers );
  51. } );
  52. it( 'config.view is a string', () => {
  53. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  54. expectResult( new ViewContainerElement( viewDocument, 'p' ), '<paragraph></paragraph>' );
  55. } );
  56. it( 'can be overwritten using converterPriority', () => {
  57. schema.register( 'p', {
  58. inheritAllFrom: '$block'
  59. } );
  60. upcastHelpers.elementToElement( { view: 'p', model: 'p' } );
  61. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph', converterPriority: 'high' } );
  62. expectResult( new ViewContainerElement( viewDocument, 'p' ), '<paragraph></paragraph>' );
  63. } );
  64. it( 'config.view is an object', () => {
  65. schema.register( 'fancyParagraph', {
  66. inheritAllFrom: '$block'
  67. } );
  68. upcastHelpers.elementToElement( {
  69. view: {
  70. name: 'p',
  71. classes: 'fancy'
  72. },
  73. model: 'fancyParagraph'
  74. } );
  75. expectResult( new ViewContainerElement( viewDocument, 'p', { class: 'fancy' } ), '<fancyParagraph></fancyParagraph>' );
  76. expectResult( new ViewContainerElement( viewDocument, 'p' ), '' );
  77. } );
  78. it( 'config.model is a function', () => {
  79. schema.register( 'heading', {
  80. inheritAllFrom: '$block',
  81. allowAttributes: [ 'level' ]
  82. } );
  83. upcastHelpers.elementToElement( {
  84. view: {
  85. name: 'p',
  86. classes: 'heading'
  87. },
  88. model: ( viewElement, { writer } ) => {
  89. return writer.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
  90. }
  91. } );
  92. expectResult(
  93. new ViewContainerElement( viewDocument, 'p', { class: 'heading', 'data-level': 2 } ),
  94. '<heading level="2"></heading>'
  95. );
  96. expectResult( new ViewContainerElement( viewDocument, 'p', { 'data-level': 2 } ), '' );
  97. } );
  98. it( 'config.view is not set - should fire conversion for every element', () => {
  99. upcastHelpers.elementToElement( {
  100. model: 'paragraph',
  101. view: /.+/
  102. } );
  103. expectResult( new ViewContainerElement( viewDocument, 'p' ), '<paragraph></paragraph>' );
  104. expectResult( new ViewContainerElement( viewDocument, 'foo' ), '<paragraph></paragraph>' );
  105. } );
  106. it( 'should fire conversion of the element children', () => {
  107. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  108. expectResult(
  109. new ViewContainerElement( viewDocument, 'p', null, new ViewText( viewDocument, 'foo' ) ),
  110. '<paragraph>foo</paragraph>'
  111. );
  112. } );
  113. it( 'should not insert a model element if it is not allowed by schema', () => {
  114. upcastHelpers.elementToElement( { view: 'h2', model: 'heading' } );
  115. expectResult( new ViewContainerElement( viewDocument, 'h2' ), '' );
  116. } );
  117. it( 'should auto-break elements', () => {
  118. schema.register( 'heading', {
  119. inheritAllFrom: '$block'
  120. } );
  121. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  122. upcastHelpers.elementToElement( { view: 'h2', model: 'heading' } );
  123. expectResult(
  124. new ViewContainerElement( viewDocument, 'p', null, [
  125. new ViewText( viewDocument, 'Foo' ),
  126. new ViewContainerElement( viewDocument, 'h2', null, new ViewText( viewDocument, 'Xyz' ) ),
  127. new ViewText( viewDocument, 'Bar' )
  128. ] ),
  129. '<paragraph>Foo</paragraph><heading>Xyz</heading><paragraph>Bar</paragraph>'
  130. );
  131. } );
  132. it( 'should not do anything if returned model element is null', () => {
  133. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  134. upcastHelpers.elementToElement( { view: 'p', model: () => null, converterPriority: 'high' } );
  135. expectResult( new ViewContainerElement( viewDocument, 'p' ), '<paragraph></paragraph>' );
  136. } );
  137. } );
  138. describe( 'elementToAttribute()', () => {
  139. it( 'should be chainable', () => {
  140. expect( upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } ) ).to.equal( upcastHelpers );
  141. } );
  142. it( 'config.view is string', () => {
  143. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } );
  144. expectResult(
  145. new ViewAttributeElement( viewDocument, 'strong', null, new ViewText( viewDocument, 'foo' ) ),
  146. '<$text bold="true">foo</$text>'
  147. );
  148. } );
  149. it( 'can be overwritten using converterPriority', () => {
  150. upcastHelpers.elementToAttribute( { view: 'strong', model: 'strong' } );
  151. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold', converterPriority: 'high' } );
  152. expectResult(
  153. new ViewAttributeElement( viewDocument, 'strong', null, new ViewText( viewDocument, 'foo' ) ),
  154. '<$text bold="true">foo</$text>'
  155. );
  156. } );
  157. it( 'config.view is an object', () => {
  158. upcastHelpers.elementToAttribute( {
  159. view: {
  160. name: 'span',
  161. classes: 'bold'
  162. },
  163. model: 'bold'
  164. } );
  165. expectResult(
  166. new ViewAttributeElement( viewDocument, 'span', { class: 'bold' }, new ViewText( viewDocument, 'foo' ) ),
  167. '<$text bold="true">foo</$text>'
  168. );
  169. expectResult( new ViewAttributeElement( viewDocument, 'span', {}, new ViewText( viewDocument, 'foo' ) ), 'foo' );
  170. } );
  171. it( 'model attribute value is given', () => {
  172. schema.extend( '$text', {
  173. allowAttributes: [ 'styled' ]
  174. } );
  175. upcastHelpers.elementToAttribute( {
  176. view: {
  177. name: 'span',
  178. classes: [ 'styled', 'styled-dark' ]
  179. },
  180. model: {
  181. key: 'styled',
  182. value: 'dark'
  183. }
  184. } );
  185. expectResult(
  186. new ViewAttributeElement( viewDocument, 'span', { class: 'styled styled-dark' }, new ViewText( viewDocument, 'foo' ) ),
  187. '<$text styled="dark">foo</$text>'
  188. );
  189. expectResult( new ViewAttributeElement( viewDocument, 'span', {}, new ViewText( viewDocument, 'foo' ) ), 'foo' );
  190. } );
  191. it( 'model attribute value is a function', () => {
  192. schema.extend( '$text', {
  193. allowAttributes: [ 'fontSize' ]
  194. } );
  195. upcastHelpers.elementToAttribute( {
  196. view: {
  197. name: 'span',
  198. styles: {
  199. 'font-size': /[\s\S]+/
  200. }
  201. },
  202. model: {
  203. key: 'fontSize',
  204. value: ( viewElement, conversionApi ) => {
  205. const fontSize = viewElement.getStyle( 'font-size' );
  206. const value = fontSize.substr( 0, fontSize.length - 2 );
  207. // To ensure conversion API is provided.
  208. expect( conversionApi.writer ).to.instanceof( Writer );
  209. if ( value <= 10 ) {
  210. return 'small';
  211. } else if ( value > 12 ) {
  212. return 'big';
  213. }
  214. return null;
  215. }
  216. }
  217. } );
  218. expectResult(
  219. new ViewAttributeElement( viewDocument, 'span', { style: 'font-size:9px' }, new ViewText( viewDocument, 'foo' ) ),
  220. '<$text fontSize="small">foo</$text>'
  221. );
  222. expectResult(
  223. new ViewAttributeElement( viewDocument, 'span', { style: 'font-size:12px' }, new ViewText( viewDocument, 'foo' ) ),
  224. 'foo'
  225. );
  226. expectResult(
  227. new ViewAttributeElement( viewDocument, 'span', { style: 'font-size:14px' }, new ViewText( viewDocument, 'foo' ) ),
  228. '<$text fontSize="big">foo</$text>'
  229. );
  230. } );
  231. it( 'should not set an attribute if it is not allowed by schema', () => {
  232. upcastHelpers.elementToAttribute( { view: 'em', model: 'italic' } );
  233. expectResult(
  234. new ViewAttributeElement( viewDocument, 'em', null, new ViewText( viewDocument, 'foo' ) ),
  235. 'foo'
  236. );
  237. } );
  238. it( 'should not do anything if returned model attribute is null', () => {
  239. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } );
  240. upcastHelpers.elementToAttribute( {
  241. view: 'strong',
  242. model: {
  243. key: 'bold',
  244. value: () => null
  245. },
  246. converterPriority: 'high'
  247. } );
  248. expectResult(
  249. new ViewAttributeElement( viewDocument, 'strong', null, new ViewText( viewDocument, 'foo' ) ),
  250. '<$text bold="true">foo</$text>'
  251. );
  252. } );
  253. it( 'should allow two converters to convert attributes on the same element', () => {
  254. upcastHelpers.elementToAttribute( {
  255. model: 'attribA',
  256. view: { name: 'span', classes: 'attrib-a' }
  257. } );
  258. upcastHelpers.elementToAttribute( {
  259. model: 'attribB',
  260. view: { name: 'span', styles: { color: 'attrib-b' } }
  261. } );
  262. expectResult(
  263. new ViewAttributeElement(
  264. viewDocument,
  265. 'span',
  266. { class: 'attrib-a', style: 'color:attrib-b;' },
  267. new ViewText( viewDocument, 'foo' )
  268. ),
  269. '<$text attribA="true" attribB="true">foo</$text>'
  270. );
  271. } );
  272. it( 'should consume element only when only is name specified', () => {
  273. upcastHelpers.elementToAttribute( {
  274. model: 'bold',
  275. view: { name: 'strong' }
  276. } );
  277. upcastHelpers.elementToAttribute( {
  278. model: 'attribA',
  279. view: { name: 'strong' }
  280. } );
  281. upcastHelpers.elementToAttribute( {
  282. model: 'attribB',
  283. view: { name: 'strong', classes: 'foo' }
  284. } );
  285. expectResult(
  286. new ViewAttributeElement( viewDocument, 'strong', { class: 'foo' }, new ViewText( viewDocument, 'foo' ) ),
  287. '<$text attribB="true" bold="true">foo</$text>'
  288. );
  289. } );
  290. // #1443.
  291. it( 'should set attributes on the element\'s children', () => {
  292. upcastHelpers.elementToAttribute( {
  293. model: 'bold',
  294. view: { name: 'strong' }
  295. } );
  296. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  297. expectResult(
  298. new ViewAttributeElement(
  299. viewDocument,
  300. 'strong',
  301. null,
  302. new ViewContainerElement( viewDocument, 'p', null, new ViewText( viewDocument, 'Foo' ) )
  303. ),
  304. '<paragraph><$text bold="true">Foo</$text></paragraph>'
  305. );
  306. } );
  307. } );
  308. describe( 'attributeToAttribute()', () => {
  309. beforeEach( () => {
  310. upcastHelpers.elementToElement( { view: 'img', model: 'image' } );
  311. schema.register( 'image', {
  312. inheritAllFrom: '$block'
  313. } );
  314. } );
  315. it( 'should be chainable', () => {
  316. expect( upcastHelpers.attributeToAttribute( { view: 'src', model: 'source' } ) ).to.equal( upcastHelpers );
  317. } );
  318. it( 'config.view is a string', () => {
  319. schema.extend( 'image', {
  320. allowAttributes: [ 'source' ]
  321. } );
  322. upcastHelpers.attributeToAttribute( { view: 'src', model: 'source' } );
  323. expectResult(
  324. new ViewAttributeElement( viewDocument, 'img', { src: 'foo.jpg' } ),
  325. '<image source="foo.jpg"></image>'
  326. );
  327. } );
  328. it( 'config.view has only key set', () => {
  329. schema.extend( 'image', {
  330. allowAttributes: [ 'source' ]
  331. } );
  332. upcastHelpers.attributeToAttribute( { view: { key: 'src' }, model: 'source' } );
  333. expectResult(
  334. new ViewAttributeElement( viewDocument, 'img', { src: 'foo.jpg' } ),
  335. '<image source="foo.jpg"></image>'
  336. );
  337. } );
  338. it( 'config.view has only key and name set', () => {
  339. schema.extend( 'image', {
  340. allowAttributes: [ 'source' ]
  341. } );
  342. upcastHelpers.attributeToAttribute( { view: { name: 'img', key: 'src' }, model: { name: 'image', key: 'source' } } );
  343. expectResult(
  344. new ViewAttributeElement( viewDocument, 'img', { src: 'foo.jpg' } ),
  345. '<image source="foo.jpg"></image>'
  346. );
  347. } );
  348. it( 'can be overwritten using converterPriority', () => {
  349. schema.extend( 'image', {
  350. allowAttributes: [ 'src', 'source' ]
  351. } );
  352. upcastHelpers.attributeToAttribute( { view: { key: 'src' }, model: 'src' } );
  353. upcastHelpers.attributeToAttribute( { view: { key: 'src' }, model: 'source', converterPriority: 'normal' } );
  354. expectResult(
  355. new ViewAttributeElement( viewDocument, 'img', { src: 'foo.jpg' } ),
  356. '<image source="foo.jpg"></image>'
  357. );
  358. } );
  359. it( 'config.view has value set', () => {
  360. schema.extend( 'image', {
  361. allowAttributes: [ 'styled' ]
  362. } );
  363. upcastHelpers.attributeToAttribute( {
  364. view: {
  365. key: 'data-style',
  366. value: /[\s\S]*/
  367. },
  368. model: 'styled'
  369. } );
  370. expectResult(
  371. new ViewAttributeElement( viewDocument, 'img', { 'data-style': 'dark' } ),
  372. '<image styled="dark"></image>'
  373. );
  374. } );
  375. it( 'model attribute value is a string', () => {
  376. schema.extend( 'image', {
  377. allowAttributes: [ 'styled' ]
  378. } );
  379. upcastHelpers.attributeToAttribute( {
  380. view: {
  381. name: 'img',
  382. key: 'class',
  383. value: 'styled-dark'
  384. },
  385. model: {
  386. key: 'styled',
  387. value: 'dark'
  388. }
  389. } );
  390. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  391. expectResult(
  392. new ViewContainerElement( viewDocument, 'img', { class: 'styled-dark' } ),
  393. '<image styled="dark"></image>'
  394. );
  395. expectResult(
  396. new ViewContainerElement( viewDocument, 'img', { class: 'styled-xxx' } ),
  397. '<image></image>'
  398. );
  399. expectResult(
  400. new ViewContainerElement( viewDocument, 'p', { class: 'styled-dark' } ),
  401. '<paragraph></paragraph>'
  402. );
  403. } );
  404. it( 'model attribute value is a function', () => {
  405. schema.extend( 'image', {
  406. allowAttributes: [ 'styled' ]
  407. } );
  408. upcastHelpers.attributeToAttribute( {
  409. view: {
  410. key: 'class',
  411. value: /styled-[\S]+/
  412. },
  413. model: {
  414. key: 'styled',
  415. value: ( viewElement, conversionApi ) => {
  416. const regexp = /styled-([\S]+)/;
  417. const match = viewElement.getAttribute( 'class' ).match( regexp );
  418. // To ensure conversion API is provided.
  419. expect( conversionApi.writer ).to.instanceof( Writer );
  420. return match[ 1 ];
  421. }
  422. }
  423. } );
  424. expectResult(
  425. new ViewAttributeElement( viewDocument, 'img', { 'class': 'styled-dark' } ),
  426. '<image styled="dark"></image>'
  427. );
  428. } );
  429. it( 'should not set an attribute if it is not allowed by schema', () => {
  430. upcastHelpers.attributeToAttribute( { view: 'src', model: 'source' } );
  431. expectResult(
  432. new ViewAttributeElement( viewDocument, 'img', { src: 'foo.jpg' } ),
  433. '<image></image>'
  434. );
  435. } );
  436. it( 'should not do anything if returned model attribute is null', () => {
  437. schema.extend( 'image', {
  438. allowAttributes: [ 'styled' ]
  439. } );
  440. upcastHelpers.attributeToAttribute( {
  441. view: {
  442. key: 'class',
  443. value: 'styled'
  444. },
  445. model: {
  446. key: 'styled',
  447. value: true
  448. }
  449. } );
  450. upcastHelpers.attributeToAttribute( {
  451. view: {
  452. key: 'class',
  453. value: 'styled'
  454. },
  455. model: {
  456. key: 'styled',
  457. value: () => null
  458. }
  459. } );
  460. expectResult(
  461. new ViewAttributeElement( viewDocument, 'img', { class: 'styled' } ),
  462. '<image styled="true"></image>'
  463. );
  464. } );
  465. // #1443.
  466. it( 'should not set attributes on the element\'s children', () => {
  467. schema.register( 'div', {
  468. inheritAllFrom: '$root',
  469. allowWhere: '$block',
  470. isLimit: true,
  471. allowAttributes: [ 'border', 'shade' ]
  472. } );
  473. upcastHelpers.elementToElement( { view: 'div', model: 'div' } );
  474. upcastHelpers.attributeToAttribute( { view: { key: 'class', value: 'shade' }, model: 'shade' } );
  475. upcastHelpers.attributeToAttribute( { view: { key: 'class', value: 'border' }, model: 'border' } );
  476. expectResult(
  477. new ViewContainerElement(
  478. viewDocument,
  479. 'div',
  480. { class: 'border' },
  481. new ViewContainerElement( viewDocument, 'div', { class: 'shade' } )
  482. ),
  483. '<div border="border"><div shade="shade"></div></div>'
  484. );
  485. } );
  486. } );
  487. describe( 'elementToMarker()', () => {
  488. beforeEach( () => {
  489. // Silence warning about deprecated method.
  490. // This whole suite will be removed when the deprecated method is removed.
  491. sinon.stub( console, 'warn' );
  492. } );
  493. afterEach( () => {
  494. console.warn.restore();
  495. } );
  496. it( 'should be chainable', () => {
  497. expect( upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search' } ) ).to.equal( upcastHelpers );
  498. } );
  499. it( 'config.view is a string', () => {
  500. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search' } );
  501. const frag = new ViewDocumentFragment( viewDocument, [
  502. new ViewText( viewDocument, 'fo' ),
  503. new ViewUIElement( viewDocument, 'marker-search' ),
  504. new ViewText( viewDocument, 'oba' ),
  505. new ViewUIElement( viewDocument, 'marker-search' ),
  506. new ViewText( viewDocument, 'r' )
  507. ] );
  508. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  509. expectResult( frag, 'foobar', marker );
  510. } );
  511. it( 'can be overwritten using converterPriority', () => {
  512. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search-result' } );
  513. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search', converterPriority: 'high' } );
  514. const frag = new ViewDocumentFragment( viewDocument, [
  515. new ViewText( viewDocument, 'fo' ),
  516. new ViewUIElement( viewDocument, 'marker-search' ),
  517. new ViewText( viewDocument, 'oba' ),
  518. new ViewUIElement( viewDocument, 'marker-search' ),
  519. new ViewText( viewDocument, 'r' )
  520. ] );
  521. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  522. expectResult( frag, 'foobar', marker );
  523. } );
  524. it( 'config.view is an object', () => {
  525. upcastHelpers.elementToMarker( {
  526. view: {
  527. name: 'span',
  528. 'data-marker': 'search'
  529. },
  530. model: 'search'
  531. } );
  532. const frag = new ViewDocumentFragment( viewDocument, [
  533. new ViewText( viewDocument, 'f' ),
  534. new ViewUIElement( viewDocument, 'span', { 'data-marker': 'search' } ),
  535. new ViewText( viewDocument, 'oob' ),
  536. new ViewUIElement( viewDocument, 'span', { 'data-marker': 'search' } ),
  537. new ViewText( viewDocument, 'ar' )
  538. ] );
  539. const marker = { name: 'search', start: [ 1 ], end: [ 4 ] };
  540. expectResult( frag, 'foobar', marker );
  541. } );
  542. it( 'config.model is a function', () => {
  543. upcastHelpers.elementToMarker( {
  544. view: 'comment',
  545. model: ( viewElement, conversionApi ) => {
  546. // To ensure conversion API is provided.
  547. expect( conversionApi.writer ).to.instanceof( Writer );
  548. return 'comment:' + viewElement.getAttribute( 'data-comment-id' );
  549. }
  550. } );
  551. const frag = new ViewDocumentFragment( viewDocument, [
  552. new ViewText( viewDocument, 'foo' ),
  553. new ViewUIElement( viewDocument, 'comment', { 'data-comment-id': 4 } ),
  554. new ViewText( viewDocument, 'b' ),
  555. new ViewUIElement( viewDocument, 'comment', { 'data-comment-id': 4 } ),
  556. new ViewText( viewDocument, 'ar' )
  557. ] );
  558. const marker = { name: 'comment:4', start: [ 3 ], end: [ 4 ] };
  559. expectResult( frag, 'foobar', marker );
  560. } );
  561. it( 'marker is in a block element', () => {
  562. upcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  563. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search' } );
  564. const element = new ViewContainerElement( viewDocument, 'p', null, [
  565. new ViewText( viewDocument, 'fo' ),
  566. new ViewUIElement( viewDocument, 'marker-search' ),
  567. new ViewText( viewDocument, 'oba' ),
  568. new ViewUIElement( viewDocument, 'marker-search' ),
  569. new ViewText( viewDocument, 'r' )
  570. ] );
  571. const marker = { name: 'search', start: [ 0, 2 ], end: [ 0, 5 ] };
  572. expectResult( element, '<paragraph>foobar</paragraph>', marker );
  573. } );
  574. } );
  575. describe( 'dataToMarker()', () => {
  576. beforeEach( () => {
  577. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  578. } );
  579. it( 'should be chainable', () => {
  580. expect( upcastHelpers.dataToMarker( { view: 'search' } ) ).to.equal( upcastHelpers );
  581. } );
  582. it( 'default conversion, inside text, non-collapsed, no name', () => {
  583. upcastHelpers.dataToMarker( { view: 'search' } );
  584. expectResult(
  585. viewParse( '<p>Fo<search-start></search-start>ob<search-end></search-end>ar</p>' ),
  586. '<paragraph>Foobar</paragraph>',
  587. { name: 'search', start: [ 0, 2 ], end: [ 0, 4 ] }
  588. );
  589. } );
  590. it( 'default conversion, inside text, non-collapsed, name', () => {
  591. upcastHelpers.dataToMarker( { view: 'group' } );
  592. expectResult(
  593. viewParse( '<p>Fo<group-start name="foo:bar:baz"></group-start>ob<group-end name="foo:bar:baz"></group-end>ar</p>' ),
  594. '<paragraph>Foobar</paragraph>',
  595. { name: 'group:foo:bar:baz', start: [ 0, 2 ], end: [ 0, 4 ] }
  596. );
  597. } );
  598. it( 'default conversion, inside text, collapsed, no name', () => {
  599. upcastHelpers.dataToMarker( { view: 'search' } );
  600. expectResult(
  601. viewParse( '<p>Foo<search-start></search-start><search-end></search-end>bar</p>' ),
  602. '<paragraph>Foobar</paragraph>',
  603. { name: 'search', start: [ 0, 3 ], end: [ 0, 3 ] }
  604. );
  605. } );
  606. it( 'default conversion, inside text, collapsed, multiple markers, no name', () => {
  607. upcastHelpers.dataToMarker( { view: 'group' } );
  608. expectResult(
  609. viewParse(
  610. '<p>' +
  611. 'Foo' +
  612. '<group-start name="abc"></group-start><group-end name="abc"></group-end>' +
  613. '<group-start name="foo"></group-start><group-end name="foo"></group-end>' +
  614. 'bar' +
  615. '</p>'
  616. ),
  617. '<paragraph>Foobar</paragraph>',
  618. [
  619. { name: 'group:abc', start: [ 0, 3 ], end: [ 0, 3 ] },
  620. { name: 'group:foo', start: [ 0, 3 ], end: [ 0, 3 ] }
  621. ]
  622. );
  623. } );
  624. it( 'default conversion, on two elements, no name', () => {
  625. upcastHelpers.dataToMarker( { view: 'search' } );
  626. expectResult(
  627. viewParse( '<p data-search-start-before="">Foo</p><p data-search-end-after="">Bar</p>' ),
  628. '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>',
  629. { name: 'search', start: [ 0 ], end: [ 2 ] }
  630. );
  631. } );
  632. it( 'default conversion, on two elements, name', () => {
  633. upcastHelpers.dataToMarker( { view: 'group' } );
  634. expectResult(
  635. viewParse( '<p data-group-start-before="foo:bar:baz">Foo</p><p data-group-end-after="foo:bar:baz">Bar</p>' ),
  636. '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>',
  637. { name: 'group:foo:bar:baz', start: [ 0 ], end: [ 2 ] }
  638. );
  639. } );
  640. it( 'default conversion, on one element, name', () => {
  641. upcastHelpers.dataToMarker( { view: 'group' } );
  642. expectResult(
  643. viewParse( '<p data-group-end-after="foo:bar:baz" data-group-start-before="foo:bar:baz">Foobar</p>' ),
  644. '<paragraph>Foobar</paragraph>',
  645. { name: 'group:foo:bar:baz', start: [ 0 ], end: [ 1 ] }
  646. );
  647. } );
  648. it( 'default conversion, collapsed before element, name', () => {
  649. upcastHelpers.dataToMarker( { view: 'group' } );
  650. expectResult(
  651. viewParse( '<p data-group-end-before="foo:bar:baz" data-group-start-before="foo:bar:baz">Foobar</p>' ),
  652. '<paragraph>Foobar</paragraph>',
  653. { name: 'group:foo:bar:baz', start: [ 0 ], end: [ 0 ] }
  654. );
  655. } );
  656. it( 'default conversion, collapsed after element, name', () => {
  657. upcastHelpers.dataToMarker( { view: 'group' } );
  658. expectResult(
  659. viewParse( '<p data-group-end-after="foo:bar:baz" data-group-start-after="foo:bar:baz">Foobar</p>' ),
  660. '<paragraph>Foobar</paragraph>',
  661. { name: 'group:foo:bar:baz', start: [ 1 ], end: [ 1 ] }
  662. );
  663. } );
  664. it( 'default conversion, mixed, multiple markers, name', () => {
  665. upcastHelpers.dataToMarker( { view: 'group' } );
  666. expectResult(
  667. viewParse(
  668. '<p data-group-start-before="abc:xyz,foo:bar">Foo</p>' +
  669. '<p>Ba<group-end name="abc:xyz"></group-end><group-end name="foo:bar"></group-end>r</p>'
  670. ),
  671. '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>',
  672. [
  673. { name: 'group:foo:bar', start: [ 0 ], end: [ 1, 2 ] },
  674. { name: 'group:abc:xyz', start: [ 0 ], end: [ 1, 2 ] }
  675. ]
  676. );
  677. } );
  678. it( 'default conversion, mixed #2, multiple markers, name', () => {
  679. upcastHelpers.dataToMarker( { view: 'group' } );
  680. expectResult(
  681. viewParse(
  682. '<p>F<group-start name="abc:xyz"></group-start><group-start name="foo:bar"></group-start>oo</p>' +
  683. '<p data-group-end-after="abc:xyz,foo:bar">Bar</p>'
  684. ),
  685. '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>',
  686. [
  687. { name: 'group:foo:bar', start: [ 0, 1 ], end: [ 2 ] },
  688. { name: 'group:abc:xyz', start: [ 0, 1 ], end: [ 2 ] }
  689. ]
  690. );
  691. } );
  692. it( 'conversion callback, mixed, multiple markers, name', () => {
  693. upcastHelpers.dataToMarker( {
  694. view: 'g',
  695. model: ( name, conversionApi ) => {
  696. // To ensure conversion API is provided.
  697. expect( conversionApi.writer ).to.instanceof( Writer );
  698. return 'group:' + name.split( '_' )[ 0 ];
  699. } } );
  700. expectResult(
  701. viewParse(
  702. '<p data-g-start-before="abc_xyz,foo_bar">Foo</p>' +
  703. '<p>Ba<g-end name="abc_xyz"></g-end><g-end name="foo_bar"></g-end>r</p>'
  704. ),
  705. '<paragraph>Foo</paragraph><paragraph>Bar</paragraph>',
  706. [
  707. { name: 'group:foo', start: [ 0 ], end: [ 1, 2 ] },
  708. { name: 'group:abc', start: [ 0 ], end: [ 1, 2 ] }
  709. ]
  710. );
  711. } );
  712. it( 'can be overwritten using converterPriority', () => {
  713. upcastHelpers.dataToMarker( { view: 'group' } );
  714. upcastHelpers.dataToMarker( { view: 'group', model: name => 'g:' + name, converterPriority: 'high' } );
  715. expectResult(
  716. viewParse( '<p>Foo<group-start name="foo"></group-start><group-end name="foo"></group-end>bar</p>' ),
  717. '<paragraph>Foobar</paragraph>',
  718. { name: 'g:foo', start: [ 0, 3 ], end: [ 0, 3 ] }
  719. );
  720. } );
  721. it( 'should convert children if the view element has not been converted yet', () => {
  722. upcastHelpers.dataToMarker( { view: 'group' } );
  723. expectResult(
  724. viewParse( '<div data-group-end-after="foo" data-group-start-before="foo"><p>Foo</p></div>' ),
  725. '<paragraph>Foo</paragraph>',
  726. [
  727. { name: 'group:foo', start: [ 0 ], end: [ 1 ] }
  728. ]
  729. );
  730. } );
  731. } );
  732. function expectResult( viewToConvert, modelString, markers ) {
  733. const conversionResult = model.change( writer => upcastDispatcher.convert( viewToConvert, writer ) );
  734. if ( markers ) {
  735. markers = Array.isArray( markers ) ? markers : [ markers ];
  736. for ( const marker of markers ) {
  737. expect( conversionResult.markers.has( marker.name ) ).to.be.true;
  738. const convertedMarker = conversionResult.markers.get( marker.name );
  739. expect( convertedMarker.start.path ).to.deep.equal( marker.start );
  740. expect( convertedMarker.end.path ).to.deep.equal( marker.end );
  741. }
  742. }
  743. expect( stringify( conversionResult ) ).to.equal( modelString );
  744. }
  745. } );
  746. describe( 'upcast-converters', () => {
  747. let dispatcher, schema, context, model, viewDocument;
  748. beforeEach( () => {
  749. model = new Model();
  750. viewDocument = new ViewDocument( new StylesProcessor() );
  751. schema = model.schema;
  752. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  753. schema.extend( '$text', { allowIn: '$root' } );
  754. context = [ '$root' ];
  755. dispatcher = new UpcastDispatcher( { schema } );
  756. } );
  757. describe( 'convertText()', () => {
  758. it( 'should return converter converting ViewText to ModelText', () => {
  759. const viewText = new ViewText( viewDocument, 'foobar' );
  760. dispatcher.on( 'text', convertText() );
  761. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer ) );
  762. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  763. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  764. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  765. } );
  766. it( 'should not convert already consumed texts', () => {
  767. const viewText = new ViewText( viewDocument, 'foofuckbafuckr' );
  768. // Default converter for elements. Returns just converted children. Added with lowest priority.
  769. dispatcher.on( 'text', convertText(), { priority: 'lowest' } );
  770. // Added with normal priority. Should make the above converter not fire.
  771. dispatcher.on( 'text', ( evt, data, conversionApi ) => {
  772. if ( conversionApi.consumable.consume( data.viewItem ) ) {
  773. const text = conversionApi.writer.createText( data.viewItem.data.replace( /fuck/gi, '****' ) );
  774. conversionApi.writer.insert( text, data.modelCursor );
  775. data.modelRange = ModelRange._createFromPositionAndShift( data.modelCursor, text.offsetSize );
  776. data.modelCursor = data.modelRange.end;
  777. }
  778. } );
  779. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  780. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  781. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  782. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foo****ba****r' );
  783. } );
  784. it( 'should not convert text if it is wrong with schema', () => {
  785. schema.addChildCheck( ( ctx, childDef ) => {
  786. if ( ( childDef.name == '$text' || childDef.name == 'paragraph' ) && ctx.endsWith( '$root' ) ) {
  787. return false;
  788. }
  789. } );
  790. const viewText = new ViewText( viewDocument, 'foobar' );
  791. dispatcher.on( 'text', convertText() );
  792. let conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  793. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  794. expect( conversionResult.childCount ).to.equal( 0 );
  795. conversionResult = model.change( writer => dispatcher.convert( viewText, writer, [ '$block' ] ) );
  796. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  797. expect( conversionResult.childCount ).to.equal( 1 );
  798. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  799. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  800. } );
  801. it( 'should auto-paragraph a text if it is not allowed at the insertion position but would be inserted if auto-paragraphed', () => {
  802. schema.addChildCheck( ( ctx, childDef ) => {
  803. if ( childDef.name == '$text' && ctx.endsWith( '$root' ) ) {
  804. return false;
  805. }
  806. } );
  807. const viewText = new ViewText( viewDocument, 'foobar' );
  808. dispatcher.on( 'text', convertText() );
  809. let conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  810. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  811. expect( conversionResult.childCount ).to.equal( 1 );
  812. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  813. expect( conversionResult.getNodeByPath( [ 0, 0 ] ) ).to.be.instanceof( ModelText );
  814. expect( conversionResult.getNodeByPath( [ 0, 0 ] ).data ).to.equal( 'foobar' );
  815. conversionResult = model.change( writer => dispatcher.convert( viewText, writer, [ '$block' ] ) );
  816. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  817. expect( conversionResult.childCount ).to.equal( 1 );
  818. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  819. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  820. } );
  821. it( 'should support unicode', () => {
  822. const viewText = new ViewText( viewDocument, 'நிலைக்கு' );
  823. dispatcher.on( 'text', convertText() );
  824. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  825. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  826. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  827. expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
  828. } );
  829. } );
  830. describe( 'convertToModelFragment()', () => {
  831. it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
  832. const viewFragment = new ViewDocumentFragment( viewDocument, [
  833. new ViewContainerElement( viewDocument, 'p', null, new ViewText( viewDocument, 'foo' ) ),
  834. new ViewText( viewDocument, 'bar' )
  835. ] );
  836. // To get any meaningful results we have to actually convert something.
  837. dispatcher.on( 'text', convertText() );
  838. // This way P element won't be converted per-se but will fire converting it's children.
  839. dispatcher.on( 'element', convertToModelFragment() );
  840. dispatcher.on( 'documentFragment', convertToModelFragment() );
  841. const conversionResult = model.change( writer => dispatcher.convert( viewFragment, writer, context ) );
  842. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  843. expect( conversionResult.maxOffset ).to.equal( 6 );
  844. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  845. } );
  846. it( 'should not convert already consumed (converted) changes', () => {
  847. const viewP = new ViewContainerElement( viewDocument, 'p', null, new ViewText( viewDocument, 'foo' ) );
  848. // To get any meaningful results we have to actually convert something.
  849. dispatcher.on( 'text', convertText() );
  850. // Default converter for elements. Returns just converted children. Added with lowest priority.
  851. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  852. // Added with normal priority. Should make the above converter not fire.
  853. dispatcher.on( 'element:p', ( evt, data, conversionApi ) => {
  854. if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
  855. const paragraph = conversionApi.writer.createElement( 'paragraph' );
  856. conversionApi.writer.insert( paragraph, data.modelCursor );
  857. conversionApi.convertChildren( data.viewItem, paragraph );
  858. data.modelRange = ModelRange._createOn( paragraph );
  859. data.modelCursor = data.modelRange.end;
  860. }
  861. } );
  862. const conversionResult = model.change( writer => dispatcher.convert( viewP, writer, context ) );
  863. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  864. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
  865. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  866. expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
  867. expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  868. } );
  869. it( 'should forward correct modelCursor', () => {
  870. const spy = sinon.spy();
  871. const view = new ViewDocumentFragment( viewDocument, [
  872. new ViewContainerElement( viewDocument, 'div', null, [
  873. new ViewText( viewDocument, 'abc' ),
  874. new ViewContainerElement( viewDocument, 'foo' )
  875. ] ),
  876. new ViewContainerElement( viewDocument, 'bar' )
  877. ] );
  878. const position = ModelPosition._createAt( new ModelElement( 'element' ), 0 );
  879. dispatcher.on( 'documentFragment', convertToModelFragment() );
  880. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  881. dispatcher.on( 'element:foo', ( evt, data ) => {
  882. // Be sure that current cursor is not the same as custom.
  883. expect( data.modelCursor ).to.not.equal( position );
  884. // Set custom cursor as a result of docFrag last child conversion.
  885. // This cursor should be forwarded by a documentFragment converter.
  886. data.modelCursor = position;
  887. // Be sure that callback was fired.
  888. spy();
  889. } );
  890. dispatcher.on( 'element:bar', ( evt, data ) => {
  891. expect( data.modelCursor ).to.equal( position );
  892. spy();
  893. } );
  894. model.change( writer => dispatcher.convert( view, writer ) );
  895. sinon.assert.calledTwice( spy );
  896. } );
  897. } );
  898. describe( 'convertSelectionChange()', () => {
  899. let model, view, viewDocument, mapper, convertSelection, modelRoot, viewRoot;
  900. beforeEach( () => {
  901. model = new Model();
  902. modelRoot = model.document.createRoot();
  903. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  904. modelSetData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  905. view = new View( new StylesProcessor() );
  906. viewDocument = view.document;
  907. viewRoot = createViewRoot( viewDocument, 'div', 'main' );
  908. viewSetData( view, '<p>foo</p><p>bar</p>' );
  909. mapper = new Mapper();
  910. mapper.bindElements( modelRoot, viewRoot );
  911. mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
  912. mapper.bindElements( modelRoot.getChild( 1 ), viewRoot.getChild( 1 ) );
  913. convertSelection = convertSelectionChange( model, mapper );
  914. } );
  915. afterEach( () => {
  916. view.destroy();
  917. } );
  918. it( 'should convert collapsed selection', () => {
  919. const viewSelection = new ViewSelection();
  920. viewSelection.setTo( ViewRange._createFromParentsAndOffsets(
  921. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 ) );
  922. convertSelection( null, { newSelection: viewSelection } );
  923. expect( modelGetData( model ) ).to.equals( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  924. expect( modelGetData( model ) ).to.equal( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  925. } );
  926. it( 'should support unicode', () => {
  927. modelSetData( model, '<paragraph>நிலைக்கு</paragraph>' );
  928. viewSetData( view, '<p>நிலைக்கு</p>' );
  929. // Re-bind elements that were just re-set.
  930. mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
  931. const viewSelection = new ViewSelection( [
  932. ViewRange._createFromParentsAndOffsets( viewRoot.getChild( 0 ).getChild( 0 ), 2, viewRoot.getChild( 0 ).getChild( 0 ), 6 )
  933. ] );
  934. convertSelection( null, { newSelection: viewSelection } );
  935. expect( modelGetData( model ) ).to.equal( '<paragraph>நி[லைக்]கு</paragraph>' );
  936. } );
  937. it( 'should convert multi ranges selection', () => {
  938. const viewSelection = new ViewSelection( [
  939. ViewRange._createFromParentsAndOffsets(
  940. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
  941. ViewRange._createFromParentsAndOffsets(
  942. viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
  943. ] );
  944. convertSelection( null, { newSelection: viewSelection } );
  945. expect( modelGetData( model ) ).to.equal(
  946. '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
  947. const ranges = Array.from( model.document.selection.getRanges() );
  948. expect( ranges.length ).to.equal( 2 );
  949. expect( ranges[ 0 ].start.parent ).to.equal( modelRoot.getChild( 0 ) );
  950. expect( ranges[ 0 ].start.offset ).to.equal( 1 );
  951. expect( ranges[ 0 ].end.parent ).to.equal( modelRoot.getChild( 0 ) );
  952. expect( ranges[ 0 ].end.offset ).to.equal( 2 );
  953. expect( ranges[ 1 ].start.parent ).to.equal( modelRoot.getChild( 1 ) );
  954. expect( ranges[ 1 ].start.offset ).to.equal( 1 );
  955. expect( ranges[ 1 ].end.parent ).to.equal( modelRoot.getChild( 1 ) );
  956. expect( ranges[ 1 ].end.offset ).to.equal( 2 );
  957. } );
  958. it( 'should convert reverse selection', () => {
  959. const viewSelection = new ViewSelection( [
  960. ViewRange._createFromParentsAndOffsets(
  961. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
  962. ViewRange._createFromParentsAndOffsets(
  963. viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
  964. ], { backward: true } );
  965. convertSelection( null, { newSelection: viewSelection } );
  966. expect( modelGetData( model ) ).to.equal( '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
  967. expect( model.document.selection.isBackward ).to.true;
  968. } );
  969. it( 'should not enqueue changes if selection has not changed', () => {
  970. const viewSelection = new ViewSelection( [
  971. ViewRange._createFromParentsAndOffsets(
  972. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 )
  973. ] );
  974. convertSelection( null, { newSelection: viewSelection } );
  975. const spy = sinon.spy();
  976. model.on( 'change', spy );
  977. convertSelection( null, { newSelection: viewSelection } );
  978. expect( spy.called ).to.be.false;
  979. } );
  980. } );
  981. } );