upcasthelpers.js 39 KB

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