advanced-converters.js 19 KB

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