8
0

buildviewconverter.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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' ) ), context );
  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 from view element to model attribute', () => {
  99. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  100. const conversionResult = dispatcher.convert(
  101. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), context
  102. );
  103. // Have to check root because result is a ModelText.
  104. expect( modelToString( conversionResult ) ).to.equal( '<$text bold="true">foo</$text>' );
  105. } );
  106. it( 'should convert from view element to model attributes using creator function', () => {
  107. buildViewConverter().for( dispatcher )
  108. .fromElement( 'a' )
  109. .toAttribute( viewElement => ( { key: 'linkHref', value: viewElement.getAttribute( 'href' ) } ) );
  110. const conversionResult = dispatcher.convert(
  111. new ViewAttributeElement( 'a', { href: 'foo.html' }, new ViewText( 'foo' ) ), context
  112. );
  113. // Have to check root because result is a ModelText.
  114. expect( modelToString( conversionResult ) ).to.equal( '<$text linkHref="foo.html">foo</$text>' );
  115. } );
  116. it( 'should convert from view attribute to model attribute', () => {
  117. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  118. buildViewConverter().for( dispatcher )
  119. .fromAttribute( 'class' )
  120. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  121. const conversionResult = dispatcher.convert(
  122. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), context
  123. );
  124. expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  125. } );
  126. it( 'should convert from view attribute and key to model attribute', () => {
  127. schema.extend( 'paragraph', { allowAttributes: 'type' } );
  128. dispatcher.on( 'documentFragment', convertToModelFragment() );
  129. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  130. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'important' ).toAttribute( 'important', true );
  131. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'theme-nice' ).toAttribute( 'theme', 'nice' );
  132. buildViewConverter().for( dispatcher ).fromAttribute( 'data-type' ).toAttribute( 'type' );
  133. const viewStructure = new ViewDocumentFragment( [
  134. new ViewContainerElement( 'p', { class: 'important' }, new ViewText( 'foo' ) ),
  135. new ViewContainerElement( 'p', { class: 'important theme-nice' }, new ViewText( 'bar' ) ),
  136. new ViewContainerElement( 'p', { 'data-type': 'foo' }, new ViewText( 'xyz' ) )
  137. ] );
  138. const conversionResult = dispatcher.convert( viewStructure, context );
  139. expect( modelToString( conversionResult ) ).to.equal(
  140. '<paragraph important="true">foo</paragraph>' +
  141. '<paragraph important="true" theme="nice">bar</paragraph>' +
  142. '<paragraph type="foo">xyz</paragraph>'
  143. );
  144. } );
  145. it( 'should convert from multiple view entities to model attribute', () => {
  146. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  147. buildViewConverter().for( dispatcher )
  148. .fromElement( 'strong' )
  149. .fromElement( 'b' )
  150. .fromAttribute( 'class', 'bold' )
  151. .fromAttribute( 'style', { 'font-weight': 'bold' } )
  152. .toAttribute( 'bold', true );
  153. const viewElement = new ViewContainerElement( 'p', null, [
  154. new ViewAttributeElement( 'strong', null, new ViewText( 'aaa' ) ),
  155. new ViewAttributeElement( 'b', null, new ViewText( 'bbb' ) ),
  156. new ViewContainerElement( 'span', { class: 'bold' }, new ViewText( 'ccc' ) ),
  157. new ViewContainerElement( 'span', { style: 'font-weight:bold; font-size:20px' }, new ViewText( 'ddd' ) )
  158. ] );
  159. const conversionResult = dispatcher.convert( viewElement, context );
  160. expect( modelToString( conversionResult ) ).to.equal( '<paragraph><$text bold="true">aaabbbcccddd</$text></paragraph>' );
  161. } );
  162. it( 'should convert from pattern to marker', () => {
  163. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  164. buildViewConverter().for( dispatcher ).from( { attribute: { 'data-name': 'search' } } ).toMarker();
  165. const viewElement = new ViewContainerElement( 'p', null, [
  166. new ViewText( 'Fo' ),
  167. new ViewAttributeElement( 'marker', { 'data-name': 'search' } ),
  168. new ViewText( 'o ba' ),
  169. new ViewAttributeElement( 'marker', { 'data-name': 'search' } ),
  170. new ViewText( 'r' )
  171. ] );
  172. const conversionResult = dispatcher.convert( viewElement, context );
  173. const markerSearch = conversionResult.markers.get( 'search' );
  174. expect( conversionResult.markers.size ).to.equal( 1 );
  175. expect( markerSearch.start.path ).to.deep.equal( [ 0, 2 ] );
  176. expect( markerSearch.end.path ).to.deep.equal( [ 0, 6 ] );
  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, context );
  191. const markerSearch = conversionResult.markers.get( 'search' );
  192. expect( conversionResult.markers.size ).to.equal( 1 );
  193. expect( markerSearch.start.path ).to.deep.equal( [ 0, 2 ] );
  194. expect( markerSearch.end.path ).to.deep.equal( [ 0, 6 ] );
  195. } );
  196. it( 'should convert from multiple view entities to marker', () => {
  197. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  198. buildViewConverter().for( dispatcher )
  199. .from( { attribute: { 'foo': 'marker' } } )
  200. .from( { attribute: { 'bar': 'marker' } } )
  201. .from( { attribute: { 'foo': 'marker', 'bar': 'marker' } } )
  202. .toMarker();
  203. const viewElement = new ViewContainerElement( 'p', null, [
  204. new ViewText( 'Fo' ),
  205. new ViewAttributeElement( 'span', { 'foo': 'marker', 'data-name': 'marker1' } ),
  206. new ViewText( 'o b' ),
  207. new ViewAttributeElement( 'span', { 'bar': 'marker', 'data-name': 'marker2' } ),
  208. new ViewText( 'a' ),
  209. new ViewAttributeElement( 'span', { 'foo': 'marker', 'bar': 'marker', 'data-name': 'marker3' } ),
  210. new ViewText( 'r' )
  211. ] );
  212. const conversionResult = dispatcher.convert( viewElement );
  213. const marker1 = conversionResult.markers.get( 'marker1' );
  214. const marker2 = conversionResult.markers.get( 'marker2' );
  215. const marker3 = conversionResult.markers.get( 'marker3' );
  216. expect( conversionResult.markers.size ).to.equal( 3 );
  217. expect( marker1.start.path ).to.deep.equal( marker1.end.path ).to.deep.equal( [ 0, 2 ] );
  218. expect( marker2.start.path ).to.deep.equal( marker2.end.path ).to.deep.equal( [ 0, 5 ] );
  219. expect( marker3.start.path ).to.deep.equal( marker3.end.path ).to.deep.equal( [ 0, 6 ] );
  220. } );
  221. it( 'should do nothing when there is no element matching to marker pattern', () => {
  222. buildViewConverter().for( dispatcher ).from( { class: 'color' } ).toMarker();
  223. const element = new ViewAttributeElement( 'span' );
  224. const result = dispatcher.convert( element );
  225. expect( result ).to.be.instanceof( ModelDocumentFragment );
  226. expect( result.childCount ).to.equal( 0 );
  227. } );
  228. it( 'should throw an error when view element in not valid to convert to marker', () => {
  229. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker();
  230. const element = new ViewAttributeElement( 'marker', { class: 'search' } );
  231. expect( () => {
  232. dispatcher.convert( element, context );
  233. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  234. } );
  235. it( 'should throw an error when model element returned by creator has not valid name', () => {
  236. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
  237. return new ModelElement( 'element', { 'data-name': 'search' } );
  238. } );
  239. const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
  240. expect( () => {
  241. dispatcher.convert( element, context );
  242. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  243. } );
  244. it( 'should throw an error when model element returned by creator has not valid data-name attribute', () => {
  245. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
  246. return new ModelElement( '$marker', { 'foo': 'search' } );
  247. } );
  248. const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
  249. expect( () => {
  250. dispatcher.convert( element, context );
  251. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  252. } );
  253. it( 'should convert from pattern to model element', () => {
  254. buildViewConverter().for( dispatcher ).from(
  255. { name: 'span', class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } }
  256. ).toElement( 'MEGATRON' );
  257. // Adding callbacks later so they are called later. MEGATRON callback is more important.
  258. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  259. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  260. let result;
  261. // Not quite megatron.
  262. result = dispatcher.convert(
  263. new ViewContainerElement( 'span', { class: 'megatron' }, new ViewText( 'foo' ) ), context
  264. );
  265. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  266. // Almost a megatron. Missing a head.
  267. result = dispatcher.convert(
  268. new ViewContainerElement( 'span', { class: 'megatron', body: 'megatron', legs: 'megatron' }, new ViewText( 'foo' ) ),
  269. context
  270. );
  271. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  272. // This would be a megatron but is a paragraph.
  273. result = dispatcher.convert(
  274. new ViewContainerElement(
  275. 'p',
  276. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  277. new ViewText( 'foo' )
  278. ),
  279. context
  280. );
  281. expect( modelToString( result ) ).to.equal( '<paragraph>foo</paragraph>' );
  282. // At last we have a megatron!
  283. result = dispatcher.convert(
  284. new ViewContainerElement(
  285. 'span',
  286. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  287. new ViewText( 'foo' )
  288. ),
  289. context
  290. );
  291. expect( modelToString( result ) ).to.equal( '<MEGATRON>foo</MEGATRON>' );
  292. } );
  293. it( 'should convert from pattern to model attribute', () => {
  294. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  295. // This time without name so default span converter will convert children.
  296. buildViewConverter().for( dispatcher )
  297. .from( { class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } } )
  298. .toAttribute( 'transformer', 'megatron' );
  299. const viewElement = new ViewContainerElement(
  300. 'span',
  301. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  302. new ViewText( 'foo' )
  303. );
  304. const conversionResult = dispatcher.convert( viewElement, context );
  305. expect( modelToString( conversionResult ) ).to.equal( '<span transformer="megatron">foo</span>' );
  306. } );
  307. it( 'should return model document fragment when converting attributes on text', () => {
  308. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  309. const viewElement = new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) );
  310. const conversionResult = dispatcher.convert( viewElement, context );
  311. expect( conversionResult.is( 'documentFragment' ) ).to.be.true;
  312. } );
  313. it( 'should set different priorities for `toElement` and `toAttribute` conversion', () => {
  314. buildViewConverter().for( dispatcher )
  315. .fromAttribute( 'class' )
  316. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  317. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  318. const conversionResult = dispatcher.convert(
  319. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), context
  320. );
  321. // Element converter was fired first even though attribute converter was added first.
  322. expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  323. } );
  324. it( 'should overwrite default priorities for converters', () => {
  325. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  326. buildViewConverter().for( dispatcher )
  327. .fromAttribute( 'class' )
  328. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  329. let result;
  330. result = dispatcher.convert(
  331. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), context
  332. );
  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' ) ), context
  339. );
  340. expect( modelToString( result ) ).to.equal( '<customP>foo</customP>' );
  341. } );
  342. it( 'should overwrite default consumed values', () => {
  343. // Converter (1).
  344. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  345. // Converter (2).
  346. buildViewConverter().for( dispatcher )
  347. .from( { name: 'p', class: 'decorated' } ).consuming( { class: 'decorated' } )
  348. .toAttribute( 'decorated', true );
  349. // Converter (3).
  350. buildViewConverter().for( dispatcher )
  351. .fromAttribute( 'class', 'small' ).consuming( { class: 'small' } )
  352. .toAttribute( 'size', 'small' );
  353. const viewElement = new ViewContainerElement( 'p', { class: 'decorated small' }, new ViewText( 'foo' ) );
  354. const conversionResult = dispatcher.convert( viewElement, context );
  355. // P element and it's children got converted by the converter (1) and the converter (1) got fired
  356. // because P name was not consumed in converter (2). Converter (3) could consume class="small" because
  357. // only class="decorated" was consumed in converter (2).
  358. expect( modelToString( conversionResult ) ).to.equal( '<paragraph decorated="true" size="small">foo</paragraph>' );
  359. } );
  360. it( 'should convert from matcher instance to model', () => {
  361. // Universal class converter, synonymous to .fromAttribute( 'class' ).
  362. buildViewConverter().for( dispatcher )
  363. .from( new ViewMatcher( { class: /.*/ } ) )
  364. .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  365. // Universal element converter.
  366. buildViewConverter().for( dispatcher )
  367. .from( new ViewMatcher( { name: /.*/ } ) )
  368. .toElement( viewElement => new ModelElement( viewElement.name ) );
  369. const viewStructure = new ViewContainerElement( 'div', { class: 'myClass' }, [
  370. new ViewContainerElement( 'abcd', null, new ViewText( 'foo' ) )
  371. ] );
  372. const conversionResult = dispatcher.convert( viewStructure, context );
  373. expect( modelToString( conversionResult ) ).to.equal( '<div class="myClass"><abcd>foo</abcd></div>' );
  374. } );
  375. it( 'should filter out structure that is wrong with schema - elements', () => {
  376. buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
  377. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  378. // Disallow $root>div.
  379. schema.addChildCheck( ( ctx, childDef ) => {
  380. if ( childDef.name == 'div' && ctx.endsWith( '$root' ) ) {
  381. return false;
  382. }
  383. } );
  384. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  385. const viewElement = new ViewContainerElement( 'div', null,
  386. new ViewContainerElement( 'p', null,
  387. new ViewText( 'foo' )
  388. )
  389. );
  390. const conversionResult = dispatcher.convert( viewElement, context );
  391. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  392. } );
  393. // TMP We can't make this test work for now.
  394. // See https://github.com/ckeditor/ckeditor5-engine/issues/1213#issuecomment-354454906
  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.addAttributeCheck( ( ctx, attributeName ) => {
  401. // if ( ctx.endsWith( 'paragraph $text' ) && attributeName == 'bold' ) {
  402. // return false;
  403. // }
  404. // } );
  405. // dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  406. // const viewElement = new ViewContainerElement( 'p', null,
  407. // new ViewAttributeElement( 'strong', null,
  408. // new ViewText( 'foo' )
  409. // )
  410. // );
  411. // const conversionResult = dispatcher.convert( viewElement, context );
  412. // expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  413. // } );
  414. it( 'should not set attribute when it is not allowed', () => {
  415. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  416. buildViewConverter().for( dispatcher ).fromElement( 'u' ).toAttribute( 'underscore', true );
  417. const viewElement = new ViewContainerElement( 'p', null,
  418. new ViewAttributeElement( 'u', null,
  419. new ViewText( 'foo' )
  420. )
  421. );
  422. const conversionResult = dispatcher.convert( viewElement, context );
  423. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  424. } );
  425. it( 'should stop to element conversion if creating function returned null', () => {
  426. buildViewConverter()
  427. .for( dispatcher )
  428. .fromElement( 'p' )
  429. .toElement( viewElement => {
  430. return viewElement.hasAttribute( 'stop' ) ? null : new ModelElement( 'paragraph' );
  431. } );
  432. const viewElement = new ViewContainerElement( 'p' );
  433. let conversionResult = dispatcher.convert( viewElement, context );
  434. expect( modelToString( conversionResult ) ).to.equal( '<paragraph></paragraph>' );
  435. viewElement.setAttribute( 'stop', true );
  436. conversionResult = dispatcher.convert( viewElement, context );
  437. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  438. expect( conversionResult.childCount ).to.equal( 0 );
  439. } );
  440. it( 'should stop to attribute conversion if creating function returned null', () => {
  441. schema.extend( 'paragraph', { allowAttributes: 'type' } );
  442. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  443. buildViewConverter().for( dispatcher ).fromAttribute( 'data-type' ).toAttribute( viewElement => {
  444. const value = viewElement.getAttribute( 'data-type' );
  445. return value == 'stop' ? null : { key: 'type', value };
  446. } );
  447. const viewElement = new ViewContainerElement( 'p', { 'data-type': 'foo' } );
  448. let conversionResult = dispatcher.convert( viewElement, context );
  449. expect( modelToString( conversionResult ) ).to.equal( '<paragraph type="foo"></paragraph>' );
  450. viewElement.setAttribute( 'data-type', 'stop' );
  451. conversionResult = dispatcher.convert( viewElement, context );
  452. expect( modelToString( conversionResult ) ).to.equal( '<paragraph></paragraph>' );
  453. } );
  454. } );