datacontroller.js 14 KB

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