8
0

datacontroller.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/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( 'insertContent', { batch, content, selection: modelDocument.selection } );
  36. expect( getData( modelDocument ) ).to.equal( '<paragraph>ax[]b</paragraph>' );
  37. } );
  38. } );
  39. describe( 'parse', () => {
  40. it( 'should set text', () => {
  41. schema.allow( { name: '$text', inside: '$root' } );
  42. const model = data.parse( '<p>foo<b>bar</b></p>' );
  43. expect( stringify( model ) ).to.equal( 'foobar' );
  44. } );
  45. it( 'should set paragraph', () => {
  46. schema.registerItem( 'paragraph', '$block' );
  47. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  48. const model = data.parse( '<p>foo<b>bar</b></p>' );
  49. expect( stringify( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
  50. } );
  51. it( 'should set two paragraphs', () => {
  52. schema.registerItem( 'paragraph', '$block' );
  53. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  54. const model = data.parse( '<p>foo</p><p>bar</p>' );
  55. expect( stringify( model ) ).to.equal(
  56. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  57. } );
  58. it( 'should set paragraphs with bold', () => {
  59. schema.registerItem( 'paragraph', '$block' );
  60. schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
  61. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  62. buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  63. const model = data.parse( '<p>foo<b>bar</b></p>' );
  64. expect( stringify( model ) ).to.equal(
  65. '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  66. } );
  67. it( 'should parse in the root context by default', () => {
  68. const model = data.parse( 'foo' );
  69. expect( stringify( model ) ).to.equal( '' );
  70. } );
  71. it( 'should accept parsing context', () => {
  72. const model = data.parse( 'foo', '$block' );
  73. expect( stringify( model ) ).to.equal( 'foo' );
  74. } );
  75. } );
  76. describe( 'set', () => {
  77. it( 'should set data to root', () => {
  78. schema.allow( { name: '$text', inside: '$root' } );
  79. data.set( 'foo' );
  80. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  81. } );
  82. it( 'should create a batch', () => {
  83. schema.allow( { name: '$text', inside: '$root' } );
  84. data.set( 'foo' );
  85. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  86. } );
  87. it( 'should fire #changesDone', () => {
  88. const spy = sinon.spy();
  89. schema.allow( { name: '$text', inside: '$root' } );
  90. modelDocument.on( 'changesDone', spy );
  91. data.set( 'foo' );
  92. expect( spy.calledOnce ).to.be.true;
  93. } );
  94. it( 'should get root name as a parameter', () => {
  95. schema.allow( { name: '$text', inside: '$root' } );
  96. data.set( 'foo', 'main' );
  97. data.set( 'Bar', 'title' );
  98. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  99. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  100. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  101. } );
  102. // This case was added when order of params was different and it really didn't work. Let's keep it
  103. // if anyone will ever try to change this.
  104. it( 'should allow setting empty data', () => {
  105. schema.allow( { name: '$text', inside: '$root' } );
  106. data.set( 'foo', 'title' );
  107. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  108. data.set( '', 'title' );
  109. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  110. } );
  111. } );
  112. describe( 'get', () => {
  113. it( 'should get paragraph with text', () => {
  114. modelDocument.schema.registerItem( 'paragraph', '$block' );
  115. setData( modelDocument, '<paragraph>foo</paragraph>' );
  116. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  117. expect( data.get() ).to.equal( '<p>foo</p>' );
  118. } );
  119. it( 'should get empty paragraph', () => {
  120. modelDocument.schema.registerItem( 'paragraph', '$block' );
  121. setData( modelDocument, '<paragraph></paragraph>' );
  122. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  123. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  124. } );
  125. it( 'should get two paragraphs', () => {
  126. modelDocument.schema.registerItem( 'paragraph', '$block' );
  127. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  128. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  129. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  130. } );
  131. it( 'should get text directly in root', () => {
  132. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  133. setData( modelDocument, 'foo' );
  134. expect( data.get() ).to.equal( 'foo' );
  135. } );
  136. it( 'should get paragraphs without bold', () => {
  137. modelDocument.schema.registerItem( 'paragraph', '$block' );
  138. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  139. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  140. expect( data.get() ).to.equal( '<p>foobar</p>' );
  141. } );
  142. it( 'should get paragraphs with bold', () => {
  143. modelDocument.schema.registerItem( 'paragraph', '$block' );
  144. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  145. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  146. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  147. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  148. } );
  149. it( 'should get root name as a parameter', () => {
  150. modelDocument.schema.registerItem( 'paragraph', '$block' );
  151. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  152. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  153. setData( modelDocument, 'Bar', { rootName: 'title' } );
  154. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  155. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  156. expect( data.get() ).to.equal( '<p>foo</p>' );
  157. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  158. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  159. } );
  160. } );
  161. describe( 'stringify', () => {
  162. it( 'should get paragraph with text', () => {
  163. modelDocument.schema.registerItem( 'paragraph', '$block' );
  164. modelDocument.schema.registerItem( 'div', '$block' );
  165. const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  166. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  167. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  168. } );
  169. } );
  170. describe( 'toView', () => {
  171. it( 'should get view element P with text', () => {
  172. modelDocument.schema.registerItem( 'paragraph', '$block' );
  173. modelDocument.schema.registerItem( 'div', '$block' );
  174. const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  175. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  176. const viewElement = data.toView( modelElement ).getChild( 0 );
  177. expect( viewElement.name ).to.equal( 'p' );
  178. expect( viewElement.childCount ).to.equal( 1 );
  179. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  180. } );
  181. } );
  182. describe( 'destroy', () => {
  183. it( 'should be there for you', () => {
  184. // Should not throw.
  185. data.destroy();
  186. expect( data ).to.respondTo( 'destroy' );
  187. } );
  188. } );
  189. describe( 'insertContent', () => {
  190. it( 'should fire the insertContent event', () => {
  191. const spy = sinon.spy();
  192. const batch = modelDocument.batch();
  193. const content = new ViewDocumentFragment( [ new ViewText( 'x' ) ] );
  194. data.on( 'insertContent', spy );
  195. data.insertContent( batch, modelDocument.selection, content );
  196. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( {
  197. batch: batch,
  198. selection: modelDocument.selection,
  199. content: content
  200. } );
  201. } );
  202. } );
  203. } );