8
0

datacontroller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Range from '../../src/model/range';
  7. import DataController from '../../src/controller/datacontroller';
  8. import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
  9. import buildViewConverter from '../../src/conversion/buildviewconverter';
  10. import buildModelConverter from '../../src/conversion/buildmodelconverter';
  11. import ModelDocumentFragment from '../../src/model/documentfragment';
  12. import ViewDocumentFragment from '../../src/view/documentfragment';
  13. import { getData, setData, stringify, parse as parseModel } from '../../src/dev-utils/model';
  14. import { parse as parseView, stringify as stringifyView } from '../../src/dev-utils/view';
  15. import count from '@ckeditor/ckeditor5-utils/src/count';
  16. describe( 'DataController', () => {
  17. let model, modelDocument, htmlDataProcessor, data, schema;
  18. beforeEach( () => {
  19. model = new Model();
  20. modelDocument = model.document;
  21. modelDocument.createRoot();
  22. modelDocument.createRoot( '$root', 'title' );
  23. htmlDataProcessor = new HtmlDataProcessor();
  24. data = new DataController( model, htmlDataProcessor );
  25. schema = model.schema;
  26. } );
  27. describe( 'constructor()', () => {
  28. it( 'works without data processor', () => {
  29. const data = new DataController( model );
  30. expect( data.processor ).to.be.undefined;
  31. } );
  32. } );
  33. describe( 'parse()', () => {
  34. it( 'should set text', () => {
  35. schema.extend( '$text', { allowIn: '$root' } );
  36. const output = data.parse( '<p>foo<b>bar</b></p>' );
  37. expect( output ).to.instanceof( ModelDocumentFragment );
  38. expect( stringify( output ) ).to.equal( 'foobar' );
  39. } );
  40. it( 'should set paragraph', () => {
  41. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  42. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  43. const output = data.parse( '<p>foo<b>bar</b></p>' );
  44. expect( output ).to.instanceof( ModelDocumentFragment );
  45. expect( stringify( output ) ).to.equal( '<paragraph>foobar</paragraph>' );
  46. } );
  47. it( 'should set two paragraphs', () => {
  48. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  49. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  50. const output = data.parse( '<p>foo</p><p>bar</p>' );
  51. expect( output ).to.instanceof( ModelDocumentFragment );
  52. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  53. } );
  54. it( 'should set paragraphs with bold', () => {
  55. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  56. schema.extend( '$text', {
  57. allowAttributes: [ 'bold' ]
  58. } );
  59. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  60. buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  61. const output = data.parse( '<p>foo<b>bar</b></p>' );
  62. expect( output ).to.instanceof( ModelDocumentFragment );
  63. expect( stringify( output ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  64. } );
  65. it( 'should parse in the root context by default', () => {
  66. const output = data.parse( 'foo' );
  67. expect( stringify( output ) ).to.equal( '' );
  68. } );
  69. it( 'should accept parsing context', () => {
  70. const output = data.parse( 'foo', [ '$block' ] );
  71. expect( stringify( output ) ).to.equal( 'foo' );
  72. } );
  73. } );
  74. describe( 'toModel()', () => {
  75. beforeEach( () => {
  76. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  77. buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  78. } );
  79. it( 'should convert content of an element #1', () => {
  80. const viewElement = parseView( '<p>foo</p>' );
  81. const output = data.toModel( viewElement );
  82. expect( output ).to.instanceof( ModelDocumentFragment );
  83. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph>' );
  84. } );
  85. it( 'should convert content of an element #2', () => {
  86. const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
  87. const output = data.toModel( viewFragment );
  88. expect( output ).to.be.instanceOf( ModelDocumentFragment );
  89. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  90. } );
  91. it( 'should accept parsing context', () => {
  92. modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
  93. schema.register( 'inlineRoot' );
  94. schema.extend( '$text', { allowIn: 'inlineRoot' } );
  95. const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
  96. // Model fragment in root.
  97. expect( stringify( data.toModel( viewFragment ) ) ).to.equal( '' );
  98. // Model fragment in inline root.
  99. expect( stringify( data.toModel( viewFragment, [ 'inlineRoot' ] ) ) ).to.equal( 'foo' );
  100. } );
  101. } );
  102. describe( 'set()', () => {
  103. it( 'should set data to root', () => {
  104. schema.extend( '$text', { allowIn: '$root' } );
  105. data.set( 'foo' );
  106. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  107. } );
  108. it( 'should create a batch', () => {
  109. schema.extend( '$text', { allowIn: '$root' } );
  110. data.set( 'foo' );
  111. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  112. } );
  113. it( 'should cause firing change event', () => {
  114. const spy = sinon.spy();
  115. schema.extend( '$text', { allowIn: '$root' } );
  116. model.document.on( 'change', spy );
  117. data.set( 'foo' );
  118. expect( spy.calledOnce ).to.be.true;
  119. } );
  120. it( 'should get root name as a parameter', () => {
  121. schema.extend( '$text', { allowIn: '$root' } );
  122. data.set( 'foo', 'main' );
  123. data.set( 'Bar', 'title' );
  124. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  125. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  126. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  127. } );
  128. // This case was added when order of params was different and it really didn't work. Let's keep it
  129. // if anyone will ever try to change this.
  130. it( 'should allow setting empty data', () => {
  131. schema.extend( '$text', { allowIn: '$root' } );
  132. data.set( 'foo', 'title' );
  133. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  134. data.set( '', 'title' );
  135. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  136. } );
  137. } );
  138. describe( 'get()', () => {
  139. it( 'should get paragraph with text', () => {
  140. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  141. setData( model, '<paragraph>foo</paragraph>' );
  142. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  143. expect( data.get() ).to.equal( '<p>foo</p>' );
  144. } );
  145. it( 'should get empty paragraph', () => {
  146. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  147. setData( model, '<paragraph></paragraph>' );
  148. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  149. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  150. } );
  151. it( 'should get two paragraphs', () => {
  152. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  153. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  154. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  155. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  156. } );
  157. it( 'should get text directly in root', () => {
  158. schema.extend( '$text', { allowIn: '$root' } );
  159. setData( model, 'foo' );
  160. expect( data.get() ).to.equal( 'foo' );
  161. } );
  162. it( 'should get paragraphs without bold', () => {
  163. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  164. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  165. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  166. expect( data.get() ).to.equal( '<p>foobar</p>' );
  167. } );
  168. it( 'should get paragraphs with bold', () => {
  169. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  170. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  171. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  172. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  173. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  174. } );
  175. it( 'should get root name as a parameter', () => {
  176. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  177. schema.extend( '$text', { allowIn: '$root' } );
  178. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  179. setData( model, 'Bar', { rootName: 'title' } );
  180. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  181. buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  182. expect( data.get() ).to.equal( '<p>foo</p>' );
  183. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  184. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  185. } );
  186. } );
  187. describe( 'stringify()', () => {
  188. beforeEach( () => {
  189. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  190. schema.register( 'div' );
  191. schema.extend( '$block', { allowIn: 'div' } );
  192. schema.extend( 'div', { allowIn: '$root' } );
  193. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  194. } );
  195. it( 'should stringify a content of an element', () => {
  196. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  197. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  198. } );
  199. it( 'should stringify a content of a document fragment', () => {
  200. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  201. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  202. } );
  203. } );
  204. describe( 'toView()', () => {
  205. beforeEach( () => {
  206. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  207. schema.register( 'div' );
  208. schema.extend( '$block', { allowIn: 'div' } );
  209. schema.extend( 'div', { allowIn: '$root' } );
  210. buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  211. } );
  212. it( 'should convert a content of an element', () => {
  213. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  214. const viewDocumentFragment = data.toView( modelElement );
  215. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  216. const viewElement = viewDocumentFragment.getChild( 0 );
  217. expect( viewElement.name ).to.equal( 'p' );
  218. expect( viewElement.childCount ).to.equal( 1 );
  219. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  220. } );
  221. it( 'should correctly convert document markers #1', () => {
  222. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  223. const modelRoot = model.document.getRoot();
  224. buildModelConverter().for( data.modelToView ).fromMarker( 'marker:a' ).toHighlight( { class: 'a' } );
  225. model.change( writer => {
  226. writer.insert( modelElement, modelRoot, 0 );
  227. writer.setMarker( 'marker:a', Range.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 1 ) );
  228. } );
  229. const viewDocumentFragment = data.toView( modelElement );
  230. const viewElement = viewDocumentFragment.getChild( 0 );
  231. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  232. } );
  233. it( 'should correctly convert document markers #2', () => {
  234. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  235. const modelRoot = model.document.getRoot();
  236. buildModelConverter().for( data.modelToView ).fromMarker( 'marker:a' ).toHighlight( { class: 'a' } );
  237. buildModelConverter().for( data.modelToView ).fromMarker( 'marker:b' ).toHighlight( { class: 'b' } );
  238. const modelP1 = modelElement.getChild( 0 );
  239. const modelP2 = modelElement.getChild( 1 );
  240. model.change( writer => {
  241. writer.insert( modelElement, modelRoot, 0 );
  242. writer.setMarker( 'marker:a', Range.createFromParentsAndOffsets( modelP1, 1, modelP1, 3 ) );
  243. writer.setMarker( 'marker:b', Range.createFromParentsAndOffsets( modelP2, 0, modelP2, 2 ) );
  244. } );
  245. const viewDocumentFragment = data.toView( modelP1 );
  246. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  247. } );
  248. it( 'should convert a document fragment', () => {
  249. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  250. const viewDocumentFragment = data.toView( modelDocumentFragment );
  251. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  252. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  253. const viewElement = viewDocumentFragment.getChild( 0 );
  254. expect( viewElement.name ).to.equal( 'p' );
  255. expect( viewElement.childCount ).to.equal( 1 );
  256. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  257. } );
  258. } );
  259. describe( 'destroy()', () => {
  260. it( 'should be there for you', () => {
  261. // Should not throw.
  262. data.destroy();
  263. expect( data ).to.respondTo( 'destroy' );
  264. } );
  265. } );
  266. } );