datacontroller.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import ModelDocumentFragment from '../../src/model/documentfragment';
  12. import ViewDocumentFragment from '../../src/view/documentfragment';
  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 {
  17. upcastElementToElement,
  18. upcastElementToAttribute
  19. } from '../../src/conversion/upcast-converters';
  20. import {
  21. downcastElementToElement,
  22. downcastAttributeToElement,
  23. downcastMarkerToHighlight
  24. } from '../../src/conversion/downcast-converters';
  25. describe( 'DataController', () => {
  26. let model, modelDocument, htmlDataProcessor, data, schema;
  27. beforeEach( () => {
  28. model = new Model();
  29. schema = model.schema;
  30. modelDocument = model.document;
  31. modelDocument.createRoot();
  32. modelDocument.createRoot( '$title', 'title' );
  33. schema.register( '$title', { inheritAllFrom: '$root' } );
  34. htmlDataProcessor = new HtmlDataProcessor();
  35. data = new DataController( model, htmlDataProcessor );
  36. } );
  37. describe( 'constructor()', () => {
  38. it( 'works without data processor', () => {
  39. const data = new DataController( model );
  40. expect( data.processor ).to.be.undefined;
  41. } );
  42. } );
  43. describe( 'parse()', () => {
  44. it( 'should set text', () => {
  45. schema.extend( '$text', { allowIn: '$root' } );
  46. const output = data.parse( '<p>foo<b>bar</b></p>' );
  47. expect( output ).to.instanceof( ModelDocumentFragment );
  48. expect( stringify( output ) ).to.equal( 'foobar' );
  49. } );
  50. it( 'should set paragraph', () => {
  51. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  52. upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
  53. const output = data.parse( '<p>foo<b>bar</b></p>' );
  54. expect( output ).to.instanceof( ModelDocumentFragment );
  55. expect( stringify( output ) ).to.equal( '<paragraph>foobar</paragraph>' );
  56. } );
  57. it( 'should set two paragraphs', () => {
  58. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  59. upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
  60. const output = data.parse( '<p>foo</p><p>bar</p>' );
  61. expect( output ).to.instanceof( ModelDocumentFragment );
  62. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  63. } );
  64. it( 'should set paragraphs with bold', () => {
  65. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  66. schema.extend( '$text', {
  67. allowAttributes: [ 'bold' ]
  68. } );
  69. upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
  70. upcastElementToAttribute( { view: 'strong', model: 'bold' } )( data.upcastDispatcher );
  71. const output = data.parse( '<p>foo<strong>bar</strong></p>' );
  72. expect( output ).to.instanceof( ModelDocumentFragment );
  73. expect( stringify( output ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  74. } );
  75. it( 'should parse in the root context by default', () => {
  76. const output = data.parse( 'foo' );
  77. expect( stringify( output ) ).to.equal( '' );
  78. } );
  79. it( 'should accept parsing context', () => {
  80. const output = data.parse( 'foo', [ '$block' ] );
  81. expect( stringify( output ) ).to.equal( 'foo' );
  82. } );
  83. } );
  84. describe( 'toModel()', () => {
  85. beforeEach( () => {
  86. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  87. upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
  88. } );
  89. it( 'should convert content of an element #1', () => {
  90. const viewElement = parseView( '<p>foo</p>' );
  91. const output = data.toModel( viewElement );
  92. expect( output ).to.instanceof( ModelDocumentFragment );
  93. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph>' );
  94. } );
  95. it( 'should convert content of an element #2', () => {
  96. const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
  97. const output = data.toModel( viewFragment );
  98. expect( output ).to.be.instanceOf( ModelDocumentFragment );
  99. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  100. } );
  101. it( 'should accept parsing context', () => {
  102. modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
  103. schema.register( 'inlineRoot' );
  104. schema.extend( '$text', { allowIn: 'inlineRoot' } );
  105. const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
  106. // Model fragment in root.
  107. expect( stringify( data.toModel( viewFragment ) ) ).to.equal( '' );
  108. // Model fragment in inline root.
  109. expect( stringify( data.toModel( viewFragment, [ 'inlineRoot' ] ) ) ).to.equal( 'foo' );
  110. } );
  111. } );
  112. describe( 'init()', () => {
  113. it( 'should be decorated', () => {
  114. const spy = sinon.spy();
  115. data.on( 'init', spy );
  116. data.init( 'foo bar' );
  117. sinon.assert.calledWithExactly( spy, sinon.match.any, [ 'foo bar' ] );
  118. } );
  119. it( 'should throw an error when document data is already initialized', () => {
  120. data.init( '<p>Foo</p>' );
  121. expect( () => {
  122. data.init( '<p>Bar</p>' );
  123. } ).to.throw(
  124. CKEditorError,
  125. 'datacontroller-init-document-not-empty: Trying to set initial data to not empty document.'
  126. );
  127. } );
  128. it( 'should set data to default main root', () => {
  129. schema.extend( '$text', { allowIn: '$root' } );
  130. data.init( 'foo' );
  131. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  132. } );
  133. it( 'should get root name as a parameter', () => {
  134. schema.extend( '$text', { allowIn: '$root' } );
  135. data.init( 'foo', 'title' );
  136. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  137. } );
  138. it( 'should create a batch', () => {
  139. schema.extend( '$text', { allowIn: '$root' } );
  140. data.init( 'foo' );
  141. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  142. } );
  143. it( 'should cause firing change event', () => {
  144. const spy = sinon.spy();
  145. schema.extend( '$text', { allowIn: '$root' } );
  146. model.document.on( 'change', spy );
  147. data.init( 'foo' );
  148. expect( spy.calledOnce ).to.be.true;
  149. } );
  150. it( 'should return a resolved Promise', () => {
  151. const promise = data.init( '<p>Foo</p>' );
  152. expect( promise ).to.be.instanceof( Promise );
  153. return promise;
  154. } );
  155. } );
  156. describe( 'set()', () => {
  157. it( 'should set data to default main root', () => {
  158. schema.extend( '$text', { allowIn: '$root' } );
  159. data.set( 'foo' );
  160. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  161. } );
  162. it( 'should create a batch', () => {
  163. schema.extend( '$text', { allowIn: '$root' } );
  164. data.set( 'foo' );
  165. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  166. } );
  167. it( 'should cause firing change event', () => {
  168. const spy = sinon.spy();
  169. schema.extend( '$text', { allowIn: '$root' } );
  170. model.document.on( 'change', spy );
  171. data.set( 'foo' );
  172. expect( spy.calledOnce ).to.be.true;
  173. } );
  174. it( 'should get root name as a parameter', () => {
  175. schema.extend( '$text', { allowIn: '$root' } );
  176. data.set( 'foo', 'main' );
  177. data.set( 'Bar', 'title' );
  178. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  179. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  180. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  181. } );
  182. it( 'should parse given data before set in a context of correct root', () => {
  183. schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } );
  184. data.set( 'foo', 'main' );
  185. data.set( 'Bar', 'title' );
  186. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
  187. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  188. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  189. } );
  190. // This case was added when order of params was different and it really didn't work. Let's keep it
  191. // if anyone will ever try to change this.
  192. it( 'should allow setting empty data', () => {
  193. schema.extend( '$text', { allowIn: '$root' } );
  194. data.set( 'foo', 'title' );
  195. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  196. data.set( '', 'title' );
  197. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  198. } );
  199. } );
  200. describe( 'get()', () => {
  201. it( 'should get paragraph with text', () => {
  202. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  203. setData( model, '<paragraph>foo</paragraph>' );
  204. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  205. expect( data.get() ).to.equal( '<p>foo</p>' );
  206. } );
  207. it( 'should get empty paragraph', () => {
  208. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  209. setData( model, '<paragraph></paragraph>' );
  210. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  211. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  212. } );
  213. it( 'should get two paragraphs', () => {
  214. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  215. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  216. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  217. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  218. } );
  219. it( 'should get text directly in root', () => {
  220. schema.extend( '$text', { allowIn: '$root' } );
  221. setData( model, 'foo' );
  222. expect( data.get() ).to.equal( 'foo' );
  223. } );
  224. it( 'should get paragraphs without bold', () => {
  225. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  226. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  227. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  228. expect( data.get() ).to.equal( '<p>foobar</p>' );
  229. } );
  230. it( 'should get paragraphs with bold', () => {
  231. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  232. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  233. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  234. downcastAttributeToElement( { model: 'bold', view: 'strong' } )( data.downcastDispatcher );
  235. expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
  236. } );
  237. it( 'should get root name as a parameter', () => {
  238. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  239. schema.extend( '$text', { allowIn: '$root' } );
  240. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  241. setData( model, 'Bar', { rootName: 'title' } );
  242. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  243. downcastAttributeToElement( { model: 'bold', view: 'strong' } )( data.downcastDispatcher );
  244. expect( data.get() ).to.equal( '<p>foo</p>' );
  245. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  246. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  247. } );
  248. } );
  249. describe( 'stringify()', () => {
  250. beforeEach( () => {
  251. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  252. schema.register( 'div' );
  253. schema.extend( '$block', { allowIn: 'div' } );
  254. schema.extend( 'div', { allowIn: '$root' } );
  255. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  256. } );
  257. it( 'should stringify a content of an element', () => {
  258. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  259. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  260. } );
  261. it( 'should stringify a content of a document fragment', () => {
  262. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  263. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  264. } );
  265. } );
  266. describe( 'toView()', () => {
  267. beforeEach( () => {
  268. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  269. schema.register( 'div' );
  270. schema.extend( '$block', { allowIn: 'div' } );
  271. schema.extend( 'div', { allowIn: '$root' } );
  272. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  273. } );
  274. it( 'should convert a content of an element', () => {
  275. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  276. const viewDocumentFragment = data.toView( modelElement );
  277. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  278. const viewElement = viewDocumentFragment.getChild( 0 );
  279. expect( viewElement.name ).to.equal( 'p' );
  280. expect( viewElement.childCount ).to.equal( 1 );
  281. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  282. } );
  283. it( 'should correctly convert document markers #1', () => {
  284. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  285. const modelRoot = model.document.getRoot();
  286. downcastMarkerToHighlight( { model: 'marker:a', view: { classes: 'a' } } )( data.downcastDispatcher );
  287. model.change( writer => {
  288. writer.insert( modelElement, modelRoot, 0 );
  289. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 1 ) );
  290. writer.addMarker( 'marker:a', { range, usingOperation: true } );
  291. } );
  292. const viewDocumentFragment = data.toView( modelElement );
  293. const viewElement = viewDocumentFragment.getChild( 0 );
  294. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  295. } );
  296. it( 'should correctly convert document markers #2', () => {
  297. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  298. const modelRoot = model.document.getRoot();
  299. downcastMarkerToHighlight( { model: 'marker:a', view: { classes: 'a' } } )( data.downcastDispatcher );
  300. downcastMarkerToHighlight( { model: 'marker:b', view: { classes: 'b' } } )( data.downcastDispatcher );
  301. const modelP1 = modelElement.getChild( 0 );
  302. const modelP2 = modelElement.getChild( 1 );
  303. model.change( writer => {
  304. writer.insert( modelElement, modelRoot, 0 );
  305. const rangeA = writer.createRange( writer.createPositionAt( modelP1, 1 ), writer.createPositionAt( modelP1, 3 ) );
  306. const rangeB = writer.createRange( writer.createPositionAt( modelP2, 0 ), writer.createPositionAt( modelP2, 2 ) );
  307. writer.addMarker( 'marker:a', { range: rangeA, usingOperation: true } );
  308. writer.addMarker( 'marker:b', { range: rangeB, usingOperation: true } );
  309. } );
  310. const viewDocumentFragment = data.toView( modelP1 );
  311. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  312. } );
  313. it( 'should convert a document fragment', () => {
  314. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  315. const viewDocumentFragment = data.toView( modelDocumentFragment );
  316. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  317. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  318. const viewElement = viewDocumentFragment.getChild( 0 );
  319. expect( viewElement.name ).to.equal( 'p' );
  320. expect( viewElement.childCount ).to.equal( 1 );
  321. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  322. } );
  323. it( 'should keep view-model mapping', () => {
  324. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  325. const viewDocumentFragment = data.toView( modelDocumentFragment );
  326. const firstModelElement = modelDocumentFragment.getChild( 0 );
  327. const firstViewElement = viewDocumentFragment.getChild( 0 );
  328. const modelRange = ModelRange._createOn( firstModelElement );
  329. const viewRange = ViewRange._createOn( firstViewElement );
  330. const mappedModelRange = data.mapper.toModelRange( viewRange );
  331. const mappedViewRange = data.mapper.toViewRange( modelRange );
  332. expect( mappedModelRange ).to.be.instanceOf( ModelRange );
  333. expect( mappedViewRange ).to.be.instanceOf( ViewRange );
  334. expect( mappedModelRange.end.nodeBefore ).to.equal( firstModelElement );
  335. expect( mappedModelRange.end.nodeAfter ).to.equal( modelDocumentFragment.getChild( 1 ) );
  336. expect( mappedViewRange.end.nodeBefore ).to.equal( firstViewElement );
  337. expect( mappedViewRange.end.nodeAfter ).to.equal( viewDocumentFragment.getChild( 1 ) );
  338. } );
  339. } );
  340. describe( 'destroy()', () => {
  341. it( 'should be there for you', () => {
  342. // Should not throw.
  343. data.destroy();
  344. expect( data ).to.respondTo( 'destroy' );
  345. } );
  346. } );
  347. } );