8
0

upcasthelpers.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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' && 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 support unicode', () => {
  802. const viewText = new ViewText( viewDocument, 'நிலைக்கு' );
  803. dispatcher.on( 'text', convertText() );
  804. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  805. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  806. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  807. expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
  808. } );
  809. } );
  810. describe( 'convertToModelFragment()', () => {
  811. it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
  812. const viewFragment = new ViewDocumentFragment( viewDocument, [
  813. new ViewContainerElement( viewDocument, 'p', null, new ViewText( viewDocument, 'foo' ) ),
  814. new ViewText( viewDocument, 'bar' )
  815. ] );
  816. // To get any meaningful results we have to actually convert something.
  817. dispatcher.on( 'text', convertText() );
  818. // This way P element won't be converted per-se but will fire converting it's children.
  819. dispatcher.on( 'element', convertToModelFragment() );
  820. dispatcher.on( 'documentFragment', convertToModelFragment() );
  821. const conversionResult = model.change( writer => dispatcher.convert( viewFragment, writer, context ) );
  822. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  823. expect( conversionResult.maxOffset ).to.equal( 6 );
  824. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  825. } );
  826. it( 'should not convert already consumed (converted) changes', () => {
  827. const viewP = new ViewContainerElement( viewDocument, 'p', null, new ViewText( viewDocument, 'foo' ) );
  828. // To get any meaningful results we have to actually convert something.
  829. dispatcher.on( 'text', convertText() );
  830. // Default converter for elements. Returns just converted children. Added with lowest priority.
  831. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  832. // Added with normal priority. Should make the above converter not fire.
  833. dispatcher.on( 'element:p', ( evt, data, conversionApi ) => {
  834. if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
  835. const paragraph = conversionApi.writer.createElement( 'paragraph' );
  836. conversionApi.writer.insert( paragraph, data.modelCursor );
  837. conversionApi.convertChildren( data.viewItem, paragraph );
  838. data.modelRange = ModelRange._createOn( paragraph );
  839. data.modelCursor = data.modelRange.end;
  840. }
  841. } );
  842. const conversionResult = model.change( writer => dispatcher.convert( viewP, writer, context ) );
  843. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  844. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
  845. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  846. expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
  847. expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  848. } );
  849. it( 'should forward correct modelCursor', () => {
  850. const spy = sinon.spy();
  851. const view = new ViewDocumentFragment( viewDocument, [
  852. new ViewContainerElement( viewDocument, 'div', null, [
  853. new ViewText( viewDocument, 'abc' ),
  854. new ViewContainerElement( viewDocument, 'foo' )
  855. ] ),
  856. new ViewContainerElement( viewDocument, 'bar' )
  857. ] );
  858. const position = ModelPosition._createAt( new ModelElement( 'element' ), 0 );
  859. dispatcher.on( 'documentFragment', convertToModelFragment() );
  860. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  861. dispatcher.on( 'element:foo', ( evt, data ) => {
  862. // Be sure that current cursor is not the same as custom.
  863. expect( data.modelCursor ).to.not.equal( position );
  864. // Set custom cursor as a result of docFrag last child conversion.
  865. // This cursor should be forwarded by a documentFragment converter.
  866. data.modelCursor = position;
  867. // Be sure that callback was fired.
  868. spy();
  869. } );
  870. dispatcher.on( 'element:bar', ( evt, data ) => {
  871. expect( data.modelCursor ).to.equal( position );
  872. spy();
  873. } );
  874. model.change( writer => dispatcher.convert( view, writer ) );
  875. sinon.assert.calledTwice( spy );
  876. } );
  877. } );
  878. describe( 'convertSelectionChange()', () => {
  879. let model, view, viewDocument, mapper, convertSelection, modelRoot, viewRoot;
  880. beforeEach( () => {
  881. model = new Model();
  882. modelRoot = model.document.createRoot();
  883. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  884. modelSetData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  885. view = new View( new StylesProcessor() );
  886. viewDocument = view.document;
  887. viewRoot = createViewRoot( viewDocument, 'div', 'main' );
  888. viewSetData( view, '<p>foo</p><p>bar</p>' );
  889. mapper = new Mapper();
  890. mapper.bindElements( modelRoot, viewRoot );
  891. mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
  892. mapper.bindElements( modelRoot.getChild( 1 ), viewRoot.getChild( 1 ) );
  893. convertSelection = convertSelectionChange( model, mapper );
  894. } );
  895. afterEach( () => {
  896. view.destroy();
  897. } );
  898. it( 'should convert collapsed selection', () => {
  899. const viewSelection = new ViewSelection();
  900. viewSelection.setTo( ViewRange._createFromParentsAndOffsets(
  901. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 ) );
  902. convertSelection( null, { newSelection: viewSelection } );
  903. expect( modelGetData( model ) ).to.equals( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  904. expect( modelGetData( model ) ).to.equal( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  905. } );
  906. it( 'should support unicode', () => {
  907. modelSetData( model, '<paragraph>நிலைக்கு</paragraph>' );
  908. viewSetData( view, '<p>நிலைக்கு</p>' );
  909. // Re-bind elements that were just re-set.
  910. mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
  911. const viewSelection = new ViewSelection( [
  912. ViewRange._createFromParentsAndOffsets( viewRoot.getChild( 0 ).getChild( 0 ), 2, viewRoot.getChild( 0 ).getChild( 0 ), 6 )
  913. ] );
  914. convertSelection( null, { newSelection: viewSelection } );
  915. expect( modelGetData( model ) ).to.equal( '<paragraph>நி[லைக்]கு</paragraph>' );
  916. } );
  917. it( 'should convert multi ranges selection', () => {
  918. const viewSelection = new ViewSelection( [
  919. ViewRange._createFromParentsAndOffsets(
  920. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
  921. ViewRange._createFromParentsAndOffsets(
  922. viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
  923. ] );
  924. convertSelection( null, { newSelection: viewSelection } );
  925. expect( modelGetData( model ) ).to.equal(
  926. '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
  927. const ranges = Array.from( model.document.selection.getRanges() );
  928. expect( ranges.length ).to.equal( 2 );
  929. expect( ranges[ 0 ].start.parent ).to.equal( modelRoot.getChild( 0 ) );
  930. expect( ranges[ 0 ].start.offset ).to.equal( 1 );
  931. expect( ranges[ 0 ].end.parent ).to.equal( modelRoot.getChild( 0 ) );
  932. expect( ranges[ 0 ].end.offset ).to.equal( 2 );
  933. expect( ranges[ 1 ].start.parent ).to.equal( modelRoot.getChild( 1 ) );
  934. expect( ranges[ 1 ].start.offset ).to.equal( 1 );
  935. expect( ranges[ 1 ].end.parent ).to.equal( modelRoot.getChild( 1 ) );
  936. expect( ranges[ 1 ].end.offset ).to.equal( 2 );
  937. } );
  938. it( 'should convert reverse selection', () => {
  939. const viewSelection = new ViewSelection( [
  940. ViewRange._createFromParentsAndOffsets(
  941. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
  942. ViewRange._createFromParentsAndOffsets(
  943. viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
  944. ], { backward: true } );
  945. convertSelection( null, { newSelection: viewSelection } );
  946. expect( modelGetData( model ) ).to.equal( '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
  947. expect( model.document.selection.isBackward ).to.true;
  948. } );
  949. it( 'should not enqueue changes if selection has not changed', () => {
  950. const viewSelection = new ViewSelection( [
  951. ViewRange._createFromParentsAndOffsets(
  952. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 )
  953. ] );
  954. convertSelection( null, { newSelection: viewSelection } );
  955. const spy = sinon.spy();
  956. model.on( 'change', spy );
  957. convertSelection( null, { newSelection: viewSelection } );
  958. expect( spy.called ).to.be.false;
  959. } );
  960. } );
  961. } );