8
0

datacontroller.js 17 KB

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