conversion.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. test( '<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. test( '<div>Foo</div>', '<paragraph>Foo</paragraph>' );
  120. test( '<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. test( '<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. test( '<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. test( '<p>Foo</p>', '<paragraph>Foo</paragraph>' );
  158. test( '<div>Foo</div>', '<paragraph>Foo</paragraph>', '<p>Foo</p>' );
  159. test( '<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. test( '<p></p>', '<paragraph></paragraph>' );
  189. test( '<p style="font-size:20px"></p>', '<paragraph></paragraph>', '<p></p>' );
  190. test( '<h2></h2>', '<heading></heading>' );
  191. test( '<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. test( '<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. test( '<p><b>Foo</b></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>' );
  206. test( '<p><strong>Foo</strong></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>', '<p><b>Foo</b></p>' );
  207. } );
  208. it( 'config.converterPriority is defined (override upcast)', () => {
  209. schema.extend( '$text', {
  210. allowAttributes: [ 'foo' ]
  211. } );
  212. conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  213. conversion.attributeToElement( { model: 'foo', view: 'strong', converterPriority: 'high' } );
  214. test(
  215. '<p><strong>Foo</strong></p>',
  216. '<paragraph><$text foo="true">Foo</$text></paragraph>',
  217. '<p><strong>Foo</strong></p>'
  218. );
  219. } );
  220. it( 'config.view is an object', () => {
  221. conversion.attributeToElement( {
  222. model: 'bold',
  223. view: {
  224. name: 'span',
  225. classes: 'bold'
  226. }
  227. } );
  228. test( '<p><span class="bold">Foo</span> bar</p>', '<paragraph><$text bold="true">Foo</$text> bar</paragraph>' );
  229. } );
  230. it( 'config.view is an object with upcastAlso defined', () => {
  231. schema.extend( '$text', {
  232. allowAttributes: [ 'bold', 'xBold' ]
  233. } );
  234. conversion.attributeToElement( {
  235. model: 'xBold',
  236. view: 'x-bold'
  237. } );
  238. conversion.attributeToElement( {
  239. model: 'bold',
  240. view: 'strong',
  241. upcastAlso: [
  242. 'b',
  243. {
  244. name: 'span',
  245. classes: 'bold'
  246. },
  247. viewElement => {
  248. const fontWeight = viewElement.getStyle( 'font-weight' );
  249. if ( fontWeight == 'bold' || Number( fontWeight ) > 500 ) {
  250. return {
  251. styles: [ 'font-weight' ]
  252. };
  253. }
  254. },
  255. // Duplicates the `x-bold` from above to test if only one attribute would be converted.
  256. // It should not convert to both bold & x-bold.
  257. viewElement => viewElement.is( 'x-bold' ) ? { name: 'x-bold' } : null
  258. ]
  259. } );
  260. test(
  261. '<p><strong>Foo</strong></p>',
  262. '<paragraph><$text bold="true">Foo</$text></paragraph>'
  263. );
  264. test(
  265. '<p><b>Foo</b></p>',
  266. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  267. '<p><strong>Foo</strong></p>'
  268. );
  269. test(
  270. '<p><span class="bold">Foo</span></p>',
  271. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  272. '<p><strong>Foo</strong></p>'
  273. );
  274. test(
  275. '<p><span style="font-weight: bold;">Foo</span></p>',
  276. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  277. '<p><strong>Foo</strong></p>'
  278. );
  279. test(
  280. '<p><span style="font-weight: 500;">Foo</span></p>',
  281. '<paragraph>Foo</paragraph>',
  282. '<p>Foo</p>'
  283. );
  284. test(
  285. '<p><span style="font-weight: 600;">Foo</span></p>',
  286. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  287. '<p><strong>Foo</strong></p>'
  288. );
  289. test(
  290. '<p style="font-weight: 600;">Foo</p>',
  291. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  292. '<p><strong>Foo</strong></p>'
  293. );
  294. test(
  295. '<p><x-bold style="font-wieght:bold">Foo</x-bold></p>',
  296. '<paragraph><$text xBold="true">Foo</$text></paragraph>',
  297. '<p><x-bold>Foo</x-bold></p>'
  298. );
  299. } );
  300. it( 'model attribute value is enumerable', () => {
  301. schema.extend( '$text', {
  302. allowAttributes: [ 'fontSize' ]
  303. } );
  304. conversion.attributeToElement( {
  305. model: {
  306. key: 'fontSize',
  307. values: [ 'big', 'small' ]
  308. },
  309. view: {
  310. big: {
  311. name: 'span',
  312. styles: {
  313. 'font-size': '1.2em'
  314. }
  315. },
  316. small: {
  317. name: 'span',
  318. styles: {
  319. 'font-size': '0.8em'
  320. }
  321. }
  322. },
  323. upcastAlso: {
  324. big: viewElement => {
  325. const fontSize = viewElement.getStyle( 'font-size' );
  326. if ( !fontSize ) {
  327. return null;
  328. }
  329. const match = fontSize.match( /(\d+)\s*px/ );
  330. if ( !match ) {
  331. return null;
  332. }
  333. const size = Number( match[ 1 ] );
  334. if ( viewElement.is( 'span' ) && size > 10 ) {
  335. return { name: true, style: [ 'font-size' ] };
  336. }
  337. return null;
  338. },
  339. small: viewElement => {
  340. const fontSize = viewElement.getStyle( 'font-size' );
  341. if ( !fontSize ) {
  342. return null;
  343. }
  344. const match = fontSize.match( /(\d+)\s*px/ );
  345. if ( !match ) {
  346. return null;
  347. }
  348. const size = Number( match[ 1 ] );
  349. if ( viewElement.is( 'span' ) && size < 10 ) {
  350. return { name: true, style: [ 'font-size' ] };
  351. }
  352. return null;
  353. }
  354. }
  355. } );
  356. test(
  357. '<p><span style="font-size:1.2em">Foo</span> bar</p>',
  358. '<paragraph><$text fontSize="big">Foo</$text> bar</paragraph>'
  359. );
  360. test(
  361. '<p><span style="font-size:12px">Foo</span> bar</p>',
  362. '<paragraph><$text fontSize="big">Foo</$text> bar</paragraph>',
  363. '<p><span style="font-size:1.2em">Foo</span> bar</p>'
  364. );
  365. test(
  366. '<p><span style="font-size:0.8em">Foo</span> bar</p>',
  367. '<paragraph><$text fontSize="small">Foo</$text> bar</paragraph>'
  368. );
  369. test(
  370. '<p><span style="font-size:8px">Foo</span> bar</p>',
  371. '<paragraph><$text fontSize="small">Foo</$text> bar</paragraph>',
  372. '<p><span style="font-size:0.8em">Foo</span> bar</p>'
  373. );
  374. test(
  375. '<p><span style="font-size:10px">Foo</span> bar</p>',
  376. '<paragraph>Foo bar</paragraph>',
  377. '<p>Foo bar</p>'
  378. );
  379. } );
  380. it( 'config.model.name is given', () => {
  381. schema.extend( '$text', {
  382. allowAttributes: [ 'textDecoration' ]
  383. } );
  384. conversion.attributeToElement( {
  385. model: {
  386. key: 'textDecoration',
  387. values: [ 'underline', 'lineThrough' ],
  388. name: '$text'
  389. },
  390. view: {
  391. underline: {
  392. name: 'span',
  393. styles: {
  394. 'text-decoration': 'underline'
  395. }
  396. },
  397. lineThrough: {
  398. name: 'span',
  399. styles: {
  400. 'text-decoration': 'line-through'
  401. }
  402. }
  403. }
  404. } );
  405. test(
  406. '<p><span style="text-decoration:underline">Foo</span></p>',
  407. '<paragraph><$text textDecoration="underline">Foo</$text></paragraph>'
  408. );
  409. test(
  410. '<p><span style="text-decoration:line-through">Foo</span></p>',
  411. '<paragraph><$text textDecoration="lineThrough">Foo</$text></paragraph>'
  412. );
  413. test(
  414. '<p><span style="text-decoration:underline">Foo</span></p>',
  415. '<paragraph><$text textDecoration="underline">Foo</$text></paragraph>'
  416. );
  417. } );
  418. } );
  419. describe( 'attributeToAttribute', () => {
  420. beforeEach( () => {
  421. conversion.elementToElement( { model: 'image', view: 'img' } );
  422. schema.register( 'image', {
  423. inheritAllFrom: '$block',
  424. } );
  425. } );
  426. it( 'config.view and config.model are strings', () => {
  427. schema.extend( 'image', {
  428. allowAttributes: [ 'source' ]
  429. } );
  430. conversion.attributeToAttribute( { model: 'source', view: 'src' } );
  431. test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
  432. } );
  433. it( 'config.view and config.model are objects', () => {
  434. schema.extend( 'image', {
  435. allowAttributes: [ 'aside' ]
  436. } );
  437. conversion.attributeToAttribute( {
  438. model: {
  439. name: 'image',
  440. key: 'aside',
  441. values: [ 'aside' ]
  442. },
  443. view: {
  444. aside: {
  445. name: 'img',
  446. key: 'class',
  447. value: [ 'aside', 'half-size' ]
  448. }
  449. }
  450. } );
  451. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  452. test( '<img class="aside half-size"></img>', '<image aside="aside"></image>' );
  453. test( '<p class="aside half-size"></p>', '<paragraph></paragraph>', '<p></p>' );
  454. } );
  455. it( 'config.view and config.model are objects - convert to style attribute', () => {
  456. schema.extend( 'image', {
  457. allowAttributes: [ 'aside' ]
  458. } );
  459. conversion.attributeToAttribute( {
  460. model: {
  461. name: 'image',
  462. key: 'aside',
  463. values: [ 'aside' ]
  464. },
  465. view: {
  466. aside: {
  467. name: 'img',
  468. key: 'style',
  469. value: {
  470. float: 'right',
  471. width: '50%',
  472. margin: '5px'
  473. }
  474. }
  475. }
  476. } );
  477. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  478. test( '<img style="float:right;margin:5px;width:50%"></img>', '<image aside="aside"></image>' );
  479. test( '<p style="float:right;margin:5px;width:50%"></p>', '<paragraph></paragraph>', '<p></p>' );
  480. } );
  481. it( 'config is an array with upcastAlso defined', () => {
  482. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  483. schema.extend( 'paragraph', {
  484. allowAttributes: [ 'align' ]
  485. } );
  486. conversion.attributeToAttribute( {
  487. model: {
  488. key: 'align',
  489. values: [ 'right', 'center' ]
  490. },
  491. view: {
  492. right: {
  493. key: 'class',
  494. value: 'align-right'
  495. },
  496. center: {
  497. key: 'class',
  498. value: 'align-center'
  499. }
  500. },
  501. upcastAlso: {
  502. right: {
  503. styles: {
  504. 'text-align': 'right'
  505. }
  506. },
  507. center: {
  508. styles: {
  509. 'text-align': 'center'
  510. }
  511. }
  512. }
  513. } );
  514. test(
  515. '<p class="align-right">Foo</p>',
  516. '<paragraph align="right">Foo</paragraph>'
  517. );
  518. test(
  519. '<p style="text-align:right">Foo</p>',
  520. '<paragraph align="right">Foo</paragraph>',
  521. '<p class="align-right">Foo</p>'
  522. );
  523. test(
  524. '<p class="align-center">Foo</p>',
  525. '<paragraph align="center">Foo</paragraph>'
  526. );
  527. test(
  528. '<p style="text-align:center">Foo</p>',
  529. '<paragraph align="center">Foo</paragraph>',
  530. '<p class="align-center">Foo</p>'
  531. );
  532. } );
  533. it( 'config.view and config.model have name and key set', () => {
  534. schema.extend( 'image', {
  535. allowAttributes: [ 'source' ]
  536. } );
  537. conversion.attributeToAttribute( {
  538. model: {
  539. name: 'image',
  540. key: 'source'
  541. },
  542. view: {
  543. name: 'img',
  544. key: 'src'
  545. }
  546. } );
  547. test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
  548. } );
  549. // #1443.
  550. it( 'should not set attributes on the element\'s children', () => {
  551. schema.register( 'div', {
  552. inheritAllFrom: '$root',
  553. allowWhere: '$block',
  554. isLimit: true,
  555. allowAttributes: [ 'border', 'shade' ]
  556. } );
  557. conversion.elementToElement(
  558. { model: 'div', view: 'div' }
  559. );
  560. conversion.attributeToAttribute( { model: 'border', view: { key: 'class', value: 'border' } } );
  561. conversion.attributeToAttribute( { model: 'shade', view: { key: 'class', value: 'shade' } } );
  562. test(
  563. '<div class="border"><div class="shade"></div></div>',
  564. '<div border="border"><div shade="shade"></div></div>'
  565. );
  566. } );
  567. it( 'config.converterPriority is defined (override downcast)', () => {
  568. schema.extend( 'image', {
  569. allowAttributes: [ 'foo' ]
  570. } );
  571. conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  572. conversion.attributeToAttribute( { model: 'foo', view: 'foofoo', converterPriority: 'high' } );
  573. test( '<img foo="foo"></img>', '<image foo="foo"></image>', '<img foofoo="foo"></img>' );
  574. } );
  575. } );
  576. function test( input, expectedModel, expectedView = null ) {
  577. loadData( input );
  578. expect( modelStringify( model.document.getRoot() ) ).to.equal( expectedModel );
  579. expect( viewStringify( viewRoot, null, { ignoreRoot: true } ) ).to.equal( expectedView || input );
  580. }
  581. function loadData( input ) {
  582. const parsedView = viewParse( input );
  583. let convertedModel;
  584. model.change( writer => {
  585. convertedModel = viewDispatcher.convert( parsedView, writer );
  586. } );
  587. model.change( writer => {
  588. writer.remove( writer.createRange(
  589. writer.createPositionAt( modelRoot, 0 ),
  590. writer.createPositionAt( modelRoot, modelRoot.maxOffset ) )
  591. );
  592. writer.insert( convertedModel, modelRoot, 0 );
  593. } );
  594. }
  595. } );
  596. } );