8
0

upcasthelpers.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 Model from '../../src/model/model';
  12. import ModelDocumentFragment from '../../src/model/documentfragment';
  13. import ModelElement from '../../src/model/element';
  14. import ModelText from '../../src/model/text';
  15. import ModelRange from '../../src/model/range';
  16. import ModelPosition from '../../src/model/position';
  17. import UpcastHelpers, { convertToModelFragment, convertText, convertSelectionChange } from '../../src/conversion/upcasthelpers';
  18. import { getData as modelGetData, setData as modelSetData, stringify } from '../../src/dev-utils/model';
  19. import View from '../../src/view/view';
  20. import createViewRoot from '../view/_utils/createroot';
  21. import { setData as viewSetData } from '../../src/dev-utils/view';
  22. import Mapper from '../../src/conversion/mapper';
  23. import ViewSelection from '../../src/view/selection';
  24. import ViewRange from '../../src/view/range';
  25. describe( 'UpcastHelpers', () => {
  26. let upcastDispatcher, model, schema, upcastHelpers;
  27. beforeEach( () => {
  28. model = new Model();
  29. schema = model.schema;
  30. schema.extend( '$text', {
  31. allowIn: '$root',
  32. allowAttributes: [ 'bold', 'attribA', 'attribB' ]
  33. } );
  34. schema.register( 'paragraph', {
  35. inheritAllFrom: '$block'
  36. } );
  37. upcastDispatcher = new UpcastDispatcher( { schema } );
  38. upcastDispatcher.on( 'text', convertText() );
  39. upcastDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  40. upcastDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
  41. upcastHelpers = new UpcastHelpers( [ upcastDispatcher ] );
  42. } );
  43. describe( '.elementToElement()', () => {
  44. it( 'should be chainable', () => {
  45. expect( upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } ) ).to.equal( upcastHelpers );
  46. } );
  47. it( 'config.view is a string', () => {
  48. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  49. expectResult( new ViewContainerElement( 'p' ), '<paragraph></paragraph>' );
  50. } );
  51. it( 'can be overwritten using converterPriority', () => {
  52. schema.register( 'p', {
  53. inheritAllFrom: '$block'
  54. } );
  55. upcastHelpers.elementToElement( { view: 'p', model: 'p' } );
  56. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph', converterPriority: 'high' } );
  57. expectResult( new ViewContainerElement( 'p' ), '<paragraph></paragraph>' );
  58. } );
  59. it( 'config.view is an object', () => {
  60. schema.register( 'fancyParagraph', {
  61. inheritAllFrom: '$block'
  62. } );
  63. upcastHelpers.elementToElement( {
  64. view: {
  65. name: 'p',
  66. classes: 'fancy'
  67. },
  68. model: 'fancyParagraph'
  69. } );
  70. expectResult( new ViewContainerElement( 'p', { class: 'fancy' } ), '<fancyParagraph></fancyParagraph>' );
  71. expectResult( new ViewContainerElement( 'p' ), '' );
  72. } );
  73. it( 'config.model is a function', () => {
  74. schema.register( 'heading', {
  75. inheritAllFrom: '$block',
  76. allowAttributes: [ 'level' ]
  77. } );
  78. upcastHelpers.elementToElement( {
  79. view: {
  80. name: 'p',
  81. classes: 'heading'
  82. },
  83. model: ( viewElement, modelWriter ) => {
  84. return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
  85. }
  86. } );
  87. expectResult( new ViewContainerElement( 'p', { class: 'heading', 'data-level': 2 } ), '<heading level="2"></heading>' );
  88. expectResult( new ViewContainerElement( 'p', { 'data-level': 2 } ), '' );
  89. } );
  90. it( 'should fire conversion of the element children', () => {
  91. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  92. expectResult( new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ), '<paragraph>foo</paragraph>' );
  93. } );
  94. it( 'should not insert a model element if it is not allowed by schema', () => {
  95. upcastHelpers.elementToElement( { view: 'h2', model: 'heading' } );
  96. expectResult( new ViewContainerElement( 'h2' ), '' );
  97. } );
  98. it( 'should auto-break elements', () => {
  99. schema.register( 'heading', {
  100. inheritAllFrom: '$block'
  101. } );
  102. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  103. upcastHelpers.elementToElement( { view: 'h2', model: 'heading' } );
  104. expectResult(
  105. new ViewContainerElement( 'p', null, [
  106. new ViewText( 'Foo' ),
  107. new ViewContainerElement( 'h2', null, new ViewText( 'Xyz' ) ),
  108. new ViewText( 'Bar' )
  109. ] ),
  110. '<paragraph>Foo</paragraph><heading>Xyz</heading><paragraph>Bar</paragraph>'
  111. );
  112. } );
  113. it( 'should not do anything if returned model element is null', () => {
  114. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  115. upcastHelpers.elementToElement( { view: 'p', model: () => null, converterPriority: 'high' } );
  116. expectResult( new ViewContainerElement( 'p' ), '<paragraph></paragraph>' );
  117. } );
  118. } );
  119. describe( '.elementToAttribute()', () => {
  120. it( 'should be chainable', () => {
  121. expect( upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } ) ).to.equal( upcastHelpers );
  122. } );
  123. it( 'config.view is string', () => {
  124. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } );
  125. expectResult(
  126. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ),
  127. '<$text bold="true">foo</$text>'
  128. );
  129. } );
  130. it( 'can be overwritten using converterPriority', () => {
  131. upcastHelpers.elementToAttribute( { view: 'strong', model: 'strong' } );
  132. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold', converterPriority: 'high' } );
  133. expectResult(
  134. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ),
  135. '<$text bold="true">foo</$text>'
  136. );
  137. } );
  138. it( 'config.view is an object', () => {
  139. upcastHelpers.elementToAttribute( {
  140. view: {
  141. name: 'span',
  142. classes: 'bold'
  143. },
  144. model: 'bold'
  145. } );
  146. expectResult(
  147. new ViewAttributeElement( 'span', { class: 'bold' }, new ViewText( 'foo' ) ),
  148. '<$text bold="true">foo</$text>'
  149. );
  150. expectResult( new ViewAttributeElement( 'span', {}, new ViewText( 'foo' ) ), 'foo' );
  151. } );
  152. it( 'model attribute value is given', () => {
  153. schema.extend( '$text', {
  154. allowAttributes: [ 'styled' ]
  155. } );
  156. upcastHelpers.elementToAttribute( {
  157. view: {
  158. name: 'span',
  159. classes: [ 'styled', 'styled-dark' ]
  160. },
  161. model: {
  162. key: 'styled',
  163. value: 'dark'
  164. }
  165. } );
  166. expectResult(
  167. new ViewAttributeElement( 'span', { class: 'styled styled-dark' }, new ViewText( 'foo' ) ),
  168. '<$text styled="dark">foo</$text>'
  169. );
  170. expectResult( new ViewAttributeElement( 'span', {}, new ViewText( 'foo' ) ), 'foo' );
  171. } );
  172. it( 'model attribute value is a function', () => {
  173. schema.extend( '$text', {
  174. allowAttributes: [ 'fontSize' ]
  175. } );
  176. upcastHelpers.elementToAttribute( {
  177. view: {
  178. name: 'span',
  179. styles: {
  180. 'font-size': /[\s\S]+/
  181. }
  182. },
  183. model: {
  184. key: 'fontSize',
  185. value: viewElement => {
  186. const fontSize = viewElement.getStyle( 'font-size' );
  187. const value = fontSize.substr( 0, fontSize.length - 2 );
  188. if ( value <= 10 ) {
  189. return 'small';
  190. } else if ( value > 12 ) {
  191. return 'big';
  192. }
  193. return null;
  194. }
  195. }
  196. } );
  197. expectResult(
  198. new ViewAttributeElement( 'span', { style: 'font-size:9px' }, new ViewText( 'foo' ) ),
  199. '<$text fontSize="small">foo</$text>'
  200. );
  201. expectResult(
  202. new ViewAttributeElement( 'span', { style: 'font-size:12px' }, new ViewText( 'foo' ) ),
  203. 'foo'
  204. );
  205. expectResult(
  206. new ViewAttributeElement( 'span', { style: 'font-size:14px' }, new ViewText( 'foo' ) ),
  207. '<$text fontSize="big">foo</$text>'
  208. );
  209. } );
  210. it( 'should not set an attribute if it is not allowed by schema', () => {
  211. upcastHelpers.elementToAttribute( { view: 'em', model: 'italic' } );
  212. expectResult(
  213. new ViewAttributeElement( 'em', null, new ViewText( 'foo' ) ),
  214. 'foo'
  215. );
  216. } );
  217. it( 'should not do anything if returned model attribute is null', () => {
  218. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } );
  219. upcastHelpers.elementToAttribute( {
  220. view: 'strong',
  221. model: {
  222. key: 'bold',
  223. value: () => null
  224. },
  225. converterPriority: 'high'
  226. } );
  227. expectResult(
  228. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ),
  229. '<$text bold="true">foo</$text>'
  230. );
  231. } );
  232. it( 'should allow two converters to convert attributes on the same element', () => {
  233. upcastHelpers.elementToAttribute( {
  234. model: 'attribA',
  235. view: { name: 'span', classes: 'attrib-a' }
  236. } );
  237. upcastHelpers.elementToAttribute( {
  238. model: 'attribB',
  239. view: { name: 'span', styles: { color: 'attrib-b' } }
  240. } );
  241. expectResult(
  242. new ViewAttributeElement( 'span', { class: 'attrib-a', style: 'color:attrib-b;' }, new ViewText( 'foo' ) ),
  243. '<$text attribA="true" attribB="true">foo</$text>'
  244. );
  245. } );
  246. it( 'should consume element only when only is name specified', () => {
  247. upcastHelpers.elementToAttribute( {
  248. model: 'bold',
  249. view: { name: 'strong' }
  250. } );
  251. upcastHelpers.elementToAttribute( {
  252. model: 'attribA',
  253. view: { name: 'strong' }
  254. } );
  255. upcastHelpers.elementToAttribute( {
  256. model: 'attribB',
  257. view: { name: 'strong', classes: 'foo' }
  258. } );
  259. expectResult(
  260. new ViewAttributeElement( 'strong', { class: 'foo' }, new ViewText( 'foo' ) ),
  261. '<$text attribB="true" bold="true">foo</$text>'
  262. );
  263. } );
  264. // #1443.
  265. it( 'should set attributes on the element\'s children', () => {
  266. upcastHelpers.elementToAttribute( {
  267. model: 'bold',
  268. view: { name: 'strong' }
  269. } );
  270. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  271. expectResult(
  272. new ViewAttributeElement(
  273. 'strong',
  274. null,
  275. new ViewContainerElement( 'p', null, new ViewText( 'Foo' ) )
  276. ),
  277. '<paragraph><$text bold="true">Foo</$text></paragraph>'
  278. );
  279. } );
  280. } );
  281. describe( '.attributeToAttribute()', () => {
  282. beforeEach( () => {
  283. upcastHelpers.elementToElement( { view: 'img', model: 'image' } );
  284. schema.register( 'image', {
  285. inheritAllFrom: '$block'
  286. } );
  287. } );
  288. it( 'should be chainable', () => {
  289. expect( upcastHelpers.attributeToAttribute( { view: 'src', model: 'source' } ) ).to.equal( upcastHelpers );
  290. } );
  291. it( 'config.view is a string', () => {
  292. schema.extend( 'image', {
  293. allowAttributes: [ 'source' ]
  294. } );
  295. upcastHelpers.attributeToAttribute( { view: 'src', model: 'source' } );
  296. expectResult(
  297. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  298. '<image source="foo.jpg"></image>'
  299. );
  300. } );
  301. it( 'config.view has only key set', () => {
  302. schema.extend( 'image', {
  303. allowAttributes: [ 'source' ]
  304. } );
  305. upcastHelpers.attributeToAttribute( { view: { key: 'src' }, model: 'source' } );
  306. expectResult(
  307. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  308. '<image source="foo.jpg"></image>'
  309. );
  310. } );
  311. it( 'config.view has only key and name set', () => {
  312. schema.extend( 'image', {
  313. allowAttributes: [ 'source' ]
  314. } );
  315. upcastHelpers.attributeToAttribute( { view: { name: 'img', key: 'src' }, model: { name: 'image', key: 'source' } } );
  316. expectResult(
  317. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  318. '<image source="foo.jpg"></image>'
  319. );
  320. } );
  321. it( 'can be overwritten using converterPriority', () => {
  322. schema.extend( 'image', {
  323. allowAttributes: [ 'src', 'source' ]
  324. } );
  325. upcastHelpers.attributeToAttribute( { view: { key: 'src' }, model: 'src' } );
  326. upcastHelpers.attributeToAttribute( { view: { key: 'src' }, model: 'source', converterPriority: 'normal' } );
  327. expectResult(
  328. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  329. '<image source="foo.jpg"></image>'
  330. );
  331. } );
  332. it( 'config.view has value set', () => {
  333. schema.extend( 'image', {
  334. allowAttributes: [ 'styled' ]
  335. } );
  336. upcastHelpers.attributeToAttribute( {
  337. view: {
  338. key: 'data-style',
  339. value: /[\s\S]*/
  340. },
  341. model: 'styled'
  342. } );
  343. expectResult(
  344. new ViewAttributeElement( 'img', { 'data-style': 'dark' } ),
  345. '<image styled="dark"></image>'
  346. );
  347. } );
  348. it( 'model attribute value is a string', () => {
  349. schema.extend( 'image', {
  350. allowAttributes: [ 'styled' ]
  351. } );
  352. upcastHelpers.attributeToAttribute( {
  353. view: {
  354. name: 'img',
  355. key: 'class',
  356. value: 'styled-dark'
  357. },
  358. model: {
  359. key: 'styled',
  360. value: 'dark'
  361. }
  362. } );
  363. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  364. expectResult(
  365. new ViewContainerElement( 'img', { class: 'styled-dark' } ),
  366. '<image styled="dark"></image>'
  367. );
  368. expectResult(
  369. new ViewContainerElement( 'img', { class: 'styled-xxx' } ),
  370. '<image></image>'
  371. );
  372. expectResult(
  373. new ViewContainerElement( 'p', { class: 'styled-dark' } ),
  374. '<paragraph></paragraph>'
  375. );
  376. } );
  377. it( 'model attribute value is a function', () => {
  378. schema.extend( 'image', {
  379. allowAttributes: [ 'styled' ]
  380. } );
  381. upcastHelpers.attributeToAttribute( {
  382. view: {
  383. key: 'class',
  384. value: /styled-[\S]+/
  385. },
  386. model: {
  387. key: 'styled',
  388. value: viewElement => {
  389. const regexp = /styled-([\S]+)/;
  390. const match = viewElement.getAttribute( 'class' ).match( regexp );
  391. return match[ 1 ];
  392. }
  393. }
  394. } );
  395. expectResult(
  396. new ViewAttributeElement( 'img', { 'class': 'styled-dark' } ),
  397. '<image styled="dark"></image>'
  398. );
  399. } );
  400. it( 'should not set an attribute if it is not allowed by schema', () => {
  401. upcastHelpers.attributeToAttribute( { view: 'src', model: 'source' } );
  402. expectResult(
  403. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  404. '<image></image>'
  405. );
  406. } );
  407. it( 'should not do anything if returned model attribute is null', () => {
  408. schema.extend( 'image', {
  409. allowAttributes: [ 'styled' ]
  410. } );
  411. upcastHelpers.attributeToAttribute( {
  412. view: {
  413. key: 'class',
  414. value: 'styled'
  415. },
  416. model: {
  417. key: 'styled',
  418. value: true
  419. }
  420. } );
  421. upcastHelpers.attributeToAttribute( {
  422. view: {
  423. key: 'class',
  424. value: 'styled'
  425. },
  426. model: {
  427. key: 'styled',
  428. value: () => null
  429. }
  430. } );
  431. expectResult(
  432. new ViewAttributeElement( 'img', { class: 'styled' } ),
  433. '<image styled="true"></image>'
  434. );
  435. } );
  436. // #1443.
  437. it( 'should not set attributes on the element\'s children', () => {
  438. schema.register( 'div', {
  439. inheritAllFrom: '$root',
  440. allowWhere: '$block',
  441. isLimit: true,
  442. allowAttributes: [ 'border', 'shade' ]
  443. } );
  444. upcastHelpers.elementToElement( { view: 'div', model: 'div' } );
  445. upcastHelpers.attributeToAttribute( { view: { key: 'class', value: 'shade' }, model: 'shade' } );
  446. upcastHelpers.attributeToAttribute( { view: { key: 'class', value: 'border' }, model: 'border' } );
  447. expectResult(
  448. new ViewContainerElement(
  449. 'div',
  450. { class: 'border' },
  451. new ViewContainerElement( 'div', { class: 'shade' } )
  452. ),
  453. '<div border="border"><div shade="shade"></div></div>'
  454. );
  455. } );
  456. } );
  457. describe( '.elementToMarker()', () => {
  458. it( 'should be chainable', () => {
  459. expect( upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search' } ) ).to.equal( upcastHelpers );
  460. } );
  461. it( 'config.view is a string', () => {
  462. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search' } );
  463. const frag = new ViewDocumentFragment( [
  464. new ViewText( 'fo' ),
  465. new ViewUIElement( 'marker-search' ),
  466. new ViewText( 'oba' ),
  467. new ViewUIElement( 'marker-search' ),
  468. new ViewText( 'r' )
  469. ] );
  470. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  471. expectResult( frag, 'foobar', marker );
  472. } );
  473. it( 'can be overwritten using converterPriority', () => {
  474. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search-result' } );
  475. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search', converterPriority: 'high' } );
  476. const frag = new ViewDocumentFragment( [
  477. new ViewText( 'fo' ),
  478. new ViewUIElement( 'marker-search' ),
  479. new ViewText( 'oba' ),
  480. new ViewUIElement( 'marker-search' ),
  481. new ViewText( 'r' )
  482. ] );
  483. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  484. expectResult( frag, 'foobar', marker );
  485. } );
  486. it( 'config.view is an object', () => {
  487. upcastHelpers.elementToMarker( {
  488. view: {
  489. name: 'span',
  490. 'data-marker': 'search'
  491. },
  492. model: 'search'
  493. } );
  494. const frag = new ViewDocumentFragment( [
  495. new ViewText( 'f' ),
  496. new ViewUIElement( 'span', { 'data-marker': 'search' } ),
  497. new ViewText( 'oob' ),
  498. new ViewUIElement( 'span', { 'data-marker': 'search' } ),
  499. new ViewText( 'ar' )
  500. ] );
  501. const marker = { name: 'search', start: [ 1 ], end: [ 4 ] };
  502. expectResult( frag, 'foobar', marker );
  503. } );
  504. it( 'config.model is a function', () => {
  505. upcastHelpers.elementToMarker( {
  506. view: 'comment',
  507. model: viewElement => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
  508. } );
  509. const frag = new ViewDocumentFragment( [
  510. new ViewText( 'foo' ),
  511. new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
  512. new ViewText( 'b' ),
  513. new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
  514. new ViewText( 'ar' )
  515. ] );
  516. const marker = { name: 'comment:4', start: [ 3 ], end: [ 4 ] };
  517. expectResult( frag, 'foobar', marker );
  518. } );
  519. it( 'marker is in a block element', () => {
  520. upcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  521. upcastHelpers.elementToMarker( { view: 'marker-search', model: 'search' } );
  522. const element = new ViewContainerElement( 'p', null, [
  523. new ViewText( 'fo' ),
  524. new ViewUIElement( 'marker-search' ),
  525. new ViewText( 'oba' ),
  526. new ViewUIElement( 'marker-search' ),
  527. new ViewText( 'r' )
  528. ] );
  529. const marker = { name: 'search', start: [ 0, 2 ], end: [ 0, 5 ] };
  530. expectResult( element, '<paragraph>foobar</paragraph>', marker );
  531. } );
  532. } );
  533. function expectResult( viewToConvert, modelString, marker ) {
  534. const conversionResult = model.change( writer => upcastDispatcher.convert( viewToConvert, writer ) );
  535. if ( marker ) {
  536. expect( conversionResult.markers.has( marker.name ) ).to.be.true;
  537. const convertedMarker = conversionResult.markers.get( marker.name );
  538. expect( convertedMarker.start.path ).to.deep.equal( marker.start );
  539. expect( convertedMarker.end.path ).to.deep.equal( marker.end );
  540. }
  541. expect( stringify( conversionResult ) ).to.equal( modelString );
  542. }
  543. } );
  544. describe( 'upcast-converters', () => {
  545. let dispatcher, schema, context, model;
  546. beforeEach( () => {
  547. model = new Model();
  548. schema = model.schema;
  549. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  550. schema.extend( '$text', { allowIn: '$root' } );
  551. context = [ '$root' ];
  552. dispatcher = new UpcastDispatcher( { schema } );
  553. } );
  554. describe( 'convertText()', () => {
  555. it( 'should return converter converting ViewText to ModelText', () => {
  556. const viewText = new ViewText( 'foobar' );
  557. dispatcher.on( 'text', convertText() );
  558. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer ) );
  559. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  560. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  561. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  562. } );
  563. it( 'should not convert already consumed texts', () => {
  564. const viewText = new ViewText( 'foofuckbafuckr' );
  565. // Default converter for elements. Returns just converted children. Added with lowest priority.
  566. dispatcher.on( 'text', convertText(), { priority: 'lowest' } );
  567. // Added with normal priority. Should make the above converter not fire.
  568. dispatcher.on( 'text', ( evt, data, conversionApi ) => {
  569. if ( conversionApi.consumable.consume( data.viewItem ) ) {
  570. const text = conversionApi.writer.createText( data.viewItem.data.replace( /fuck/gi, '****' ) );
  571. conversionApi.writer.insert( text, data.modelCursor );
  572. data.modelRange = ModelRange._createFromPositionAndShift( data.modelCursor, text.offsetSize );
  573. data.modelCursor = data.modelRange.end;
  574. }
  575. } );
  576. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  577. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  578. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  579. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foo****ba****r' );
  580. } );
  581. it( 'should not convert text if it is wrong with schema', () => {
  582. schema.addChildCheck( ( ctx, childDef ) => {
  583. if ( childDef.name == '$text' && ctx.endsWith( '$root' ) ) {
  584. return false;
  585. }
  586. } );
  587. const viewText = new ViewText( 'foobar' );
  588. dispatcher.on( 'text', convertText() );
  589. let conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  590. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  591. expect( conversionResult.childCount ).to.equal( 0 );
  592. conversionResult = model.change( writer => dispatcher.convert( viewText, writer, [ '$block' ] ) );
  593. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  594. expect( conversionResult.childCount ).to.equal( 1 );
  595. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  596. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  597. } );
  598. it( 'should support unicode', () => {
  599. const viewText = new ViewText( 'நிலைக்கு' );
  600. dispatcher.on( 'text', convertText() );
  601. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  602. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  603. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  604. expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
  605. } );
  606. } );
  607. describe( 'convertToModelFragment()', () => {
  608. it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
  609. const viewFragment = new ViewDocumentFragment( [
  610. new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ),
  611. new ViewText( 'bar' )
  612. ] );
  613. // To get any meaningful results we have to actually convert something.
  614. dispatcher.on( 'text', convertText() );
  615. // This way P element won't be converted per-se but will fire converting it's children.
  616. dispatcher.on( 'element', convertToModelFragment() );
  617. dispatcher.on( 'documentFragment', convertToModelFragment() );
  618. const conversionResult = model.change( writer => dispatcher.convert( viewFragment, writer, context ) );
  619. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  620. expect( conversionResult.maxOffset ).to.equal( 6 );
  621. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  622. } );
  623. it( 'should not convert already consumed (converted) changes', () => {
  624. const viewP = new ViewContainerElement( 'p', null, new ViewText( 'foo' ) );
  625. // To get any meaningful results we have to actually convert something.
  626. dispatcher.on( 'text', convertText() );
  627. // Default converter for elements. Returns just converted children. Added with lowest priority.
  628. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  629. // Added with normal priority. Should make the above converter not fire.
  630. dispatcher.on( 'element:p', ( evt, data, conversionApi ) => {
  631. if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
  632. const paragraph = conversionApi.writer.createElement( 'paragraph' );
  633. conversionApi.writer.insert( paragraph, data.modelCursor );
  634. conversionApi.convertChildren( data.viewItem, ModelPosition._createAt( paragraph, 0 ) );
  635. data.modelRange = ModelRange._createOn( paragraph );
  636. data.modelCursor = data.modelRange.end;
  637. }
  638. } );
  639. const conversionResult = model.change( writer => dispatcher.convert( viewP, writer, context ) );
  640. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  641. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
  642. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  643. expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
  644. expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  645. } );
  646. it( 'should forward correct modelCursor', () => {
  647. const spy = sinon.spy();
  648. const view = new ViewDocumentFragment( [
  649. new ViewContainerElement( 'div', null, [ new ViewText( 'abc' ), new ViewContainerElement( 'foo' ) ] ),
  650. new ViewContainerElement( 'bar' )
  651. ] );
  652. const position = ModelPosition._createAt( new ModelElement( 'element' ), 0 );
  653. dispatcher.on( 'documentFragment', convertToModelFragment() );
  654. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  655. dispatcher.on( 'element:foo', ( evt, data ) => {
  656. // Be sure that current cursor is not the same as custom.
  657. expect( data.modelCursor ).to.not.equal( position );
  658. // Set custom cursor as a result of docFrag last child conversion.
  659. // This cursor should be forwarded by a documentFragment converter.
  660. data.modelCursor = position;
  661. // Be sure that callback was fired.
  662. spy();
  663. } );
  664. dispatcher.on( 'element:bar', ( evt, data ) => {
  665. expect( data.modelCursor ).to.equal( position );
  666. spy();
  667. } );
  668. model.change( writer => dispatcher.convert( view, writer ) );
  669. sinon.assert.calledTwice( spy );
  670. } );
  671. } );
  672. describe( 'convertSelectionChange()', () => {
  673. let model, view, viewDocument, mapper, convertSelection, modelRoot, viewRoot;
  674. beforeEach( () => {
  675. model = new Model();
  676. modelRoot = model.document.createRoot();
  677. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  678. modelSetData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  679. view = new View();
  680. viewDocument = view.document;
  681. viewRoot = createViewRoot( viewDocument, 'div', 'main' );
  682. viewSetData( view, '<p>foo</p><p>bar</p>' );
  683. mapper = new Mapper();
  684. mapper.bindElements( modelRoot, viewRoot );
  685. mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
  686. mapper.bindElements( modelRoot.getChild( 1 ), viewRoot.getChild( 1 ) );
  687. convertSelection = convertSelectionChange( model, mapper );
  688. } );
  689. afterEach( () => {
  690. view.destroy();
  691. } );
  692. it( 'should convert collapsed selection', () => {
  693. const viewSelection = new ViewSelection();
  694. viewSelection.setTo( ViewRange._createFromParentsAndOffsets(
  695. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 ) );
  696. convertSelection( null, { newSelection: viewSelection } );
  697. expect( modelGetData( model ) ).to.equals( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  698. expect( modelGetData( model ) ).to.equal( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  699. } );
  700. it( 'should support unicode', () => {
  701. modelSetData( model, '<paragraph>நிலைக்கு</paragraph>' );
  702. viewSetData( view, '<p>நிலைக்கு</p>' );
  703. // Re-bind elements that were just re-set.
  704. mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
  705. const viewSelection = new ViewSelection( [
  706. ViewRange._createFromParentsAndOffsets( viewRoot.getChild( 0 ).getChild( 0 ), 2, viewRoot.getChild( 0 ).getChild( 0 ), 6 )
  707. ] );
  708. convertSelection( null, { newSelection: viewSelection } );
  709. expect( modelGetData( model ) ).to.equal( '<paragraph>நி[லைக்]கு</paragraph>' );
  710. } );
  711. it( 'should convert multi ranges selection', () => {
  712. const viewSelection = new ViewSelection( [
  713. ViewRange._createFromParentsAndOffsets(
  714. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
  715. ViewRange._createFromParentsAndOffsets(
  716. viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
  717. ] );
  718. convertSelection( null, { newSelection: viewSelection } );
  719. expect( modelGetData( model ) ).to.equal(
  720. '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
  721. const ranges = Array.from( model.document.selection.getRanges() );
  722. expect( ranges.length ).to.equal( 2 );
  723. expect( ranges[ 0 ].start.parent ).to.equal( modelRoot.getChild( 0 ) );
  724. expect( ranges[ 0 ].start.offset ).to.equal( 1 );
  725. expect( ranges[ 0 ].end.parent ).to.equal( modelRoot.getChild( 0 ) );
  726. expect( ranges[ 0 ].end.offset ).to.equal( 2 );
  727. expect( ranges[ 1 ].start.parent ).to.equal( modelRoot.getChild( 1 ) );
  728. expect( ranges[ 1 ].start.offset ).to.equal( 1 );
  729. expect( ranges[ 1 ].end.parent ).to.equal( modelRoot.getChild( 1 ) );
  730. expect( ranges[ 1 ].end.offset ).to.equal( 2 );
  731. } );
  732. it( 'should convert reverse selection', () => {
  733. const viewSelection = new ViewSelection( [
  734. ViewRange._createFromParentsAndOffsets(
  735. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
  736. ViewRange._createFromParentsAndOffsets(
  737. viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
  738. ], { backward: true } );
  739. convertSelection( null, { newSelection: viewSelection } );
  740. expect( modelGetData( model ) ).to.equal( '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
  741. expect( model.document.selection.isBackward ).to.true;
  742. } );
  743. it( 'should not enqueue changes if selection has not changed', () => {
  744. const viewSelection = new ViewSelection( [
  745. ViewRange._createFromParentsAndOffsets(
  746. viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 )
  747. ] );
  748. convertSelection( null, { newSelection: viewSelection } );
  749. const spy = sinon.spy();
  750. model.on( 'change', spy );
  751. convertSelection( null, { newSelection: viewSelection } );
  752. expect( spy.called ).to.be.false;
  753. } );
  754. } );
  755. } );