datacontroller.js 21 KB

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