8
0

buildviewconverter.js 23 KB

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