buildviewconverter.js 22 KB

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