datacontroller.js 17 KB

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