upcasthelpers.js 30 KB

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