datacontroller.js 15 KB

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