8
0

upcast-converters.js 25 KB

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