datacontroller.js 18 KB

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