datacontroller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 set data to default main root', () => {
  112. schema.extend( '$text', { allowIn: '$root' } );
  113. data.set( 'foo' );
  114. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  115. } );
  116. it( 'should create a batch', () => {
  117. schema.extend( '$text', { allowIn: '$root' } );
  118. data.set( 'foo' );
  119. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  120. } );
  121. it( 'should cause firing change event', () => {
  122. const spy = sinon.spy();
  123. schema.extend( '$text', { allowIn: '$root' } );
  124. model.document.on( 'change', spy );
  125. data.set( 'foo' );
  126. expect( spy.calledOnce ).to.be.true;
  127. } );
  128. it( 'should get root name as a parameter', () => {
  129. schema.extend( '$text', { allowIn: '$root' } );
  130. data.set( 'foo', 'main' );
  131. data.set( 'Bar', 'title' );
  132. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  133. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  134. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  135. } );
  136. it( 'should parse given data before set in a context of correct root', () => {
  137. schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } );
  138. data.set( 'foo', 'main' );
  139. data.set( 'Bar', 'title' );
  140. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
  141. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  142. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  143. } );
  144. // This case was added when order of params was different and it really didn't work. Let's keep it
  145. // if anyone will ever try to change this.
  146. it( 'should allow setting empty data', () => {
  147. schema.extend( '$text', { allowIn: '$root' } );
  148. data.set( 'foo', 'title' );
  149. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  150. data.set( '', 'title' );
  151. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  152. } );
  153. } );
  154. describe( 'get()', () => {
  155. it( 'should get paragraph with text', () => {
  156. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  157. setData( model, '<paragraph>foo</paragraph>' );
  158. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  159. expect( data.get() ).to.equal( '<p>foo</p>' );
  160. } );
  161. it( 'should get empty paragraph', () => {
  162. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  163. setData( model, '<paragraph></paragraph>' );
  164. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  165. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  166. } );
  167. it( 'should get two paragraphs', () => {
  168. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  169. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  170. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  171. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  172. } );
  173. it( 'should get text directly in root', () => {
  174. schema.extend( '$text', { allowIn: '$root' } );
  175. setData( model, 'foo' );
  176. expect( data.get() ).to.equal( 'foo' );
  177. } );
  178. it( 'should get paragraphs without bold', () => {
  179. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  180. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  181. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  182. expect( data.get() ).to.equal( '<p>foobar</p>' );
  183. } );
  184. it( 'should get paragraphs with 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. downcastAttributeToElement( 'bold', { view: 'strong' } )( data.downcastDispatcher );
  189. expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
  190. } );
  191. it( 'should get root name as a parameter', () => {
  192. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  193. schema.extend( '$text', { allowIn: '$root' } );
  194. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  195. setData( model, 'Bar', { rootName: 'title' } );
  196. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  197. downcastAttributeToElement( 'bold', { view: 'strong' } )( data.downcastDispatcher );
  198. expect( data.get() ).to.equal( '<p>foo</p>' );
  199. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  200. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  201. } );
  202. } );
  203. describe( 'stringify()', () => {
  204. beforeEach( () => {
  205. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  206. schema.register( 'div' );
  207. schema.extend( '$block', { allowIn: 'div' } );
  208. schema.extend( 'div', { allowIn: '$root' } );
  209. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  210. } );
  211. it( 'should stringify a content of an element', () => {
  212. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  213. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  214. } );
  215. it( 'should stringify a content of a document fragment', () => {
  216. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  217. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  218. } );
  219. } );
  220. describe( 'toView()', () => {
  221. beforeEach( () => {
  222. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  223. schema.register( 'div' );
  224. schema.extend( '$block', { allowIn: 'div' } );
  225. schema.extend( 'div', { allowIn: '$root' } );
  226. downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
  227. } );
  228. it( 'should convert a content of an element', () => {
  229. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  230. const viewDocumentFragment = data.toView( modelElement );
  231. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  232. const viewElement = viewDocumentFragment.getChild( 0 );
  233. expect( viewElement.name ).to.equal( 'p' );
  234. expect( viewElement.childCount ).to.equal( 1 );
  235. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  236. } );
  237. it( 'should correctly convert document markers #1', () => {
  238. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  239. const modelRoot = model.document.getRoot();
  240. downcastMarkerToHighlight( { model: 'marker:a', view: { class: 'a' } } )( data.downcastDispatcher );
  241. model.change( writer => {
  242. writer.insert( modelElement, modelRoot, 0 );
  243. writer.setMarker( 'marker:a', Range.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 1 ), { usingOperation: true } );
  244. } );
  245. const viewDocumentFragment = data.toView( modelElement );
  246. const viewElement = viewDocumentFragment.getChild( 0 );
  247. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  248. } );
  249. it( 'should correctly convert document markers #2', () => {
  250. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  251. const modelRoot = model.document.getRoot();
  252. downcastMarkerToHighlight( { model: 'marker:a', view: { class: 'a' } } )( data.downcastDispatcher );
  253. downcastMarkerToHighlight( { model: 'marker:b', view: { class: 'b' } } )( data.downcastDispatcher );
  254. const modelP1 = modelElement.getChild( 0 );
  255. const modelP2 = modelElement.getChild( 1 );
  256. model.change( writer => {
  257. writer.insert( modelElement, modelRoot, 0 );
  258. writer.setMarker( 'marker:a', Range.createFromParentsAndOffsets( modelP1, 1, modelP1, 3 ), { usingOperation: true } );
  259. writer.setMarker( 'marker:b', Range.createFromParentsAndOffsets( modelP2, 0, modelP2, 2 ), { usingOperation: true } );
  260. } );
  261. const viewDocumentFragment = data.toView( modelP1 );
  262. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  263. } );
  264. it( 'should convert a document fragment', () => {
  265. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  266. const viewDocumentFragment = data.toView( modelDocumentFragment );
  267. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  268. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  269. const viewElement = viewDocumentFragment.getChild( 0 );
  270. expect( viewElement.name ).to.equal( 'p' );
  271. expect( viewElement.childCount ).to.equal( 1 );
  272. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  273. } );
  274. } );
  275. describe( 'destroy()', () => {
  276. it( 'should be there for you', () => {
  277. // Should not throw.
  278. data.destroy();
  279. expect( data ).to.respondTo( 'destroy' );
  280. } );
  281. } );
  282. } );