upcast-converters.js 25 KB

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