conversion.js 20 KB

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