buildviewconverter.js 23 KB

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