8
0

advanced-converters.js 19 KB

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