datacontroller.js 17 KB

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