datacontroller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelDocument from '../../src/model/document';
  6. import DataController from '../../src/controller/datacontroller';
  7. import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
  8. import buildViewConverter from '../../src/conversion/buildviewconverter';
  9. import buildModelConverter from '../../src/conversion/buildmodelconverter';
  10. import ModelDocumentFragment from '../../src/model/documentfragment';
  11. import ModelElement from '../../src/model/element';
  12. import ModelText from '../../src/model/text';
  13. import ModelSelection from '../../src/model/selection';
  14. import ViewDocumentFragment from '../../src/view/documentfragment';
  15. import { getData, setData, stringify, parse as parseModel } from '../../src/dev-utils/model';
  16. import { parse as parseView } from '../../src/dev-utils/view';
  17. import count from '@ckeditor/ckeditor5-utils/src/count';
  18. describe( 'DataController', () => {
  19. let modelDocument, htmlDataProcessor, data, schema;
  20. beforeEach( () => {
  21. modelDocument = new ModelDocument();
  22. modelDocument.createRoot();
  23. modelDocument.createRoot( '$root', 'title' );
  24. htmlDataProcessor = new HtmlDataProcessor();
  25. data = new DataController( modelDocument, htmlDataProcessor );
  26. schema = modelDocument.schema;
  27. } );
  28. describe( 'constructor()', () => {
  29. it( 'works without data processor', () => {
  30. const data = new DataController( modelDocument );
  31. expect( data.processor ).to.be.undefined;
  32. } );
  33. it( 'should add insertContent listener', () => {
  34. const batch = modelDocument.batch();
  35. const content = new ModelDocumentFragment( [ new ModelText( 'x' ) ] );
  36. schema.registerItem( 'paragraph', '$block' );
  37. setData( modelDocument, '<paragraph>a[]b</paragraph>' );
  38. data.fire( 'insertContent', { content, selection: modelDocument.selection, batch } );
  39. expect( getData( modelDocument ) ).to.equal( '<paragraph>ax[]b</paragraph>' );
  40. expect( batch.deltas.length ).to.be.above( 0 );
  41. } );
  42. it( 'should add deleteContent listener', () => {
  43. schema.registerItem( 'paragraph', '$block' );
  44. setData( modelDocument, '<paragraph>f[oo</paragraph><paragraph>ba]r</paragraph>' );
  45. const batch = modelDocument.batch();
  46. data.fire( 'deleteContent', { batch, selection: modelDocument.selection } );
  47. expect( getData( modelDocument ) ).to.equal( '<paragraph>f[]</paragraph><paragraph>r</paragraph>' );
  48. expect( batch.deltas ).to.not.be.empty;
  49. } );
  50. it( 'should add deleteContent listener which passes ', () => {
  51. schema.registerItem( 'paragraph', '$block' );
  52. setData( modelDocument, '<paragraph>f[oo</paragraph><paragraph>ba]r</paragraph>' );
  53. const batch = modelDocument.batch();
  54. data.fire( 'deleteContent', {
  55. batch,
  56. selection: modelDocument.selection,
  57. options: { merge: true }
  58. } );
  59. expect( getData( modelDocument ) ).to.equal( '<paragraph>f[]r</paragraph>' );
  60. } );
  61. it( 'should add modifySelection listener', () => {
  62. schema.registerItem( 'paragraph', '$block' );
  63. setData( modelDocument, '<paragraph>foo[]bar</paragraph>' );
  64. data.fire( 'modifySelection', {
  65. selection: modelDocument.selection,
  66. options: {
  67. direction: 'backward'
  68. }
  69. } );
  70. expect( getData( modelDocument ) )
  71. .to.equal( '<paragraph>fo[o]bar</paragraph>' );
  72. expect( modelDocument.selection.isBackward ).to.true;
  73. } );
  74. it( 'should add getSelectedContent listener', () => {
  75. schema.registerItem( 'paragraph', '$block' );
  76. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  77. const evtData = {
  78. selection: modelDocument.selection
  79. };
  80. data.fire( 'getSelectedContent', evtData );
  81. expect( stringify( evtData.content ) ).to.equal( 'ob' );
  82. } );
  83. } );
  84. describe( 'parse', () => {
  85. it( 'should set text', () => {
  86. schema.allow( { name: '$text', inside: '$root' } );
  87. const model = data.parse( '<p>foo<b>bar</b></p>' );
  88. expect( stringify( model ) ).to.equal( 'foobar' );
  89. } );
  90. it( 'should set paragraph', () => {
  91. schema.registerItem( 'paragraph', '$block' );
  92. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  93. const model = data.parse( '<p>foo<b>bar</b></p>' );
  94. expect( stringify( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
  95. } );
  96. it( 'should set two paragraphs', () => {
  97. schema.registerItem( 'paragraph', '$block' );
  98. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  99. const model = data.parse( '<p>foo</p><p>bar</p>' );
  100. expect( stringify( model ) ).to.equal(
  101. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  102. } );
  103. it( 'should set paragraphs with bold', () => {
  104. schema.registerItem( 'paragraph', '$block' );
  105. schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
  106. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  107. buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  108. const model = data.parse( '<p>foo<b>bar</b></p>' );
  109. expect( stringify( model ) ).to.equal(
  110. '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  111. } );
  112. it( 'should parse in the root context by default', () => {
  113. const model = data.parse( 'foo' );
  114. expect( stringify( model ) ).to.equal( '' );
  115. } );
  116. it( 'should accept parsing context', () => {
  117. const model = data.parse( 'foo', '$block' );
  118. expect( stringify( model ) ).to.equal( 'foo' );
  119. } );
  120. } );
  121. describe( 'toModel', () => {
  122. beforeEach( () => {
  123. schema.registerItem( 'paragraph', '$block' );
  124. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  125. } );
  126. it( 'should convert content of an element #1', () => {
  127. const viewElement = parseView( '<p>foo</p>' );
  128. const modelElement = data.toModel( viewElement );
  129. expect( modelElement ).to.be.instanceOf( ModelElement );
  130. expect( stringify( modelElement ) ).to.equal( '<paragraph>foo</paragraph>' );
  131. } );
  132. it( 'should convert content of an element #2', () => {
  133. const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
  134. const modelFragment = data.toModel( viewFragment );
  135. expect( modelFragment ).to.be.instanceOf( ModelDocumentFragment );
  136. expect( stringify( modelFragment ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  137. } );
  138. it( 'should accept parsing context', () => {
  139. modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
  140. schema.registerItem( 'inlineRoot' );
  141. schema.allow( { name: '$text', inside: 'inlineRoot' } );
  142. const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
  143. const modelFragmentInRoot = data.toModel( viewFragment );
  144. expect( stringify( modelFragmentInRoot ) ).to.equal( '' );
  145. const modelFragmentInInlineRoot = data.toModel( viewFragment, 'inlineRoot' );
  146. expect( stringify( modelFragmentInInlineRoot ) ).to.equal( 'foo' );
  147. } );
  148. } );
  149. describe( 'set', () => {
  150. it( 'should set data to root', () => {
  151. schema.allow( { name: '$text', inside: '$root' } );
  152. data.set( 'foo' );
  153. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  154. } );
  155. it( 'should create a batch', () => {
  156. schema.allow( { name: '$text', inside: '$root' } );
  157. data.set( 'foo' );
  158. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  159. } );
  160. it( 'should fire #changesDone', () => {
  161. const spy = sinon.spy();
  162. schema.allow( { name: '$text', inside: '$root' } );
  163. modelDocument.on( 'changesDone', spy );
  164. data.set( 'foo' );
  165. expect( spy.calledOnce ).to.be.true;
  166. } );
  167. it( 'should get root name as a parameter', () => {
  168. schema.allow( { name: '$text', inside: '$root' } );
  169. data.set( 'foo', 'main' );
  170. data.set( 'Bar', 'title' );
  171. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  172. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  173. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  174. } );
  175. // This case was added when order of params was different and it really didn't work. Let's keep it
  176. // if anyone will ever try to change this.
  177. it( 'should allow setting empty data', () => {
  178. schema.allow( { name: '$text', inside: '$root' } );
  179. data.set( 'foo', 'title' );
  180. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  181. data.set( '', 'title' );
  182. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  183. } );
  184. } );
  185. describe( 'get', () => {
  186. it( 'should get paragraph with text', () => {
  187. modelDocument.schema.registerItem( 'paragraph', '$block' );
  188. setData( modelDocument, '<paragraph>foo</paragraph>' );
  189. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  190. expect( data.get() ).to.equal( '<p>foo</p>' );
  191. } );
  192. it( 'should get empty paragraph', () => {
  193. modelDocument.schema.registerItem( 'paragraph', '$block' );
  194. setData( modelDocument, '<paragraph></paragraph>' );
  195. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  196. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  197. } );
  198. it( 'should get two paragraphs', () => {
  199. modelDocument.schema.registerItem( 'paragraph', '$block' );
  200. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  201. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  202. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  203. } );
  204. it( 'should get text directly in root', () => {
  205. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  206. setData( modelDocument, 'foo' );
  207. expect( data.get() ).to.equal( 'foo' );
  208. } );
  209. it( 'should get paragraphs without bold', () => {
  210. modelDocument.schema.registerItem( 'paragraph', '$block' );
  211. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  212. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  213. expect( data.get() ).to.equal( '<p>foobar</p>' );
  214. } );
  215. it( 'should get paragraphs with bold', () => {
  216. modelDocument.schema.registerItem( 'paragraph', '$block' );
  217. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  218. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  219. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  220. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  221. } );
  222. it( 'should get root name as a parameter', () => {
  223. modelDocument.schema.registerItem( 'paragraph', '$block' );
  224. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  225. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  226. setData( modelDocument, 'Bar', { rootName: 'title' } );
  227. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  228. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  229. expect( data.get() ).to.equal( '<p>foo</p>' );
  230. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  231. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  232. } );
  233. } );
  234. describe( 'stringify', () => {
  235. beforeEach( () => {
  236. modelDocument.schema.registerItem( 'paragraph', '$block' );
  237. modelDocument.schema.registerItem( 'div' );
  238. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  239. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  240. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  241. } );
  242. it( 'should stringify a content of an element', () => {
  243. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  244. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  245. } );
  246. it( 'should stringify a content of a document fragment', () => {
  247. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  248. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  249. } );
  250. } );
  251. describe( 'toView', () => {
  252. beforeEach( () => {
  253. modelDocument.schema.registerItem( 'paragraph', '$block' );
  254. modelDocument.schema.registerItem( 'div' );
  255. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  256. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  257. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  258. } );
  259. it( 'should convert a content of an element', () => {
  260. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  261. const viewDocumentFragment = data.toView( modelElement );
  262. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  263. const viewElement = viewDocumentFragment.getChild( 0 );
  264. expect( viewElement.name ).to.equal( 'p' );
  265. expect( viewElement.childCount ).to.equal( 1 );
  266. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  267. } );
  268. it( 'should convert a document fragment', () => {
  269. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  270. const viewDocumentFragment = data.toView( modelDocumentFragment );
  271. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  272. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  273. const viewElement = viewDocumentFragment.getChild( 0 );
  274. expect( viewElement.name ).to.equal( 'p' );
  275. expect( viewElement.childCount ).to.equal( 1 );
  276. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  277. } );
  278. } );
  279. describe( 'destroy', () => {
  280. it( 'should be there for you', () => {
  281. // Should not throw.
  282. data.destroy();
  283. expect( data ).to.respondTo( 'destroy' );
  284. } );
  285. } );
  286. describe( 'insertContent', () => {
  287. it( 'should fire the insertContent event', () => {
  288. const spy = sinon.spy();
  289. const content = new ModelDocumentFragment( [ new ModelText( 'x' ) ] );
  290. const batch = modelDocument.batch();
  291. schema.allow( { name: '$text', inside: '$root' } );
  292. data.on( 'insertContent', spy );
  293. data.insertContent( content, modelDocument.selection, batch );
  294. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( {
  295. batch: batch,
  296. selection: modelDocument.selection,
  297. content: content
  298. } );
  299. } );
  300. } );
  301. describe( 'deleteContent', () => {
  302. it( 'should fire the deleteContent event', () => {
  303. const spy = sinon.spy();
  304. const batch = modelDocument.batch();
  305. data.on( 'deleteContent', spy );
  306. data.deleteContent( modelDocument.selection, batch );
  307. const evtData = spy.args[ 0 ][ 1 ];
  308. expect( evtData.batch ).to.equal( batch );
  309. expect( evtData.selection ).to.equal( modelDocument.selection );
  310. } );
  311. } );
  312. describe( 'modifySelection', () => {
  313. it( 'should fire the deleteContent event', () => {
  314. const spy = sinon.spy();
  315. const opts = { direction: 'backward' };
  316. data.on( 'modifySelection', spy );
  317. data.modifySelection( modelDocument.selection, opts );
  318. const evtData = spy.args[ 0 ][ 1 ];
  319. expect( evtData.selection ).to.equal( modelDocument.selection );
  320. expect( evtData.options ).to.equal( opts );
  321. } );
  322. } );
  323. describe( 'getSelectedContent', () => {
  324. it( 'should fire the getSelectedContent event', () => {
  325. const spy = sinon.spy();
  326. const sel = new ModelSelection();
  327. data.on( 'getSelectedContent', spy );
  328. data.getSelectedContent( sel );
  329. const evtData = spy.args[ 0 ][ 1 ];
  330. expect( evtData.selection ).to.equal( sel );
  331. } );
  332. it( 'should return the evtData.content of the getSelectedContent event', () => {
  333. const frag = new ModelDocumentFragment();
  334. data.on( 'getSelectedContent', ( evt, data ) => {
  335. data.content = frag;
  336. evt.stop();
  337. }, { priority: 'high' } );
  338. expect( data.getSelectedContent() ).to.equal( frag );
  339. } );
  340. } );
  341. } );