datacontroller.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 Model from '../../src/model/model';
  6. import ModelRange from '../../src/model/range';
  7. import ViewRange from '../../src/view/range';
  8. import DataController from '../../src/controller/datacontroller';
  9. import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
  10. import ModelDocumentFragment from '../../src/model/documentfragment';
  11. import ViewDocumentFragment from '../../src/view/documentfragment';
  12. import ViewDocument from '../../src/view/document';
  13. import { getData, setData, stringify, parse as parseModel } from '../../src/dev-utils/model';
  14. import { parse as parseView, stringify as stringifyView } from '../../src/dev-utils/view';
  15. import count from '@ckeditor/ckeditor5-utils/src/count';
  16. import UpcastHelpers from '../../src/conversion/upcasthelpers';
  17. import DowncastHelpers from '../../src/conversion/downcasthelpers';
  18. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  19. import { StylesProcessor } from '../../src/view/stylesmap';
  20. describe( 'DataController', () => {
  21. let model, modelDocument, htmlDataProcessor, data, schema, upcastHelpers, downcastHelpers, viewDocument;
  22. beforeEach( () => {
  23. const stylesProcessor = new StylesProcessor();
  24. model = new Model();
  25. schema = model.schema;
  26. modelDocument = model.document;
  27. modelDocument.createRoot();
  28. modelDocument.createRoot( '$title', 'title' );
  29. schema.register( '$title', { inheritAllFrom: '$root' } );
  30. viewDocument = new ViewDocument( stylesProcessor );
  31. htmlDataProcessor = new HtmlDataProcessor( viewDocument );
  32. data = new DataController( model, stylesProcessor );
  33. data.processor = htmlDataProcessor;
  34. upcastHelpers = new UpcastHelpers( [ data.upcastDispatcher ] );
  35. downcastHelpers = new DowncastHelpers( [ data.downcastDispatcher ] );
  36. } );
  37. describe( 'constructor()', () => {
  38. it( 'sets the model and styles processor properties', () => {
  39. const stylesProcessor = new StylesProcessor();
  40. const data = new DataController( model, stylesProcessor );
  41. expect( data.model ).to.equal( model );
  42. expect( data.stylesProcessor ).to.equal( stylesProcessor );
  43. } );
  44. it( 'should create the #viewDocument property', () => {
  45. const stylesProcessor = new StylesProcessor();
  46. const data = new DataController( model, stylesProcessor );
  47. expect( data.viewDocument ).to.be.instanceOf( ViewDocument );
  48. } );
  49. } );
  50. describe( 'parse()', () => {
  51. it( 'should set text', () => {
  52. schema.extend( '$text', { allowIn: '$root' } );
  53. const output = data.parse( '<p>foo<b>bar</b></p>' );
  54. expect( output ).to.instanceof( ModelDocumentFragment );
  55. expect( stringify( output ) ).to.equal( 'foobar' );
  56. } );
  57. it( 'should set paragraph', () => {
  58. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  59. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  60. const output = data.parse( '<p>foo<b>bar</b></p>' );
  61. expect( output ).to.instanceof( ModelDocumentFragment );
  62. expect( stringify( output ) ).to.equal( '<paragraph>foobar</paragraph>' );
  63. } );
  64. it( 'should set two paragraphs', () => {
  65. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  66. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  67. const output = data.parse( '<p>foo</p><p>bar</p>' );
  68. expect( output ).to.instanceof( ModelDocumentFragment );
  69. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  70. } );
  71. it( 'should set paragraphs with bold', () => {
  72. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  73. schema.extend( '$text', {
  74. allowAttributes: [ 'bold' ]
  75. } );
  76. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  77. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } );
  78. const output = data.parse( '<p>foo<strong>bar</strong></p>' );
  79. expect( output ).to.instanceof( ModelDocumentFragment );
  80. expect( stringify( output ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  81. } );
  82. it( 'should parse in the root context by default', () => {
  83. const output = data.parse( 'foo' );
  84. expect( stringify( output ) ).to.equal( '' );
  85. } );
  86. it( 'should accept parsing context', () => {
  87. const output = data.parse( 'foo', [ '$block' ] );
  88. expect( stringify( output ) ).to.equal( 'foo' );
  89. } );
  90. } );
  91. describe( 'toModel()', () => {
  92. beforeEach( () => {
  93. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  94. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  95. } );
  96. it( 'should convert content of an element #1', () => {
  97. const viewElement = parseView( '<p>foo</p>' );
  98. const output = data.toModel( viewElement );
  99. expect( output ).to.instanceof( ModelDocumentFragment );
  100. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph>' );
  101. } );
  102. it( 'should convert content of an element #2', () => {
  103. const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
  104. const output = data.toModel( viewFragment );
  105. expect( output ).to.be.instanceOf( ModelDocumentFragment );
  106. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  107. } );
  108. it( 'should accept parsing context', () => {
  109. modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
  110. schema.register( 'inlineRoot' );
  111. schema.extend( '$text', { allowIn: 'inlineRoot' } );
  112. const viewFragment = new ViewDocumentFragment( viewDocument, [ parseView( 'foo' ) ] );
  113. // Model fragment in root.
  114. expect( stringify( data.toModel( viewFragment ) ) ).to.equal( '' );
  115. // Model fragment in inline root.
  116. expect( stringify( data.toModel( viewFragment, [ 'inlineRoot' ] ) ) ).to.equal( 'foo' );
  117. } );
  118. } );
  119. describe( 'init()', () => {
  120. it( 'should be decorated', () => {
  121. const spy = sinon.spy();
  122. data.on( 'init', spy );
  123. data.init( 'foo bar' );
  124. sinon.assert.calledWithExactly( spy, sinon.match.any, [ 'foo bar' ] );
  125. } );
  126. it( 'should fire ready event after init', () => {
  127. const spy = sinon.spy();
  128. data.on( 'ready', spy );
  129. data.init( 'foo bar' );
  130. sinon.assert.called( spy );
  131. } );
  132. it( 'should throw an error when document data is already initialized', () => {
  133. data.init( '<p>Foo</p>' );
  134. expectToThrowCKEditorError( () => {
  135. data.init( '<p>Bar</p>' );
  136. }, /datacontroller-init-document-not-empty:/, model );
  137. } );
  138. it( 'should set data to default main root', () => {
  139. schema.extend( '$text', { allowIn: '$root' } );
  140. data.init( 'foo' );
  141. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  142. } );
  143. it( 'should set data to multiple roots at once', () => {
  144. schema.extend( '$text', { allowIn: '$root' } );
  145. data.init( { main: 'bar', title: 'baz' } );
  146. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'bar' );
  147. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'baz' );
  148. } );
  149. it( 'should get root name as a parameter', () => {
  150. schema.extend( '$text', { allowIn: '$root' } );
  151. data.init( { title: 'foo' } );
  152. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  153. } );
  154. it( 'should create a batch', () => {
  155. schema.extend( '$text', { allowIn: '$root' } );
  156. data.init( 'foo' );
  157. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  158. } );
  159. it( 'should cause firing change event', () => {
  160. const spy = sinon.spy();
  161. schema.extend( '$text', { allowIn: '$root' } );
  162. model.document.on( 'change', spy );
  163. data.init( 'foo' );
  164. expect( spy.calledOnce ).to.be.true;
  165. } );
  166. it( 'should return a resolved Promise', () => {
  167. const promise = data.init( '<p>Foo</p>' );
  168. expect( promise ).to.be.instanceof( Promise );
  169. return promise;
  170. } );
  171. it( 'should throw an error when non-existent root is used (single)', () => {
  172. expectToThrowCKEditorError( () => {
  173. data.init( { nonexistent: '<p>Bar</p>' } );
  174. }, /^datacontroller-init-non-existent-root:/ );
  175. } );
  176. it( 'should throw an error when non-existent root is used (one of many)', () => {
  177. schema.extend( '$text', { allowIn: '$root' } );
  178. expectToThrowCKEditorError( () => {
  179. data.init( { main: 'bar', nonexistent: '<p>Bar</p>' } );
  180. }, /^datacontroller-init-non-existent-root:/, model );
  181. expect( getData( model, { withoutSelection: true } ) ).to.equal( '' );
  182. } );
  183. } );
  184. describe( 'set()', () => {
  185. it( 'should set data to default main root', () => {
  186. schema.extend( '$text', { allowIn: '$root' } );
  187. data.set( 'foo' );
  188. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  189. } );
  190. it( 'should create a batch', () => {
  191. schema.extend( '$text', { allowIn: '$root' } );
  192. data.set( 'foo' );
  193. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  194. } );
  195. it( 'should cause firing change event', () => {
  196. const spy = sinon.spy();
  197. schema.extend( '$text', { allowIn: '$root' } );
  198. model.document.on( 'change', spy );
  199. data.set( 'foo' );
  200. expect( spy.calledOnce ).to.be.true;
  201. } );
  202. it( 'should get root name as a parameter', () => {
  203. schema.extend( '$text', { allowIn: '$root' } );
  204. data.set( 'foo' );
  205. data.set( { title: 'Bar' } );
  206. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  207. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  208. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  209. } );
  210. it( 'should parse given data before set in a context of correct root', () => {
  211. schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } );
  212. data.set( 'foo', 'main' );
  213. data.set( { title: 'Bar' } );
  214. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
  215. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  216. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  217. } );
  218. // This case was added when order of params was different and it really didn't work. Let's keep it
  219. // if anyone will ever try to change this.
  220. it( 'should allow setting empty data', () => {
  221. schema.extend( '$text', { allowIn: '$root' } );
  222. data.set( { title: 'foo' } );
  223. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  224. data.set( { title: '' } );
  225. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  226. } );
  227. it( 'should throw an error when non-existent root is used (single)', () => {
  228. expectToThrowCKEditorError( () => {
  229. data.set( { nonexistent: '<p>Bar</p>' } );
  230. }, /datacontroller-set-non-existent-root:/, model );
  231. } );
  232. it( 'should throw an error when non-existent root is used (one of many) without touching any roots data', () => {
  233. schema.extend( '$text', { allowIn: '$root' } );
  234. data.set( 'foo' );
  235. expectToThrowCKEditorError( () => {
  236. data.set( { main: 'bar', nonexistent: '<p>Bar</p>' } );
  237. }, /datacontroller-set-non-existent-root:/, model );
  238. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  239. } );
  240. // https://github.com/ckeditor/ckeditor5-engine/issues/1721.
  241. it( 'should not throw when setting the data with markers that already exist in the editor', () => {
  242. schema.extend( '$text', { allowIn: '$root' } );
  243. data.set( 'foo' );
  244. downcastHelpers.markerToElement( { model: 'marker', view: 'marker' } );
  245. upcastHelpers.elementToMarker( { view: 'marker', model: 'marker' } );
  246. model.change( writer => {
  247. writer.addMarker( 'marker', { range: writer.createRangeIn( modelDocument.getRoot() ), usingOperation: true } );
  248. } );
  249. expect( () => {
  250. data.set( data.get() );
  251. } ).not.to.throw();
  252. } );
  253. } );
  254. describe( 'get()', () => {
  255. it( 'should get paragraph with text', () => {
  256. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  257. setData( model, '<paragraph>foo</paragraph>' );
  258. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  259. expect( data.get() ).to.equal( '<p>foo</p>' );
  260. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo</p>' );
  261. } );
  262. it( 'should trim empty paragraph by default', () => {
  263. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  264. setData( model, '<paragraph></paragraph>' );
  265. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  266. expect( data.get() ).to.equal( '' );
  267. expect( data.get( { trim: 'empty' } ) ).to.equal( '' );
  268. } );
  269. it( 'should get empty paragraph (with trim=none)', () => {
  270. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  271. setData( model, '<paragraph></paragraph>' );
  272. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  273. expect( data.get( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  274. } );
  275. it( 'should get two paragraphs', () => {
  276. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  277. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  278. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  279. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  280. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  281. } );
  282. it( 'should get text directly in root', () => {
  283. schema.extend( '$text', { allowIn: '$root' } );
  284. setData( model, 'foo' );
  285. expect( data.get() ).to.equal( 'foo' );
  286. expect( data.get( { trim: 'empty' } ) ).to.equal( 'foo' );
  287. } );
  288. it( 'should get paragraphs without bold', () => {
  289. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  290. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  291. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  292. expect( data.get() ).to.equal( '<p>foobar</p>' );
  293. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foobar</p>' );
  294. } );
  295. it( 'should get paragraphs with bold', () => {
  296. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  297. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  298. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  299. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  300. expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
  301. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo<strong>bar</strong></p>' );
  302. } );
  303. it( 'should get root name as a parameter', () => {
  304. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  305. schema.extend( '$text', { allowIn: '$root' } );
  306. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  307. setData( model, 'Bar', { rootName: 'title' } );
  308. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  309. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  310. expect( data.get() ).to.equal( '<p>foo</p>' );
  311. expect( data.get( { rootName: 'main' } ) ).to.equal( '<p>foo</p>' );
  312. expect( data.get( { rootName: 'title' } ) ).to.equal( 'Bar' );
  313. } );
  314. it( 'should throw an error when non-existent root is used', () => {
  315. expectToThrowCKEditorError( () => {
  316. data.get( { rootName: 'nonexistent' } );
  317. }, /datacontroller-get-non-existent-root:/ );
  318. } );
  319. } );
  320. describe( 'stringify()', () => {
  321. beforeEach( () => {
  322. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  323. schema.register( 'div' );
  324. schema.extend( '$block', { allowIn: 'div' } );
  325. schema.extend( 'div', { allowIn: '$root' } );
  326. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  327. } );
  328. it( 'should stringify a content of an element', () => {
  329. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  330. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  331. } );
  332. it( 'should stringify a content of a document fragment', () => {
  333. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  334. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  335. } );
  336. } );
  337. describe( 'toView()', () => {
  338. beforeEach( () => {
  339. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  340. schema.register( 'div' );
  341. schema.extend( '$block', { allowIn: 'div' } );
  342. schema.extend( 'div', { allowIn: '$root' } );
  343. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  344. } );
  345. it( 'should use #viewDocument as a parent for returned document fragments', () => {
  346. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  347. const viewDocumentFragment = data.toView( modelElement );
  348. expect( viewDocumentFragment.document ).to.equal( data.viewDocument );
  349. } );
  350. it( 'should convert a content of an element', () => {
  351. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  352. const viewDocumentFragment = data.toView( modelElement );
  353. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  354. const viewElement = viewDocumentFragment.getChild( 0 );
  355. expect( viewElement.name ).to.equal( 'p' );
  356. expect( viewElement.childCount ).to.equal( 1 );
  357. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  358. } );
  359. it( 'should correctly convert document markers #1', () => {
  360. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  361. const modelRoot = model.document.getRoot();
  362. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  363. model.change( writer => {
  364. writer.insert( modelElement, modelRoot, 0 );
  365. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 1 ) );
  366. writer.addMarker( 'marker:a', { range, usingOperation: true } );
  367. } );
  368. const viewDocumentFragment = data.toView( modelElement );
  369. const viewElement = viewDocumentFragment.getChild( 0 );
  370. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  371. } );
  372. it( 'should correctly convert document markers #2', () => {
  373. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  374. const modelRoot = model.document.getRoot();
  375. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  376. downcastHelpers.markerToHighlight( { model: 'marker:b', view: { classes: 'b' } } );
  377. const modelP1 = modelElement.getChild( 0 );
  378. const modelP2 = modelElement.getChild( 1 );
  379. model.change( writer => {
  380. writer.insert( modelElement, modelRoot, 0 );
  381. const rangeA = writer.createRange( writer.createPositionAt( modelP1, 1 ), writer.createPositionAt( modelP1, 3 ) );
  382. const rangeB = writer.createRange( writer.createPositionAt( modelP2, 0 ), writer.createPositionAt( modelP2, 2 ) );
  383. writer.addMarker( 'marker:a', { range: rangeA, usingOperation: true } );
  384. writer.addMarker( 'marker:b', { range: rangeB, usingOperation: true } );
  385. } );
  386. const viewDocumentFragment = data.toView( modelP1 );
  387. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  388. } );
  389. it( 'should convert a document fragment', () => {
  390. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  391. const viewDocumentFragment = data.toView( modelDocumentFragment );
  392. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  393. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  394. const viewElement = viewDocumentFragment.getChild( 0 );
  395. expect( viewElement.name ).to.equal( 'p' );
  396. expect( viewElement.childCount ).to.equal( 1 );
  397. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  398. } );
  399. it( 'should keep view-model mapping', () => {
  400. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  401. const viewDocumentFragment = data.toView( modelDocumentFragment );
  402. const firstModelElement = modelDocumentFragment.getChild( 0 );
  403. const firstViewElement = viewDocumentFragment.getChild( 0 );
  404. const modelRange = ModelRange._createOn( firstModelElement );
  405. const viewRange = ViewRange._createOn( firstViewElement );
  406. const mappedModelRange = data.mapper.toModelRange( viewRange );
  407. const mappedViewRange = data.mapper.toViewRange( modelRange );
  408. expect( mappedModelRange ).to.be.instanceOf( ModelRange );
  409. expect( mappedViewRange ).to.be.instanceOf( ViewRange );
  410. expect( mappedModelRange.end.nodeBefore ).to.equal( firstModelElement );
  411. expect( mappedModelRange.end.nodeAfter ).to.equal( modelDocumentFragment.getChild( 1 ) );
  412. expect( mappedViewRange.end.nodeBefore ).to.equal( firstViewElement );
  413. expect( mappedViewRange.end.nodeAfter ).to.equal( viewDocumentFragment.getChild( 1 ) );
  414. } );
  415. } );
  416. describe( 'destroy()', () => {
  417. it( 'should be there for you', () => {
  418. // Should not throw.
  419. data.destroy();
  420. expect( data ).to.respondTo( 'destroy' );
  421. } );
  422. } );
  423. describe( 'addStyleProcessorRules()', () => {
  424. it( 'should execute callback with an instance of StyleProcessor as the first argument', () => {
  425. const stylesProcessor = new StylesProcessor();
  426. const data = new DataController( model, stylesProcessor );
  427. const spy = sinon.spy();
  428. data.addStyleProcessorRules( spy );
  429. sinon.assert.calledOnce( spy );
  430. sinon.assert.calledWithExactly( spy, stylesProcessor );
  431. } );
  432. } );
  433. } );