buildviewconverter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: conversion */
  6. import buildViewConverter from '/ckeditor5/engine/conversion/buildviewconverter.js';
  7. import ModelSchema from '/ckeditor5/engine/model/schema.js';
  8. import ModelDocument from '/ckeditor5/engine/model/document.js';
  9. import ModelElement from '/ckeditor5/engine/model/element.js';
  10. import ModelTextProxy from '/ckeditor5/engine/model/textproxy.js';
  11. import ModelRange from '/ckeditor5/engine/model/range.js';
  12. import ModelWalker from '/ckeditor5/engine/model/treewalker.js';
  13. import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  14. import ViewContainerElement from '/ckeditor5/engine/view/containerelement.js';
  15. import ViewAttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  16. import ViewText from '/ckeditor5/engine/view/text.js';
  17. import ViewMatcher from '/ckeditor5/engine/view/matcher.js';
  18. import ViewConversionDispatcher from '/ckeditor5/engine/conversion/viewconversiondispatcher.js';
  19. import { convertToModelFragment, convertText } from '/ckeditor5/engine/conversion/view-to-model-converters.js';
  20. function modelAttributesToString( item ) {
  21. let result = '';
  22. for ( let 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. let attributes = modelAttributesToString( item );
  31. result = attributes ? '<$text' + attributes + '>' + item.data + '</$text>' : item.data;
  32. } else {
  33. let walker = new ModelWalker( { boundaries: ModelRange.createIn( item ), shallow: true } );
  34. for ( let value of walker ) {
  35. result += modelToString( value.item );
  36. }
  37. if ( item instanceof ModelElement ) {
  38. let attributes = modelAttributesToString( item );
  39. result = '<' + item.name + attributes + '>' + result + '</' + item.name + '>';
  40. }
  41. }
  42. return result;
  43. }
  44. const textAttributes = [ undefined, 'linkHref', 'linkTitle', 'bold', 'italic', 'style' ];
  45. const pAttributes = [ undefined, 'class', 'important', 'theme', 'decorated', 'size' ];
  46. describe( 'View converter builder', () => {
  47. let dispatcher, modelDoc, modelRoot, schema, objWithContext;
  48. beforeEach( () => {
  49. // `additionalData` parameter for `.convert` calls.
  50. objWithContext = { context: [ '$root' ] };
  51. schema = new ModelSchema();
  52. schema.registerItem( 'paragraph', '$block' );
  53. schema.registerItem( 'div', '$block' );
  54. schema.registerItem( 'customP', 'paragraph' );
  55. schema.registerItem( 'image', '$inline' );
  56. schema.registerItem( 'span', '$inline' );
  57. schema.registerItem( 'MEGATRON', '$inline' ); // Yes, folks, we are building MEGATRON.
  58. schema.registerItem( 'abcd', '$inline' );
  59. schema.allow( { name: '$inline', attributes: textAttributes, inside: '$root' } );
  60. schema.allow( { name: 'image', attributes: [ 'src' ], inside: '$root' } );
  61. schema.allow( { name: 'image', attributes: [ 'src' ], inside: '$block' } );
  62. schema.allow( { name: '$text', attributes: textAttributes, inside: '$block' } );
  63. schema.allow( { name: '$text', attributes: textAttributes, inside: '$root' } );
  64. schema.allow( { name: 'paragraph', attributes: pAttributes, inside: '$root' } );
  65. schema.allow( { name: 'span', attributes: [ 'transformer' ], inside: '$root' } );
  66. schema.allow( { name: 'div', attributes: [ 'class' ], inside: '$root' } );
  67. dispatcher = new ViewConversionDispatcher( { schema } );
  68. dispatcher.on( 'text', convertText() );
  69. modelDoc = new ModelDocument();
  70. modelRoot = modelDoc.createRoot();
  71. } );
  72. it( 'should convert from view element to model element', () => {
  73. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  74. const result = dispatcher.convert( new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ), objWithContext );
  75. modelRoot.appendChildren( result );
  76. expect( modelToString( result ) ).to.equal( '<paragraph>foo</paragraph>' );
  77. } );
  78. it( 'should convert from view element to model element using creator function', () => {
  79. buildViewConverter().for( dispatcher )
  80. .fromElement( 'img' )
  81. .toElement( ( viewElement ) => new ModelElement( 'image', { src: viewElement.getAttribute( 'src' ) } ) );
  82. const result = dispatcher.convert( new ViewContainerElement( 'img', { src: 'foo.jpg' } ), objWithContext );
  83. modelRoot.appendChildren( result );
  84. expect( modelToString( result ) ).to.equal( '<image src="foo.jpg"></image>' );
  85. } );
  86. it( 'should convert from view element to model attribute', () => {
  87. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  88. const result = dispatcher.convert( new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), objWithContext );
  89. modelRoot.appendChildren( result );
  90. // Have to check root because result is a ModelText.
  91. expect( modelToString( modelRoot ) ).to.equal( '<$root><$text bold="true">foo</$text></$root>' );
  92. } );
  93. it( 'should convert from view element to model attributes using creator function', () => {
  94. buildViewConverter().for( dispatcher )
  95. .fromElement( 'a' )
  96. .toAttribute( ( viewElement ) => ( { key: 'linkHref', value: viewElement.getAttribute( 'href' ) } ) );
  97. const result = dispatcher.convert( new ViewAttributeElement( 'a', { href: 'foo.html' }, new ViewText( 'foo' ) ), objWithContext );
  98. modelRoot.appendChildren( result );
  99. // Have to check root because result is a ModelText.
  100. expect( modelToString( modelRoot ) ).to.equal( '<$root><$text linkHref="foo.html">foo</$text></$root>' );
  101. } );
  102. it( 'should convert from view attribute to model attribute', () => {
  103. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  104. buildViewConverter().for( dispatcher )
  105. .fromAttribute( 'class' )
  106. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  107. const result = dispatcher.convert( new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext );
  108. modelRoot.appendChildren( result );
  109. expect( modelToString( result ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  110. } );
  111. it( 'should convert from view attribute and key to model attribute', () => {
  112. dispatcher.on( 'documentFragment', convertToModelFragment() );
  113. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  114. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'important' ).toAttribute( 'important', true );
  115. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'theme-nice' ).toAttribute( 'theme', 'nice' );
  116. const viewStructure = new ViewDocumentFragment( [
  117. new ViewContainerElement( 'p', { class: 'important' }, new ViewText( 'foo' ) ),
  118. new ViewContainerElement( 'p', { class: 'important theme-nice' }, new ViewText( 'bar' ) )
  119. ] );
  120. const result = dispatcher.convert( viewStructure, objWithContext );
  121. expect( modelToString( result ) )
  122. .to.equal( '<paragraph important="true">foo</paragraph><paragraph important="true" theme="nice">bar</paragraph>' );
  123. } );
  124. it( 'should convert from multiple view entities to model attribute', () => {
  125. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  126. buildViewConverter().for( dispatcher )
  127. .fromElement( 'strong' )
  128. .fromElement( 'b' )
  129. .fromAttribute( 'class', 'bold' )
  130. .fromAttribute( 'style', { 'font-weight': 'bold' } )
  131. .toAttribute( 'bold', true );
  132. const viewElement = new ViewContainerElement( 'p', null, [
  133. new ViewAttributeElement( 'strong', null, new ViewText( 'aaa' ) ),
  134. new ViewAttributeElement( 'b', null, new ViewText( 'bbb' ) ),
  135. new ViewContainerElement( 'span', { class: 'bold' }, new ViewText( 'ccc' ) ),
  136. new ViewContainerElement( 'span', { style: 'font-weight:bold; font-size:20px' }, new ViewText( 'ddd' ) )
  137. ] );
  138. const result = dispatcher.convert( viewElement, objWithContext );
  139. modelRoot.appendChildren( result );
  140. expect( modelToString( result ) ).to.equal( '<paragraph><$text bold="true">aaabbbcccddd</$text></paragraph>' );
  141. } );
  142. it( 'should convert from pattern to model element', () => {
  143. buildViewConverter().for( dispatcher ).from(
  144. { name: 'span', class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } }
  145. ).toElement( 'MEGATRON' );
  146. // Adding callbacks later so they are called later. MEGATRON callback is more important.
  147. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  148. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  149. let result;
  150. // Not quite megatron.
  151. result = dispatcher.convert( new ViewContainerElement( 'span', { class: 'megatron' }, new ViewText( 'foo' ) ), objWithContext );
  152. modelRoot.appendChildren( result );
  153. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  154. // Almost a megatron. Missing a head.
  155. result = dispatcher.convert(
  156. new ViewContainerElement( 'span', { class: 'megatron', body: 'megatron', legs: 'megatron' }, new ViewText( 'foo' ) ),
  157. objWithContext
  158. );
  159. modelRoot.appendChildren( result );
  160. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  161. // This would be a megatron but is a paragraph.
  162. result = dispatcher.convert(
  163. new ViewContainerElement(
  164. 'p',
  165. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  166. new ViewText( 'foo' )
  167. ),
  168. objWithContext
  169. );
  170. modelRoot.appendChildren( result );
  171. expect( modelToString( result ) ).to.equal( '<paragraph>foo</paragraph>' );
  172. // At last we have a megatron!
  173. result = dispatcher.convert(
  174. new ViewContainerElement(
  175. 'span',
  176. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  177. new ViewText( 'foo' )
  178. ),
  179. objWithContext
  180. );
  181. modelRoot.appendChildren( result );
  182. expect( modelToString( result ) ).to.equal( '<MEGATRON>foo</MEGATRON>' );
  183. } );
  184. it( 'should convert from pattern to model attribute', () => {
  185. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  186. // This time without name so default span converter will convert children.
  187. buildViewConverter().for( dispatcher )
  188. .from( { class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } } )
  189. .toAttribute( 'transformer', 'megatron' );
  190. let viewElement = new ViewContainerElement(
  191. 'span',
  192. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  193. new ViewText( 'foo' )
  194. );
  195. let result = dispatcher.convert( viewElement, objWithContext );
  196. modelRoot.appendChildren( result );
  197. expect( modelToString( result ) ).to.equal( '<span transformer="megatron">foo</span>' );
  198. } );
  199. it( 'should set different priorities for `toElement` and `toAttribute` conversion', () => {
  200. buildViewConverter().for( dispatcher )
  201. .fromAttribute( 'class' )
  202. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  203. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  204. let result = dispatcher.convert( new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext );
  205. modelRoot.appendChildren( result );
  206. // Element converter was fired first even though attribute converter was added first.
  207. expect( modelToString( result ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  208. } );
  209. it( 'should overwrite default priorities for converters', () => {
  210. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  211. buildViewConverter().for( dispatcher )
  212. .fromAttribute( 'class' )
  213. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  214. let result;
  215. result = dispatcher.convert( new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext );
  216. modelRoot.appendChildren( result );
  217. expect( modelToString( result ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  218. buildViewConverter().for( dispatcher )
  219. .from( { name: 'p', class: 'myClass' } ).withPriority( 'high' )
  220. .toElement( 'customP' );
  221. result = dispatcher.convert( new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext );
  222. modelRoot.appendChildren( result );
  223. expect( modelToString( result ) ).to.equal( '<customP>foo</customP>' );
  224. } );
  225. it( 'should overwrite default consumed values', () => {
  226. // Converter (1).
  227. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  228. // Converter (2).
  229. buildViewConverter().for( dispatcher )
  230. .from( { name: 'p', class: 'decorated' } ).consuming( { class: 'decorated' } )
  231. .toAttribute( 'decorated', true );
  232. // Converter (3).
  233. buildViewConverter().for( dispatcher )
  234. .fromAttribute( 'class', 'small' ).consuming( { class: 'small' } )
  235. .toAttribute( 'size', 'small' );
  236. const viewElement = new ViewContainerElement( 'p', { class: 'decorated small' }, new ViewText( 'foo' ) );
  237. const result = dispatcher.convert( viewElement, objWithContext );
  238. modelRoot.appendChildren( result );
  239. // P element and it's children got converted by the converter (1) and the converter (1) got fired
  240. // because P name was not consumed in converter (2). Converter (3) could consume class="small" because
  241. // only class="decorated" was consumed in converter (2).
  242. expect( modelToString( result ) ).to.equal( '<paragraph decorated="true" size="small">foo</paragraph>' );
  243. } );
  244. it( 'should convert from matcher instance to model', () => {
  245. // Universal class converter, synonymous to .fromAttribute( 'class' ).
  246. buildViewConverter().for( dispatcher )
  247. .from( new ViewMatcher( { class: /.*/ } ) )
  248. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  249. // Universal element converter.
  250. buildViewConverter().for( dispatcher )
  251. .from( new ViewMatcher( { name: /.*/ } ) )
  252. .toElement( ( viewElement ) => new ModelElement( viewElement.name ) );
  253. let viewStructure = new ViewContainerElement( 'div', { class: 'myClass' }, [
  254. new ViewContainerElement( 'abcd', null, new ViewText( 'foo' ) )
  255. ] );
  256. let result = dispatcher.convert( viewStructure, objWithContext );
  257. modelRoot.appendChildren( result );
  258. expect( modelToString( result ) ).to.equal( '<div class="myClass"><abcd>foo</abcd></div>' );
  259. } );
  260. it( 'should filter out structure that is wrong with schema', () => {
  261. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  262. buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
  263. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  264. schema.disallow( { name: '$text', attributes: 'bold', inside: 'paragraph' } );
  265. schema.disallow( { name: 'div', inside: '$root' } );
  266. dispatcher.on( 'element', convertToModelFragment() );
  267. let viewElement = new ViewContainerElement( 'div', null,
  268. new ViewContainerElement( 'p', null,
  269. new ViewAttributeElement( 'strong', null,
  270. new ViewText( 'foo' )
  271. )
  272. )
  273. );
  274. let result = dispatcher.convert( viewElement, objWithContext );
  275. expect( modelToString( result ) ).to.equal( '<paragraph>foo</paragraph>' );
  276. } );
  277. } );