datacontroller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. it( 'should return a resolved Promise', () => {
  150. const promise = data.init( '<p>Foo</p>' );
  151. expect( promise ).to.be.instanceof( Promise );
  152. return promise;
  153. } );
  154. } );
  155. describe( 'set()', () => {
  156. it( 'should set data to default main root', () => {
  157. schema.extend( '$text', { allowIn: '$root' } );
  158. data.set( 'foo' );
  159. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  160. } );
  161. it( 'should create a batch', () => {
  162. schema.extend( '$text', { allowIn: '$root' } );
  163. data.set( 'foo' );
  164. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  165. } );
  166. it( 'should cause firing change event', () => {
  167. const spy = sinon.spy();
  168. schema.extend( '$text', { allowIn: '$root' } );
  169. model.document.on( 'change', spy );
  170. data.set( 'foo' );
  171. expect( spy.calledOnce ).to.be.true;
  172. } );
  173. it( 'should get root name as a parameter', () => {
  174. schema.extend( '$text', { allowIn: '$root' } );
  175. data.set( 'foo', 'main' );
  176. data.set( 'Bar', 'title' );
  177. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  178. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  179. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  180. } );
  181. it( 'should parse given data before set in a context of correct root', () => {
  182. schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } );
  183. data.set( 'foo', 'main' );
  184. data.set( 'Bar', 'title' );
  185. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
  186. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  187. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  188. } );
  189. // This case was added when order of params was different and it really didn't work. Let's keep it
  190. // if anyone will ever try to change this.
  191. it( 'should allow setting empty data', () => {
  192. schema.extend( '$text', { allowIn: '$root' } );
  193. data.set( 'foo', 'title' );
  194. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  195. data.set( '', 'title' );
  196. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  197. } );
  198. } );
  199. describe( 'get()', () => {
  200. it( 'should get paragraph with text', () => {
  201. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  202. setData( model, '<paragraph>foo</paragraph>' );
  203. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  204. expect( data.get() ).to.equal( '<p>foo</p>' );
  205. } );
  206. it( 'should get empty paragraph', () => {
  207. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  208. setData( model, '<paragraph></paragraph>' );
  209. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  210. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  211. } );
  212. it( 'should get two paragraphs', () => {
  213. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  214. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  215. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  216. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  217. } );
  218. it( 'should get text directly in root', () => {
  219. schema.extend( '$text', { allowIn: '$root' } );
  220. setData( model, 'foo' );
  221. expect( data.get() ).to.equal( 'foo' );
  222. } );
  223. it( 'should get paragraphs without bold', () => {
  224. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  225. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  226. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  227. expect( data.get() ).to.equal( '<p>foobar</p>' );
  228. } );
  229. it( 'should get paragraphs with bold', () => {
  230. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  231. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  232. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  233. downcastAttributeToElement( { model: 'bold', view: 'strong' } )( data.downcastDispatcher );
  234. expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
  235. } );
  236. it( 'should get root name as a parameter', () => {
  237. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  238. schema.extend( '$text', { allowIn: '$root' } );
  239. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  240. setData( model, 'Bar', { rootName: 'title' } );
  241. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  242. downcastAttributeToElement( { model: 'bold', view: 'strong' } )( data.downcastDispatcher );
  243. expect( data.get() ).to.equal( '<p>foo</p>' );
  244. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  245. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  246. } );
  247. } );
  248. describe( 'stringify()', () => {
  249. beforeEach( () => {
  250. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  251. schema.register( 'div' );
  252. schema.extend( '$block', { allowIn: 'div' } );
  253. schema.extend( 'div', { allowIn: '$root' } );
  254. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  255. } );
  256. it( 'should stringify a content of an element', () => {
  257. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  258. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  259. } );
  260. it( 'should stringify a content of a document fragment', () => {
  261. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  262. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  263. } );
  264. } );
  265. describe( 'toView()', () => {
  266. beforeEach( () => {
  267. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  268. schema.register( 'div' );
  269. schema.extend( '$block', { allowIn: 'div' } );
  270. schema.extend( 'div', { allowIn: '$root' } );
  271. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  272. } );
  273. it( 'should convert a content of an element', () => {
  274. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  275. const viewDocumentFragment = data.toView( modelElement );
  276. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  277. const viewElement = viewDocumentFragment.getChild( 0 );
  278. expect( viewElement.name ).to.equal( 'p' );
  279. expect( viewElement.childCount ).to.equal( 1 );
  280. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  281. } );
  282. it( 'should correctly convert document markers #1', () => {
  283. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  284. const modelRoot = model.document.getRoot();
  285. downcastMarkerToHighlight( { model: 'marker:a', view: { classes: 'a' } } )( data.downcastDispatcher );
  286. model.change( writer => {
  287. writer.insert( modelElement, modelRoot, 0 );
  288. const range = Range.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 1 );
  289. writer.addMarker( 'marker:a', { range, usingOperation: true } );
  290. } );
  291. const viewDocumentFragment = data.toView( modelElement );
  292. const viewElement = viewDocumentFragment.getChild( 0 );
  293. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  294. } );
  295. it( 'should correctly convert document markers #2', () => {
  296. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  297. const modelRoot = model.document.getRoot();
  298. downcastMarkerToHighlight( { model: 'marker:a', view: { classes: 'a' } } )( data.downcastDispatcher );
  299. downcastMarkerToHighlight( { model: 'marker:b', view: { classes: 'b' } } )( data.downcastDispatcher );
  300. const modelP1 = modelElement.getChild( 0 );
  301. const modelP2 = modelElement.getChild( 1 );
  302. model.change( writer => {
  303. writer.insert( modelElement, modelRoot, 0 );
  304. const rangeA = Range.createFromParentsAndOffsets( modelP1, 1, modelP1, 3 );
  305. const rangeB = Range.createFromParentsAndOffsets( modelP2, 0, modelP2, 2 );
  306. writer.addMarker( 'marker:a', { range: rangeA, usingOperation: true } );
  307. writer.addMarker( 'marker:b', { range: rangeB, usingOperation: true } );
  308. } );
  309. const viewDocumentFragment = data.toView( modelP1 );
  310. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  311. } );
  312. it( 'should convert a document fragment', () => {
  313. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  314. const viewDocumentFragment = data.toView( modelDocumentFragment );
  315. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  316. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  317. const viewElement = viewDocumentFragment.getChild( 0 );
  318. expect( viewElement.name ).to.equal( 'p' );
  319. expect( viewElement.childCount ).to.equal( 1 );
  320. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  321. } );
  322. } );
  323. describe( 'destroy()', () => {
  324. it( 'should be there for you', () => {
  325. // Should not throw.
  326. data.destroy();
  327. expect( data ).to.respondTo( 'destroy' );
  328. } );
  329. } );
  330. } );