buildviewconverter.js 22 KB

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