datacontroller.js 16 KB

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