8
0

view-converter-builder.js 15 KB

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