datacontroller.js 8.5 KB

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