conversion.js 19 KB

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