buildviewconverter.js 22 KB

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