datacontroller.js 12 KB

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