8
0

datacontroller.js 13 KB

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