upcasthelpers.js 25 KB

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