upcast-converters.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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 converterPriority', () => {
  49. schema.register( 'p', {
  50. inheritAllFrom: '$block'
  51. } );
  52. const helperA = upcastElementToElement( { view: 'p', model: 'p' } );
  53. const helperB = upcastElementToElement( { view: 'p', model: 'paragraph', converterPriority: '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, converterPriority: '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 converterPriority', () => {
  133. const helperA = upcastElementToAttribute( { view: 'strong', model: 'strong' } );
  134. const helperB = upcastElementToAttribute( { view: 'strong', model: 'bold', converterPriority: '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. converterPriority: '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 converterPriority', () => {
  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', converterPriority: '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. // #1443.
  438. it( 'should not set attributes on the element\'s children', () => {
  439. schema.register( 'div', {
  440. inheritAllFrom: '$root',
  441. allowWhere: '$block',
  442. isLimit: true,
  443. allowAttributes: [ 'border', 'shade' ]
  444. } );
  445. conversion.for( 'upcast' ).add( upcastElementToElement( { view: 'div', model: 'div' } ) );
  446. const shadeHelper = upcastAttributeToAttribute( { view: { key: 'class', value: 'shade' }, model: 'shade' } );
  447. const borderHelper = upcastAttributeToAttribute( { view: { key: 'class', value: 'border' }, model: 'border' } );
  448. conversion.for( 'upcast' ).add( shadeHelper );
  449. conversion.for( 'upcast' ).add( borderHelper );
  450. expectResult(
  451. new ViewContainerElement(
  452. 'div',
  453. { class: 'border' },
  454. new ViewContainerElement( 'div', { class: 'shade' } )
  455. ),
  456. '<div border="border"><div shade="shade"></div></div>'
  457. );
  458. } );
  459. } );
  460. describe( 'upcastElementToMarker', () => {
  461. it( 'config.view is a string', () => {
  462. const helper = upcastElementToMarker( { view: 'marker-search', model: 'search' } );
  463. conversion.for( 'upcast' ).add( helper );
  464. const frag = new ViewDocumentFragment( [
  465. new ViewText( 'fo' ),
  466. new ViewUIElement( 'marker-search' ),
  467. new ViewText( 'oba' ),
  468. new ViewUIElement( 'marker-search' ),
  469. new ViewText( 'r' )
  470. ] );
  471. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  472. expectResult( frag, 'foobar', marker );
  473. } );
  474. it( 'can be overwritten using converterPriority', () => {
  475. const helperA = upcastElementToMarker( { view: 'marker-search', model: 'search-result' } );
  476. const helperB = upcastElementToMarker( { view: 'marker-search', model: 'search', converterPriority: 'high' } );
  477. conversion.for( 'upcast' ).add( helperA ).add( helperB );
  478. const frag = new ViewDocumentFragment( [
  479. new ViewText( 'fo' ),
  480. new ViewUIElement( 'marker-search' ),
  481. new ViewText( 'oba' ),
  482. new ViewUIElement( 'marker-search' ),
  483. new ViewText( 'r' )
  484. ] );
  485. const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
  486. expectResult( frag, 'foobar', marker );
  487. } );
  488. it( 'config.view is an object', () => {
  489. const helper = upcastElementToMarker( {
  490. view: {
  491. name: 'span',
  492. 'data-marker': 'search'
  493. },
  494. model: 'search'
  495. } );
  496. conversion.for( 'upcast' ).add( helper );
  497. const frag = new ViewDocumentFragment( [
  498. new ViewText( 'f' ),
  499. new ViewUIElement( 'span', { 'data-marker': 'search' } ),
  500. new ViewText( 'oob' ),
  501. new ViewUIElement( 'span', { 'data-marker': 'search' } ),
  502. new ViewText( 'ar' )
  503. ] );
  504. const marker = { name: 'search', start: [ 1 ], end: [ 4 ] };
  505. expectResult( frag, 'foobar', marker );
  506. } );
  507. it( 'config.model is a function', () => {
  508. const helper = upcastElementToMarker( {
  509. view: 'comment',
  510. model: viewElement => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
  511. } );
  512. conversion.for( 'upcast' ).add( helper );
  513. const frag = new ViewDocumentFragment( [
  514. new ViewText( 'foo' ),
  515. new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
  516. new ViewText( 'b' ),
  517. new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
  518. new ViewText( 'ar' )
  519. ] );
  520. const marker = { name: 'comment:4', start: [ 3 ], end: [ 4 ] };
  521. expectResult( frag, 'foobar', marker );
  522. } );
  523. it( 'marker is in a block element', () => {
  524. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'paragraph', view: 'p' } ) );
  525. const helper = upcastElementToMarker( { view: 'marker-search', model: 'search' } );
  526. conversion.for( 'upcast' ).add( helper );
  527. const element = new ViewContainerElement( 'p', null, [
  528. new ViewText( 'fo' ),
  529. new ViewUIElement( 'marker-search' ),
  530. new ViewText( 'oba' ),
  531. new ViewUIElement( 'marker-search' ),
  532. new ViewText( 'r' )
  533. ] );
  534. const marker = { name: 'search', start: [ 0, 2 ], end: [ 0, 5 ] };
  535. expectResult( element, '<paragraph>foobar</paragraph>', marker );
  536. } );
  537. } );
  538. function expectResult( viewToConvert, modelString, marker ) {
  539. const conversionResult = model.change( writer => upcastDispatcher.convert( viewToConvert, writer ) );
  540. if ( marker ) {
  541. expect( conversionResult.markers.has( marker.name ) ).to.be.true;
  542. const convertedMarker = conversionResult.markers.get( marker.name );
  543. expect( convertedMarker.start.path ).to.deep.equal( marker.start );
  544. expect( convertedMarker.end.path ).to.deep.equal( marker.end );
  545. }
  546. expect( stringify( conversionResult ) ).to.equal( modelString );
  547. }
  548. } );
  549. describe( 'upcast-converters', () => {
  550. let dispatcher, schema, context, model;
  551. beforeEach( () => {
  552. model = new Model();
  553. schema = model.schema;
  554. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  555. schema.extend( '$text', { allowIn: '$root' } );
  556. context = [ '$root' ];
  557. dispatcher = new UpcastDispatcher( { schema } );
  558. } );
  559. describe( 'convertText()', () => {
  560. it( 'should return converter converting ViewText to ModelText', () => {
  561. const viewText = new ViewText( 'foobar' );
  562. dispatcher.on( 'text', convertText() );
  563. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer ) );
  564. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  565. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  566. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  567. } );
  568. it( 'should not convert already consumed texts', () => {
  569. const viewText = new ViewText( 'foofuckbafuckr' );
  570. // Default converter for elements. Returns just converted children. Added with lowest priority.
  571. dispatcher.on( 'text', convertText(), { priority: 'lowest' } );
  572. // Added with normal priority. Should make the above converter not fire.
  573. dispatcher.on( 'text', ( evt, data, conversionApi ) => {
  574. if ( conversionApi.consumable.consume( data.viewItem ) ) {
  575. const text = conversionApi.writer.createText( data.viewItem.data.replace( /fuck/gi, '****' ) );
  576. conversionApi.writer.insert( text, data.modelCursor );
  577. data.modelRange = ModelRange.createFromPositionAndShift( data.modelCursor, text.offsetSize );
  578. data.modelCursor = data.modelRange.end;
  579. }
  580. } );
  581. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  582. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  583. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  584. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foo****ba****r' );
  585. } );
  586. it( 'should not convert text if it is wrong with schema', () => {
  587. schema.addChildCheck( ( ctx, childDef ) => {
  588. if ( childDef.name == '$text' && ctx.endsWith( '$root' ) ) {
  589. return false;
  590. }
  591. } );
  592. const viewText = new ViewText( 'foobar' );
  593. dispatcher.on( 'text', convertText() );
  594. let conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  595. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  596. expect( conversionResult.childCount ).to.equal( 0 );
  597. conversionResult = model.change( writer => dispatcher.convert( viewText, writer, [ '$block' ] ) );
  598. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  599. expect( conversionResult.childCount ).to.equal( 1 );
  600. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  601. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  602. } );
  603. it( 'should support unicode', () => {
  604. const viewText = new ViewText( 'நிலைக்கு' );
  605. dispatcher.on( 'text', convertText() );
  606. const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
  607. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  608. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  609. expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
  610. } );
  611. } );
  612. describe( 'convertToModelFragment()', () => {
  613. it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
  614. const viewFragment = new ViewDocumentFragment( [
  615. new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ),
  616. new ViewText( 'bar' )
  617. ] );
  618. // To get any meaningful results we have to actually convert something.
  619. dispatcher.on( 'text', convertText() );
  620. // This way P element won't be converted per-se but will fire converting it's children.
  621. dispatcher.on( 'element', convertToModelFragment() );
  622. dispatcher.on( 'documentFragment', convertToModelFragment() );
  623. const conversionResult = model.change( writer => dispatcher.convert( viewFragment, writer, context ) );
  624. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  625. expect( conversionResult.maxOffset ).to.equal( 6 );
  626. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  627. } );
  628. it( 'should not convert already consumed (converted) changes', () => {
  629. const viewP = new ViewContainerElement( 'p', null, new ViewText( 'foo' ) );
  630. // To get any meaningful results we have to actually convert something.
  631. dispatcher.on( 'text', convertText() );
  632. // Default converter for elements. Returns just converted children. Added with lowest priority.
  633. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  634. // Added with normal priority. Should make the above converter not fire.
  635. dispatcher.on( 'element:p', ( evt, data, conversionApi ) => {
  636. if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
  637. const paragraph = conversionApi.writer.createElement( 'paragraph' );
  638. conversionApi.writer.insert( paragraph, data.modelCursor );
  639. conversionApi.convertChildren( data.viewItem, ModelPosition.createAt( paragraph ) );
  640. data.modelRange = ModelRange.createOn( paragraph );
  641. data.modelCursor = data.modelRange.end;
  642. }
  643. } );
  644. const conversionResult = model.change( writer => dispatcher.convert( viewP, writer, context ) );
  645. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  646. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
  647. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  648. expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
  649. expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  650. } );
  651. it( 'should forward correct modelCursor', () => {
  652. const spy = sinon.spy();
  653. const view = new ViewDocumentFragment( [
  654. new ViewContainerElement( 'div', null, [ new ViewText( 'abc' ), new ViewContainerElement( 'foo' ) ] ),
  655. new ViewContainerElement( 'bar' )
  656. ] );
  657. const position = ModelPosition.createAt( new ModelElement( 'element' ) );
  658. dispatcher.on( 'documentFragment', convertToModelFragment() );
  659. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  660. dispatcher.on( 'element:foo', ( evt, data ) => {
  661. // Be sure that current cursor is not the same as custom.
  662. expect( data.modelCursor ).to.not.equal( position );
  663. // Set custom cursor as a result of docFrag last child conversion.
  664. // This cursor should be forwarded by a documentFragment converter.
  665. data.modelCursor = position;
  666. // Be sure that callback was fired.
  667. spy();
  668. } );
  669. dispatcher.on( 'element:bar', ( evt, data ) => {
  670. expect( data.modelCursor ).to.equal( position );
  671. spy();
  672. } );
  673. model.change( writer => dispatcher.convert( view, writer ) );
  674. sinon.assert.calledTwice( spy );
  675. } );
  676. } );
  677. } );