datacontroller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 ViewDocumentFragment from '../../src/view/documentfragment';
  14. import { getData, setData, stringify, parse as parseModel } from '../../src/dev-utils/model';
  15. import { parse as parseView } from '../../src/dev-utils/view';
  16. import count from '@ckeditor/ckeditor5-utils/src/count';
  17. describe( 'DataController', () => {
  18. let modelDocument, htmlDataProcessor, data, schema;
  19. beforeEach( () => {
  20. modelDocument = new ModelDocument();
  21. modelDocument.createRoot();
  22. modelDocument.createRoot( '$root', 'title' );
  23. htmlDataProcessor = new HtmlDataProcessor();
  24. data = new DataController( modelDocument, htmlDataProcessor );
  25. schema = modelDocument.schema;
  26. } );
  27. describe( 'constructor()', () => {
  28. it( 'works without data processor', () => {
  29. const data = new DataController( modelDocument );
  30. expect( data.processor ).to.be.undefined;
  31. } );
  32. } );
  33. describe( 'parse', () => {
  34. it( 'should set text', () => {
  35. schema.allow( { name: '$text', inside: '$root' } );
  36. const model = data.parse( '<p>foo<b>bar</b></p>' );
  37. expect( model ).to.instanceof( ModelDocumentFragment );
  38. expect( stringify( model ) ).to.equal( 'foobar' );
  39. } );
  40. it( 'should set paragraph', () => {
  41. schema.registerItem( 'paragraph', '$block' );
  42. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  43. const model = data.parse( '<p>foo<b>bar</b></p>' );
  44. expect( model ).to.instanceof( ModelDocumentFragment );
  45. expect( stringify( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
  46. } );
  47. it( 'should set two paragraphs', () => {
  48. schema.registerItem( 'paragraph', '$block' );
  49. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  50. const model = data.parse( '<p>foo</p><p>bar</p>' );
  51. expect( model ).to.instanceof( ModelDocumentFragment );
  52. expect( stringify( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  53. } );
  54. it( 'should set paragraphs with bold', () => {
  55. schema.registerItem( 'paragraph', '$block' );
  56. schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
  57. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  58. buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  59. const model = data.parse( '<p>foo<b>bar</b></p>' );
  60. expect( model ).to.instanceof( ModelDocumentFragment );
  61. expect( stringify( model ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  62. } );
  63. it( 'should parse in the root context by default', () => {
  64. const model = data.parse( 'foo' );
  65. expect( stringify( model ) ).to.equal( '' );
  66. } );
  67. it( 'should accept parsing context', () => {
  68. const model = data.parse( 'foo', '$block' );
  69. expect( stringify( model ) ).to.equal( 'foo' );
  70. } );
  71. } );
  72. describe( 'toModel', () => {
  73. beforeEach( () => {
  74. schema.registerItem( 'paragraph', '$block' );
  75. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  76. } );
  77. it( 'should convert content of an element #1', () => {
  78. const viewElement = parseView( '<p>foo</p>' );
  79. const model = data.toModel( viewElement );
  80. expect( model ).to.instanceof( ModelDocumentFragment );
  81. expect( stringify( model ) ).to.equal( '<paragraph>foo</paragraph>' );
  82. } );
  83. it( 'should convert content of an element #2', () => {
  84. const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
  85. const model = data.toModel( viewFragment );
  86. expect( model ).to.be.instanceOf( ModelDocumentFragment );
  87. expect( stringify( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  88. } );
  89. it( 'should accept parsing context', () => {
  90. modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
  91. schema.registerItem( 'inlineRoot' );
  92. schema.allow( { name: '$text', inside: 'inlineRoot' } );
  93. const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
  94. // Model fragment in root.
  95. expect( stringify( data.toModel( viewFragment ) ) ).to.equal( '' );
  96. // Model fragment in inline root.
  97. expect( stringify( data.toModel( viewFragment, 'inlineRoot' ) ) ).to.equal( 'foo' );
  98. } );
  99. } );
  100. describe( 'set', () => {
  101. it( 'should set data to root', () => {
  102. schema.allow( { name: '$text', inside: '$root' } );
  103. data.set( 'foo' );
  104. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  105. } );
  106. it( 'should create a batch', () => {
  107. schema.allow( { name: '$text', inside: '$root' } );
  108. data.set( 'foo' );
  109. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  110. } );
  111. it( 'should fire #changesDone', () => {
  112. const spy = sinon.spy();
  113. schema.allow( { name: '$text', inside: '$root' } );
  114. modelDocument.on( 'changesDone', spy );
  115. data.set( 'foo' );
  116. expect( spy.calledOnce ).to.be.true;
  117. } );
  118. it( 'should get root name as a parameter', () => {
  119. schema.allow( { name: '$text', inside: '$root' } );
  120. data.set( 'foo', 'main' );
  121. data.set( 'Bar', 'title' );
  122. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  123. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  124. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  125. } );
  126. // This case was added when order of params was different and it really didn't work. Let's keep it
  127. // if anyone will ever try to change this.
  128. it( 'should allow setting empty data', () => {
  129. schema.allow( { name: '$text', inside: '$root' } );
  130. data.set( 'foo', 'title' );
  131. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  132. data.set( '', 'title' );
  133. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  134. } );
  135. } );
  136. describe( 'get', () => {
  137. it( 'should get paragraph with text', () => {
  138. modelDocument.schema.registerItem( 'paragraph', '$block' );
  139. setData( modelDocument, '<paragraph>foo</paragraph>' );
  140. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  141. expect( data.get() ).to.equal( '<p>foo</p>' );
  142. } );
  143. it( 'should get empty paragraph', () => {
  144. modelDocument.schema.registerItem( 'paragraph', '$block' );
  145. setData( modelDocument, '<paragraph></paragraph>' );
  146. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  147. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  148. } );
  149. it( 'should get two paragraphs', () => {
  150. modelDocument.schema.registerItem( 'paragraph', '$block' );
  151. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  152. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  153. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  154. } );
  155. it( 'should get text directly in root', () => {
  156. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  157. setData( modelDocument, 'foo' );
  158. expect( data.get() ).to.equal( 'foo' );
  159. } );
  160. it( 'should get paragraphs without bold', () => {
  161. modelDocument.schema.registerItem( 'paragraph', '$block' );
  162. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  163. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  164. expect( data.get() ).to.equal( '<p>foobar</p>' );
  165. } );
  166. it( 'should get paragraphs with bold', () => {
  167. modelDocument.schema.registerItem( 'paragraph', '$block' );
  168. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  169. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  170. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  171. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  172. } );
  173. it( 'should get root name as a parameter', () => {
  174. modelDocument.schema.registerItem( 'paragraph', '$block' );
  175. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  176. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  177. setData( modelDocument, 'Bar', { rootName: 'title' } );
  178. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  179. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  180. expect( data.get() ).to.equal( '<p>foo</p>' );
  181. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  182. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  183. } );
  184. } );
  185. describe( 'stringify', () => {
  186. beforeEach( () => {
  187. modelDocument.schema.registerItem( 'paragraph', '$block' );
  188. modelDocument.schema.registerItem( 'div' );
  189. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  190. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  191. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  192. } );
  193. it( 'should stringify a content of an element', () => {
  194. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  195. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  196. } );
  197. it( 'should stringify a content of a document fragment', () => {
  198. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  199. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  200. } );
  201. } );
  202. describe( 'toView', () => {
  203. beforeEach( () => {
  204. modelDocument.schema.registerItem( 'paragraph', '$block' );
  205. modelDocument.schema.registerItem( 'div' );
  206. modelDocument.schema.allow( { name: '$block', inside: 'div' } );
  207. modelDocument.schema.allow( { name: 'div', inside: '$root' } );
  208. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  209. } );
  210. it( 'should convert a content of an element', () => {
  211. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  212. const viewDocumentFragment = data.toView( modelElement );
  213. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  214. const viewElement = viewDocumentFragment.getChild( 0 );
  215. expect( viewElement.name ).to.equal( 'p' );
  216. expect( viewElement.childCount ).to.equal( 1 );
  217. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  218. } );
  219. it( 'should convert a document fragment', () => {
  220. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
  221. const viewDocumentFragment = data.toView( modelDocumentFragment );
  222. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  223. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  224. const viewElement = viewDocumentFragment.getChild( 0 );
  225. expect( viewElement.name ).to.equal( 'p' );
  226. expect( viewElement.childCount ).to.equal( 1 );
  227. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  228. } );
  229. } );
  230. describe( 'destroy', () => {
  231. it( 'should be there for you', () => {
  232. // Should not throw.
  233. data.destroy();
  234. expect( data ).to.respondTo( 'destroy' );
  235. } );
  236. } );
  237. describe( 'insertContent', () => {
  238. it( 'should be decorated', () => {
  239. schema.allow( { name: '$text', inside: '$root' } ); // To surpress warnings.
  240. const spy = sinon.spy();
  241. data.on( 'insertContent', spy );
  242. data.insertContent( new ModelText( 'a' ), modelDocument.selection );
  243. expect( spy.calledOnce ).to.be.true;
  244. } );
  245. it( 'should insert content (item)', () => {
  246. schema.registerItem( 'paragraph', '$block' );
  247. setData( modelDocument, '<paragraph>fo[]ar</paragraph>' );
  248. data.insertContent( new ModelText( 'ob' ), modelDocument.selection );
  249. expect( getData( modelDocument ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  250. } );
  251. it( 'should insert content (document fragment)', () => {
  252. schema.registerItem( 'paragraph', '$block' );
  253. setData( modelDocument, '<paragraph>fo[]ar</paragraph>' );
  254. data.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ), modelDocument.selection );
  255. expect( getData( modelDocument ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  256. } );
  257. } );
  258. describe( 'deleteContent', () => {
  259. it( 'should be decorated', () => {
  260. const spy = sinon.spy();
  261. data.on( 'deleteContent', spy );
  262. data.deleteContent( modelDocument.selection );
  263. expect( spy.calledOnce ).to.be.true;
  264. } );
  265. it( 'should delete selected content', () => {
  266. schema.registerItem( 'paragraph', '$block' );
  267. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  268. data.deleteContent( modelDocument.selection, modelDocument.batch() );
  269. expect( getData( modelDocument ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  270. } );
  271. } );
  272. describe( 'modifySelection', () => {
  273. it( 'should be decorated', () => {
  274. schema.registerItem( 'paragraph', '$block' );
  275. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  276. const spy = sinon.spy();
  277. data.on( 'modifySelection', spy );
  278. data.modifySelection( modelDocument.selection );
  279. expect( spy.calledOnce ).to.be.true;
  280. } );
  281. it( 'should modify a selection', () => {
  282. schema.registerItem( 'paragraph', '$block' );
  283. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  284. data.modifySelection( modelDocument.selection, { direction: 'backward' } );
  285. expect( getData( modelDocument ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  286. } );
  287. } );
  288. describe( 'getSelectedContent', () => {
  289. it( 'should be decorated', () => {
  290. const spy = sinon.spy();
  291. const sel = new ModelSelection();
  292. data.on( 'getSelectedContent', spy );
  293. data.getSelectedContent( sel );
  294. expect( spy.calledOnce ).to.be.true;
  295. } );
  296. it( 'should return selected content', () => {
  297. schema.registerItem( 'paragraph', '$block' );
  298. setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
  299. const content = data.getSelectedContent( modelDocument.selection );
  300. expect( stringify( content ) ).to.equal( 'ob' );
  301. } );
  302. } );
  303. } );