buildviewconverter.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import buildViewConverter from '../../src/conversion/buildviewconverter';
  6. import ModelSchema from '../../src/model/schema';
  7. import ModelDocument from '../../src/model/document';
  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 ( 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', inside: '$inline' } );
  63. schema.allow( { name: '$text', attributes: textAttributes, inside: '$block' } );
  64. schema.allow( { name: '$text', attributes: textAttributes, inside: '$root' } );
  65. schema.allow( { name: 'paragraph', attributes: pAttributes, inside: '$root' } );
  66. schema.allow( { name: 'span', attributes: [ 'transformer' ], inside: '$root' } );
  67. schema.allow( { name: 'div', attributes: [ 'class' ], inside: '$root' } );
  68. dispatcher = new ViewConversionDispatcher( { schema } );
  69. dispatcher.on( 'text', convertText() );
  70. modelDoc = new ModelDocument();
  71. modelRoot = modelDoc.createRoot();
  72. } );
  73. it( 'should convert from view element to model element', () => {
  74. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  75. const { conversionResult } = dispatcher.convert( new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ), objWithContext );
  76. modelRoot.appendChildren( conversionResult );
  77. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  78. } );
  79. it( 'should convert from view element to model element using creator function', () => {
  80. buildViewConverter().for( dispatcher )
  81. .fromElement( 'img' )
  82. .toElement( ( viewElement ) => new ModelElement( 'image', { src: viewElement.getAttribute( 'src' ) } ) );
  83. const { conversionResult } = dispatcher.convert( new ViewContainerElement( 'img', { src: 'foo.jpg' } ), objWithContext );
  84. modelRoot.appendChildren( conversionResult );
  85. expect( modelToString( conversionResult ) ).to.equal( '<image src="foo.jpg"></image>' );
  86. } );
  87. it( 'should convert from view element to model attribute', () => {
  88. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  89. const { conversionResult } = dispatcher.convert(
  90. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), objWithContext
  91. );
  92. modelRoot.appendChildren( conversionResult );
  93. // Have to check root because result is a ModelText.
  94. expect( modelToString( modelRoot ) ).to.equal( '<$root><$text bold="true">foo</$text></$root>' );
  95. } );
  96. it( 'should convert from view element to model attributes using creator function', () => {
  97. buildViewConverter().for( dispatcher )
  98. .fromElement( 'a' )
  99. .toAttribute( ( viewElement ) => ( { key: 'linkHref', value: viewElement.getAttribute( 'href' ) } ) );
  100. const { conversionResult } = dispatcher.convert(
  101. new ViewAttributeElement( 'a', { href: 'foo.html' }, new ViewText( 'foo' ) ), objWithContext
  102. );
  103. modelRoot.appendChildren( conversionResult );
  104. // Have to check root because result is a ModelText.
  105. expect( modelToString( modelRoot ) ).to.equal( '<$root><$text linkHref="foo.html">foo</$text></$root>' );
  106. } );
  107. it( 'should convert from view attribute to model attribute', () => {
  108. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  109. buildViewConverter().for( dispatcher )
  110. .fromAttribute( 'class' )
  111. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  112. const { conversionResult } = dispatcher.convert(
  113. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext
  114. );
  115. modelRoot.appendChildren( conversionResult );
  116. expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  117. } );
  118. it( 'should convert from view attribute and key to model attribute', () => {
  119. dispatcher.on( 'documentFragment', convertToModelFragment() );
  120. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  121. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'important' ).toAttribute( 'important', true );
  122. buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'theme-nice' ).toAttribute( 'theme', 'nice' );
  123. const viewStructure = new ViewDocumentFragment( [
  124. new ViewContainerElement( 'p', { class: 'important' }, new ViewText( 'foo' ) ),
  125. new ViewContainerElement( 'p', { class: 'important theme-nice' }, new ViewText( 'bar' ) )
  126. ] );
  127. const { conversionResult } = dispatcher.convert( viewStructure, objWithContext );
  128. expect( modelToString( conversionResult ) )
  129. .to.equal( '<paragraph important="true">foo</paragraph><paragraph important="true" theme="nice">bar</paragraph>' );
  130. } );
  131. it( 'should convert from multiple view entities to model attribute', () => {
  132. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  133. buildViewConverter().for( dispatcher )
  134. .fromElement( 'strong' )
  135. .fromElement( 'b' )
  136. .fromAttribute( 'class', 'bold' )
  137. .fromAttribute( 'style', { 'font-weight': 'bold' } )
  138. .toAttribute( 'bold', true );
  139. const viewElement = new ViewContainerElement( 'p', null, [
  140. new ViewAttributeElement( 'strong', null, new ViewText( 'aaa' ) ),
  141. new ViewAttributeElement( 'b', null, new ViewText( 'bbb' ) ),
  142. new ViewContainerElement( 'span', { class: 'bold' }, new ViewText( 'ccc' ) ),
  143. new ViewContainerElement( 'span', { style: 'font-weight:bold; font-size:20px' }, new ViewText( 'ddd' ) )
  144. ] );
  145. const { conversionResult } = dispatcher.convert( viewElement, objWithContext );
  146. modelRoot.appendChildren( conversionResult );
  147. expect( modelToString( conversionResult ) ).to.equal( '<paragraph><$text bold="true">aaabbbcccddd</$text></paragraph>' );
  148. } );
  149. it( 'should convert from pattern to marker', () => {
  150. buildViewConverter().for( dispatcher ).from( { attribute: { 'data-name': 'search' } } ).toMarker();
  151. const viewElement = new ViewAttributeElement( 'span', { 'data-name': 'search' } );
  152. const { conversionResult } = dispatcher.convert( viewElement, objWithContext );
  153. modelRoot.appendChildren( conversionResult );
  154. expect( modelToString( conversionResult ) ).to.equal( '<$marker data-name="search"></$marker>' );
  155. } );
  156. it( 'should convert from element to marker using creator function', () => {
  157. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( ( data ) => {
  158. return new ModelElement( '$marker', { 'data-name': data.getAttribute( 'class' ) } );
  159. } );
  160. const element = new ViewAttributeElement( 'marker', { class: 'search' } );
  161. const { conversionResult } = dispatcher.convert( element, objWithContext );
  162. modelRoot.appendChildren( conversionResult );
  163. expect( modelToString( conversionResult ) ).to.equal( '<$marker data-name="search"></$marker>' );
  164. } );
  165. it( 'should convert from multiple view entities to marker', () => {
  166. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  167. buildViewConverter().for( dispatcher )
  168. .from( { attribute: { 'foo': 'marker' } } )
  169. .from( { attribute: { 'bar': 'marker' } } )
  170. .from( { attribute: { 'foo': 'marker', 'bar': 'marker' } } )
  171. .toMarker();
  172. const viewElement = new ViewContainerElement( 'p', null, [
  173. new ViewAttributeElement( 'span', { 'foo': 'marker', 'data-name': 'marker1' } ),
  174. new ViewAttributeElement( 'span', { 'bar': 'marker', 'data-name': 'marker2' } ),
  175. new ViewAttributeElement( 'span', { 'foo': 'marker', 'bar': 'marker', 'data-name': 'marker3' } )
  176. ] );
  177. const { conversionResult, markers } = dispatcher.convert( viewElement, objWithContext );
  178. expect( modelToString( conversionResult ) ).to.equal( '<paragraph></paragraph>' );
  179. expect( markers.size ).to.equal( 3 );
  180. expect( markers.has( 'marker1' ) ).to.true;
  181. expect( markers.has( 'marker2' ) ).to.true;
  182. expect( markers.has( 'marker3' ) ).to.true;
  183. } );
  184. it( 'should do nothing when there is no element matching to marker pattern', () => {
  185. buildViewConverter().for( dispatcher ).from( { class: 'color' } ).toMarker();
  186. const element = new ViewAttributeElement( 'span' );
  187. expect( dispatcher.convert( element, objWithContext ).conversionResult ).to.null;
  188. } );
  189. it( 'should throw an error when view element in not valid to convert to marker', () => {
  190. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker();
  191. const element = new ViewAttributeElement( 'marker', { class: 'search' } );
  192. expect( () => {
  193. dispatcher.convert( element, objWithContext );
  194. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  195. } );
  196. it( 'should throw an error when model element returned by creator has not valid name', () => {
  197. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
  198. return new ModelElement( 'element', { 'data-name': 'search' } );
  199. } );
  200. const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
  201. expect( () => {
  202. dispatcher.convert( element, objWithContext );
  203. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  204. } );
  205. it( 'should throw an error when model element returned by creator has not valid data-name attribute', () => {
  206. buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
  207. return new ModelElement( '$marker', { 'foo': 'search' } );
  208. } );
  209. const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
  210. expect( () => {
  211. dispatcher.convert( element, objWithContext );
  212. } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
  213. } );
  214. it( 'should convert from pattern to model element', () => {
  215. buildViewConverter().for( dispatcher ).from(
  216. { name: 'span', class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } }
  217. ).toElement( 'MEGATRON' );
  218. // Adding callbacks later so they are called later. MEGATRON callback is more important.
  219. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  220. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  221. let result;
  222. // Not quite megatron.
  223. result = dispatcher.convert(
  224. new ViewContainerElement( 'span', { class: 'megatron' }, new ViewText( 'foo' ) ), objWithContext
  225. ).conversionResult;
  226. modelRoot.appendChildren( result );
  227. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  228. // Almost a megatron. Missing a head.
  229. result = dispatcher.convert(
  230. new ViewContainerElement( 'span', { class: 'megatron', body: 'megatron', legs: 'megatron' }, new ViewText( 'foo' ) ),
  231. objWithContext
  232. ).conversionResult;
  233. modelRoot.appendChildren( result );
  234. expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
  235. // This would be a megatron but is a paragraph.
  236. result = dispatcher.convert(
  237. new ViewContainerElement(
  238. 'p',
  239. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  240. new ViewText( 'foo' )
  241. ),
  242. objWithContext
  243. ).conversionResult;
  244. modelRoot.appendChildren( result );
  245. expect( modelToString( result ) ).to.equal( '<paragraph>foo</paragraph>' );
  246. // At last we have a megatron!
  247. result = dispatcher.convert(
  248. new ViewContainerElement(
  249. 'span',
  250. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  251. new ViewText( 'foo' )
  252. ),
  253. objWithContext
  254. ).conversionResult;
  255. modelRoot.appendChildren( result );
  256. expect( modelToString( result ) ).to.equal( '<MEGATRON>foo</MEGATRON>' );
  257. } );
  258. it( 'should convert from pattern to model attribute', () => {
  259. buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
  260. // This time without name so default span converter will convert children.
  261. buildViewConverter().for( dispatcher )
  262. .from( { class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } } )
  263. .toAttribute( 'transformer', 'megatron' );
  264. let viewElement = new ViewContainerElement(
  265. 'span',
  266. { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
  267. new ViewText( 'foo' )
  268. );
  269. let { conversionResult } = dispatcher.convert( viewElement, objWithContext );
  270. modelRoot.appendChildren( conversionResult );
  271. expect( modelToString( conversionResult ) ).to.equal( '<span transformer="megatron">foo</span>' );
  272. } );
  273. it( 'should set different priorities for `toElement` and `toAttribute` conversion', () => {
  274. buildViewConverter().for( dispatcher )
  275. .fromAttribute( 'class' )
  276. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  277. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  278. let { conversionResult } = dispatcher.convert(
  279. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext
  280. );
  281. modelRoot.appendChildren( conversionResult );
  282. // Element converter was fired first even though attribute converter was added first.
  283. expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  284. } );
  285. it( 'should overwrite default priorities for converters', () => {
  286. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  287. buildViewConverter().for( dispatcher )
  288. .fromAttribute( 'class' )
  289. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  290. let result;
  291. result = dispatcher.convert(
  292. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext
  293. ).conversionResult;
  294. modelRoot.appendChildren( result );
  295. expect( modelToString( result ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
  296. buildViewConverter().for( dispatcher )
  297. .from( { name: 'p', class: 'myClass' } ).withPriority( 'high' )
  298. .toElement( 'customP' );
  299. result = dispatcher.convert(
  300. new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), objWithContext
  301. ).conversionResult;
  302. modelRoot.appendChildren( result );
  303. expect( modelToString( result ) ).to.equal( '<customP>foo</customP>' );
  304. } );
  305. it( 'should overwrite default consumed values', () => {
  306. // Converter (1).
  307. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  308. // Converter (2).
  309. buildViewConverter().for( dispatcher )
  310. .from( { name: 'p', class: 'decorated' } ).consuming( { class: 'decorated' } )
  311. .toAttribute( 'decorated', true );
  312. // Converter (3).
  313. buildViewConverter().for( dispatcher )
  314. .fromAttribute( 'class', 'small' ).consuming( { class: 'small' } )
  315. .toAttribute( 'size', 'small' );
  316. const viewElement = new ViewContainerElement( 'p', { class: 'decorated small' }, new ViewText( 'foo' ) );
  317. const { conversionResult } = dispatcher.convert( viewElement, objWithContext );
  318. modelRoot.appendChildren( conversionResult );
  319. // P element and it's children got converted by the converter (1) and the converter (1) got fired
  320. // because P name was not consumed in converter (2). Converter (3) could consume class="small" because
  321. // only class="decorated" was consumed in converter (2).
  322. expect( modelToString( conversionResult ) ).to.equal( '<paragraph decorated="true" size="small">foo</paragraph>' );
  323. } );
  324. it( 'should convert from matcher instance to model', () => {
  325. // Universal class converter, synonymous to .fromAttribute( 'class' ).
  326. buildViewConverter().for( dispatcher )
  327. .from( new ViewMatcher( { class: /.*/ } ) )
  328. .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
  329. // Universal element converter.
  330. buildViewConverter().for( dispatcher )
  331. .from( new ViewMatcher( { name: /.*/ } ) )
  332. .toElement( ( viewElement ) => new ModelElement( viewElement.name ) );
  333. let viewStructure = new ViewContainerElement( 'div', { class: 'myClass' }, [
  334. new ViewContainerElement( 'abcd', null, new ViewText( 'foo' ) )
  335. ] );
  336. let { conversionResult } = dispatcher.convert( viewStructure, objWithContext );
  337. modelRoot.appendChildren( conversionResult );
  338. expect( modelToString( conversionResult ) ).to.equal( '<div class="myClass"><abcd>foo</abcd></div>' );
  339. } );
  340. it( 'should filter out structure that is wrong with schema - elements', () => {
  341. buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
  342. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  343. schema.disallow( { name: 'div', inside: '$root' } );
  344. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  345. let viewElement = new ViewContainerElement( 'div', null,
  346. new ViewContainerElement( 'p', null,
  347. new ViewText( 'foo' )
  348. )
  349. );
  350. let { conversionResult } = dispatcher.convert( viewElement, objWithContext );
  351. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  352. } );
  353. it( 'should filter out structure that is wrong with schema - attributes', () => {
  354. buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
  355. buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
  356. schema.disallow( { name: '$text', attributes: 'bold', inside: 'paragraph' } );
  357. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  358. let viewElement = new ViewContainerElement( 'p', null,
  359. new ViewAttributeElement( 'strong', null,
  360. new ViewText( 'foo' )
  361. )
  362. );
  363. let { conversionResult } = dispatcher.convert( viewElement, objWithContext );
  364. expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
  365. } );
  366. } );