datacontroller.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view */
  6. import ModelDocument from 'ckeditor5/engine/model/document.js';
  7. import DataController from 'ckeditor5/engine/controller/datacontroller.js';
  8. import HtmlDataProcessor from 'ckeditor5/engine/dataprocessor/htmldataprocessor.js';
  9. import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
  10. import buildModelConverter from 'ckeditor5/engine/conversion/buildmodelconverter.js';
  11. import ViewDocumentFragment from 'ckeditor5/engine/view/documentfragment.js';
  12. import ViewText from 'ckeditor5/engine/view/text.js';
  13. import { getData, setData, stringify, parse } from 'ckeditor5/engine/dev-utils/model.js';
  14. import count from 'ckeditor5/utils/count.js';
  15. describe( 'DataController', () => {
  16. let modelDocument, htmlDataProcessor, data, schema;
  17. beforeEach( () => {
  18. modelDocument = new ModelDocument();
  19. modelDocument.createRoot();
  20. modelDocument.createRoot( '$root', 'title' );
  21. htmlDataProcessor = new HtmlDataProcessor();
  22. data = new DataController( modelDocument, htmlDataProcessor );
  23. schema = modelDocument.schema;
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'works without data processor', () => {
  27. const data = new DataController( modelDocument );
  28. expect( data.processor ).to.be.undefined;
  29. } );
  30. it( 'should add insertContent listener', () => {
  31. const batch = modelDocument.batch();
  32. const content = new ViewDocumentFragment( [ new ViewText( 'x' ) ] );
  33. schema.registerItem( 'paragraph', '$block' );
  34. setData( modelDocument, '<paragraph>a[]b</paragraph>' );
  35. data.fire( 'insert', { content, selection: modelDocument.selection, batch } );
  36. expect( getData( modelDocument ) ).to.equal( '<paragraph>ax[]b</paragraph>' );
  37. expect( batch.deltas.length ).to.be.above( 0 );
  38. } );
  39. } );
  40. describe( 'parse', () => {
  41. it( 'should set text', () => {
  42. schema.allow( { name: '$text', inside: '$root' } );
  43. const model = data.parse( '<p>foo<b>bar</b></p>' );
  44. expect( stringify( model ) ).to.equal( 'foobar' );
  45. } );
  46. it( 'should set paragraph', () => {
  47. schema.registerItem( 'paragraph', '$block' );
  48. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  49. const model = data.parse( '<p>foo<b>bar</b></p>' );
  50. expect( stringify( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
  51. } );
  52. it( 'should set two paragraphs', () => {
  53. schema.registerItem( 'paragraph', '$block' );
  54. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  55. const model = data.parse( '<p>foo</p><p>bar</p>' );
  56. expect( stringify( model ) ).to.equal(
  57. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  58. } );
  59. it( 'should set paragraphs with bold', () => {
  60. schema.registerItem( 'paragraph', '$block' );
  61. schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
  62. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  63. buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  64. const model = data.parse( '<p>foo<b>bar</b></p>' );
  65. expect( stringify( model ) ).to.equal(
  66. '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  67. } );
  68. it( 'should parse in the root context by default', () => {
  69. const model = data.parse( 'foo' );
  70. expect( stringify( model ) ).to.equal( '' );
  71. } );
  72. it( 'should accept parsing context', () => {
  73. const model = data.parse( 'foo', '$block' );
  74. expect( stringify( model ) ).to.equal( 'foo' );
  75. } );
  76. } );
  77. describe( 'set', () => {
  78. it( 'should set data to root', () => {
  79. schema.allow( { name: '$text', inside: '$root' } );
  80. data.set( 'foo' );
  81. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  82. } );
  83. it( 'should create a batch', () => {
  84. schema.allow( { name: '$text', inside: '$root' } );
  85. data.set( 'foo' );
  86. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  87. } );
  88. it( 'should fire #changesDone', () => {
  89. const spy = sinon.spy();
  90. schema.allow( { name: '$text', inside: '$root' } );
  91. modelDocument.on( 'changesDone', spy );
  92. data.set( 'foo' );
  93. expect( spy.calledOnce ).to.be.true;
  94. } );
  95. it( 'should get root name as a parameter', () => {
  96. schema.allow( { name: '$text', inside: '$root' } );
  97. data.set( 'foo', 'main' );
  98. data.set( 'Bar', 'title' );
  99. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  100. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  101. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  102. } );
  103. // This case was added when order of params was different and it really didn't work. Let's keep it
  104. // if anyone will ever try to change this.
  105. it( 'should allow setting empty data', () => {
  106. schema.allow( { name: '$text', inside: '$root' } );
  107. data.set( 'foo', 'title' );
  108. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  109. data.set( '', 'title' );
  110. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  111. } );
  112. } );
  113. describe( 'get', () => {
  114. it( 'should get paragraph with text', () => {
  115. modelDocument.schema.registerItem( 'paragraph', '$block' );
  116. setData( modelDocument, '<paragraph>foo</paragraph>' );
  117. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  118. expect( data.get() ).to.equal( '<p>foo</p>' );
  119. } );
  120. it( 'should get empty paragraph', () => {
  121. modelDocument.schema.registerItem( 'paragraph', '$block' );
  122. setData( modelDocument, '<paragraph></paragraph>' );
  123. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  124. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  125. } );
  126. it( 'should get two paragraphs', () => {
  127. modelDocument.schema.registerItem( 'paragraph', '$block' );
  128. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  129. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  130. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  131. } );
  132. it( 'should get text directly in root', () => {
  133. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  134. setData( modelDocument, 'foo' );
  135. expect( data.get() ).to.equal( 'foo' );
  136. } );
  137. it( 'should get paragraphs without bold', () => {
  138. modelDocument.schema.registerItem( 'paragraph', '$block' );
  139. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  140. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  141. expect( data.get() ).to.equal( '<p>foobar</p>' );
  142. } );
  143. it( 'should get paragraphs with bold', () => {
  144. modelDocument.schema.registerItem( 'paragraph', '$block' );
  145. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  146. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  147. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  148. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  149. } );
  150. it( 'should get root name as a parameter', () => {
  151. modelDocument.schema.registerItem( 'paragraph', '$block' );
  152. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  153. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  154. setData( modelDocument, 'Bar', { rootName: 'title' } );
  155. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  156. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  157. expect( data.get() ).to.equal( '<p>foo</p>' );
  158. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  159. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  160. } );
  161. } );
  162. describe( 'stringify', () => {
  163. it( 'should get paragraph with text', () => {
  164. modelDocument.schema.registerItem( 'paragraph', '$block' );
  165. modelDocument.schema.registerItem( 'div', '$block' );
  166. const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  167. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  168. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  169. } );
  170. } );
  171. describe( 'toView', () => {
  172. it( 'should get view element P with text', () => {
  173. modelDocument.schema.registerItem( 'paragraph', '$block' );
  174. modelDocument.schema.registerItem( 'div', '$block' );
  175. const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  176. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  177. const viewElement = data.toView( modelElement ).getChild( 0 );
  178. expect( viewElement.name ).to.equal( 'p' );
  179. expect( viewElement.childCount ).to.equal( 1 );
  180. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  181. } );
  182. } );
  183. describe( 'destroy', () => {
  184. it( 'should be there for you', () => {
  185. // Should not throw.
  186. data.destroy();
  187. expect( data ).to.respondTo( 'destroy' );
  188. } );
  189. } );
  190. describe( 'insert', () => {
  191. it( 'should fire the insert event', () => {
  192. const spy = sinon.spy();
  193. const content = new ViewDocumentFragment( [ new ViewText( 'x' ) ] );
  194. const batch = modelDocument.batch();
  195. data.on( 'insert', spy );
  196. data.insert( content, modelDocument.selection, batch );
  197. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( {
  198. batch: batch,
  199. selection: modelDocument.selection,
  200. content: content
  201. } );
  202. } );
  203. } );
  204. } );