upcast-converters.js 25 KB

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