datacontroller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. it( 'should throw an error when non-existent root is used (single)', () => {
  157. expect( () => {
  158. data.init( { nonexistent: '<p>Bar</p>' } );
  159. } ).to.throw(
  160. CKEditorError,
  161. 'datacontroller-init-non-existent-root: Attempting to init data on a non-existing root.'
  162. );
  163. } );
  164. it( 'should throw an error when non-existent root is used (one of many)', () => {
  165. schema.extend( '$text', { allowIn: '$root' } );
  166. expect( () => {
  167. data.init( { main: 'bar', nonexistent: '<p>Bar</p>' } );
  168. } ).to.throw(
  169. CKEditorError,
  170. 'datacontroller-init-non-existent-root: Attempting to init data on a non-existing root.'
  171. );
  172. expect( getData( model, { withoutSelection: true } ) ).to.equal( '' );
  173. } );
  174. } );
  175. describe( 'set()', () => {
  176. it( 'should set data to default main root', () => {
  177. schema.extend( '$text', { allowIn: '$root' } );
  178. data.set( 'foo' );
  179. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  180. } );
  181. it( 'should create a batch', () => {
  182. schema.extend( '$text', { allowIn: '$root' } );
  183. data.set( 'foo' );
  184. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  185. } );
  186. it( 'should cause firing change event', () => {
  187. const spy = sinon.spy();
  188. schema.extend( '$text', { allowIn: '$root' } );
  189. model.document.on( 'change', spy );
  190. data.set( 'foo' );
  191. expect( spy.calledOnce ).to.be.true;
  192. } );
  193. it( 'should get root name as a parameter', () => {
  194. schema.extend( '$text', { allowIn: '$root' } );
  195. data.set( 'foo' );
  196. data.set( { title: 'Bar' } );
  197. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  198. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  199. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  200. } );
  201. it( 'should parse given data before set in a context of correct root', () => {
  202. schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } );
  203. data.set( 'foo', 'main' );
  204. data.set( { title: 'Bar' } );
  205. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
  206. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  207. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  208. } );
  209. // This case was added when order of params was different and it really didn't work. Let's keep it
  210. // if anyone will ever try to change this.
  211. it( 'should allow setting empty data', () => {
  212. schema.extend( '$text', { allowIn: '$root' } );
  213. data.set( { title: 'foo' } );
  214. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  215. data.set( { title: '' } );
  216. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  217. } );
  218. it( 'should throw an error when non-existent root is used (single)', () => {
  219. expect( () => {
  220. data.set( { nonexistent: '<p>Bar</p>' } );
  221. } ).to.throw(
  222. CKEditorError,
  223. 'datacontroller-set-non-existent-root: Attempting to set data on a non-existing root.'
  224. );
  225. } );
  226. it( 'should throw an error when non-existent root is used (one of many) without touching any roots data', () => {
  227. schema.extend( '$text', { allowIn: '$root' } );
  228. data.set( 'foo' );
  229. expect( () => {
  230. data.set( { main: 'bar', nonexistent: '<p>Bar</p>' } );
  231. } ).to.throw(
  232. CKEditorError,
  233. 'datacontroller-set-non-existent-root: Attempting to set data on a non-existing root.'
  234. );
  235. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  236. } );
  237. } );
  238. describe( 'get()', () => {
  239. it( 'should get paragraph with text', () => {
  240. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  241. setData( model, '<paragraph>foo</paragraph>' );
  242. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  243. expect( data.get() ).to.equal( '<p>foo</p>' );
  244. } );
  245. it( 'should get empty paragraph', () => {
  246. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  247. setData( model, '<paragraph></paragraph>' );
  248. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  249. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  250. } );
  251. it( 'should get two paragraphs', () => {
  252. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  253. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  254. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  255. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  256. } );
  257. it( 'should get text directly in root', () => {
  258. schema.extend( '$text', { allowIn: '$root' } );
  259. setData( model, 'foo' );
  260. expect( data.get() ).to.equal( 'foo' );
  261. } );
  262. it( 'should get paragraphs without bold', () => {
  263. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  264. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  265. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  266. expect( data.get() ).to.equal( '<p>foobar</p>' );
  267. } );
  268. it( 'should get paragraphs with bold', () => {
  269. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  270. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  271. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  272. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  273. expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
  274. } );
  275. it( 'should get root name as a parameter', () => {
  276. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  277. schema.extend( '$text', { allowIn: '$root' } );
  278. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  279. setData( model, 'Bar', { rootName: 'title' } );
  280. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  281. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  282. expect( data.get() ).to.equal( '<p>foo</p>' );
  283. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  284. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  285. } );
  286. it( 'should throw an error when non-existent root is used', () => {
  287. expect( () => {
  288. data.get( 'nonexistent' );
  289. } ).to.throw(
  290. CKEditorError,
  291. 'datacontroller-get-non-existent-root: Attempting to get data from a non-existing root.'
  292. );
  293. } );
  294. } );
  295. describe( 'stringify()', () => {
  296. beforeEach( () => {
  297. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  298. schema.register( 'div' );
  299. schema.extend( '$block', { allowIn: 'div' } );
  300. schema.extend( 'div', { allowIn: '$root' } );
  301. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  302. } );
  303. it( 'should stringify a content of an element', () => {
  304. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  305. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  306. } );
  307. it( 'should stringify a content of a document fragment', () => {
  308. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  309. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  310. } );
  311. } );
  312. describe( 'toView()', () => {
  313. beforeEach( () => {
  314. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  315. schema.register( 'div' );
  316. schema.extend( '$block', { allowIn: 'div' } );
  317. schema.extend( 'div', { allowIn: '$root' } );
  318. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  319. } );
  320. it( 'should convert a content of an element', () => {
  321. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  322. const viewDocumentFragment = data.toView( modelElement );
  323. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  324. const viewElement = viewDocumentFragment.getChild( 0 );
  325. expect( viewElement.name ).to.equal( 'p' );
  326. expect( viewElement.childCount ).to.equal( 1 );
  327. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  328. } );
  329. it( 'should correctly convert document markers #1', () => {
  330. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  331. const modelRoot = model.document.getRoot();
  332. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  333. model.change( writer => {
  334. writer.insert( modelElement, modelRoot, 0 );
  335. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 1 ) );
  336. writer.addMarker( 'marker:a', { range, usingOperation: true } );
  337. } );
  338. const viewDocumentFragment = data.toView( modelElement );
  339. const viewElement = viewDocumentFragment.getChild( 0 );
  340. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  341. } );
  342. it( 'should correctly convert document markers #2', () => {
  343. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  344. const modelRoot = model.document.getRoot();
  345. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  346. downcastHelpers.markerToHighlight( { model: 'marker:b', view: { classes: 'b' } } );
  347. const modelP1 = modelElement.getChild( 0 );
  348. const modelP2 = modelElement.getChild( 1 );
  349. model.change( writer => {
  350. writer.insert( modelElement, modelRoot, 0 );
  351. const rangeA = writer.createRange( writer.createPositionAt( modelP1, 1 ), writer.createPositionAt( modelP1, 3 ) );
  352. const rangeB = writer.createRange( writer.createPositionAt( modelP2, 0 ), writer.createPositionAt( modelP2, 2 ) );
  353. writer.addMarker( 'marker:a', { range: rangeA, usingOperation: true } );
  354. writer.addMarker( 'marker:b', { range: rangeB, usingOperation: true } );
  355. } );
  356. const viewDocumentFragment = data.toView( modelP1 );
  357. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  358. } );
  359. it( 'should convert a document fragment', () => {
  360. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  361. const viewDocumentFragment = data.toView( modelDocumentFragment );
  362. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  363. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  364. const viewElement = viewDocumentFragment.getChild( 0 );
  365. expect( viewElement.name ).to.equal( 'p' );
  366. expect( viewElement.childCount ).to.equal( 1 );
  367. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  368. } );
  369. it( 'should keep view-model mapping', () => {
  370. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  371. const viewDocumentFragment = data.toView( modelDocumentFragment );
  372. const firstModelElement = modelDocumentFragment.getChild( 0 );
  373. const firstViewElement = viewDocumentFragment.getChild( 0 );
  374. const modelRange = ModelRange._createOn( firstModelElement );
  375. const viewRange = ViewRange._createOn( firstViewElement );
  376. const mappedModelRange = data.mapper.toModelRange( viewRange );
  377. const mappedViewRange = data.mapper.toViewRange( modelRange );
  378. expect( mappedModelRange ).to.be.instanceOf( ModelRange );
  379. expect( mappedViewRange ).to.be.instanceOf( ViewRange );
  380. expect( mappedModelRange.end.nodeBefore ).to.equal( firstModelElement );
  381. expect( mappedModelRange.end.nodeAfter ).to.equal( modelDocumentFragment.getChild( 1 ) );
  382. expect( mappedViewRange.end.nodeBefore ).to.equal( firstViewElement );
  383. expect( mappedViewRange.end.nodeAfter ).to.equal( viewDocumentFragment.getChild( 1 ) );
  384. } );
  385. } );
  386. describe( 'destroy()', () => {
  387. it( 'should be there for you', () => {
  388. // Should not throw.
  389. data.destroy();
  390. expect( data ).to.respondTo( 'destroy' );
  391. } );
  392. } );
  393. } );