8
0

datacontroller.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import 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 be decorated', () => {
  186. const spy = sinon.spy();
  187. data.on( 'set', spy );
  188. data.set( 'foo bar' );
  189. sinon.assert.calledWithExactly( spy, sinon.match.any, [ 'foo bar' ] );
  190. } );
  191. it( 'should set data to default main root', () => {
  192. schema.extend( '$text', { allowIn: '$root' } );
  193. data.set( 'foo' );
  194. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  195. } );
  196. it( 'should create a batch', () => {
  197. schema.extend( '$text', { allowIn: '$root' } );
  198. data.set( 'foo' );
  199. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  200. } );
  201. it( 'should cause firing change event', () => {
  202. const spy = sinon.spy();
  203. schema.extend( '$text', { allowIn: '$root' } );
  204. model.document.on( 'change', spy );
  205. data.set( 'foo' );
  206. expect( spy.calledOnce ).to.be.true;
  207. } );
  208. it( 'should get root name as a parameter', () => {
  209. schema.extend( '$text', { allowIn: '$root' } );
  210. data.set( 'foo' );
  211. data.set( { title: 'Bar' } );
  212. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  213. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  214. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  215. } );
  216. it( 'should parse given data before set in a context of correct root', () => {
  217. schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } );
  218. data.set( 'foo', 'main' );
  219. data.set( { title: 'Bar' } );
  220. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
  221. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  222. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  223. } );
  224. // This case was added when order of params was different and it really didn't work. Let's keep it
  225. // if anyone will ever try to change this.
  226. it( 'should allow setting empty data', () => {
  227. schema.extend( '$text', { allowIn: '$root' } );
  228. data.set( { title: 'foo' } );
  229. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  230. data.set( { title: '' } );
  231. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  232. } );
  233. it( 'should throw an error when non-existent root is used (single)', () => {
  234. expectToThrowCKEditorError( () => {
  235. data.set( { nonexistent: '<p>Bar</p>' } );
  236. }, /datacontroller-set-non-existent-root:/, model );
  237. } );
  238. it( 'should throw an error when non-existent root is used (one of many) without touching any roots data', () => {
  239. schema.extend( '$text', { allowIn: '$root' } );
  240. data.set( 'foo' );
  241. expectToThrowCKEditorError( () => {
  242. data.set( { main: 'bar', nonexistent: '<p>Bar</p>' } );
  243. }, /datacontroller-set-non-existent-root:/, model );
  244. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  245. } );
  246. // https://github.com/ckeditor/ckeditor5-engine/issues/1721.
  247. it( 'should not throw when setting the data with markers that already exist in the editor', () => {
  248. schema.extend( '$text', { allowIn: '$root' } );
  249. data.set( 'foo' );
  250. downcastHelpers.markerToData( { model: 'marker' } );
  251. upcastHelpers.dataToMarker( { view: 'marker' } );
  252. model.change( writer => {
  253. writer.addMarker( 'marker', { range: writer.createRangeIn( modelDocument.getRoot() ), usingOperation: true } );
  254. } );
  255. expect( () => {
  256. data.set( data.get() );
  257. } ).not.to.throw();
  258. } );
  259. } );
  260. describe( 'get()', () => {
  261. it( 'should get paragraph with text', () => {
  262. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  263. setData( model, '<paragraph>foo</paragraph>' );
  264. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  265. expect( data.get() ).to.equal( '<p>foo</p>' );
  266. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo</p>' );
  267. } );
  268. it( 'should trim empty paragraph by default', () => {
  269. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  270. setData( model, '<paragraph></paragraph>' );
  271. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  272. expect( data.get() ).to.equal( '' );
  273. expect( data.get( { trim: 'empty' } ) ).to.equal( '' );
  274. } );
  275. it( 'should get empty paragraph (with trim=none)', () => {
  276. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  277. setData( model, '<paragraph></paragraph>' );
  278. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  279. expect( data.get( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  280. } );
  281. it( 'should get two paragraphs', () => {
  282. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  283. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  284. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  285. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  286. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  287. } );
  288. it( 'should get text directly in root', () => {
  289. schema.extend( '$text', { allowIn: '$root' } );
  290. setData( model, 'foo' );
  291. expect( data.get() ).to.equal( 'foo' );
  292. expect( data.get( { trim: 'empty' } ) ).to.equal( 'foo' );
  293. } );
  294. it( 'should get paragraphs without bold', () => {
  295. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  296. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  297. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  298. expect( data.get() ).to.equal( '<p>foobar</p>' );
  299. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foobar</p>' );
  300. } );
  301. it( 'should get paragraphs with bold', () => {
  302. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  303. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  304. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  305. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  306. expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
  307. expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo<strong>bar</strong></p>' );
  308. } );
  309. it( 'should get root name as a parameter', () => {
  310. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  311. schema.extend( '$text', { allowIn: '$root' } );
  312. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  313. setData( model, 'Bar', { rootName: 'title' } );
  314. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  315. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  316. expect( data.get() ).to.equal( '<p>foo</p>' );
  317. expect( data.get( { rootName: 'main' } ) ).to.equal( '<p>foo</p>' );
  318. expect( data.get( { rootName: 'title' } ) ).to.equal( 'Bar' );
  319. } );
  320. it( 'should throw an error when non-existent root is used', () => {
  321. expectToThrowCKEditorError( () => {
  322. data.get( { rootName: 'nonexistent' } );
  323. }, /datacontroller-get-non-existent-root:/ );
  324. } );
  325. it( 'should allow to provide additional options for retrieving data - insert conversion', () => {
  326. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  327. data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
  328. conversionApi.consumable.consume( data.item, 'insert' );
  329. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  330. const viewElement = conversionApi.writer.createContainerElement( 'p', {
  331. attribute: conversionApi.options.attributeValue
  332. } );
  333. conversionApi.mapper.bindElements( data.item, viewElement );
  334. conversionApi.writer.insert( viewPosition, viewElement );
  335. }, { priority: 'high' } );
  336. setData( model, '<paragraph>foo</paragraph>' );
  337. expect( data.get( { attributeValue: 'foo' } ) ).to.equal( '<p attribute="foo">foo</p>' );
  338. expect( data.get( { attributeValue: 'bar' } ) ).to.equal( '<p attribute="bar">foo</p>' );
  339. } );
  340. it( 'should allow to provide additional options for retrieving data - attribute conversion', () => {
  341. schema.register( 'paragraph', { inheritAllFrom: '$block', allowAttributes: [ 'foo' ] } );
  342. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  343. data.downcastDispatcher.on( 'attribute:foo', ( evt, data, conversionApi ) => {
  344. if ( data.attributeNewValue === conversionApi.options.skipAttribute ) {
  345. return;
  346. }
  347. const viewRange = conversionApi.mapper.toViewRange( data.range );
  348. const viewElement = conversionApi.writer.createAttributeElement( data.attributeNewValue );
  349. conversionApi.writer.wrap( viewRange, viewElement );
  350. } );
  351. setData( model, '<paragraph>f<$text foo="a">o</$text>ob<$text foo="b">a</$text>r</paragraph>' );
  352. expect( data.get() ).to.equal( '<p>f<a>o</a>ob<b>a</b>r</p>' );
  353. expect( data.get( { skipAttribute: 'a' } ) ).to.equal( '<p>foob<b>a</b>r</p>' );
  354. expect( data.get( { skipAttribute: 'b' } ) ).to.equal( '<p>f<a>o</a>obar</p>' );
  355. } );
  356. it( 'should allow to provide additional options for retrieving data - addMarker conversion', () => {
  357. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  358. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  359. data.downcastDispatcher.on( 'addMarker', ( evt, data, conversionApi ) => {
  360. if ( conversionApi.options.skipMarker ) {
  361. return;
  362. }
  363. const viewElement = conversionApi.writer.createAttributeElement( 'marker' );
  364. const viewRange = conversionApi.mapper.toViewRange( data.markerRange );
  365. conversionApi.writer.wrap( viewRange, viewElement );
  366. } );
  367. setData( model, '<paragraph>foo</paragraph>' );
  368. const root = model.document.getRoot();
  369. model.change( writer => {
  370. const start = writer.createPositionFromPath( root, [ 0, 1 ] );
  371. const end = writer.createPositionFromPath( root, [ 0, 2 ] );
  372. writer.addMarker( 'marker', {
  373. range: writer.createRange( start, end ),
  374. usingOperation: false
  375. } );
  376. } );
  377. expect( data.get( { skipMarker: false } ) ).to.equal( '<p>f<marker>o</marker>o</p>' );
  378. expect( data.get( { skipMarker: true } ) ).to.equal( '<p>foo</p>' );
  379. } );
  380. } );
  381. describe( 'stringify()', () => {
  382. beforeEach( () => {
  383. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  384. schema.register( 'div' );
  385. schema.extend( '$block', { allowIn: 'div' } );
  386. schema.extend( 'div', { allowIn: '$root' } );
  387. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  388. } );
  389. it( 'should stringify a content of an element', () => {
  390. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  391. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  392. } );
  393. it( 'should stringify a content of a document fragment', () => {
  394. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  395. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  396. } );
  397. it( 'should allow to provide additional options to the conversion process', () => {
  398. const spy = sinon.spy();
  399. data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
  400. spy( conversionApi.options );
  401. }, { priority: 'high' } );
  402. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  403. const options = { foo: 'bar' };
  404. data.stringify( modelDocumentFragment );
  405. expect( spy.lastCall.args[ 0 ] ).to.not.equal( options );
  406. data.stringify( modelDocumentFragment, options );
  407. expect( spy.lastCall.args[ 0 ] ).to.equal( options );
  408. } );
  409. } );
  410. describe( 'toView()', () => {
  411. beforeEach( () => {
  412. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  413. schema.register( 'div' );
  414. schema.extend( '$block', { allowIn: 'div' } );
  415. schema.extend( 'div', { allowIn: '$root' } );
  416. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  417. } );
  418. it( 'should use #viewDocument as a parent for returned document fragments', () => {
  419. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  420. const viewDocumentFragment = data.toView( modelElement );
  421. expect( viewDocumentFragment.document ).to.equal( data.viewDocument );
  422. } );
  423. it( 'should convert a content of an element', () => {
  424. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  425. const viewDocumentFragment = data.toView( modelElement );
  426. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  427. const viewElement = viewDocumentFragment.getChild( 0 );
  428. expect( viewElement.name ).to.equal( 'p' );
  429. expect( viewElement.childCount ).to.equal( 1 );
  430. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  431. } );
  432. it( 'should correctly convert document markers #1', () => {
  433. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  434. const modelRoot = model.document.getRoot();
  435. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  436. model.change( writer => {
  437. writer.insert( modelElement, modelRoot, 0 );
  438. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 1 ) );
  439. writer.addMarker( 'marker:a', { range, usingOperation: true } );
  440. } );
  441. const viewDocumentFragment = data.toView( modelElement );
  442. const viewElement = viewDocumentFragment.getChild( 0 );
  443. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  444. } );
  445. it( 'should correctly convert document markers #2', () => {
  446. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  447. const modelRoot = model.document.getRoot();
  448. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  449. downcastHelpers.markerToHighlight( { model: 'marker:b', view: { classes: 'b' } } );
  450. const modelP1 = modelElement.getChild( 0 );
  451. const modelP2 = modelElement.getChild( 1 );
  452. model.change( writer => {
  453. writer.insert( modelElement, modelRoot, 0 );
  454. const rangeA = writer.createRange( writer.createPositionAt( modelP1, 1 ), writer.createPositionAt( modelP1, 3 ) );
  455. const rangeB = writer.createRange( writer.createPositionAt( modelP2, 0 ), writer.createPositionAt( modelP2, 2 ) );
  456. writer.addMarker( 'marker:a', { range: rangeA, usingOperation: true } );
  457. writer.addMarker( 'marker:b', { range: rangeB, usingOperation: true } );
  458. } );
  459. const viewDocumentFragment = data.toView( modelP1 );
  460. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  461. } );
  462. it( 'should convert a document fragment', () => {
  463. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  464. const viewDocumentFragment = data.toView( modelDocumentFragment );
  465. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  466. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  467. const viewElement = viewDocumentFragment.getChild( 0 );
  468. expect( viewElement.name ).to.equal( 'p' );
  469. expect( viewElement.childCount ).to.equal( 1 );
  470. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  471. } );
  472. it( 'should keep view-model mapping', () => {
  473. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  474. const viewDocumentFragment = data.toView( modelDocumentFragment );
  475. const firstModelElement = modelDocumentFragment.getChild( 0 );
  476. const firstViewElement = viewDocumentFragment.getChild( 0 );
  477. const modelRange = ModelRange._createOn( firstModelElement );
  478. const viewRange = ViewRange._createOn( firstViewElement );
  479. const mappedModelRange = data.mapper.toModelRange( viewRange );
  480. const mappedViewRange = data.mapper.toViewRange( modelRange );
  481. expect( mappedModelRange ).to.be.instanceOf( ModelRange );
  482. expect( mappedViewRange ).to.be.instanceOf( ViewRange );
  483. expect( mappedModelRange.end.nodeBefore ).to.equal( firstModelElement );
  484. expect( mappedModelRange.end.nodeAfter ).to.equal( modelDocumentFragment.getChild( 1 ) );
  485. expect( mappedViewRange.end.nodeBefore ).to.equal( firstViewElement );
  486. expect( mappedViewRange.end.nodeAfter ).to.equal( viewDocumentFragment.getChild( 1 ) );
  487. } );
  488. it( 'should allow to provide additional options to the conversion process', () => {
  489. const root = model.document.getRoot();
  490. const spy = sinon.spy();
  491. data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
  492. spy( conversionApi.options );
  493. }, { priority: 'high' } );
  494. data.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
  495. spy( conversionApi.options );
  496. }, { priority: 'high' } );
  497. setData( model, '<paragraph>foo</paragraph>' );
  498. model.change( writer => {
  499. writer.addMarker( 'marker', {
  500. range: model.createRange( model.createPositionFromPath( root, [ 0, 1 ] ) ),
  501. usingOperation: false
  502. } );
  503. } );
  504. const options = { foo: 'bar' };
  505. data.toView( root, options );
  506. sinon.assert.calledTwice( spy );
  507. expect( spy.firstCall.args[ 0 ] ).to.equal( options );
  508. expect( spy.lastCall.args[ 0 ] ).to.equal( options );
  509. } );
  510. } );
  511. describe( 'destroy()', () => {
  512. it( 'should be there for you', () => {
  513. // Should not throw.
  514. data.destroy();
  515. expect( data ).to.respondTo( 'destroy' );
  516. } );
  517. } );
  518. describe( 'addStyleProcessorRules()', () => {
  519. it( 'should execute callback with an instance of StyleProcessor as the first argument', () => {
  520. const stylesProcessor = new StylesProcessor();
  521. const data = new DataController( model, stylesProcessor );
  522. const spy = sinon.spy();
  523. data.addStyleProcessorRules( spy );
  524. sinon.assert.calledOnce( spy );
  525. sinon.assert.calledWithExactly( spy, stylesProcessor );
  526. } );
  527. } );
  528. } );