upcast-converters.js 27 KB

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