upcast-converters.js 23 KB

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