upcast-converters.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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( 'config.view has only key and name set', () => {
  301. schema.extend( 'image', {
  302. allowAttributes: [ 'source' ]
  303. } );
  304. const helper = upcastAttributeToAttribute( { view: { name: 'img', key: 'src' }, model: { name: 'image', key: 'source' } } );
  305. conversion.for( 'upcast' ).add( helper );
  306. expectResult(
  307. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  308. '<image source="foo.jpg"></image>'
  309. );
  310. } );
  311. it( 'can be overwritten using priority', () => {
  312. schema.extend( 'image', {
  313. allowAttributes: [ 'src', 'source' ]
  314. } );
  315. const helperA = upcastAttributeToAttribute( { view: { key: 'src' }, model: 'src' } );
  316. const helperB = upcastAttributeToAttribute( { view: { key: 'src' }, model: 'source', priority: 'normal' } );
  317. conversion.for( 'upcast' ).add( helperA ).add( helperB );
  318. expectResult(
  319. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  320. '<image source="foo.jpg"></image>'
  321. );
  322. } );
  323. it( 'config.view has value set', () => {
  324. schema.extend( 'image', {
  325. allowAttributes: [ 'styled' ]
  326. } );
  327. const helper = upcastAttributeToAttribute( {
  328. view: {
  329. key: 'data-style',
  330. value: /[\s\S]*/
  331. },
  332. model: 'styled'
  333. } );
  334. conversion.for( 'upcast' ).add( helper );
  335. expectResult(
  336. new ViewAttributeElement( 'img', { 'data-style': 'dark' } ),
  337. '<image styled="dark"></image>'
  338. );
  339. } );
  340. it( 'model attribute value is a string', () => {
  341. schema.extend( 'image', {
  342. allowAttributes: [ 'styled' ]
  343. } );
  344. const helper = upcastAttributeToAttribute( {
  345. view: {
  346. name: 'img',
  347. key: 'class',
  348. value: 'styled-dark'
  349. },
  350. model: {
  351. key: 'styled',
  352. value: 'dark'
  353. }
  354. } );
  355. conversion.for( 'upcast' )
  356. .add( helper )
  357. .add( upcastElementToElement( { view: 'p', model: 'paragraph' } ) );
  358. expectResult(
  359. new ViewContainerElement( 'img', { class: 'styled-dark' } ),
  360. '<image styled="dark"></image>'
  361. );
  362. expectResult(
  363. new ViewContainerElement( 'img', { class: 'styled-xxx' } ),
  364. '<image></image>'
  365. );
  366. expectResult(
  367. new ViewContainerElement( 'p', { class: 'styled-dark' } ),
  368. '<paragraph></paragraph>'
  369. );
  370. } );
  371. it( 'model attribute value is a function', () => {
  372. schema.extend( 'image', {
  373. allowAttributes: [ 'styled' ]
  374. } );
  375. const helper = upcastAttributeToAttribute( {
  376. view: {
  377. key: 'class',
  378. value: /styled-[\S]+/
  379. },
  380. model: {
  381. key: 'styled',
  382. value: viewElement => {
  383. const regexp = /styled-([\S]+)/;
  384. const match = viewElement.getAttribute( 'class' ).match( regexp );
  385. return match[ 1 ];
  386. }
  387. }
  388. } );
  389. conversion.for( 'upcast' ).add( helper );
  390. expectResult(
  391. new ViewAttributeElement( 'img', { 'class': 'styled-dark' } ),
  392. '<image styled="dark"></image>'
  393. );
  394. } );
  395. it( 'should not set an attribute if it is not allowed by schema', () => {
  396. const helper = upcastAttributeToAttribute( { view: 'src', model: 'source' } );
  397. conversion.for( 'upcast' ).add( helper );
  398. expectResult(
  399. new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
  400. '<image></image>'
  401. );
  402. } );
  403. it( 'should not do anything if returned model attribute is null', () => {
  404. schema.extend( 'image', {
  405. allowAttributes: [ 'styled' ]
  406. } );
  407. const helperA = upcastAttributeToAttribute( {
  408. view: {
  409. key: 'class',
  410. value: 'styled'
  411. },
  412. model: {
  413. key: 'styled',
  414. value: true
  415. }
  416. } );
  417. const helperB = upcastAttributeToAttribute( {
  418. view: {
  419. key: 'class',
  420. value: 'styled'
  421. },
  422. model: {
  423. key: 'styled',
  424. value: () => null
  425. }
  426. } );
  427. conversion.for( 'upcast' ).add( helperA ).add( helperB );
  428. expectResult(
  429. new ViewAttributeElement( 'img', { class: 'styled' } ),
  430. '<image styled="true"></image>'
  431. );
  432. } );
  433. } );
  434. describe( 'upcastElementToMarker', () => {
  435. it( 'config.view is a string', () => {
  436. const helper = upcastElementToMarker( { view: 'marker-search', model: 'search' } );
  437. conversion.for( 'upcast' ).add( helper );
  438. const frag = new ViewDocumentFragment( [
  439. new ViewText( 'fo' ),
  440. new ViewUIElement( 'marker-search' ),
  441. new ViewText( 'oba' ),
  442. new ViewUIElement( 'marker-search' ),
  443. new ViewText( 'r' )
  444. ] );
  445. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  446. expectResult( frag, 'foobar', marker );
  447. } );
  448. it( 'can be overwritten using priority', () => {
  449. const helperA = upcastElementToMarker( { view: 'marker-search', model: 'search-result' } );
  450. const helperB = upcastElementToMarker( { view: 'marker-search', model: 'search', priority: 'high' } );
  451. conversion.for( 'upcast' ).add( helperA ).add( helperB );
  452. const frag = new ViewDocumentFragment( [
  453. new ViewText( 'fo' ),
  454. new ViewUIElement( 'marker-search' ),
  455. new ViewText( 'oba' ),
  456. new ViewUIElement( 'marker-search' ),
  457. new ViewText( 'r' )
  458. ] );
  459. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  460. expectResult( frag, 'foobar', marker );
  461. } );
  462. it( 'config.view is an object', () => {
  463. const helper = upcastElementToMarker( {
  464. view: {
  465. name: 'span',
  466. 'data-marker': 'search'
  467. },
  468. model: 'search'
  469. } );
  470. conversion.for( 'upcast' ).add( helper );
  471. const frag = new ViewDocumentFragment( [
  472. new ViewText( 'f' ),
  473. new ViewUIElement( 'span', { 'data-marker': 'search' } ),
  474. new ViewText( 'oob' ),
  475. new ViewUIElement( 'span', { 'data-marker': 'search' } ),
  476. new ViewText( 'ar' )
  477. ] );
  478. const marker = { name: 'search', start: [ 1 ], end: [ 4 ] };
  479. expectResult( frag, 'foobar', marker );
  480. } );
  481. it( 'config.model is a function', () => {
  482. const helper = upcastElementToMarker( {
  483. view: 'comment',
  484. model: viewElement => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
  485. } );
  486. conversion.for( 'upcast' ).add( helper );
  487. const frag = new ViewDocumentFragment( [
  488. new ViewText( 'foo' ),
  489. new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
  490. new ViewText( 'b' ),
  491. new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
  492. new ViewText( 'ar' )
  493. ] );
  494. const marker = { name: 'comment:4', start: [ 3 ], end: [ 4 ] };
  495. expectResult( frag, 'foobar', marker );
  496. } );
  497. it( 'marker is in a block element', () => {
  498. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'paragraph', view: 'p' } ) );
  499. const helper = upcastElementToMarker( { view: 'marker-search', model: 'search' } );
  500. conversion.for( 'upcast' ).add( helper );
  501. const element = new ViewContainerElement( 'p', null, [
  502. new ViewText( 'fo' ),
  503. new ViewUIElement( 'marker-search' ),
  504. new ViewText( 'oba' ),
  505. new ViewUIElement( 'marker-search' ),
  506. new ViewText( 'r' )
  507. ] );
  508. const marker = { name: 'search', start: [ 0, 2 ], end: [ 0, 5 ] };
  509. expectResult( element, '<paragraph>foobar</paragraph>', marker );
  510. } );
  511. } );
  512. function expectResult( viewToConvert, modelString, marker ) {
  513. const conversionResult = model.change( writer => upcastDispatcher.convert( viewToConvert, writer ) );
  514. if ( marker ) {
  515. expect( conversionResult.markers.has( marker.name ) ).to.be.true;
  516. const convertedMarker = conversionResult.markers.get( marker.name );
  517. expect( convertedMarker.start.path ).to.deep.equal( marker.start );
  518. expect( convertedMarker.end.path ).to.deep.equal( marker.end );
  519. }
  520. expect( stringify( conversionResult ) ).to.equal( modelString );
  521. }
  522. } );
  523. describe( 'upcast-converters', () => {
  524. let dispatcher, schema, context, model;
  525. beforeEach( () => {
  526. model = new Model();
  527. schema = model.schema;
  528. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  529. schema.extend( '$text', { allowIn: '$root' } );
  530. context = [ '$root' ];
  531. dispatcher = new UpcastDispatcher( { schema } );
  532. } );
  533. describe( 'convertText()', () => {
  534. it( 'should return converter converting ViewText to ModelText', () => {
  535. const viewText = new ViewText( 'foobar' );
  536. dispatcher.on( 'text', convertText() );
  537. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer ) );
  538. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  539. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  540. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  541. } );
  542. it( 'should not convert already consumed texts', () => {
  543. const viewText = new ViewText( 'foofuckbafuckr' );
  544. // Default converter for elements. Returns just converted children. Added with lowest priority.
  545. dispatcher.on( 'text', convertText(), { priority: 'lowest' } );
  546. // Added with normal priority. Should make the above converter not fire.
  547. dispatcher.on( 'text', ( evt, data, conversionApi ) => {
  548. if ( conversionApi.consumable.consume( data.viewItem ) ) {
  549. const text = conversionApi.writer.createText( data.viewItem.data.replace( /fuck/gi, '****' ) );
  550. conversionApi.writer.insert( text, data.modelCursor );
  551. data.modelRange = ModelRange.createFromPositionAndShift( data.modelCursor, text.offsetSize );
  552. data.modelCursor = data.modelRange.end;
  553. }
  554. } );
  555. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  556. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  557. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  558. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foo****ba****r' );
  559. } );
  560. it( 'should not convert text if it is wrong with schema', () => {
  561. schema.addChildCheck( ( ctx, childDef ) => {
  562. if ( childDef.name == '$text' && ctx.endsWith( '$root' ) ) {
  563. return false;
  564. }
  565. } );
  566. const viewText = new ViewText( 'foobar' );
  567. dispatcher.on( 'text', convertText() );
  568. let conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  569. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  570. expect( conversionResult.childCount ).to.equal( 0 );
  571. conversionResult = model.change( writer => dispatcher.convert( viewText, writer, [ '$block' ] ) );
  572. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  573. expect( conversionResult.childCount ).to.equal( 1 );
  574. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  575. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  576. } );
  577. it( 'should support unicode', () => {
  578. const viewText = new ViewText( 'நிலைக்கு' );
  579. dispatcher.on( 'text', convertText() );
  580. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  581. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  582. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  583. expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
  584. } );
  585. } );
  586. describe( 'convertToModelFragment()', () => {
  587. it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
  588. const viewFragment = new ViewDocumentFragment( [
  589. new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ),
  590. new ViewText( 'bar' )
  591. ] );
  592. // To get any meaningful results we have to actually convert something.
  593. dispatcher.on( 'text', convertText() );
  594. // This way P element won't be converted per-se but will fire converting it's children.
  595. dispatcher.on( 'element', convertToModelFragment() );
  596. dispatcher.on( 'documentFragment', convertToModelFragment() );
  597. const conversionResult = model.change( writer => dispatcher.convert( viewFragment, writer, context ) );
  598. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  599. expect( conversionResult.maxOffset ).to.equal( 6 );
  600. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  601. } );
  602. it( 'should not convert already consumed (converted) changes', () => {
  603. const viewP = new ViewContainerElement( 'p', null, new ViewText( 'foo' ) );
  604. // To get any meaningful results we have to actually convert something.
  605. dispatcher.on( 'text', convertText() );
  606. // Default converter for elements. Returns just converted children. Added with lowest priority.
  607. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  608. // Added with normal priority. Should make the above converter not fire.
  609. dispatcher.on( 'element:p', ( evt, data, conversionApi ) => {
  610. if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
  611. const paragraph = conversionApi.writer.createElement( 'paragraph' );
  612. conversionApi.writer.insert( paragraph, data.modelCursor );
  613. conversionApi.convertChildren( data.viewItem, ModelPosition.createAt( paragraph ) );
  614. data.modelRange = ModelRange.createOn( paragraph );
  615. data.modelCursor = data.modelRange.end;
  616. }
  617. } );
  618. const conversionResult = model.change( writer => dispatcher.convert( viewP, writer, context ) );
  619. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  620. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
  621. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  622. expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
  623. expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  624. } );
  625. it( 'should forward correct modelCursor', () => {
  626. const spy = sinon.spy();
  627. const view = new ViewDocumentFragment( [
  628. new ViewContainerElement( 'div', null, [ new ViewText( 'abc' ), new ViewContainerElement( 'foo' ) ] ),
  629. new ViewContainerElement( 'bar' )
  630. ] );
  631. const position = ModelPosition.createAt( new ModelElement( 'element' ) );
  632. dispatcher.on( 'documentFragment', convertToModelFragment() );
  633. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  634. dispatcher.on( 'element:foo', ( evt, data ) => {
  635. // Be sure that current cursor is not the same as custom.
  636. expect( data.modelCursor ).to.not.equal( position );
  637. // Set custom cursor as a result of docFrag last child conversion.
  638. // This cursor should be forwarded by a documentFragment converter.
  639. data.modelCursor = position;
  640. // Be sure that callback was fired.
  641. spy();
  642. } );
  643. dispatcher.on( 'element:bar', ( evt, data ) => {
  644. expect( data.modelCursor ).to.equal( position );
  645. spy();
  646. } );
  647. model.change( writer => dispatcher.convert( view, writer ) );
  648. sinon.assert.calledTwice( spy );
  649. } );
  650. } );
  651. } );