8
0

buildviewconverter.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import buildViewConverter from '../../src/conversion/buildviewconverter';
  6. import Model from '../../src/model/model';
  7. import ModelDocumentFragment from '../../src/model/documentfragment';
  8. import ModelElement from '../../src/model/element';
  9. import ModelTextProxy from '../../src/model/textproxy';
  10. import ModelRange from '../../src/model/range';
  11. import ModelWalker from '../../src/model/treewalker';
  12. import ViewDocumentFragment from '../../src/view/documentfragment';
  13. import ViewContainerElement from '../../src/view/containerelement';
  14. import ViewAttributeElement from '../../src/view/attributeelement';
  15. import ViewText from '../../src/view/text';
  16. import ViewMatcher from '../../src/view/matcher';
  17. import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
  18. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  19. import { convertToModelFragment, convertText } from '../../src/conversion/view-to-model-converters';
  20. import { stringify } from '../../src/dev-utils/model';
  21. function modelAttributesToString( item ) {
  22. let result = '';
  23. for ( const attr of item.getAttributes() ) {
  24. result += ' ' + attr[ 0 ] + '="' + attr[ 1 ] + '"';
  25. }
  26. return result;
  27. }
  28. function modelToString( item ) {
  29. let result = '';
  30. if ( item instanceof ModelTextProxy ) {
  31. const attributes = modelAttributesToString( item );
  32. result = attributes ? '<$text' + attributes + '>' + item.data + '</$text>' : item.data;
  33. } else {
  34. const walker = new ModelWalker( { boundaries: ModelRange.createIn( item ), shallow: true } );
  35. for ( const value of walker ) {
  36. result += modelToString( value.item );
  37. }
  38. if ( item instanceof ModelElement ) {
  39. const attributes = modelAttributesToString( item );
  40. result = '<' + item.name + attributes + '>' + result + '</' + item.name + '>';
  41. }
  42. }
  43. return result;
  44. }
  45. const textAttributes = [ 'linkHref', 'linkTitle', 'bold', 'italic', 'style' ];
  46. const pAttributes = [ 'class', 'important', 'theme', 'decorated', 'size' ];
  47. describe( 'View converter builder', () => {
  48. let dispatcher, model, schema, additionalData;
  49. beforeEach( () => {
  50. model = new Model();
  51. // `additionalData` parameter for `.convert` calls.
  52. additionalData = { context: [ '$root' ] };
  53. schema = model.schema;
  54. schema.register( 'paragraph', {
  55. inheritAllFrom: '$block',
  56. allowAttributes: pAttributes
  57. } );
  58. schema.register( 'div', {
  59. inheritAllFrom: '$block',
  60. allowAttributes: 'class'
  61. } );
  62. schema.register( 'customP', {
  63. inheritAllFrom: 'paragraph'
  64. } );
  65. schema.register( 'image', {
  66. inheritAllFrom: '$text',
  67. allowAttributes: 'src'
  68. } );
  69. schema.register( 'span', {
  70. inheritAllFrom: '$text',
  71. allowAttributes: 'transformer'
  72. } );
  73. // Yes, folks, we are building MEGATRON.
  74. schema.register( 'MEGATRON', {
  75. inheritAllFrom: '$text'
  76. } );
  77. schema.register( 'abcd', {
  78. inheritAllFrom: '$text'
  79. } );
  80. schema.extend( '$text', {
  81. allowAttributes: textAttributes
  82. } );
  83. dispatcher = new ViewConversionDispatcher( model, { schema } );
  84. dispatcher.on( 'text', convertText() );
  85. } );
  86. it( 'should convert from view element to model element', () => {
  87. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  88. const conversionResult = dispatcher.convert( new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ), additionalData );
  89. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  90. } );
  91. it( 'should convert from view element to model element using creator function', () => {
  92. buildViewConverter().for( dispatcher )
  93. .fromElement( 'img' )
  94. .toElement( viewElement => new ModelElement( 'image', { src: viewElement.getAttribute( 'src' ) } ) );
  95. const conversionResult = dispatcher.convert( new ViewContainerElement( 'img', { src: 'foo.jpg' } ), additionalData );
  96. expect( modelToString( conversionResult ) ).to.equal( '<image src="foo.jpg"></image>' );
  97. } );
  98. it( 'should convert from view element to model attribute', () => {
  99. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  100. const conversionResult = dispatcher.convert(
  101. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  102. );
  103. // Have to check root because result is a ModelText.
  104. expect( modelToString( conversionResult ) ).to.equal( '<$text bold="true">foo</$text>' );
  105. } );
  106. it( 'should convert from view element to model attributes using creator function', () => {
  107. buildViewConverter().for( dispatcher )
  108. .fromElement( 'a' )
  109. .toAttribute( viewElement => ( { key: 'linkHref', value: viewElement.getAttribute( 'href' ) } ) );
  110. const conversionResult = dispatcher.convert(
  111. new ViewAttributeElement( 'a', { href: 'foo.html' }, new ViewText( 'foo' ) ), additionalData
  112. );
  113. // Have to check root because result is a ModelText.
  114. expect( modelToString( conversionResult ) ).to.equal( '<$text linkHref="foo.html">foo</$text>' );
  115. } );
  116. it( 'should convert from view attribute to model attribute', () => {
  117. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  118. buildViewConverter().for( dispatcher )
  119. .fromAttribute( 'class' )
  120. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  121. const conversionResult = dispatcher.convert(
  122. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), additionalData
  123. );
  124. expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  125. } );
  126. it( 'should convert from view attribute and key to model attribute', () => {
  127. schema.extend( 'paragraph', { allowAttributes: 'type' } );
  128. dispatcher.on( 'documentFragment', convertToModelFragment() );
  129. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  130. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'important' ).toAttribute( 'important', true );
  131. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'theme-nice' ).toAttribute( 'theme', 'nice' );
  132. buildViewConverter().for( dispatcher ).fromAttribute( 'data-type' ).toAttribute( 'type' );
  133. const viewStructure = new ViewDocumentFragment( [
  134. new ViewContainerElement( 'p', { class: 'important' }, new ViewText( 'foo' ) ),
  135. new ViewContainerElement( 'p', { class: 'important theme-nice' }, new ViewText( 'bar' ) ),
  136. new ViewContainerElement( 'p', { 'data-type': 'foo' }, new ViewText( 'xyz' ) )
  137. ] );
  138. const conversionResult = dispatcher.convert( viewStructure, additionalData );
  139. expect( modelToString( conversionResult ) ).to.equal(
  140. '<paragraph important="true">foo</paragraph>' +
  141. '<paragraph important="true" theme="nice">bar</paragraph>' +
  142. '<paragraph type="foo">xyz</paragraph>'
  143. );
  144. } );
  145. it( 'should convert from multiple view entities to model attribute', () => {
  146. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  147. buildViewConverter().for( dispatcher )
  148. .fromElement( 'strong' )
  149. .fromElement( 'b' )
  150. .fromAttribute( 'class', 'bold' )
  151. .fromAttribute( 'style', { 'font-weight': 'bold' } )
  152. .toAttribute( 'bold', true );
  153. const viewElement = new ViewContainerElement( 'p', null, [
  154. new ViewAttributeElement( 'strong', null, new ViewText( 'aaa' ) ),
  155. new ViewAttributeElement( 'b', null, new ViewText( 'bbb' ) ),
  156. new ViewContainerElement( 'span', { class: 'bold' }, new ViewText( 'ccc' ) ),
  157. new ViewContainerElement( 'span', { style: 'font-weight:bold; font-size:20px' }, new ViewText( 'ddd' ) )
  158. ] );
  159. const conversionResult = dispatcher.convert( viewElement, additionalData );
  160. expect( modelToString( conversionResult ) ).to.equal( '<paragraph><$text bold="true">aaabbbcccddd</$text></paragraph>' );
  161. } );
  162. it( 'should convert from pattern to marker', () => {
  163. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  164. buildViewConverter().for( dispatcher ).from( { attribute: { 'data-name': 'search' } } ).toMarker();
  165. const viewElement = new ViewContainerElement( 'p', null, [
  166. new ViewText( 'Fo' ),
  167. new ViewAttributeElement( 'marker', { 'data-name': 'search' } ),
  168. new ViewText( 'o ba' ),
  169. new ViewAttributeElement( 'marker', { 'data-name': 'search' } ),
  170. new ViewText( 'r' )
  171. ] );
  172. const conversionResult = dispatcher.convert( viewElement, additionalData );
  173. const markerSearch = conversionResult.markers.get( 'search' );
  174. expect( conversionResult.markers.size ).to.equal( 1 );
  175. expect( stringify( conversionResult, markerSearch ) ).to.equal( '<paragraph>Fo[o ba]r</paragraph>' );
  176. } );
  177. it( 'should convert from element to marker using creator function', () => {
  178. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  179. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( data => {
  180. return new ModelElement( '$marker', { 'data-name': data.getAttribute( 'class' ) } );
  181. } );
  182. const viewElement = new ViewContainerElement( 'p', null, [
  183. new ViewText( 'Fo' ),
  184. new ViewAttributeElement( 'marker', { 'class': 'search' } ),
  185. new ViewText( 'o ba' ),
  186. new ViewAttributeElement( 'marker', { 'class': 'search' } ),
  187. new ViewText( 'r' )
  188. ] );
  189. const conversionResult = dispatcher.convert( viewElement, additionalData );
  190. const markerSearch = conversionResult.markers.get( 'search' );
  191. expect( conversionResult.markers.size ).to.equal( 1 );
  192. expect( stringify( conversionResult, markerSearch ) ).to.equal( '<paragraph>Fo[o ba]r</paragraph>' );
  193. } );
  194. it( 'should convert from multiple view entities to marker', () => {
  195. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  196. buildViewConverter().for( dispatcher )
  197. .from( { attribute: { 'foo': 'marker' } } )
  198. .from( { attribute: { 'bar': 'marker' } } )
  199. .from( { attribute: { 'foo': 'marker', 'bar': 'marker' } } )
  200. .toMarker();
  201. const viewElement = new ViewContainerElement( 'p', null, [
  202. new ViewText( 'Fo' ),
  203. new ViewAttributeElement( 'span', { 'foo': 'marker', 'data-name': 'marker1' } ),
  204. new ViewText( 'o b' ),
  205. new ViewAttributeElement( 'span', { 'bar': 'marker', 'data-name': 'marker2' } ),
  206. new ViewText( 'a' ),
  207. new ViewAttributeElement( 'span', { 'foo': 'marker', 'bar': 'marker', 'data-name': 'marker3' } ),
  208. new ViewText( 'r' )
  209. ] );
  210. const conversionResult = dispatcher.convert( viewElement, additionalData );
  211. const marker1 = conversionResult.markers.get( 'marker1' );
  212. const marker2 = conversionResult.markers.get( 'marker2' );
  213. const marker3 = conversionResult.markers.get( 'marker3' );
  214. expect( conversionResult.markers.size ).to.equal( 3 );
  215. expect( stringify( conversionResult, marker1 ) ).to.equal( '<paragraph>Fo[]o bar</paragraph>' );
  216. expect( stringify( conversionResult, marker2 ) ).to.equal( '<paragraph>Foo b[]ar</paragraph>' );
  217. expect( stringify( conversionResult, marker3 ) ).to.equal( '<paragraph>Foo ba[]r</paragraph>' );
  218. } );
  219. it( 'should do nothing when there is no element matching to marker pattern', () => {
  220. buildViewConverter().for( dispatcher ).from( { class: 'color' } ).toMarker();
  221. const element = new ViewAttributeElement( 'span' );
  222. const result = dispatcher.convert( element, additionalData );
  223. expect( result ).to.be.instanceof( ModelDocumentFragment );
  224. expect( result.childCount ).to.equal( 0 );
  225. } );
  226. it( 'should throw an error when view element in not valid to convert to marker', () => {
  227. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker();
  228. const element = new ViewAttributeElement( 'marker', { class: 'search' } );
  229. expect( () => {
  230. dispatcher.convert( element, additionalData );
  231. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  232. } );
  233. it( 'should throw an error when model element returned by creator has not valid name', () => {
  234. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
  235. return new ModelElement( 'element', { 'data-name': 'search' } );
  236. } );
  237. const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
  238. expect( () => {
  239. dispatcher.convert( element, additionalData );
  240. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  241. } );
  242. it( 'should throw an error when model element returned by creator has not valid data-name attribute', () => {
  243. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
  244. return new ModelElement( '$marker', { 'foo': 'search' } );
  245. } );
  246. const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
  247. expect( () => {
  248. dispatcher.convert( element, additionalData );
  249. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  250. } );
  251. it( 'should convert from pattern to model element', () => {
  252. buildViewConverter().for( dispatcher ).from(
  253. { name: 'span', class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } }
  254. ).toElement( 'MEGATRON' );
  255. // Adding callbacks later so they are called later. MEGATRON callback is more important.
  256. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  257. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  258. let result;
  259. // Not quite megatron.
  260. result = dispatcher.convert(
  261. new ViewContainerElement( 'span', { class: 'megatron' }, new ViewText( 'foo' ) ), additionalData
  262. );
  263. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  264. // Almost a megatron. Missing a head.
  265. result = dispatcher.convert(
  266. new ViewContainerElement( 'span', { class: 'megatron', body: 'megatron', legs: 'megatron' }, new ViewText( 'foo' ) ),
  267. additionalData
  268. );
  269. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  270. // This would be a megatron but is a paragraph.
  271. result = dispatcher.convert(
  272. new ViewContainerElement(
  273. 'p',
  274. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  275. new ViewText( 'foo' )
  276. ),
  277. additionalData
  278. );
  279. expect( modelToString( result ) ).to.equal( '<paragraph>foo</paragraph>' );
  280. // At last we have a megatron!
  281. result = dispatcher.convert(
  282. new ViewContainerElement(
  283. 'span',
  284. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  285. new ViewText( 'foo' )
  286. ),
  287. additionalData
  288. );
  289. expect( modelToString( result ) ).to.equal( '<MEGATRON>foo</MEGATRON>' );
  290. } );
  291. it( 'should convert from pattern to model attribute', () => {
  292. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  293. // This time without name so default span converter will convert children.
  294. buildViewConverter().for( dispatcher )
  295. .from( { class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } } )
  296. .toAttribute( 'transformer', 'megatron' );
  297. const viewElement = new ViewContainerElement(
  298. 'span',
  299. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  300. new ViewText( 'foo' )
  301. );
  302. const conversionResult = dispatcher.convert( viewElement, additionalData );
  303. expect( modelToString( conversionResult ) ).to.equal( '<span transformer="megatron">foo</span>' );
  304. } );
  305. it( 'should return model document fragment when converting attributes on text', () => {
  306. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  307. const viewElement = new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) );
  308. const conversionResult = dispatcher.convert( viewElement, additionalData );
  309. expect( conversionResult.is( 'documentFragment' ) ).to.be.true;
  310. } );
  311. it( 'should set different priorities for `toElement` and `toAttribute` conversion', () => {
  312. buildViewConverter().for( dispatcher )
  313. .fromAttribute( 'class' )
  314. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  315. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  316. const conversionResult = dispatcher.convert(
  317. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), additionalData
  318. );
  319. // Element converter was fired first even though attribute converter was added first.
  320. expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  321. } );
  322. it( 'should overwrite default priorities for converters', () => {
  323. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  324. buildViewConverter().for( dispatcher )
  325. .fromAttribute( 'class' )
  326. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  327. let result;
  328. result = dispatcher.convert(
  329. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), additionalData
  330. );
  331. expect( modelToString( result ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  332. buildViewConverter().for( dispatcher )
  333. .from( { name: 'p', class: 'myClass' } ).withPriority( 'high' )
  334. .toElement( 'customP' );
  335. result = dispatcher.convert(
  336. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), additionalData
  337. );
  338. expect( modelToString( result ) ).to.equal( '<customP>foo</customP>' );
  339. } );
  340. it( 'should overwrite default consumed values', () => {
  341. // Converter (1).
  342. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  343. // Converter (2).
  344. buildViewConverter().for( dispatcher )
  345. .from( { name: 'p', class: 'decorated' } ).consuming( { class: 'decorated' } )
  346. .toAttribute( 'decorated', true );
  347. // Converter (3).
  348. buildViewConverter().for( dispatcher )
  349. .fromAttribute( 'class', 'small' ).consuming( { class: 'small' } )
  350. .toAttribute( 'size', 'small' );
  351. const viewElement = new ViewContainerElement( 'p', { class: 'decorated small' }, new ViewText( 'foo' ) );
  352. const conversionResult = dispatcher.convert( viewElement, additionalData );
  353. // P element and it's children got converted by the converter (1) and the converter (1) got fired
  354. // because P name was not consumed in converter (2). Converter (3) could consume class="small" because
  355. // only class="decorated" was consumed in converter (2).
  356. expect( modelToString( conversionResult ) ).to.equal( '<paragraph decorated="true" size="small">foo</paragraph>' );
  357. } );
  358. it( 'should convert from matcher instance to model', () => {
  359. // Universal class converter, synonymous to .fromAttribute( 'class' ).
  360. buildViewConverter().for( dispatcher )
  361. .from( new ViewMatcher( { class: /.*/ } ) )
  362. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  363. // Universal element converter.
  364. buildViewConverter().for( dispatcher )
  365. .from( new ViewMatcher( { name: /.*/ } ) )
  366. .toElement( viewElement => new ModelElement( viewElement.name ) );
  367. const viewStructure = new ViewContainerElement( 'div', { class: 'myClass' }, [
  368. new ViewContainerElement( 'abcd', null, new ViewText( 'foo' ) )
  369. ] );
  370. const conversionResult = dispatcher.convert( viewStructure, additionalData );
  371. expect( modelToString( conversionResult ) ).to.equal( '<div class="myClass"><abcd>foo</abcd></div>' );
  372. } );
  373. it( 'should filter out structure that is wrong with schema - elements', () => {
  374. buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
  375. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  376. // TODO this requires a callback
  377. // schema.xdisallow( { name: 'div', inside: '$root' } );
  378. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  379. const viewElement = new ViewContainerElement( 'div', null,
  380. new ViewContainerElement( 'p', null,
  381. new ViewText( 'foo' )
  382. )
  383. );
  384. const conversionResult = dispatcher.convert( viewElement, additionalData );
  385. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  386. } );
  387. it( 'should filter out structure that is wrong with schema - attributes', () => {
  388. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  389. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  390. // TODO this requires a callback
  391. // schema.xdisallow( { name: '$text', attributes: 'bold', inside: 'paragraph' } );
  392. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  393. const viewElement = new ViewContainerElement( 'p', null,
  394. new ViewAttributeElement( 'strong', null,
  395. new ViewText( 'foo' )
  396. )
  397. );
  398. const conversionResult = dispatcher.convert( viewElement, additionalData );
  399. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  400. } );
  401. it( 'should stop to element conversion if creating function returned null', () => {
  402. buildViewConverter()
  403. .for( dispatcher )
  404. .fromElement( 'p' )
  405. .toElement( viewElement => {
  406. return viewElement.hasAttribute( 'stop' ) ? null : new ModelElement( 'paragraph' );
  407. } );
  408. const viewElement = new ViewContainerElement( 'p' );
  409. let conversionResult = dispatcher.convert( viewElement, additionalData );
  410. expect( modelToString( conversionResult ) ).to.equal( '<paragraph></paragraph>' );
  411. viewElement.setAttribute( 'stop', true );
  412. conversionResult = dispatcher.convert( viewElement, additionalData );
  413. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  414. expect( conversionResult.childCount ).to.equal( 0 );
  415. } );
  416. it( 'should stop to attribute conversion if creating function returned null', () => {
  417. schema.extend( 'paragraph', { allowAttributes: 'type' } );
  418. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  419. buildViewConverter().for( dispatcher ).fromAttribute( 'data-type' ).toAttribute( viewElement => {
  420. const value = viewElement.getAttribute( 'data-type' );
  421. return value == 'stop' ? null : { key: 'type', value };
  422. } );
  423. const viewElement = new ViewContainerElement( 'p', { 'data-type': 'foo' } );
  424. let conversionResult = dispatcher.convert( viewElement, additionalData );
  425. expect( modelToString( conversionResult ) ).to.equal( '<paragraph type="foo"></paragraph>' );
  426. viewElement.setAttribute( 'data-type', 'stop' );
  427. conversionResult = dispatcher.convert( viewElement, additionalData );
  428. expect( modelToString( conversionResult ) ).to.equal( '<paragraph></paragraph>' );
  429. } );
  430. } );