datacontroller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 { getData, setData, stringify, parse } from '/ckeditor5/engine/dev-utils/model.js';
  12. import count from '/ckeditor5/utils/count.js';
  13. describe( 'DataController', () => {
  14. let modelDocument, htmlDataProcessor, data, schema;
  15. beforeEach( () => {
  16. modelDocument = new ModelDocument();
  17. modelDocument.createRoot();
  18. modelDocument.createRoot( '$root', 'title' );
  19. htmlDataProcessor = new HtmlDataProcessor();
  20. data = new DataController( modelDocument, htmlDataProcessor );
  21. schema = modelDocument.schema;
  22. } );
  23. describe( 'constructor', () => {
  24. it( 'works without data processor', () => {
  25. const data = new DataController( modelDocument );
  26. expect( data.processor ).to.be.undefined;
  27. } );
  28. } );
  29. describe( 'parse', () => {
  30. it( 'should set text', () => {
  31. schema.allow( { name: '$text', inside: '$root' } );
  32. const model = data.parse( '<p>foo<b>bar</b></p>' );
  33. expect( stringify( model ) ).to.equal( 'foobar' );
  34. } );
  35. it( 'should set paragraph', () => {
  36. schema.registerItem( 'paragraph', '$block' );
  37. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  38. const model = data.parse( '<p>foo<b>bar</b></p>' );
  39. expect( stringify( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
  40. } );
  41. it( 'should set two paragraphs', () => {
  42. schema.registerItem( 'paragraph', '$block' );
  43. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  44. const model = data.parse( '<p>foo</p><p>bar</p>' );
  45. expect( stringify( model ) ).to.equal(
  46. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  47. } );
  48. it( 'should set paragraphs with bold', () => {
  49. schema.registerItem( 'paragraph', '$block' );
  50. schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
  51. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  52. buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  53. const model = data.parse( '<p>foo<b>bar</b></p>' );
  54. expect( stringify( model ) ).to.equal(
  55. '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  56. } );
  57. it( 'should parse in the root context by default', () => {
  58. const model = data.parse( 'foo' );
  59. expect( stringify( model ) ).to.equal( '' );
  60. } );
  61. it( 'should accept parsing context', () => {
  62. const model = data.parse( 'foo', '$block' );
  63. expect( stringify( model ) ).to.equal( 'foo' );
  64. } );
  65. } );
  66. describe( 'set', () => {
  67. it( 'should set data to root', () => {
  68. schema.allow( { name: '$text', inside: '$root' } );
  69. data.set( 'foo' );
  70. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  71. } );
  72. it( 'should create a batch', () => {
  73. schema.allow( { name: '$text', inside: '$root' } );
  74. data.set( 'foo' );
  75. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  76. } );
  77. it( 'should fire #changesDone', () => {
  78. const spy = sinon.spy();
  79. schema.allow( { name: '$text', inside: '$root' } );
  80. modelDocument.on( 'changesDone', spy );
  81. data.set( 'foo' );
  82. expect( spy.calledOnce ).to.be.true;
  83. } );
  84. it( 'should get root name as a parameter', () => {
  85. schema.allow( { name: '$text', inside: '$root' } );
  86. data.set( 'foo', 'main' );
  87. data.set( 'Bar', 'title' );
  88. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  89. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  90. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  91. } );
  92. // This case was added when order of params was different and it really didn't work. Let's keep it
  93. // if anyone will ever try to change this.
  94. it( 'should allow setting empty data', () => {
  95. schema.allow( { name: '$text', inside: '$root' } );
  96. data.set( 'foo', 'title' );
  97. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  98. data.set( '', 'title' );
  99. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  100. } );
  101. } );
  102. describe( 'get', () => {
  103. it( 'should get paragraph with text', () => {
  104. modelDocument.schema.registerItem( 'paragraph', '$block' );
  105. setData( modelDocument, '<paragraph>foo</paragraph>' );
  106. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  107. expect( data.get() ).to.equal( '<p>foo</p>' );
  108. } );
  109. it( 'should get empty paragraph', () => {
  110. modelDocument.schema.registerItem( 'paragraph', '$block' );
  111. setData( modelDocument, '<paragraph></paragraph>' );
  112. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  113. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  114. } );
  115. it( 'should get two paragraphs', () => {
  116. modelDocument.schema.registerItem( 'paragraph', '$block' );
  117. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  118. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  119. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  120. } );
  121. it( 'should get text directly in root', () => {
  122. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  123. setData( modelDocument, 'foo' );
  124. expect( data.get() ).to.equal( 'foo' );
  125. } );
  126. it( 'should get paragraphs without bold', () => {
  127. modelDocument.schema.registerItem( 'paragraph', '$block' );
  128. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  129. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  130. expect( data.get() ).to.equal( '<p>foobar</p>' );
  131. } );
  132. it( 'should get paragraphs with bold', () => {
  133. modelDocument.schema.registerItem( 'paragraph', '$block' );
  134. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  135. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  136. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  137. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  138. } );
  139. it( 'should get root name as a parameter', () => {
  140. modelDocument.schema.registerItem( 'paragraph', '$block' );
  141. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  142. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  143. setData( modelDocument, 'Bar', { rootName: 'title' } );
  144. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  145. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  146. expect( data.get() ).to.equal( '<p>foo</p>' );
  147. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  148. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  149. } );
  150. } );
  151. describe( 'stringify', () => {
  152. it( 'should get paragraph with text', () => {
  153. modelDocument.schema.registerItem( 'paragraph', '$block' );
  154. modelDocument.schema.registerItem( 'div', '$block' );
  155. const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  156. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  157. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  158. } );
  159. } );
  160. describe( 'toView', () => {
  161. it( 'should get view element P with text', () => {
  162. modelDocument.schema.registerItem( 'paragraph', '$block' );
  163. modelDocument.schema.registerItem( 'div', '$block' );
  164. const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
  165. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  166. const viewElement = data.toView( modelElement ).getChild( 0 );
  167. expect( viewElement.name ).to.equal( 'p' );
  168. expect( viewElement.childCount ).to.equal( 1 );
  169. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  170. } );
  171. } );
  172. describe( 'destroy', () => {
  173. it( 'should be there for you', () => {
  174. // Should not throw.
  175. data.destroy();
  176. expect( data ).to.respondTo( 'destroy' );
  177. } );
  178. } );
  179. } );