datacontroller.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 } from '/tests/engine/_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. } );
  58. describe( 'set', () => {
  59. it( 'should set data to root', () => {
  60. schema.allow( { name: '$text', inside: '$root' } );
  61. data.set( 'foo' );
  62. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  63. } );
  64. it( 'should create a batch', () => {
  65. schema.allow( { name: '$text', inside: '$root' } );
  66. data.set( 'foo' );
  67. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  68. } );
  69. it( 'should fire #changesDone', () => {
  70. const spy = sinon.spy();
  71. schema.allow( { name: '$text', inside: '$root' } );
  72. modelDocument.on( 'changesDone', spy );
  73. data.set( 'foo' );
  74. expect( spy.calledOnce ).to.be.true;
  75. } );
  76. it( 'should get root name as a parameter', () => {
  77. schema.allow( { name: '$text', inside: '$root' } );
  78. data.set( 'foo', 'main' );
  79. data.set( 'Bar', 'title' );
  80. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  81. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  82. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  83. } );
  84. // This case was added when order of params was different and it really didn't work. Let's keep it
  85. // if anyone will ever try to change this.
  86. it( 'should allow setting empty data', () => {
  87. schema.allow( { name: '$text', inside: '$root' } );
  88. data.set( 'foo', 'title' );
  89. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  90. data.set( '', 'title' );
  91. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  92. } );
  93. } );
  94. describe( 'get', () => {
  95. it( 'should get paragraph with text', () => {
  96. modelDocument.schema.registerItem( 'paragraph', '$block' );
  97. setData( modelDocument, '<paragraph>foo</paragraph>' );
  98. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  99. expect( data.get() ).to.equal( '<p>foo</p>' );
  100. } );
  101. it( 'should get empty paragraph', () => {
  102. modelDocument.schema.registerItem( 'paragraph', '$block' );
  103. setData( modelDocument, '<paragraph></paragraph>' );
  104. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  105. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  106. } );
  107. it( 'should get two paragraphs', () => {
  108. modelDocument.schema.registerItem( 'paragraph', '$block' );
  109. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  110. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  111. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  112. } );
  113. it( 'should get text directly in root', () => {
  114. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  115. setData( modelDocument, 'foo' );
  116. expect( data.get() ).to.equal( 'foo' );
  117. } );
  118. it( 'should get paragraphs without bold', () => {
  119. modelDocument.schema.registerItem( 'paragraph', '$block' );
  120. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  121. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  122. expect( data.get() ).to.equal( '<p>foobar</p>' );
  123. } );
  124. it( 'should get paragraphs with bold', () => {
  125. modelDocument.schema.registerItem( 'paragraph', '$block' );
  126. setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  127. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  128. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  129. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  130. } );
  131. it( 'should get root name as a parameter', () => {
  132. modelDocument.schema.registerItem( 'paragraph', '$block' );
  133. modelDocument.schema.allow( { name: '$text', inside: '$root' } );
  134. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  135. setData( modelDocument, 'Bar', { rootName: 'title' } );
  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</p>' );
  139. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  140. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  141. } );
  142. } );
  143. describe( 'destroy', () => {
  144. it( 'should be there for you', () => {
  145. // Should not throw.
  146. data.destroy();
  147. expect( data ).to.respondTo( 'destroy' );
  148. } );
  149. } );
  150. } );