8
0

conversion.js 17 KB

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