8
0

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