datacontroller.js 16 KB

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