datacontroller.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 BuildViewConverterFor from '/ckeditor5/engine/conversion/view-converter-builder.js';
  10. import BuildModelConverterFor from '/ckeditor5/engine/conversion/model-converter-builder.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. BuildViewConverterFor( 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. BuildViewConverterFor( 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. BuildViewConverterFor( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  52. BuildViewConverterFor( 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. setData( modelDocument, '<paragraph>foo</paragraph>' );
  97. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  98. expect( data.get() ).to.equal( '<p>foo</p>' );
  99. } );
  100. it( 'should get empty paragraph', () => {
  101. setData( modelDocument, '<paragraph></paragraph>' );
  102. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  103. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  104. } );
  105. it( 'should get two paragraphs', () => {
  106. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  107. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  108. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  109. } );
  110. it( 'should get text directly in root', () => {
  111. setData( modelDocument, 'foo' );
  112. expect( data.get() ).to.equal( 'foo' );
  113. } );
  114. it( 'should get paragraphs without bold', () => {
  115. setData( modelDocument, '<paragraph>foo<$text bold=true>bar</$text></paragraph>' );
  116. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  117. expect( data.get() ).to.equal( '<p>foobar</p>' );
  118. } );
  119. it( 'should get paragraphs with bold', () => {
  120. setData( modelDocument, '<paragraph>foo<$text bold=true>bar</$text></paragraph>' );
  121. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  122. BuildModelConverterFor( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  123. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  124. } );
  125. it( 'should get root name as a parameter', () => {
  126. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  127. setData( modelDocument, 'Bar', { rootName: 'title' } );
  128. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  129. BuildModelConverterFor( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  130. expect( data.get() ).to.equal( '<p>foo</p>' );
  131. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  132. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  133. } );
  134. } );
  135. describe( 'destroy', () => {
  136. it( 'should be there for you', () => {
  137. // Should not throw.
  138. data.destroy();
  139. expect( data ).to.respondTo( 'destroy' );
  140. } );
  141. } );
  142. } );