datacontroller.js 14 KB

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