advanced-converters.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import ModelElement from '../../src/model/element';
  7. import ModelText from '../../src/model/text';
  8. import ModelTextProxy from '../../src/model/textproxy';
  9. import ModelRange from '../../src/model/range';
  10. import ModelPosition from '../../src/model/position';
  11. import ModelWalker from '../../src/model/treewalker';
  12. import ViewElement from '../../src/view/element';
  13. import ViewContainerElement from '../../src/view/containerelement';
  14. import ViewAttributeElement from '../../src/view/attributeelement';
  15. import ViewText from '../../src/view/text';
  16. import viewWriter from '../../src/view/writer';
  17. import ViewPosition from '../../src/view/position';
  18. import ViewRange from '../../src/view/range';
  19. import EditingController from '../../src/controller/editingcontroller';
  20. import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
  21. import {
  22. insertElement,
  23. changeAttribute,
  24. wrap
  25. } from '../../src/conversion/model-to-view-converters';
  26. import { convertToModelFragment, convertText } from '../../src/conversion/view-to-model-converters';
  27. describe( 'advanced-converters', () => {
  28. let model, modelDoc, modelRoot, viewRoot, modelDispatcher, viewDispatcher;
  29. beforeEach( () => {
  30. model = new Model();
  31. modelDoc = model.document;
  32. modelRoot = modelDoc.createRoot();
  33. const editing = new EditingController( model );
  34. viewRoot = editing.createRoot( 'div' );
  35. viewDispatcher = new ViewConversionDispatcher( model, { schema: { check: () => true } } );
  36. viewDispatcher.on( 'text', convertText() );
  37. viewDispatcher.on( 'documentFragment', convertToModelFragment() );
  38. modelDispatcher = editing.modelToView;
  39. } );
  40. function viewAttributesToString( item ) {
  41. let result = '';
  42. for ( const key of item.getAttributeKeys() ) {
  43. const value = item.getAttribute( key );
  44. if ( value ) {
  45. result += ' ' + key + '="' + value + '"';
  46. }
  47. }
  48. return result;
  49. }
  50. function viewToString( item ) {
  51. let result = '';
  52. if ( item instanceof ViewText ) {
  53. result = item.data;
  54. } else {
  55. // ViewElement or ViewDocumentFragment.
  56. for ( const child of item.getChildren() ) {
  57. result += viewToString( child );
  58. }
  59. if ( item instanceof ViewElement ) {
  60. result = '<' + item.name + viewAttributesToString( item ) + '>' + result + '</' + item.name + '>';
  61. }
  62. }
  63. return result;
  64. }
  65. function modelAttributesToString( item ) {
  66. let result = '';
  67. for ( const attr of item.getAttributes() ) {
  68. result += ' ' + attr[ 0 ] + '="' + attr[ 1 ] + '"';
  69. }
  70. return result;
  71. }
  72. function modelToString( item ) {
  73. let result = '';
  74. if ( item instanceof ModelTextProxy ) {
  75. const attributes = modelAttributesToString( item );
  76. result = attributes ? '<$text' + attributes + '>' + item.data + '</$text>' : item.data;
  77. } else {
  78. const walker = new ModelWalker( { boundaries: ModelRange.createIn( item ), shallow: true } );
  79. for ( const value of walker ) {
  80. result += modelToString( value.item );
  81. }
  82. if ( item instanceof ModelElement ) {
  83. const attributes = modelAttributesToString( item );
  84. result = '<' + item.name + attributes + '>' + result + '</' + item.name + '>';
  85. }
  86. }
  87. return result;
  88. }
  89. // Converter overwrites default attribute converter for `linkHref` and `linkTitle` attribute is set on `quote` element.
  90. //
  91. // Model:
  92. //
  93. // [quote {linkHref='foo.html' linkTitle='Foo source'}]
  94. // ├─ f
  95. // ├─ o
  96. // └─ o
  97. //
  98. // foo {linkHref='foo.html' linkTitle='Foo title'}
  99. //
  100. // View:
  101. //
  102. // <blockquote>
  103. // ├─ foo
  104. // └─ <a href="foo.html" title="Foo source">
  105. // └─ see source
  106. //
  107. // <a href="foo.html" title="Foo title">
  108. // └─ foo
  109. describe( 'custom attribute handling for given element', () => {
  110. beforeEach( () => {
  111. // Normal model-to-view converters for links.
  112. modelDispatcher.on( 'attribute:linkHref', wrap( value => new ViewAttributeElement( 'a', { href: value } ) ) );
  113. modelDispatcher.on( 'attribute:linkTitle', wrap( value => new ViewAttributeElement( 'a', { title: value } ) ) );
  114. // Normal view-to-model converters for links.
  115. viewDispatcher.on( 'element:a', ( evt, data, consumable, conversionApi ) => {
  116. if ( consumable.consume( data.input, { name: true, attribute: 'href' } ) ) {
  117. if ( !data.output ) {
  118. data.output = conversionApi.convertChildren( data.input, consumable );
  119. }
  120. for ( const child of data.output ) {
  121. child.setAttribute( 'linkHref', data.input.getAttribute( 'href' ) );
  122. }
  123. }
  124. } );
  125. viewDispatcher.on( 'element:a', ( evt, data, consumable, conversionApi ) => {
  126. if ( consumable.consume( data.input, { attribute: 'title' } ) ) {
  127. if ( !data.output ) {
  128. data.output = conversionApi.convertChildren( data.input, consumable );
  129. }
  130. for ( const child of data.output ) {
  131. child.setAttribute( 'linkTitle', data.input.getAttribute( 'title' ) );
  132. }
  133. }
  134. } );
  135. // Model-to-view converter for quote element.
  136. modelDispatcher.on( 'insert:quote', ( evt, data, consumable, conversionApi ) => {
  137. consumable.consume( data.item, 'insert' );
  138. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  139. const viewElement = new ViewContainerElement( 'blockquote' );
  140. conversionApi.mapper.bindElements( data.item, viewElement );
  141. viewWriter.insert( viewPosition, viewElement );
  142. }, { priority: 'high' } );
  143. modelDispatcher.on( 'attribute:linkHref:quote', linkHrefOnQuoteConverter, { priority: 'high' } );
  144. modelDispatcher.on( 'attribute:linkTitle:quote', linkTitleOnQuoteConverter, { priority: 'high' } );
  145. function linkHrefOnQuoteConverter( evt, data, consumable, conversionApi ) {
  146. if ( !consumable.consume( data.item, 'attribute:linkHref' ) ) {
  147. return;
  148. }
  149. const viewQuote = conversionApi.mapper.toViewElement( data.item );
  150. if ( data.attributeNewValue === null ) {
  151. // Attribute was removed -> remove the view link.
  152. const viewLink = viewQuote.getChild( viewQuote.childCount - 1 );
  153. viewWriter.remove( ViewRange.createOn( viewLink ) );
  154. consumable.consume( data.item, 'attribute:linkTitle' );
  155. } else if ( data.attributeOldValue === null ) {
  156. // Attribute was added -> add the view link.
  157. const viewLink = new ViewAttributeElement(
  158. 'a', { href: data.item.getAttribute( 'linkHref' ) }, new ViewText( 'see source' )
  159. );
  160. if ( consumable.consume( data.item, 'attribute:linkTitle' ) && data.item.getAttribute( 'linkTitle' ) !== null ) {
  161. viewLink.setAttribute( 'title', data.item.getAttribute( 'linkTitle' ) );
  162. }
  163. viewWriter.insert( new ViewPosition( viewQuote, viewQuote.childCount ), viewLink );
  164. } else {
  165. // Attribute has changed -> change the existing view link.
  166. const viewLink = viewQuote.getChild( viewQuote.childCount - 1 );
  167. viewLink.setAttribute( 'href', data.attributeNewValue );
  168. }
  169. }
  170. function linkTitleOnQuoteConverter( evt, data, consumable, conversionApi ) {
  171. if ( !consumable.consume( data.item, 'attribute:linkTitle' ) ) {
  172. return;
  173. }
  174. const viewQuote = conversionApi.mapper.toViewElement( data.item );
  175. const viewLink = viewQuote.getChild( viewQuote.childCount - 1 );
  176. if ( !viewLink ) {
  177. return;
  178. }
  179. if ( data.attributeNewValue === null ) {
  180. viewLink.removeAttribute( 'title' );
  181. } else {
  182. viewLink.setAttribute( 'title', data.attributeNewValue );
  183. }
  184. }
  185. // View-to-model converter for quote element.
  186. viewDispatcher.on( 'element:blockquote', ( evt, data, consumable, conversionApi ) => {
  187. if ( consumable.consume( data.input, { name: true } ) ) {
  188. data.output = new ModelElement( 'quote' );
  189. const viewA = data.input.getChild( data.input.childCount - 1 );
  190. // Convert the special "a" first, before converting all children.
  191. if ( viewA instanceof ViewElement && viewA.name == 'a' && consumable.consume( viewA, { name: true } ) ) {
  192. if ( consumable.consume( viewA, { attribute: 'href' } ) ) {
  193. data.output.setAttribute( 'linkHref', viewA.getAttribute( 'href' ) );
  194. }
  195. if ( consumable.consume( viewA, { attribute: 'title' } ) ) {
  196. data.output.setAttribute( 'linkTitle', viewA.getAttribute( 'title' ) );
  197. }
  198. }
  199. const children = conversionApi.convertChildren( data.input, consumable );
  200. data.output.appendChildren( children );
  201. }
  202. } );
  203. } );
  204. it( 'should convert model text with linkHref and linkTitle to view', () => {
  205. const modelText = new ModelText( 'foo', { linkHref: 'foo.html', linkTitle: 'Foo title' } );
  206. // Let's insert text with link attributes.
  207. model.change( writer => {
  208. writer.insert(
  209. modelText,
  210. new ModelPosition( modelRoot, [ 0 ] )
  211. );
  212. } );
  213. let range = ModelRange.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 3 );
  214. expect( viewToString( viewRoot ) ).to.equal( '<div><a href="foo.html" title="Foo title">foo</a></div>' );
  215. // Let's change link's attributes.
  216. model.change( writer => {
  217. writer.setAttribute( 'linkHref', 'bar.html', range );
  218. writer.setAttribute( 'linkTitle', 'Bar title', range );
  219. } );
  220. expect( viewToString( viewRoot ) ).to.equal( '<div><a href="bar.html" title="Bar title">foo</a></div>' );
  221. // Let's remove a letter from the link.
  222. model.change( writer => {
  223. writer.remove( ModelRange.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 1 ) );
  224. } );
  225. expect( viewToString( viewRoot ) ).to.equal( '<div><a href="bar.html" title="Bar title">oo</a></div>' );
  226. // Let's remove just one attribute.
  227. model.change( writer => {
  228. range = ModelRange.createIn( modelRoot );
  229. writer.removeAttribute( 'linkTitle', range );
  230. } );
  231. expect( viewToString( viewRoot ) ).to.equal( '<div><a href="bar.html">oo</a></div>' );
  232. // Let's remove the other attribute.
  233. model.change( writer => {
  234. range = ModelRange.createIn( modelRoot );
  235. writer.removeAttribute( 'linkHref', range );
  236. } );
  237. expect( viewToString( viewRoot ) ).to.equal( '<div>oo</div>' );
  238. } );
  239. it( 'should convert a view element to model', () => {
  240. const viewElement = new ViewAttributeElement( 'a', { href: 'foo.html', title: 'Foo title' }, new ViewText( 'foo' ) );
  241. const modelText = viewDispatcher.convert( viewElement ).getChild( 0 );
  242. expect( modelText ).to.be.instanceof( ModelText );
  243. expect( modelText.data ).to.equal( 'foo' );
  244. expect( modelText.getAttribute( 'linkHref' ) ).to.equal( 'foo.html' );
  245. expect( modelText.getAttribute( 'linkTitle' ) ).to.equal( 'Foo title' );
  246. } );
  247. it( 'should convert quote model element with linkHref and linkTitle attribute to view', () => {
  248. modelDispatcher.on( 'attribute:bold', wrap( new ViewAttributeElement( 'strong' ) ) );
  249. const modelElement = new ModelElement( 'quote', { linkHref: 'foo.html', linkTitle: 'Foo source' }, new ModelText( 'foo' ) );
  250. // Let's insert a quote element with link attribute.
  251. model.change( writer => {
  252. writer.insert(
  253. modelElement,
  254. new ModelPosition( modelRoot, [ 0 ] )
  255. );
  256. } );
  257. let expected = '<div><blockquote>foo<a href="foo.html" title="Foo source">see source</a></blockquote></div>';
  258. expect( viewToString( viewRoot ) ).to.equal( expected );
  259. // And insert some additional content into it.
  260. model.change( writer => {
  261. writer.insert(
  262. new ModelText( 'bar', { bold: true } ),
  263. new ModelPosition( modelRoot, [ 0, 3 ] )
  264. );
  265. } );
  266. expected = '<div><blockquote>foo<strong>bar</strong><a href="foo.html" title="Foo source">see source</a></blockquote></div>';
  267. expect( viewToString( viewRoot ) ).to.equal( expected );
  268. // Let's change some attributes.
  269. model.change( writer => {
  270. writer.removeAttribute( 'linkTitle', modelElement );
  271. writer.setAttribute( 'linkHref', 'bar.html', modelElement );
  272. } );
  273. expected = '<div><blockquote>foo<strong>bar</strong><a href="bar.html">see source</a></blockquote></div>';
  274. expect( viewToString( viewRoot ) ).to.equal( expected );
  275. // Let's remove the only attribute connected with link.
  276. model.change( writer => {
  277. writer.removeAttribute( 'linkHref', modelElement );
  278. } );
  279. expected = '<div><blockquote>foo<strong>bar</strong></blockquote></div>';
  280. expect( viewToString( viewRoot ) ).to.equal( expected );
  281. } );
  282. it( 'should convert view blockquote with a element to model', () => {
  283. const viewElement = new ViewContainerElement(
  284. 'blockquote',
  285. null,
  286. [
  287. new ViewText( 'foo' ),
  288. new ViewAttributeElement(
  289. 'a',
  290. {
  291. href: 'foo.html',
  292. title: 'Foo source'
  293. },
  294. new ViewText( 'see source' )
  295. )
  296. ]
  297. );
  298. const modelElement = viewDispatcher.convert( viewElement );
  299. expect( modelToString( modelElement ) ).to.equal( '<quote linkHref="foo.html" linkTitle="Foo source">foo</quote>' );
  300. } );
  301. } );
  302. // Default view converter for tables that will convert table structure into paragraphs if tables are not supported.
  303. // TRs are supposed to become paragraphs and TDs content should be separated using space.
  304. it( 'default table view to model converter', () => {
  305. viewDispatcher.on( 'element:a', ( evt, data, consumable, conversionApi ) => {
  306. if ( consumable.consume( data.input, { name: true, attribute: 'href' } ) ) {
  307. if ( !data.output ) {
  308. data.output = conversionApi.convertChildren( data.input, consumable );
  309. }
  310. for ( const child of data.output ) {
  311. child.setAttribute( 'linkHref', data.input.getAttribute( 'href' ) );
  312. }
  313. }
  314. } );
  315. viewDispatcher.on( 'element:tr', ( evt, data, consumable, conversionApi ) => {
  316. if ( consumable.consume( data.input, { name: true } ) ) {
  317. data.output = new ModelElement( 'paragraph' );
  318. const children = conversionApi.convertChildren( data.input, consumable );
  319. for ( let i = 1; i < children.childCount; i++ ) {
  320. const child = children.getChild( i );
  321. if ( child instanceof ModelText && child.previousSibling instanceof ModelText ) {
  322. children.insertChildren( i, new ModelText( ' ' ) );
  323. i++;
  324. }
  325. }
  326. data.output.appendChildren( children );
  327. }
  328. } );
  329. viewDispatcher.on( 'element:table', ( evt, data, consumable, conversionApi ) => {
  330. if ( consumable.consume( data.input, { name: true } ) ) {
  331. data.output = conversionApi.convertChildren( data.input, consumable );
  332. }
  333. } );
  334. viewDispatcher.on( 'element:td', ( evt, data, consumable, conversionApi ) => {
  335. if ( consumable.consume( data.input, { name: true } ) ) {
  336. data.output = conversionApi.convertChildren( data.input, consumable );
  337. }
  338. } );
  339. const viewTable = new ViewContainerElement( 'table', null, [
  340. new ViewContainerElement( 'tr', null, [
  341. new ViewContainerElement( 'td', null, new ViewText( 'foo' ) ),
  342. new ViewContainerElement( 'td', null, new ViewAttributeElement( 'a', { href: 'bar.html' }, new ViewText( 'bar' ) ) )
  343. ] ),
  344. new ViewContainerElement( 'tr', null, [
  345. new ViewContainerElement( 'td' ),
  346. new ViewContainerElement( 'td', null, new ViewText( 'abc' ) )
  347. ] )
  348. ] );
  349. expect( modelToString( viewDispatcher.convert( viewTable ) ) )
  350. .to.equal( '<paragraph>foo <$text linkHref="bar.html">bar</$text></paragraph><paragraph>abc</paragraph>' );
  351. } );
  352. // Model converter that converts any non-converted elements and attributes into view elements and attributes.
  353. // View converter that converts any non-converted elements and attributes into model elements and attributes.
  354. describe( 'universal converter', () => {
  355. beforeEach( () => {
  356. // "Universal" converters
  357. modelDispatcher.on( 'insert', insertElement( data => new ViewContainerElement( data.item.name ) ), { priority: 'lowest' } );
  358. modelDispatcher.on( 'attribute', changeAttribute(), { priority: 'lowest' } );
  359. viewDispatcher.on( 'element', ( evt, data, consumable, conversionApi ) => {
  360. if ( consumable.consume( data.input, { name: true } ) ) {
  361. data.output = new ModelElement( data.input.name );
  362. for ( const key of data.input.getAttributeKeys() ) {
  363. if ( consumable.consume( data.input, { attribute: key } ) ) {
  364. data.output.setAttribute( key, data.input.getAttribute( key ) );
  365. }
  366. }
  367. data.output.appendChildren( conversionApi.convertChildren( data.input, consumable ) );
  368. }
  369. }, { priority: 'lowest' } );
  370. // "Real" converters -- added with higher priority. Should overwrite the "universal" converters.
  371. modelDispatcher.on( 'insert:image', insertElement( new ViewContainerElement( 'img' ) ) );
  372. modelDispatcher.on( 'attribute:bold', wrap( new ViewAttributeElement( 'strong' ) ) );
  373. viewDispatcher.on( 'element:img', ( evt, data, consumable ) => {
  374. if ( consumable.consume( data.input, { name: true } ) ) {
  375. const modelImage = new ModelElement( 'image' );
  376. for ( const attributeKey of data.input.getAttributeKeys() ) {
  377. modelImage.setAttribute( attributeKey, data.input.getAttribute( attributeKey ) );
  378. }
  379. data.output = modelImage;
  380. }
  381. } );
  382. viewDispatcher.on( 'element:strong', ( evt, data, consumable, conversionApi ) => {
  383. if ( consumable.consume( data.input, { name: true } ) ) {
  384. if ( !data.output ) {
  385. data.output = conversionApi.convertChildren( data.input, consumable );
  386. }
  387. for ( const child of data.output ) {
  388. child.setAttribute( 'bold', true );
  389. }
  390. }
  391. } );
  392. } );
  393. it( 'should convert model to view', () => {
  394. const modelElement = new ModelElement( 'table', { cellpadding: 5, cellspacing: 5 }, [
  395. new ModelElement( 'tr', null, [
  396. new ModelElement( 'td', null, [
  397. new ModelText( 'foo ' ),
  398. new ModelText( 'abc', { bold: true } ),
  399. new ModelText( ' bar' )
  400. ] ),
  401. new ModelElement( 'td', null, [
  402. new ModelElement( 'foo', { foo: 'bar' }, new ModelText( 'bar' ) )
  403. ] )
  404. ] )
  405. ] );
  406. modelRoot.appendChildren( modelElement );
  407. modelDispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  408. expect( viewToString( viewRoot ) ).to.equal(
  409. '<div>' +
  410. '<table cellpadding="5" cellspacing="5">' +
  411. '<tr>' +
  412. '<td>foo <strong>abc</strong> bar</td>' +
  413. '<td><foo foo="bar">bar</foo></td>' +
  414. '</tr>' +
  415. '</table>' +
  416. '</div>'
  417. );
  418. } );
  419. it( 'should convert view to model', () => {
  420. const viewElement = new ViewContainerElement( 'table', { cellpadding: 5, cellspacing: 5 }, [
  421. new ViewContainerElement( 'tr', null, [
  422. new ViewContainerElement( 'td', null, [
  423. new ViewText( 'foo ' ),
  424. new ViewAttributeElement( 'strong', null, new ViewText( 'abc' ) ),
  425. new ViewText( ' bar' )
  426. ] ),
  427. new ViewContainerElement( 'td', null, new ViewContainerElement( 'foo', { foo: 'bar' }, new ViewText( 'bar' ) ) )
  428. ] )
  429. ] );
  430. const modelElement = viewDispatcher.convert( viewElement );
  431. expect( modelToString( modelElement ) ).to.equal(
  432. '<table cellpadding="5" cellspacing="5">' +
  433. '<tr>' +
  434. '<td>foo <$text bold="true">abc</$text> bar</td>' +
  435. '<td><foo foo="bar">bar</foo></td>' +
  436. '</tr>' +
  437. '</table>'
  438. );
  439. } );
  440. } );
  441. } );