definition-based-converters.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelElement from '../../src/model/element';
  6. import ModelText from '../../src/model/text';
  7. import ModelRange from '../../src/model/range';
  8. import ViewElement from '../../src/view/element';
  9. import ViewAttributeElement from '../../src/view/attributeelement';
  10. import ViewText from '../../src/view/text';
  11. import { convertText } from '../../src/conversion/view-to-model-converters';
  12. import {
  13. modelAttributeToViewAttributeElement,
  14. viewToModelAttribute,
  15. modelElementToViewContainerElement,
  16. viewToModelElement
  17. } from '../../src/conversion/definition-based-converters';
  18. import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
  19. import ModelSchema from '../../src/model/schema';
  20. import ModelWalker from '../../src/model/treewalker';
  21. import ModelTextProxy from '../../src/model/textproxy';
  22. import Model from '../../src/model/model';
  23. import ModelPosition from '../../src/model/position';
  24. import EditingController from '../../src/controller/editingcontroller';
  25. function viewAttributesToString( item ) {
  26. let result = '';
  27. for ( const key of item.getAttributeKeys() ) {
  28. const value = item.getAttribute( key );
  29. if ( value ) {
  30. result += ' ' + key + '="' + value + '"';
  31. }
  32. }
  33. return result;
  34. }
  35. function modelToString( item ) {
  36. let result = '';
  37. if ( item instanceof ModelTextProxy ) {
  38. const attributes = modelAttributesToString( item );
  39. result = attributes ? '<$text' + attributes + '>' + item.data + '</$text>' : item.data;
  40. } else {
  41. const walker = new ModelWalker( { boundaries: ModelRange.createIn( item ), shallow: true } );
  42. for ( const value of walker ) {
  43. result += modelToString( value.item );
  44. }
  45. if ( item instanceof ModelElement ) {
  46. const attributes = modelAttributesToString( item );
  47. result = '<' + item.name + attributes + '>' + result + '</' + item.name + '>';
  48. }
  49. }
  50. return result;
  51. }
  52. function modelAttributesToString( item ) {
  53. let result = '';
  54. for ( const attr of item.getAttributes() ) {
  55. result += ' ' + attr[ 0 ] + '="' + attr[ 1 ] + '"';
  56. }
  57. return result;
  58. }
  59. function viewToString( item ) {
  60. let result = '';
  61. if ( item instanceof ViewText ) {
  62. result = item.data;
  63. } else {
  64. // ViewElement or ViewDocumentFragment.
  65. for ( const child of item.getChildren() ) {
  66. result += viewToString( child );
  67. }
  68. if ( item instanceof ViewElement ) {
  69. result = '<' + item.name + viewAttributesToString( item ) + '>' + result + '</' + item.name + '>';
  70. }
  71. }
  72. return result;
  73. }
  74. describe( 'definition-based-converters', () => {
  75. let model, dispatcher, modelDoc, modelRoot, viewRoot, controller, additionalData, schema;
  76. beforeEach( () => {
  77. model = new Model();
  78. } );
  79. function setupViewToModelTests() {
  80. additionalData = { context: [ '$root' ] };
  81. schema = new ModelSchema();
  82. dispatcher = new ViewConversionDispatcher( model, { schema } );
  83. }
  84. function setupModelToViewTests() {
  85. modelDoc = model.document;
  86. modelRoot = modelDoc.createRoot();
  87. controller = new EditingController( model );
  88. controller.createRoot( 'div' );
  89. viewRoot = controller.view.getRoot();
  90. dispatcher = controller.modelToView;
  91. }
  92. describe( 'Attribute converters', () => {
  93. function testModelConversion( definition, expectedConversion ) {
  94. modelAttributeToViewAttributeElement( 'foo', definition, [ dispatcher ] );
  95. const modelElement = new ModelText( 'foo', { foo: 'bar' } );
  96. model.change( writer => {
  97. writer.insert( modelElement, ModelPosition.createAt( modelRoot, 0 ) );
  98. } );
  99. expect( viewToString( viewRoot ) ).to.equal( expectedConversion );
  100. model.change( writer => {
  101. writer.removeAttribute( 'foo', modelElement );
  102. } );
  103. expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
  104. }
  105. describe( 'model to view conversion', () => {
  106. beforeEach( () => {
  107. setupModelToViewTests();
  108. } );
  109. it( 'using passed view element name', () => {
  110. testModelConversion( { model: 'bar', view: 'strong' }, '<div><strong>foo</strong></div>' );
  111. } );
  112. it( 'using passed view element object', () => {
  113. testModelConversion( { model: 'bar', view: { name: 'strong' } }, '<div><strong>foo</strong></div>' );
  114. } );
  115. it( 'using passed view element object with style object', () => {
  116. testModelConversion( {
  117. model: 'bar',
  118. view: { name: 'span', style: { 'font-weight': 'bold' } }
  119. }, '<div><span style="font-weight:bold;">foo</span></div>' );
  120. } );
  121. it( 'using passed view element object with class string', () => {
  122. testModelConversion( { model: 'bar', view: { name: 'span', class: 'foo' } }, '<div><span class="foo">foo</span></div>' );
  123. } );
  124. it( 'using passed view element object with class array', () => {
  125. testModelConversion( {
  126. model: 'bar',
  127. view: { name: 'span', class: [ 'foo', 'foo-bar' ] }
  128. }, '<div><span class="foo foo-bar">foo</span></div>' );
  129. } );
  130. it( 'using passed view element object with attributes', () => {
  131. testModelConversion( {
  132. model: 'bar',
  133. view: { name: 'span', attribute: { 'data-foo': 'bar' } }
  134. }, '<div><span data-foo="bar">foo</span></div>' );
  135. } );
  136. it( 'should do nothing for undefined value', () => {
  137. modelAttributeToViewAttributeElement( 'foo', { model: 'bar', view: 'strong' }, [ dispatcher ] );
  138. const modelElement = new ModelText( 'foo', { foo: 'baz' } );
  139. model.change( writer => {
  140. writer.insert( modelElement, ModelPosition.createAt( modelRoot, 0 ) );
  141. } );
  142. expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
  143. } );
  144. } );
  145. describe( 'view to model conversion', () => {
  146. beforeEach( () => {
  147. setupViewToModelTests();
  148. schema.registerItem( 'div', '$block' );
  149. schema.allow( { name: '$inline', attributes: [ 'foo' ], inside: '$root' } );
  150. schema.allow( { name: '$text', inside: '$root' } );
  151. dispatcher.on( 'text', convertText() );
  152. } );
  153. it( 'should convert using element name', () => {
  154. viewToModelAttribute( 'foo', { model: 'bar', view: 'strong' }, [ dispatcher ] );
  155. const conversionResult = dispatcher.convert(
  156. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  157. );
  158. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  159. } );
  160. it( 'should convert using object', () => {
  161. viewToModelAttribute( 'foo', { model: 'bar', view: { name: 'strong' } }, [ dispatcher ] );
  162. const conversionResult = dispatcher.convert(
  163. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  164. );
  165. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  166. } );
  167. it( 'should convert using class string', () => {
  168. viewToModelAttribute( 'foo', { model: 'bar', view: { name: 'span', class: 'foo' } }, [ dispatcher ] );
  169. const conversionResult = dispatcher.convert(
  170. new ViewAttributeElement( 'span', { class: 'foo' }, new ViewText( 'foo' ) ), additionalData
  171. );
  172. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  173. } );
  174. it( 'should convert using class array', () => {
  175. viewToModelAttribute( 'foo', {
  176. model: 'bar',
  177. view: { name: 'span', class: [ 'foo', 'bar' ] }
  178. }, [ dispatcher ] );
  179. const conversionResult = dispatcher.convert(
  180. new ViewAttributeElement( 'span', { class: 'foo bar' }, new ViewText( 'foo' ) ), additionalData
  181. );
  182. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  183. } );
  184. it( 'should convert using style object', () => {
  185. viewToModelAttribute( 'foo', {
  186. model: 'bar',
  187. view: { name: 'span', style: { 'font-weight': 'bold' } }
  188. }, [ dispatcher ] );
  189. const conversionResult = dispatcher.convert(
  190. new ViewAttributeElement( 'span', { style: 'font-weight:bold' }, new ViewText( 'foo' ) ), additionalData
  191. );
  192. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  193. } );
  194. it( 'should convert using attributes object', () => {
  195. viewToModelAttribute( 'foo', {
  196. model: 'bar',
  197. view: { name: 'span', attribute: { 'data-foo': 'bar' } }
  198. }, [ dispatcher ] );
  199. const conversionResult = dispatcher.convert(
  200. new ViewAttributeElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  201. );
  202. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  203. } );
  204. it( 'should convert using acceptAlso array', () => {
  205. viewToModelAttribute( 'foo', {
  206. model: 'bar',
  207. view: 'strong',
  208. acceptsAlso: [
  209. { name: 'span', class: [ 'foo', 'bar' ] },
  210. { name: 'span', attribute: { 'data-foo': 'bar' } }
  211. ]
  212. }, [ dispatcher ] );
  213. const conversionResult = dispatcher.convert(
  214. new ViewAttributeElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  215. );
  216. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  217. } );
  218. it( 'should convert using priority', () => {
  219. viewToModelAttribute( 'foo', { model: 'baz', view: 'strong' }, [ dispatcher ] );
  220. viewToModelAttribute( 'foo', { model: 'bar', view: { name: 'strong', priority: 'high' } }, [ dispatcher ] );
  221. const conversionResult = dispatcher.convert(
  222. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  223. );
  224. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  225. } );
  226. } );
  227. } );
  228. describe( 'Element converters', () => {
  229. function testModelConversion( definition, expectedResult ) {
  230. modelElementToViewContainerElement( definition, [ dispatcher ] );
  231. const modelElement = new ModelElement( 'foo', null, new ModelText( 'bar' ) );
  232. model.change( writer => {
  233. writer.insert( modelElement, ModelPosition.createAt( modelRoot, 0 ) );
  234. } );
  235. expect( viewToString( viewRoot ) ).to.equal( '<div>' + expectedResult + '</div>' );
  236. }
  237. describe( 'model to view conversion', () => {
  238. beforeEach( () => {
  239. setupModelToViewTests();
  240. } );
  241. it( 'using passed view element name', () => {
  242. testModelConversion( { model: 'foo', view: 'code' }, '<code>bar</code>' );
  243. } );
  244. it( 'using passed view element object', () => {
  245. testModelConversion( { model: 'foo', view: { name: 'code' } }, '<code>bar</code>' );
  246. } );
  247. it( 'using passed view element object with style object', () => {
  248. testModelConversion( {
  249. model: 'foo',
  250. view: { name: 'span', style: { 'font-weight': 'bold' } }
  251. }, '<span style="font-weight:bold;">bar</span>' );
  252. } );
  253. it( 'using passed view element object with class string', () => {
  254. testModelConversion( { model: 'foo', view: { name: 'span', class: 'foo' } }, '<span class="foo">bar</span>' );
  255. } );
  256. it( 'using passed view element object with class array', () => {
  257. testModelConversion( {
  258. model: 'foo',
  259. view: { name: 'span', class: [ 'foo', 'foo-bar' ] }
  260. }, '<span class="foo foo-bar">bar</span>' );
  261. } );
  262. it( 'using passed view element object with attributes', () => {
  263. testModelConversion( {
  264. model: 'foo',
  265. view: { name: 'span', attribute: { 'data-foo': 'bar' } }
  266. }, '<span data-foo="bar">bar</span>' );
  267. } );
  268. } );
  269. describe( 'view to model conversion', () => {
  270. beforeEach( () => {
  271. setupViewToModelTests();
  272. schema.registerItem( 'div', '$block' );
  273. schema.registerItem( 'bar', '$block' );
  274. schema.registerItem( 'baz', '$block' );
  275. schema.allow( { name: '$inline', attribute: [ 'foo' ], inside: '$root' } );
  276. schema.allow( { name: '$text', inside: '$inline' } );
  277. dispatcher.on( 'text', convertText() );
  278. } );
  279. it( 'should convert using element name', () => {
  280. viewToModelElement( { model: 'bar', view: 'strong' }, [ dispatcher ] );
  281. const conversionResult = dispatcher.convert(
  282. new ViewElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  283. );
  284. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  285. } );
  286. it( 'should convert using object', () => {
  287. viewToModelElement( { model: 'bar', view: { name: 'strong' } }, [ dispatcher ] );
  288. const conversionResult = dispatcher.convert(
  289. new ViewElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  290. );
  291. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  292. } );
  293. it( 'should convert using class string', () => {
  294. viewToModelElement( { model: 'bar', view: { name: 'span', class: 'foo' } }, [ dispatcher ] );
  295. const conversionResult = dispatcher.convert(
  296. new ViewElement( 'span', { class: 'foo' }, new ViewText( 'foo' ) ), additionalData
  297. );
  298. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  299. } );
  300. it( 'should convert using class array', () => {
  301. viewToModelElement( { model: 'bar', view: { name: 'span', class: [ 'foo', 'bar' ] } }, [ dispatcher ] );
  302. const conversionResult = dispatcher.convert(
  303. new ViewElement( 'span', { class: 'foo bar' }, new ViewText( 'foo' ) ), additionalData
  304. );
  305. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  306. } );
  307. it( 'should convert using style object', () => {
  308. viewToModelElement( { model: 'bar', view: { name: 'span', style: { 'font-weight': 'bold' } } }, [ dispatcher ] );
  309. const conversionResult = dispatcher.convert(
  310. new ViewElement( 'span', { style: 'font-weight:bold' }, new ViewText( 'foo' ) ), additionalData
  311. );
  312. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  313. } );
  314. it( 'should convert using attributes object', () => {
  315. viewToModelElement( { model: 'bar', view: { name: 'span', attribute: { 'data-foo': 'bar' } } }, [ dispatcher ] );
  316. const conversionResult = dispatcher.convert(
  317. new ViewElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  318. );
  319. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  320. } );
  321. it( 'should convert using acceptAlso array', () => {
  322. viewToModelElement( {
  323. model: 'bar',
  324. view: 'strong',
  325. acceptsAlso: [
  326. { name: 'span', class: [ 'foo', 'bar' ] },
  327. { name: 'span', attribute: { 'data-foo': 'bar' } }
  328. ]
  329. }, [ dispatcher ] );
  330. const conversionResult = dispatcher.convert(
  331. new ViewElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  332. );
  333. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  334. } );
  335. it( 'should convert using priority', () => {
  336. viewToModelElement( { model: 'baz', view: 'strong' }, [ dispatcher ] );
  337. viewToModelElement( { model: 'bar', view: { name: 'strong', priority: 'high' } }, [ dispatcher ] );
  338. const conversionResult = dispatcher.convert(
  339. new ViewElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  340. );
  341. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  342. } );
  343. } );
  344. } );
  345. } );