8
0

definition-based-converters.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 convert when changing attribute', () => {
  137. const definition1 = { model: 'bar', view: { name: 'span', class: 'bar' } };
  138. const definition2 = { model: 'baz', view: { name: 'span', class: 'baz' } };
  139. modelAttributeToViewAttributeElement( 'foo', [ definition1, definition2 ], [ dispatcher ] );
  140. const modelElement = new ModelText( 'foo', { foo: 'bar' } );
  141. model.change( writer => {
  142. writer.insert( modelElement, ModelPosition.createAt( modelRoot, 0 ) );
  143. } );
  144. expect( viewToString( viewRoot ) ).to.equal( '<div><span class="bar">foo</span></div>' );
  145. model.change( writer => {
  146. writer.setAttribute( 'foo', 'baz', modelElement );
  147. } );
  148. expect( viewToString( viewRoot ) ).to.equal( '<div><span class="baz">foo</span></div>' );
  149. } );
  150. it( 'should do nothing for undefined value', () => {
  151. modelAttributeToViewAttributeElement( 'foo', [ { model: 'bar', view: 'strong' } ], [ dispatcher ] );
  152. const modelElement = new ModelText( 'foo', { foo: 'baz' } );
  153. model.change( writer => {
  154. writer.insert( modelElement, ModelPosition.createAt( modelRoot, 0 ) );
  155. } );
  156. expect( viewToString( viewRoot ) ).to.equal( '<div>foo</div>' );
  157. } );
  158. } );
  159. describe( 'view to model conversion', () => {
  160. beforeEach( () => {
  161. setupViewToModelTests();
  162. schema.register( 'div', { inheritAllFrom: '$block' } );
  163. schema.extend( '$text', {
  164. allowIn: '$root',
  165. allowAttributes: 'foo'
  166. } );
  167. dispatcher.on( 'text', convertText() );
  168. } );
  169. it( 'should convert using element name', () => {
  170. viewToModelAttribute( 'foo', { model: 'bar', view: 'strong' }, [ dispatcher ] );
  171. const conversionResult = dispatcher.convert(
  172. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  173. );
  174. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  175. } );
  176. it( 'should convert using object', () => {
  177. viewToModelAttribute( 'foo', { model: 'bar', view: { name: 'strong' } }, [ dispatcher ] );
  178. const conversionResult = dispatcher.convert(
  179. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  180. );
  181. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  182. } );
  183. it( 'should convert using class string', () => {
  184. viewToModelAttribute( 'foo', { model: 'bar', view: { name: 'span', class: 'foo' } }, [ dispatcher ] );
  185. const conversionResult = dispatcher.convert(
  186. new ViewAttributeElement( 'span', { class: 'foo' }, new ViewText( 'foo' ) ), additionalData
  187. );
  188. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  189. } );
  190. it( 'should convert using class array', () => {
  191. viewToModelAttribute( 'foo', {
  192. model: 'bar',
  193. view: { name: 'span', class: [ 'foo', 'bar' ] }
  194. }, [ dispatcher ] );
  195. const conversionResult = dispatcher.convert(
  196. new ViewAttributeElement( 'span', { class: 'foo bar' }, new ViewText( 'foo' ) ), additionalData
  197. );
  198. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  199. } );
  200. it( 'should convert using style object', () => {
  201. viewToModelAttribute( 'foo', {
  202. model: 'bar',
  203. view: { name: 'span', style: { 'font-weight': 'bold' } }
  204. }, [ dispatcher ] );
  205. const conversionResult = dispatcher.convert(
  206. new ViewAttributeElement( 'span', { style: 'font-weight:bold' }, new ViewText( 'foo' ) ), additionalData
  207. );
  208. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  209. } );
  210. it( 'should convert using attributes object', () => {
  211. viewToModelAttribute( 'foo', {
  212. model: 'bar',
  213. view: { name: 'span', attribute: { 'data-foo': 'bar' } }
  214. }, [ dispatcher ] );
  215. const conversionResult = dispatcher.convert(
  216. new ViewAttributeElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  217. );
  218. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  219. } );
  220. it( 'should convert using acceptAlso array', () => {
  221. viewToModelAttribute( 'foo', {
  222. model: 'bar',
  223. view: 'strong',
  224. acceptsAlso: [
  225. { name: 'span', class: [ 'foo', 'bar' ] },
  226. { name: 'span', attribute: { 'data-foo': 'bar' } }
  227. ]
  228. }, [ dispatcher ] );
  229. const conversionResult = dispatcher.convert(
  230. new ViewAttributeElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  231. );
  232. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  233. } );
  234. it( 'should convert using priority', () => {
  235. viewToModelAttribute( 'foo', { model: 'baz', view: 'strong' }, [ dispatcher ] );
  236. viewToModelAttribute( 'foo', { model: 'bar', view: { name: 'strong', priority: 'high' } }, [ dispatcher ] );
  237. const conversionResult = dispatcher.convert(
  238. new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  239. );
  240. expect( modelToString( conversionResult ) ).to.equal( '<$text foo="bar">foo</$text>' );
  241. } );
  242. } );
  243. } );
  244. describe( 'Element converters', () => {
  245. function testModelConversion( definition, expectedResult ) {
  246. modelElementToViewContainerElement( definition, [ dispatcher ] );
  247. const modelElement = new ModelElement( 'foo', null, new ModelText( 'bar' ) );
  248. model.change( writer => {
  249. writer.insert( modelElement, ModelPosition.createAt( modelRoot, 0 ) );
  250. } );
  251. expect( viewToString( viewRoot ) ).to.equal( '<div>' + expectedResult + '</div>' );
  252. }
  253. describe( 'model to view conversion', () => {
  254. beforeEach( () => {
  255. setupModelToViewTests();
  256. } );
  257. it( 'using passed view element name', () => {
  258. testModelConversion( { model: 'foo', view: 'code' }, '<code>bar</code>' );
  259. } );
  260. it( 'using passed view element object', () => {
  261. testModelConversion( { model: 'foo', view: { name: 'code' } }, '<code>bar</code>' );
  262. } );
  263. it( 'using passed view element object with style object', () => {
  264. testModelConversion( {
  265. model: 'foo',
  266. view: { name: 'span', style: { 'font-weight': 'bold' } }
  267. }, '<span style="font-weight:bold;">bar</span>' );
  268. } );
  269. it( 'using passed view element object with class string', () => {
  270. testModelConversion( { model: 'foo', view: { name: 'span', class: 'foo' } }, '<span class="foo">bar</span>' );
  271. } );
  272. it( 'using passed view element object with class array', () => {
  273. testModelConversion( {
  274. model: 'foo',
  275. view: { name: 'span', class: [ 'foo', 'foo-bar' ] }
  276. }, '<span class="foo foo-bar">bar</span>' );
  277. } );
  278. it( 'using passed view element object with attributes', () => {
  279. testModelConversion( {
  280. model: 'foo',
  281. view: { name: 'span', attribute: { 'data-foo': 'bar' } }
  282. }, '<span data-foo="bar">bar</span>' );
  283. } );
  284. } );
  285. describe( 'view to model conversion', () => {
  286. beforeEach( () => {
  287. setupViewToModelTests();
  288. schema.register( 'div', { inheritAllFrom: '$block' } );
  289. schema.register( 'bar', { inheritAllFrom: '$block' } );
  290. schema.register( 'baz', { inheritAllFrom: '$block' } );
  291. schema.extend( '$text', {
  292. allowIn: '$root',
  293. allowAttributes: 'foo'
  294. } );
  295. dispatcher.on( 'text', convertText() );
  296. } );
  297. it( 'should convert using element name', () => {
  298. viewToModelElement( { model: 'bar', view: 'strong' }, [ dispatcher ] );
  299. const conversionResult = dispatcher.convert(
  300. new ViewElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  301. );
  302. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  303. } );
  304. it( 'should convert using object', () => {
  305. viewToModelElement( { model: 'bar', view: { name: 'strong' } }, [ dispatcher ] );
  306. const conversionResult = dispatcher.convert(
  307. new ViewElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  308. );
  309. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  310. } );
  311. it( 'should convert using class string', () => {
  312. viewToModelElement( { model: 'bar', view: { name: 'span', class: 'foo' } }, [ dispatcher ] );
  313. const conversionResult = dispatcher.convert(
  314. new ViewElement( 'span', { class: 'foo' }, new ViewText( 'foo' ) ), additionalData
  315. );
  316. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  317. } );
  318. it( 'should convert using class array', () => {
  319. viewToModelElement( { model: 'bar', view: { name: 'span', class: [ 'foo', 'bar' ] } }, [ dispatcher ] );
  320. const conversionResult = dispatcher.convert(
  321. new ViewElement( 'span', { class: 'foo bar' }, new ViewText( 'foo' ) ), additionalData
  322. );
  323. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  324. } );
  325. it( 'should convert using style object', () => {
  326. viewToModelElement( { model: 'bar', view: { name: 'span', style: { 'font-weight': 'bold' } } }, [ dispatcher ] );
  327. const conversionResult = dispatcher.convert(
  328. new ViewElement( 'span', { style: 'font-weight:bold' }, new ViewText( 'foo' ) ), additionalData
  329. );
  330. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  331. } );
  332. it( 'should convert using attributes object', () => {
  333. viewToModelElement( { model: 'bar', view: { name: 'span', attribute: { 'data-foo': 'bar' } } }, [ dispatcher ] );
  334. const conversionResult = dispatcher.convert(
  335. new ViewElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  336. );
  337. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  338. } );
  339. it( 'should convert using acceptAlso array', () => {
  340. viewToModelElement( {
  341. model: 'bar',
  342. view: 'strong',
  343. acceptsAlso: [
  344. { name: 'span', class: [ 'foo', 'bar' ] },
  345. { name: 'span', attribute: { 'data-foo': 'bar' } }
  346. ]
  347. }, [ dispatcher ] );
  348. const conversionResult = dispatcher.convert(
  349. new ViewElement( 'span', { 'data-foo': 'bar' }, new ViewText( 'foo' ) ), additionalData
  350. );
  351. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  352. } );
  353. it( 'should convert using priority', () => {
  354. viewToModelElement( { model: 'baz', view: 'strong' }, [ dispatcher ] );
  355. viewToModelElement( { model: 'bar', view: { name: 'strong', priority: 'high' } }, [ dispatcher ] );
  356. const conversionResult = dispatcher.convert(
  357. new ViewElement( 'strong', null, new ViewText( 'foo' ) ), additionalData
  358. );
  359. expect( modelToString( conversionResult ) ).to.equal( '<bar>foo</bar>' );
  360. } );
  361. } );
  362. } );
  363. } );