buildviewconverter.js 23 KB

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