advanced-converters.js 19 KB

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