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 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>' );
  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>' );
  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>' );
  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>' );
  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' );
  66. expect( stringify( model ) ).to.equal( '' );
  67. } );
  68. it( 'should accept parsing context', () => {
  69. const model = data.parse( 'foo', '$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 );
  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 );
  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 ) ) ).to.equal( '' );
  97. // Model fragment in inline root.
  98. expect( stringify( data.toModel( viewFragment, '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. beforeEach( () => {
  188. modelDocument.schema.registerItem( 'paragraph', '$block' );
  189. modelDocument.schema.registerItem( 'div' );
  190. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  191. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  192. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  193. } );
  194. it( 'should stringify a content of an element', () => {
  195. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  196. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  197. } );
  198. it( 'should stringify a content of a document fragment', () => {
  199. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  200. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  201. } );
  202. } );
  203. describe( 'toView', () => {
  204. beforeEach( () => {
  205. modelDocument.schema.registerItem( 'paragraph', '$block' );
  206. modelDocument.schema.registerItem( 'div' );
  207. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  208. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  209. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  210. } );
  211. it( 'should convert a content of an element', () => {
  212. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  213. const viewDocumentFragment = data.toView( modelElement );
  214. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  215. const viewElement = viewDocumentFragment.getChild( 0 );
  216. expect( viewElement.name ).to.equal( 'p' );
  217. expect( viewElement.childCount ).to.equal( 1 );
  218. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  219. } );
  220. it( 'should convert a document fragment', () => {
  221. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  222. const viewDocumentFragment = data.toView( modelDocumentFragment );
  223. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  224. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  225. const viewElement = viewDocumentFragment.getChild( 0 );
  226. expect( viewElement.name ).to.equal( 'p' );
  227. expect( viewElement.childCount ).to.equal( 1 );
  228. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  229. } );
  230. } );
  231. describe( 'destroy', () => {
  232. it( 'should be there for you', () => {
  233. // Should not throw.
  234. data.destroy();
  235. expect( data ).to.respondTo( 'destroy' );
  236. } );
  237. } );
  238. describe( 'insertContent', () => {
  239. it( 'should be decorated', () => {
  240. schema.allow( { name: '$text', inside: '$root' } ); // To surpress warnings.
  241. const spy = sinon.spy();
  242. data.on( 'insertContent', spy );
  243. data.insertContent( new ModelText( 'a' ), modelDocument.selection );
  244. expect( spy.calledOnce ).to.be.true;
  245. } );
  246. it( 'should insert content (item)', () => {
  247. schema.registerItem( 'paragraph', '$block' );
  248. setData( modelDocument, '<paragraph>fo[]ar</paragraph>' );
  249. data.insertContent( new ModelText( 'ob' ), modelDocument.selection );
  250. expect( getData( modelDocument ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  251. } );
  252. it( 'should insert content (document fragment)', () => {
  253. schema.registerItem( 'paragraph', '$block' );
  254. setData( modelDocument, '<paragraph>fo[]ar</paragraph>' );
  255. data.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ), modelDocument.selection );
  256. expect( getData( modelDocument ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  257. } );
  258. } );
  259. describe( 'deleteContent', () => {
  260. it( 'should be decorated', () => {
  261. const spy = sinon.spy();
  262. data.on( 'deleteContent', spy );
  263. data.deleteContent( modelDocument.selection );
  264. expect( spy.calledOnce ).to.be.true;
  265. } );
  266. it( 'should delete selected content', () => {
  267. schema.registerItem( 'paragraph', '$block' );
  268. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  269. data.deleteContent( modelDocument.selection, modelDocument.batch() );
  270. expect( getData( modelDocument ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  271. } );
  272. } );
  273. describe( 'modifySelection', () => {
  274. it( 'should be decorated', () => {
  275. schema.registerItem( 'paragraph', '$block' );
  276. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  277. const spy = sinon.spy();
  278. data.on( 'modifySelection', spy );
  279. data.modifySelection( modelDocument.selection );
  280. expect( spy.calledOnce ).to.be.true;
  281. } );
  282. it( 'should modify a selection', () => {
  283. schema.registerItem( 'paragraph', '$block' );
  284. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  285. data.modifySelection( modelDocument.selection, { direction: 'backward' } );
  286. expect( getData( modelDocument ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  287. } );
  288. } );
  289. describe( 'getSelectedContent', () => {
  290. it( 'should be decorated', () => {
  291. const spy = sinon.spy();
  292. const sel = new ModelSelection();
  293. data.on( 'getSelectedContent', spy );
  294. data.getSelectedContent( sel );
  295. expect( spy.calledOnce ).to.be.true;
  296. } );
  297. it( 'should return selected content', () => {
  298. schema.registerItem( 'paragraph', '$block' );
  299. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  300. const content = data.getSelectedContent( modelDocument.selection );
  301. expect( stringify( content ) ).to.equal( 'ob' );
  302. } );
  303. } );
  304. describe( 'hasContent', () => {
  305. let root;
  306. beforeEach( () => {
  307. schema.registerItem( 'paragraph', '$block' );
  308. schema.registerItem( 'div', '$block' );
  309. schema.allow( { name: '$block', inside: 'div' } );
  310. schema.registerItem( 'image' );
  311. schema.allow( { name: 'image', inside: 'div' } );
  312. schema.objects.add( 'image' );
  313. setData(
  314. modelDocument,
  315. '<div>' +
  316. '<paragraph></paragraph>' +
  317. '</div>' +
  318. '<paragraph>foo</paragraph>' +
  319. '<div>' +
  320. '<image></image>' +
  321. '</div>'
  322. );
  323. root = modelDocument.getRoot();
  324. } );
  325. it( 'should return true if given element has text node', () => {
  326. const pFoo = root.getChild( 1 );
  327. expect( data.hasContent( pFoo ) ).to.be.true;
  328. } );
  329. it( 'should return true if given element has element that is an object', () => {
  330. const divImg = root.getChild( 2 );
  331. expect( data.hasContent( divImg ) ).to.be.true;
  332. } );
  333. it( 'should return false if given element has no elements', () => {
  334. const pEmpty = root.getChild( 0 ).getChild( 0 );
  335. expect( data.hasContent( pEmpty ) ).to.be.false;
  336. } );
  337. it( 'should return false if given element has only elements that are not objects', () => {
  338. const divP = root.getChild( 0 );
  339. expect( data.hasContent( divP ) ).to.be.false;
  340. } );
  341. it( 'should return true if there is a text node in given range', () => {
  342. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 2 );
  343. expect( data.hasContent( range ) ).to.be.true;
  344. } );
  345. it( 'should return true if there is a part of text node in given range', () => {
  346. const pFoo = root.getChild( 1 );
  347. const range = ModelRange.createFromParentsAndOffsets( pFoo, 1, pFoo, 2 );
  348. expect( data.hasContent( range ) ).to.be.true;
  349. } );
  350. it( 'should return true if there is element that is an object in given range', () => {
  351. const divImg = root.getChild( 2 );
  352. const range = ModelRange.createFromParentsAndOffsets( divImg, 0, divImg, 1 );
  353. expect( data.hasContent( range ) ).to.be.true;
  354. } );
  355. it( 'should return false if range is collapsed', () => {
  356. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 1 );
  357. expect( data.hasContent( range ) ).to.be.false;
  358. } );
  359. it( 'should return false if range has only elements that are not objects', () => {
  360. const range = ModelRange.createFromParentsAndOffsets( root, 0, root, 1 );
  361. expect( data.hasContent( range ) ).to.be.false;
  362. } );
  363. } );
  364. } );