buildviewconverter.js 23 KB

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