8
0

datacontroller.js 6.6 KB

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