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