8
0

upcast-converters.js 23 KB

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