conversion.js 20 KB

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