datacontroller.js 11 KB

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