datacontroller.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 { modelItem } = data.parse( '<p>foo<b>bar</b></p>' );
  89. expect( stringify( modelItem ) ).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 { modelItem } = data.parse( '<p>foo<b>bar</b></p>' );
  95. expect( stringify( modelItem ) ).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 { modelItem } = data.parse( '<p>foo</p><p>bar</p>' );
  101. expect( stringify( modelItem ) ).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 { modelItem } = data.parse( '<p>foo<b>bar</b></p>' );
  110. expect( stringify( modelItem ) ).to.equal(
  111. '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  112. } );
  113. it( 'should return markers data', () => {
  114. schema.registerItem( 'paragraph', '$block' );
  115. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  116. buildViewConverter().for( data.viewToModel ).fromElement( 'marker' ).toMarker();
  117. const { markersData } = data.parse( '<p>f<marker data-name="search"></marker>oob<marker data-name="search"></marker>ar</p>' );
  118. expect( markersData ).to.instanceof( Map );
  119. expect( markersData.size ).to.equal( 1 );
  120. expect( markersData.get( 'search' ) ).to.deep.equal( { startPath: [ 0, 1 ], endPath: [ 0, 4 ] } );
  121. } );
  122. it( 'should parse in the root context by default', () => {
  123. const { modelItem } = data.parse( 'foo' );
  124. expect( stringify( modelItem ) ).to.equal( '' );
  125. } );
  126. it( 'should accept parsing context', () => {
  127. const { modelItem } = data.parse( 'foo', '$block' );
  128. expect( stringify( modelItem ) ).to.equal( 'foo' );
  129. } );
  130. } );
  131. describe( 'toModel', () => {
  132. beforeEach( () => {
  133. schema.registerItem( 'paragraph', '$block' );
  134. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  135. } );
  136. it( 'should convert content of an element #1', () => {
  137. const viewElement = parseView( '<p>foo</p>' );
  138. const { modelItem } = data.toModel( viewElement );
  139. expect( modelItem ).to.be.instanceOf( ModelElement );
  140. expect( stringify( modelItem ) ).to.equal( '<paragraph>foo</paragraph>' );
  141. } );
  142. it( 'should convert content of an element #2', () => {
  143. const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
  144. const { modelItem } = data.toModel( viewFragment );
  145. expect( modelItem ).to.be.instanceOf( ModelDocumentFragment );
  146. expect( stringify( modelItem ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  147. } );
  148. it( 'should convert marker stamps', () => {
  149. const viewElement = parseView( '<p>f<marker data-name="search"></marker>o<marker data-name="search"></marker>o</p>' );
  150. buildViewConverter().for( data.viewToModel ).fromElement( 'marker' ).toMarker();
  151. const { markersData } = data.toModel( viewElement );
  152. expect( markersData ).to.instanceof( Map );
  153. expect( markersData.size ).to.equal( 1 );
  154. expect( markersData.get( 'search' ) ).to.deep.equal( { startPath: [ 1 ], endPath: [ 2 ] } );
  155. } );
  156. it( 'should accept parsing context', () => {
  157. modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
  158. schema.registerItem( 'inlineRoot' );
  159. schema.allow( { name: '$text', inside: 'inlineRoot' } );
  160. const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
  161. // Model fragment in root.
  162. expect( stringify( data.toModel( viewFragment ).modelItem ) ).to.equal( '' );
  163. // Model fragment in inline root.
  164. expect( stringify( data.toModel( viewFragment, 'inlineRoot' ).modelItem ) ).to.equal( 'foo' );
  165. } );
  166. } );
  167. describe( 'set', () => {
  168. it( 'should set data to root', () => {
  169. schema.allow( { name: '$text', inside: '$root' } );
  170. data.set( 'foo' );
  171. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  172. } );
  173. it( 'should set markers to `modelDocument#markers` collection', () => {
  174. modelDocument.schema.registerItem( 'paragraph', '$block' );
  175. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  176. buildViewConverter().for( data.viewToModel ).fromElement( 'm' ).toMarker();
  177. data.set(
  178. '<p>' +
  179. 'Fo' +
  180. '<m data-name="search"></m>' +
  181. 'o ba' +
  182. '<m data-name="comment"></m>' +
  183. 'r bi' +
  184. '<m data-name="search"></m>' +
  185. 'z' +
  186. '</p>'
  187. );
  188. expect( Array.from( modelDocument.markers ).length ).to.equal( 2 );
  189. const paragraph = modelDocument.getRoot().getChild( 0 );
  190. const commentMarkerRange = ModelRange.createFromParentsAndOffsets( paragraph, 6, paragraph, 6 );
  191. const searchMarkerRange = ModelRange.createFromParentsAndOffsets( paragraph, 2, paragraph, 10 );
  192. expect( modelDocument.markers.get( 'comment' ).getRange().isEqual( commentMarkerRange ) ).to.true;
  193. expect( modelDocument.markers.get( 'search' ).getRange().isEqual( searchMarkerRange ) ).to.true;
  194. } );
  195. it( 'should create a batch', () => {
  196. schema.allow( { name: '$text', inside: '$root' } );
  197. data.set( 'foo' );
  198. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  199. } );
  200. it( 'should fire #changesDone', () => {
  201. const spy = sinon.spy();
  202. schema.allow( { name: '$text', inside: '$root' } );
  203. modelDocument.on( 'changesDone', spy );
  204. data.set( 'foo' );
  205. expect( spy.calledOnce ).to.be.true;
  206. } );
  207. it( 'should get root name as a parameter', () => {
  208. schema.allow( { name: '$text', inside: '$root' } );
  209. data.set( 'foo', 'main' );
  210. data.set( 'Bar', 'title' );
  211. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  212. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  213. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  214. } );
  215. // This case was added when order of params was different and it really didn't work. Let's keep it
  216. // if anyone will ever try to change this.
  217. it( 'should allow setting empty data', () => {
  218. schema.allow( { name: '$text', inside: '$root' } );
  219. data.set( 'foo', 'title' );
  220. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  221. data.set( '', 'title' );
  222. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  223. } );
  224. } );
  225. describe( 'get', () => {
  226. it( 'should get paragraph with text', () => {
  227. modelDocument.schema.registerItem( 'paragraph', '$block' );
  228. setData( modelDocument, '<paragraph>foo</paragraph>' );
  229. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  230. expect( data.get() ).to.equal( '<p>foo</p>' );
  231. } );
  232. it( 'should get empty paragraph', () => {
  233. modelDocument.schema.registerItem( 'paragraph', '$block' );
  234. setData( modelDocument, '<paragraph></paragraph>' );
  235. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  236. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  237. } );
  238. it( 'should get two paragraphs', () => {
  239. modelDocument.schema.registerItem( 'paragraph', '$block' );
  240. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  241. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  242. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  243. } );
  244. it( 'should get text directly in root', () => {
  245. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  246. setData( modelDocument, 'foo' );
  247. expect( data.get() ).to.equal( 'foo' );
  248. } );
  249. it( 'should get paragraphs without bold', () => {
  250. modelDocument.schema.registerItem( 'paragraph', '$block' );
  251. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  252. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  253. expect( data.get() ).to.equal( '<p>foobar</p>' );
  254. } );
  255. it( 'should get paragraphs with bold', () => {
  256. modelDocument.schema.registerItem( 'paragraph', '$block' );
  257. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  258. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  259. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  260. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  261. } );
  262. it( 'should get root name as a parameter', () => {
  263. modelDocument.schema.registerItem( 'paragraph', '$block' );
  264. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  265. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  266. setData( modelDocument, 'Bar', { rootName: 'title' } );
  267. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  268. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  269. expect( data.get() ).to.equal( '<p>foo</p>' );
  270. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  271. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  272. } );
  273. } );
  274. describe( 'stringify', () => {
  275. beforeEach( () => {
  276. modelDocument.schema.registerItem( 'paragraph', '$block' );
  277. modelDocument.schema.registerItem( 'div' );
  278. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  279. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  280. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  281. } );
  282. it( 'should stringify a content of an element', () => {
  283. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  284. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  285. } );
  286. it( 'should stringify a content of a document fragment', () => {
  287. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  288. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  289. } );
  290. } );
  291. describe( 'toView', () => {
  292. beforeEach( () => {
  293. modelDocument.schema.registerItem( 'paragraph', '$block' );
  294. modelDocument.schema.registerItem( 'div' );
  295. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  296. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  297. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  298. } );
  299. it( 'should convert a content of an element', () => {
  300. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  301. const viewDocumentFragment = data.toView( modelElement );
  302. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  303. const viewElement = viewDocumentFragment.getChild( 0 );
  304. expect( viewElement.name ).to.equal( 'p' );
  305. expect( viewElement.childCount ).to.equal( 1 );
  306. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  307. } );
  308. it( 'should convert a document fragment', () => {
  309. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  310. const viewDocumentFragment = data.toView( modelDocumentFragment );
  311. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  312. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  313. const viewElement = viewDocumentFragment.getChild( 0 );
  314. expect( viewElement.name ).to.equal( 'p' );
  315. expect( viewElement.childCount ).to.equal( 1 );
  316. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  317. } );
  318. } );
  319. describe( 'destroy', () => {
  320. it( 'should be there for you', () => {
  321. // Should not throw.
  322. data.destroy();
  323. expect( data ).to.respondTo( 'destroy' );
  324. } );
  325. } );
  326. describe( 'insertContent', () => {
  327. it( 'should fire the insertContent event', () => {
  328. const spy = sinon.spy();
  329. const content = new ModelDocumentFragment( [ new ModelText( 'x' ) ] );
  330. const batch = modelDocument.batch();
  331. schema.allow( { name: '$text', inside: '$root' } );
  332. data.on( 'insertContent', spy );
  333. data.insertContent( content, modelDocument.selection, batch );
  334. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( {
  335. batch: batch,
  336. selection: modelDocument.selection,
  337. content: content
  338. } );
  339. } );
  340. } );
  341. describe( 'deleteContent', () => {
  342. it( 'should fire the deleteContent event', () => {
  343. const spy = sinon.spy();
  344. const batch = modelDocument.batch();
  345. data.on( 'deleteContent', spy );
  346. data.deleteContent( modelDocument.selection, batch );
  347. const evtData = spy.args[ 0 ][ 1 ];
  348. expect( evtData.batch ).to.equal( batch );
  349. expect( evtData.selection ).to.equal( modelDocument.selection );
  350. } );
  351. } );
  352. describe( 'modifySelection', () => {
  353. it( 'should fire the deleteContent event', () => {
  354. const spy = sinon.spy();
  355. const opts = { direction: 'backward' };
  356. data.on( 'modifySelection', spy );
  357. data.modifySelection( modelDocument.selection, opts );
  358. const evtData = spy.args[ 0 ][ 1 ];
  359. expect( evtData.selection ).to.equal( modelDocument.selection );
  360. expect( evtData.options ).to.equal( opts );
  361. } );
  362. } );
  363. describe( 'getSelectedContent', () => {
  364. it( 'should fire the getSelectedContent event', () => {
  365. const spy = sinon.spy();
  366. const sel = new ModelSelection();
  367. data.on( 'getSelectedContent', spy );
  368. data.getSelectedContent( sel );
  369. const evtData = spy.args[ 0 ][ 1 ];
  370. expect( evtData.selection ).to.equal( sel );
  371. } );
  372. it( 'should return the evtData.content of the getSelectedContent event', () => {
  373. const frag = new ModelDocumentFragment();
  374. data.on( 'getSelectedContent', ( evt, data ) => {
  375. data.content = frag;
  376. evt.stop();
  377. }, { priority: 'high' } );
  378. expect( data.getSelectedContent() ).to.equal( frag );
  379. } );
  380. } );
  381. } );