conversion.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Conversion, { ConversionHelpers } from '../../src/conversion/conversion';
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. import UpcastDispatcher from '../../src/conversion/upcastdispatcher';
  8. import UpcastHelpers, { convertText, convertToModelFragment } from '../../src/conversion/upcasthelpers';
  9. import DowncastHelpers from '../../src/conversion/downcasthelpers';
  10. import EditingController from '../../src/controller/editingcontroller';
  11. import Model from '../../src/model/model';
  12. import { parse as viewParse, stringify as viewStringify } from '../../src/dev-utils/view';
  13. import { stringify as modelStringify } from '../../src/dev-utils/model';
  14. describe( 'Conversion', () => {
  15. let conversion, dispA, dispB, dispC;
  16. beforeEach( () => {
  17. conversion = new Conversion();
  18. // Placeholders. Will be used only to see if their were given as attribute for a spy function.
  19. dispA = Symbol( 'dispA' );
  20. dispB = Symbol( 'dispB' );
  21. dispC = Symbol( 'dispC' );
  22. conversion.register( 'ab', new UpcastHelpers( [ dispA, dispB ] ) );
  23. conversion.register( 'a', new UpcastHelpers( dispA ) );
  24. conversion.register( 'b', new UpcastHelpers( dispB ) );
  25. conversion.register( 'c', new DowncastHelpers( dispC ) );
  26. } );
  27. describe( 'register()', () => {
  28. it( 'should throw when trying to use same group name twice', () => {
  29. expect( () => {
  30. conversion.register( 'ab' );
  31. } ).to.throw( CKEditorError, /conversion-register-group-exists/ );
  32. } );
  33. } );
  34. describe( 'for()', () => {
  35. it( 'should return ConversionHelpers', () => {
  36. expect( conversion.for( 'ab' ) ).to.be.instanceof( ConversionHelpers );
  37. } );
  38. it( 'should throw if non-existing group name has been used', () => {
  39. expect( () => {
  40. conversion.for( 'foo' );
  41. } ).to.throw( CKEditorError, /conversion-for-unknown-group/ );
  42. } );
  43. it( 'should return proper helpers for group', () => {
  44. expect( conversion.for( 'ab' ) ).to.be.instanceof( UpcastHelpers );
  45. expect( conversion.for( 'a' ) ).to.be.instanceof( UpcastHelpers );
  46. expect( conversion.for( 'b' ) ).to.be.instanceof( UpcastHelpers );
  47. expect( conversion.for( 'c' ) ).to.be.instanceof( DowncastHelpers );
  48. } );
  49. } );
  50. describe( 'add()', () => {
  51. let helperA, helperB;
  52. beforeEach( () => {
  53. helperA = sinon.stub();
  54. helperB = sinon.stub();
  55. } );
  56. it( 'should be chainable', () => {
  57. const forResult = conversion.for( 'ab' );
  58. const addResult = forResult.add( () => {} );
  59. expect( addResult ).to.equal( addResult.add( () => {} ) );
  60. } );
  61. it( 'should fire given helper for every dispatcher in given group', () => {
  62. conversion.for( 'ab' ).add( helperA );
  63. expect( helperA.calledWithExactly( dispA ) ).to.be.true;
  64. expect( helperA.calledWithExactly( dispB ) ).to.be.true;
  65. conversion.for( 'b' ).add( helperB );
  66. expect( helperB.calledWithExactly( dispA ) ).to.be.false;
  67. expect( helperB.calledWithExactly( dispB ) ).to.be.true;
  68. } );
  69. } );
  70. describe( 'converters', () => {
  71. let viewDispatcher, model, schema, conversion, modelRoot, viewRoot;
  72. beforeEach( () => {
  73. model = new Model();
  74. const controller = new EditingController( model );
  75. const modelDoc = model.document;
  76. modelRoot = modelDoc.createRoot();
  77. viewRoot = controller.view.document.getRoot();
  78. // Set name of view root the same as dom root.
  79. // This is a mock of attaching view root to dom root.
  80. viewRoot._name = 'div';
  81. schema = model.schema;
  82. schema.extend( '$text', {
  83. allowIn: '$root',
  84. allowAttributes: [ 'bold' ]
  85. } );
  86. schema.register( 'paragraph', {
  87. inheritAllFrom: '$block'
  88. } );
  89. viewDispatcher = new UpcastDispatcher( model, { schema } );
  90. viewDispatcher.on( 'text', convertText() );
  91. viewDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  92. viewDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
  93. conversion = new Conversion();
  94. conversion.register( 'upcast', new UpcastHelpers( [ viewDispatcher ] ) );
  95. conversion.register( 'downcast', new DowncastHelpers( controller.downcastDispatcher ) );
  96. } );
  97. describe( 'elementToElement', () => {
  98. it( 'config.view is a string', () => {
  99. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  100. test( '<p>Foo</p>', '<paragraph>Foo</paragraph>' );
  101. } );
  102. it( 'config.converterPriority is defined', () => {
  103. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  104. conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
  105. test( '<div>Foo</div>', '<paragraph>Foo</paragraph>' );
  106. test( '<p>Foo</p>', '<paragraph>Foo</paragraph>', '<div>Foo</div>' );
  107. } );
  108. it( 'config.view is an object', () => {
  109. schema.register( 'fancyParagraph', {
  110. inheritAllFrom: 'paragraph'
  111. } );
  112. conversion.elementToElement( {
  113. model: 'fancyParagraph',
  114. view: {
  115. name: 'p',
  116. classes: 'fancy'
  117. }
  118. } );
  119. test( '<p class="fancy">Foo</p>', '<fancyParagraph>Foo</fancyParagraph>' );
  120. } );
  121. it( 'config.view is an object with upcastAlso defined', () => {
  122. conversion.elementToElement( {
  123. model: 'paragraph',
  124. view: 'p',
  125. upcastAlso: [
  126. 'div',
  127. {
  128. // Any element with `display: block` style.
  129. styles: {
  130. display: 'block'
  131. }
  132. }
  133. ]
  134. } );
  135. test( '<p>Foo</p>', '<paragraph>Foo</paragraph>' );
  136. test( '<div>Foo</div>', '<paragraph>Foo</paragraph>', '<p>Foo</p>' );
  137. test( '<span style="display:block">Foo</span>', '<paragraph>Foo</paragraph>', '<p>Foo</p>' );
  138. } );
  139. it( 'upcastAlso given as a function', () => {
  140. schema.register( 'heading', {
  141. inheritAllFrom: '$block'
  142. } );
  143. conversion.elementToElement( {
  144. model: 'heading',
  145. view: 'h2',
  146. upcastAlso: viewElement => {
  147. const fontSize = viewElement.getStyle( 'font-size' );
  148. if ( !fontSize ) {
  149. return null;
  150. }
  151. const match = fontSize.match( /(\d+)\s*px/ );
  152. if ( !match ) {
  153. return null;
  154. }
  155. const size = Number( match[ 1 ] );
  156. if ( size >= 26 ) {
  157. return { name: true, style: [ 'font-size' ] };
  158. }
  159. return null;
  160. }
  161. } );
  162. conversion.elementToElement( {
  163. model: 'paragraph',
  164. view: 'p'
  165. } );
  166. test( '<p></p>', '<paragraph></paragraph>' );
  167. test( '<p style="font-size:20px"></p>', '<paragraph></paragraph>', '<p></p>' );
  168. test( '<h2></h2>', '<heading></heading>' );
  169. test( '<p style="font-size:26px"></p>', '<heading></heading>', '<h2></h2>' );
  170. } );
  171. } );
  172. describe( 'attributeToElement', () => {
  173. beforeEach( () => {
  174. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  175. } );
  176. it( 'config.view is a string', () => {
  177. conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  178. test( '<p><strong>Foo</strong> bar</p>', '<paragraph><$text bold="true">Foo</$text> bar</paragraph>' );
  179. } );
  180. it( 'config.converterPriority is defined', () => {
  181. conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  182. conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
  183. test( '<p><b>Foo</b></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>' );
  184. test( '<p><strong>Foo</strong></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>', '<p><b>Foo</b></p>' );
  185. } );
  186. it( 'config.view is an object', () => {
  187. conversion.attributeToElement( {
  188. model: 'bold',
  189. view: {
  190. name: 'span',
  191. classes: 'bold'
  192. }
  193. } );
  194. test( '<p><span class="bold">Foo</span> bar</p>', '<paragraph><$text bold="true">Foo</$text> bar</paragraph>' );
  195. } );
  196. it( 'config.view is an object with upcastAlso defined', () => {
  197. conversion.attributeToElement( {
  198. model: 'bold',
  199. view: 'strong',
  200. upcastAlso: [
  201. 'b',
  202. {
  203. name: 'span',
  204. classes: 'bold'
  205. },
  206. {
  207. name: 'span',
  208. styles: {
  209. 'font-weight': 'bold'
  210. }
  211. },
  212. viewElement => {
  213. const fontWeight = viewElement.getStyle( 'font-weight' );
  214. if ( viewElement.is( 'span' ) && fontWeight && /\d+/.test( fontWeight ) && Number( fontWeight ) > 500 ) {
  215. return {
  216. name: true,
  217. styles: [ 'font-weight' ]
  218. };
  219. }
  220. }
  221. ]
  222. } );
  223. test(
  224. '<p><strong>Foo</strong></p>',
  225. '<paragraph><$text bold="true">Foo</$text></paragraph>'
  226. );
  227. test(
  228. '<p><b>Foo</b></p>',
  229. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  230. '<p><strong>Foo</strong></p>'
  231. );
  232. test(
  233. '<p><span class="bold">Foo</span></p>',
  234. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  235. '<p><strong>Foo</strong></p>'
  236. );
  237. test(
  238. '<p><span style="font-weight: bold;">Foo</span></p>',
  239. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  240. '<p><strong>Foo</strong></p>'
  241. );
  242. test(
  243. '<p><span style="font-weight: 500;">Foo</span></p>',
  244. '<paragraph>Foo</paragraph>',
  245. '<p>Foo</p>'
  246. );
  247. test(
  248. '<p><span style="font-weight: 600;">Foo</span></p>',
  249. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  250. '<p><strong>Foo</strong></p>'
  251. );
  252. } );
  253. it( 'model attribute value is enumerable', () => {
  254. schema.extend( '$text', {
  255. allowAttributes: [ 'fontSize' ]
  256. } );
  257. conversion.attributeToElement( {
  258. model: {
  259. key: 'fontSize',
  260. values: [ 'big', 'small' ]
  261. },
  262. view: {
  263. big: {
  264. name: 'span',
  265. styles: {
  266. 'font-size': '1.2em'
  267. }
  268. },
  269. small: {
  270. name: 'span',
  271. styles: {
  272. 'font-size': '0.8em'
  273. }
  274. }
  275. },
  276. upcastAlso: {
  277. big: viewElement => {
  278. const fontSize = viewElement.getStyle( 'font-size' );
  279. if ( !fontSize ) {
  280. return null;
  281. }
  282. const match = fontSize.match( /(\d+)\s*px/ );
  283. if ( !match ) {
  284. return null;
  285. }
  286. const size = Number( match[ 1 ] );
  287. if ( viewElement.is( 'span' ) && size > 10 ) {
  288. return { name: true, style: [ 'font-size' ] };
  289. }
  290. return null;
  291. },
  292. small: viewElement => {
  293. const fontSize = viewElement.getStyle( 'font-size' );
  294. if ( !fontSize ) {
  295. return null;
  296. }
  297. const match = fontSize.match( /(\d+)\s*px/ );
  298. if ( !match ) {
  299. return null;
  300. }
  301. const size = Number( match[ 1 ] );
  302. if ( viewElement.is( 'span' ) && size < 10 ) {
  303. return { name: true, style: [ 'font-size' ] };
  304. }
  305. return null;
  306. }
  307. }
  308. } );
  309. test(
  310. '<p><span style="font-size:1.2em">Foo</span> bar</p>',
  311. '<paragraph><$text fontSize="big">Foo</$text> bar</paragraph>'
  312. );
  313. test(
  314. '<p><span style="font-size:12px">Foo</span> bar</p>',
  315. '<paragraph><$text fontSize="big">Foo</$text> bar</paragraph>',
  316. '<p><span style="font-size:1.2em">Foo</span> bar</p>'
  317. );
  318. test(
  319. '<p><span style="font-size:0.8em">Foo</span> bar</p>',
  320. '<paragraph><$text fontSize="small">Foo</$text> bar</paragraph>'
  321. );
  322. test(
  323. '<p><span style="font-size:8px">Foo</span> bar</p>',
  324. '<paragraph><$text fontSize="small">Foo</$text> bar</paragraph>',
  325. '<p><span style="font-size:0.8em">Foo</span> bar</p>'
  326. );
  327. test(
  328. '<p><span style="font-size:10px">Foo</span> bar</p>',
  329. '<paragraph>Foo bar</paragraph>',
  330. '<p>Foo bar</p>'
  331. );
  332. } );
  333. it( 'config.model.name is given', () => {
  334. schema.extend( '$text', {
  335. allowAttributes: [ 'textDecoration' ]
  336. } );
  337. conversion.attributeToElement( {
  338. model: {
  339. key: 'textDecoration',
  340. values: [ 'underline', 'lineThrough' ],
  341. name: '$text'
  342. },
  343. view: {
  344. underline: {
  345. name: 'span',
  346. styles: {
  347. 'text-decoration': 'underline'
  348. }
  349. },
  350. lineThrough: {
  351. name: 'span',
  352. styles: {
  353. 'text-decoration': 'line-through'
  354. }
  355. }
  356. }
  357. } );
  358. test(
  359. '<p><span style="text-decoration:underline">Foo</span></p>',
  360. '<paragraph><$text textDecoration="underline">Foo</$text></paragraph>'
  361. );
  362. test(
  363. '<p><span style="text-decoration:line-through">Foo</span></p>',
  364. '<paragraph><$text textDecoration="lineThrough">Foo</$text></paragraph>'
  365. );
  366. test(
  367. '<p><span style="text-decoration:underline">Foo</span></p>',
  368. '<paragraph><$text textDecoration="underline">Foo</$text></paragraph>'
  369. );
  370. } );
  371. } );
  372. describe( 'attributeToAttribute', () => {
  373. beforeEach( () => {
  374. conversion.elementToElement( { model: 'image', view: 'img' } );
  375. schema.register( 'image', {
  376. inheritAllFrom: '$block',
  377. } );
  378. } );
  379. it( 'config.view and config.model are strings', () => {
  380. schema.extend( 'image', {
  381. allowAttributes: [ 'source' ]
  382. } );
  383. conversion.attributeToAttribute( { model: 'source', view: 'src' } );
  384. test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
  385. } );
  386. it( 'config.view and config.model are objects', () => {
  387. schema.extend( 'image', {
  388. allowAttributes: [ 'aside' ]
  389. } );
  390. conversion.attributeToAttribute( {
  391. model: {
  392. name: 'image',
  393. key: 'aside',
  394. values: [ 'aside' ]
  395. },
  396. view: {
  397. aside: {
  398. name: 'img',
  399. key: 'class',
  400. value: [ 'aside', 'half-size' ]
  401. }
  402. }
  403. } );
  404. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  405. test( '<img class="aside half-size"></img>', '<image aside="aside"></image>' );
  406. test( '<p class="aside half-size"></p>', '<paragraph></paragraph>', '<p></p>' );
  407. } );
  408. it( 'config.view and config.model are objects - convert to style attribute', () => {
  409. schema.extend( 'image', {
  410. allowAttributes: [ 'aside' ]
  411. } );
  412. conversion.attributeToAttribute( {
  413. model: {
  414. name: 'image',
  415. key: 'aside',
  416. values: [ 'aside' ]
  417. },
  418. view: {
  419. aside: {
  420. name: 'img',
  421. key: 'style',
  422. value: {
  423. float: 'right',
  424. width: '50%',
  425. margin: '5px'
  426. }
  427. }
  428. }
  429. } );
  430. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  431. test( '<img style="float:right;margin:5px;width:50%"></img>', '<image aside="aside"></image>' );
  432. test( '<p style="float:right;margin:5px;width:50%"></p>', '<paragraph></paragraph>', '<p></p>' );
  433. } );
  434. it( 'config is an array with upcastAlso defined', () => {
  435. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  436. schema.extend( 'paragraph', {
  437. allowAttributes: [ 'align' ]
  438. } );
  439. conversion.attributeToAttribute( {
  440. model: {
  441. key: 'align',
  442. values: [ 'right', 'center' ]
  443. },
  444. view: {
  445. right: {
  446. key: 'class',
  447. value: 'align-right'
  448. },
  449. center: {
  450. key: 'class',
  451. value: 'align-center'
  452. }
  453. },
  454. upcastAlso: {
  455. right: {
  456. styles: {
  457. 'text-align': 'right'
  458. }
  459. },
  460. center: {
  461. styles: {
  462. 'text-align': 'center'
  463. }
  464. }
  465. }
  466. } );
  467. test(
  468. '<p class="align-right">Foo</p>',
  469. '<paragraph align="right">Foo</paragraph>'
  470. );
  471. test(
  472. '<p style="text-align:right">Foo</p>',
  473. '<paragraph align="right">Foo</paragraph>',
  474. '<p class="align-right">Foo</p>'
  475. );
  476. test(
  477. '<p class="align-center">Foo</p>',
  478. '<paragraph align="center">Foo</paragraph>'
  479. );
  480. test(
  481. '<p style="text-align:center">Foo</p>',
  482. '<paragraph align="center">Foo</paragraph>',
  483. '<p class="align-center">Foo</p>'
  484. );
  485. } );
  486. it( 'config.view and config.model have name and key set', () => {
  487. schema.extend( 'image', {
  488. allowAttributes: [ 'source' ]
  489. } );
  490. conversion.attributeToAttribute( {
  491. model: {
  492. name: 'image',
  493. key: 'source'
  494. },
  495. view: {
  496. name: 'img',
  497. key: 'src'
  498. }
  499. } );
  500. test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
  501. } );
  502. // #1443.
  503. it( 'should not set attributes on the element\'s children', () => {
  504. schema.register( 'div', {
  505. inheritAllFrom: '$root',
  506. allowWhere: '$block',
  507. isLimit: true,
  508. allowAttributes: [ 'border', 'shade' ]
  509. } );
  510. conversion.elementToElement(
  511. { model: 'div', view: 'div' }
  512. );
  513. conversion.attributeToAttribute( { model: 'border', view: { key: 'class', value: 'border' } } );
  514. conversion.attributeToAttribute( { model: 'shade', view: { key: 'class', value: 'shade' } } );
  515. test(
  516. '<div class="border"><div class="shade"></div></div>',
  517. '<div border="border"><div shade="shade"></div></div>'
  518. );
  519. } );
  520. } );
  521. function test( input, expectedModel, expectedView = null ) {
  522. loadData( input );
  523. expect( modelStringify( model.document.getRoot() ) ).to.equal( expectedModel );
  524. expect( viewStringify( viewRoot, null, { ignoreRoot: true } ) ).to.equal( expectedView || input );
  525. }
  526. function loadData( input ) {
  527. const parsedView = viewParse( input );
  528. let convertedModel;
  529. model.change( writer => {
  530. convertedModel = viewDispatcher.convert( parsedView, writer );
  531. } );
  532. model.change( writer => {
  533. writer.remove( writer.createRange(
  534. writer.createPositionAt( modelRoot, 0 ),
  535. writer.createPositionAt( modelRoot, modelRoot.maxOffset ) )
  536. );
  537. writer.insert( convertedModel, modelRoot, 0 );
  538. } );
  539. }
  540. } );
  541. } );
  542. describe( 'ConversionHelpers', () => {
  543. describe( 'add()', () => {
  544. const dispA = Symbol( 'dispA' );
  545. const dispB = Symbol( 'dispB' );
  546. it( 'should call a helper for one defined dispatcher', () => {
  547. const spy = sinon.spy();
  548. const helpers = new ConversionHelpers( dispA );
  549. helpers.add( spy );
  550. sinon.assert.calledOnce( spy );
  551. sinon.assert.calledWithExactly( spy, dispA );
  552. } );
  553. it( 'should call helper for all defined dispatcherers', () => {
  554. const spy = sinon.spy();
  555. const helpers = new ConversionHelpers( [ dispA, dispB ] );
  556. helpers.add( spy );
  557. sinon.assert.calledTwice( spy );
  558. sinon.assert.calledWithExactly( spy, dispA );
  559. sinon.assert.calledWithExactly( spy, dispB );
  560. } );
  561. it( 'should be chainable', () => {
  562. const spy = sinon.spy();
  563. const helpers = new ConversionHelpers( dispA );
  564. expect( helpers.add( spy ) ).to.equal( helpers );
  565. } );
  566. } );
  567. } );