8
0

datacontroller.js 14 KB

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