datacontroller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 ModelElement from '../../src/model/element';
  12. import ModelText from '../../src/model/text';
  13. import ModelSelection from '../../src/model/selection';
  14. import ModelRange from '../../src/model/range';
  15. import ViewDocumentFragment from '../../src/view/documentfragment';
  16. import { getData, setData, stringify, parse as parseModel } from '../../src/dev-utils/model';
  17. import { parse as parseView } from '../../src/dev-utils/view';
  18. import count from '@ckeditor/ckeditor5-utils/src/count';
  19. describe( 'DataController', () => {
  20. let modelDocument, htmlDataProcessor, data, schema;
  21. beforeEach( () => {
  22. modelDocument = new ModelDocument();
  23. modelDocument.createRoot();
  24. modelDocument.createRoot( '$root', 'title' );
  25. htmlDataProcessor = new HtmlDataProcessor();
  26. data = new DataController( modelDocument, htmlDataProcessor );
  27. schema = modelDocument.schema;
  28. } );
  29. describe( 'constructor()', () => {
  30. it( 'works without data processor', () => {
  31. const data = new DataController( modelDocument );
  32. expect( data.processor ).to.be.undefined;
  33. } );
  34. it( 'should add insertContent listener', () => {
  35. const batch = modelDocument.batch();
  36. const content = new ModelDocumentFragment( [ new ModelText( 'x' ) ] );
  37. schema.registerItem( 'paragraph', '$block' );
  38. setData( modelDocument, '<paragraph>a[]b</paragraph>' );
  39. data.fire( 'insertContent', { content, selection: modelDocument.selection, batch } );
  40. expect( getData( modelDocument ) ).to.equal( '<paragraph>ax[]b</paragraph>' );
  41. expect( batch.deltas.length ).to.be.above( 0 );
  42. } );
  43. it( 'should add deleteContent listener', () => {
  44. schema.registerItem( 'paragraph', '$block' );
  45. setData( modelDocument, '<paragraph>f[oo</paragraph><paragraph>ba]r</paragraph>' );
  46. const batch = modelDocument.batch();
  47. data.fire( 'deleteContent', { batch, selection: modelDocument.selection } );
  48. expect( getData( modelDocument ) ).to.equal( '<paragraph>f[]</paragraph><paragraph>r</paragraph>' );
  49. expect( batch.deltas ).to.not.be.empty;
  50. } );
  51. it( 'should add deleteContent listener which passes ', () => {
  52. schema.registerItem( 'paragraph', '$block' );
  53. setData( modelDocument, '<paragraph>f[oo</paragraph><paragraph>ba]r</paragraph>' );
  54. const batch = modelDocument.batch();
  55. data.fire( 'deleteContent', {
  56. batch,
  57. selection: modelDocument.selection,
  58. options: { merge: true }
  59. } );
  60. expect( getData( modelDocument ) ).to.equal( '<paragraph>f[]r</paragraph>' );
  61. } );
  62. it( 'should add modifySelection listener', () => {
  63. schema.registerItem( 'paragraph', '$block' );
  64. setData( modelDocument, '<paragraph>foo[]bar</paragraph>' );
  65. data.fire( 'modifySelection', {
  66. selection: modelDocument.selection,
  67. options: {
  68. direction: 'backward'
  69. }
  70. } );
  71. expect( getData( modelDocument ) )
  72. .to.equal( '<paragraph>fo[o]bar</paragraph>' );
  73. expect( modelDocument.selection.isBackward ).to.true;
  74. } );
  75. it( 'should add getSelectedContent listener', () => {
  76. schema.registerItem( 'paragraph', '$block' );
  77. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  78. const evtData = {
  79. selection: modelDocument.selection
  80. };
  81. data.fire( 'getSelectedContent', evtData );
  82. expect( stringify( evtData.content ) ).to.equal( 'ob' );
  83. } );
  84. } );
  85. describe( 'parse', () => {
  86. it( 'should set text', () => {
  87. schema.allow( { name: '$text', inside: '$root' } );
  88. const model = data.parse( '<p>foo<b>bar</b></p>' );
  89. expect( stringify( model ) ).to.equal( 'foobar' );
  90. } );
  91. it( 'should set paragraph', () => {
  92. schema.registerItem( 'paragraph', '$block' );
  93. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  94. const model = data.parse( '<p>foo<b>bar</b></p>' );
  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( stringify( model ) ).to.equal(
  102. '<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( stringify( model ) ).to.equal(
  111. '<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 modelElement = data.toModel( viewElement );
  130. expect( modelElement ).to.be.instanceOf( ModelElement );
  131. expect( stringify( modelElement ) ).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 modelFragment = data.toModel( viewFragment );
  136. expect( modelFragment ).to.be.instanceOf( ModelDocumentFragment );
  137. expect( stringify( modelFragment ) ).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. const modelFragmentInRoot = data.toModel( viewFragment );
  145. expect( stringify( modelFragmentInRoot ) ).to.equal( '' );
  146. const modelFragmentInInlineRoot = data.toModel( viewFragment, 'inlineRoot' );
  147. expect( stringify( modelFragmentInInlineRoot ) ).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 extract markers stamps from converted data and set to `modelDocument#markers` collection', () => {
  157. modelDocument.schema.registerItem( 'paragraph', '$block' );
  158. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  159. buildViewConverter().for( data.viewToModel ).fromElement( 'm' ).toMarker();
  160. data.set(
  161. '<p>' +
  162. 'F' +
  163. '<m data-name="comment"></m>' +
  164. 'o' +
  165. '<m data-name="search"></m>' +
  166. 'o ba' +
  167. '<m data-name="comment"></m>' +
  168. 'r bi' +
  169. '<m data-name="search"></m>' +
  170. 'z' +
  171. '</p>'
  172. );
  173. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( '<paragraph>Foo bar biz</paragraph>' );
  174. expect( Array.from( modelDocument.markers ).length ).to.equal( 2 );
  175. const paragraph = modelDocument.getRoot().getChild( 0 );
  176. const commentMarkerRange = ModelRange.createFromParentsAndOffsets( paragraph, 1, paragraph, 6 );
  177. const searchMarkerRange = ModelRange.createFromParentsAndOffsets( paragraph, 2, paragraph, 10 );
  178. expect( modelDocument.markers.get( 'comment' ).getRange().isEqual( commentMarkerRange ) ).to.true;
  179. expect( modelDocument.markers.get( 'search' ).getRange().isEqual( searchMarkerRange ) ).to.true;
  180. } );
  181. it( 'should extract collapsed markers stamps from converted data and set to `modelDocument#markers` collection', () => {
  182. modelDocument.schema.registerItem( 'paragraph', '$block' );
  183. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  184. buildViewConverter().for( data.viewToModel ).fromElement( 'm' ).toMarker();
  185. data.set( '<p>F<m data-name="comment"></m>o<m data-name="search"></m>o ba</m>r biz</p>' );
  186. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( '<paragraph>Foo bar biz</paragraph>' );
  187. expect( Array.from( modelDocument.markers ).length ).to.equal( 2 );
  188. const paragraph = modelDocument.getRoot().getChild( 0 );
  189. const commentMarkerRange = ModelRange.createFromParentsAndOffsets( paragraph, 1, paragraph, 1 );
  190. const searchMarkerRange = ModelRange.createFromParentsAndOffsets( paragraph, 2, paragraph, 2 );
  191. expect( modelDocument.markers.get( 'comment' ).getRange().isEqual( commentMarkerRange ) ).to.true;
  192. expect( modelDocument.markers.get( 'search' ).getRange().isEqual( searchMarkerRange ) ).to.true;
  193. } );
  194. it( 'should create a batch', () => {
  195. schema.allow( { name: '$text', inside: '$root' } );
  196. data.set( 'foo' );
  197. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  198. } );
  199. it( 'should fire #changesDone', () => {
  200. const spy = sinon.spy();
  201. schema.allow( { name: '$text', inside: '$root' } );
  202. modelDocument.on( 'changesDone', spy );
  203. data.set( 'foo' );
  204. expect( spy.calledOnce ).to.be.true;
  205. } );
  206. it( 'should get root name as a parameter', () => {
  207. schema.allow( { name: '$text', inside: '$root' } );
  208. data.set( 'foo', 'main' );
  209. data.set( 'Bar', 'title' );
  210. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  211. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  212. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  213. } );
  214. // This case was added when order of params was different and it really didn't work. Let's keep it
  215. // if anyone will ever try to change this.
  216. it( 'should allow setting empty data', () => {
  217. schema.allow( { name: '$text', inside: '$root' } );
  218. data.set( 'foo', 'title' );
  219. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  220. data.set( '', 'title' );
  221. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  222. } );
  223. } );
  224. describe( 'get', () => {
  225. it( 'should get paragraph with text', () => {
  226. modelDocument.schema.registerItem( 'paragraph', '$block' );
  227. setData( modelDocument, '<paragraph>foo</paragraph>' );
  228. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  229. expect( data.get() ).to.equal( '<p>foo</p>' );
  230. } );
  231. it( 'should get empty paragraph', () => {
  232. modelDocument.schema.registerItem( 'paragraph', '$block' );
  233. setData( modelDocument, '<paragraph></paragraph>' );
  234. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  235. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  236. } );
  237. it( 'should get two paragraphs', () => {
  238. modelDocument.schema.registerItem( 'paragraph', '$block' );
  239. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  240. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  241. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  242. } );
  243. it( 'should get text directly in root', () => {
  244. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  245. setData( modelDocument, 'foo' );
  246. expect( data.get() ).to.equal( 'foo' );
  247. } );
  248. it( 'should get paragraphs without bold', () => {
  249. modelDocument.schema.registerItem( 'paragraph', '$block' );
  250. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  251. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  252. expect( data.get() ).to.equal( '<p>foobar</p>' );
  253. } );
  254. it( 'should get paragraphs with bold', () => {
  255. modelDocument.schema.registerItem( 'paragraph', '$block' );
  256. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  257. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  258. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  259. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  260. } );
  261. it( 'should get root name as a parameter', () => {
  262. modelDocument.schema.registerItem( 'paragraph', '$block' );
  263. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  264. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  265. setData( modelDocument, 'Bar', { rootName: 'title' } );
  266. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  267. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  268. expect( data.get() ).to.equal( '<p>foo</p>' );
  269. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  270. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  271. } );
  272. } );
  273. describe( 'stringify', () => {
  274. beforeEach( () => {
  275. modelDocument.schema.registerItem( 'paragraph', '$block' );
  276. modelDocument.schema.registerItem( 'div' );
  277. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  278. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  279. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  280. } );
  281. it( 'should stringify a content of an element', () => {
  282. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  283. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  284. } );
  285. it( 'should stringify a content of a document fragment', () => {
  286. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  287. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  288. } );
  289. } );
  290. describe( 'toView', () => {
  291. beforeEach( () => {
  292. modelDocument.schema.registerItem( 'paragraph', '$block' );
  293. modelDocument.schema.registerItem( 'div' );
  294. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  295. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  296. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  297. } );
  298. it( 'should convert a content of an element', () => {
  299. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  300. const viewDocumentFragment = data.toView( modelElement );
  301. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  302. const viewElement = viewDocumentFragment.getChild( 0 );
  303. expect( viewElement.name ).to.equal( 'p' );
  304. expect( viewElement.childCount ).to.equal( 1 );
  305. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  306. } );
  307. it( 'should convert a document fragment', () => {
  308. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  309. const viewDocumentFragment = data.toView( modelDocumentFragment );
  310. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  311. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  312. const viewElement = viewDocumentFragment.getChild( 0 );
  313. expect( viewElement.name ).to.equal( 'p' );
  314. expect( viewElement.childCount ).to.equal( 1 );
  315. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  316. } );
  317. } );
  318. describe( 'destroy', () => {
  319. it( 'should be there for you', () => {
  320. // Should not throw.
  321. data.destroy();
  322. expect( data ).to.respondTo( 'destroy' );
  323. } );
  324. } );
  325. describe( 'insertContent', () => {
  326. it( 'should fire the insertContent event', () => {
  327. const spy = sinon.spy();
  328. const content = new ModelDocumentFragment( [ new ModelText( 'x' ) ] );
  329. const batch = modelDocument.batch();
  330. schema.allow( { name: '$text', inside: '$root' } );
  331. data.on( 'insertContent', spy );
  332. data.insertContent( content, modelDocument.selection, batch );
  333. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( {
  334. batch: batch,
  335. selection: modelDocument.selection,
  336. content: content
  337. } );
  338. } );
  339. it( 'should remove markers stamps from content', () => {
  340. const spy = sinon.spy();
  341. const content = new ModelDocumentFragment( [
  342. new ModelText( 'x' ),
  343. new ModelElement( '$marker', { 'data-name': 'search' } ),
  344. new ModelText( 'y' ),
  345. new ModelElement( '$marker', { 'data-name': 'search' } ),
  346. new ModelText( 'z' )
  347. ] );
  348. data.on( 'insertContent', spy );
  349. data.insertContent( content, modelDocument.selection );
  350. expect( stringify( spy.args[ 0 ][ 1 ].content ) ).to.equal( 'xyz' );
  351. } );
  352. } );
  353. describe( 'deleteContent', () => {
  354. it( 'should fire the deleteContent event', () => {
  355. const spy = sinon.spy();
  356. const batch = modelDocument.batch();
  357. data.on( 'deleteContent', spy );
  358. data.deleteContent( modelDocument.selection, batch );
  359. const evtData = spy.args[ 0 ][ 1 ];
  360. expect( evtData.batch ).to.equal( batch );
  361. expect( evtData.selection ).to.equal( modelDocument.selection );
  362. } );
  363. } );
  364. describe( 'modifySelection', () => {
  365. it( 'should fire the deleteContent event', () => {
  366. const spy = sinon.spy();
  367. const opts = { direction: 'backward' };
  368. data.on( 'modifySelection', spy );
  369. data.modifySelection( modelDocument.selection, opts );
  370. const evtData = spy.args[ 0 ][ 1 ];
  371. expect( evtData.selection ).to.equal( modelDocument.selection );
  372. expect( evtData.options ).to.equal( opts );
  373. } );
  374. } );
  375. describe( 'getSelectedContent', () => {
  376. it( 'should fire the getSelectedContent event', () => {
  377. const spy = sinon.spy();
  378. const sel = new ModelSelection();
  379. data.on( 'getSelectedContent', spy );
  380. data.getSelectedContent( sel );
  381. const evtData = spy.args[ 0 ][ 1 ];
  382. expect( evtData.selection ).to.equal( sel );
  383. } );
  384. it( 'should return the evtData.content of the getSelectedContent event', () => {
  385. const frag = new ModelDocumentFragment();
  386. data.on( 'getSelectedContent', ( evt, data ) => {
  387. data.content = frag;
  388. evt.stop();
  389. }, { priority: 'high' } );
  390. expect( data.getSelectedContent() ).to.equal( frag );
  391. } );
  392. } );
  393. } );