conversion.js 20 KB

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