datacontroller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 ModelRange from '../../src/model/range';
  7. import ViewRange from '../../src/view/range';
  8. import DataController from '../../src/controller/datacontroller';
  9. import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  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. import UpcastHelpers from '../../src/conversion/upcast-converters';
  17. import DowncastHelpers from '../../src/conversion/downcast-converters';
  18. describe( 'DataController', () => {
  19. let model, modelDocument, htmlDataProcessor, data, schema, upcastHelpers, downcastHelpers;
  20. beforeEach( () => {
  21. model = new Model();
  22. schema = model.schema;
  23. modelDocument = model.document;
  24. modelDocument.createRoot();
  25. modelDocument.createRoot( '$title', 'title' );
  26. schema.register( '$title', { inheritAllFrom: '$root' } );
  27. htmlDataProcessor = new HtmlDataProcessor();
  28. data = new DataController( model, htmlDataProcessor );
  29. upcastHelpers = new UpcastHelpers( data.upcastDispatcher );
  30. downcastHelpers = new DowncastHelpers( data.downcastDispatcher );
  31. } );
  32. describe( 'constructor()', () => {
  33. it( 'works without data processor', () => {
  34. const data = new DataController( model );
  35. expect( data.processor ).to.be.undefined;
  36. } );
  37. } );
  38. describe( 'parse()', () => {
  39. it( 'should set text', () => {
  40. schema.extend( '$text', { allowIn: '$root' } );
  41. const output = data.parse( '<p>foo<b>bar</b></p>' );
  42. expect( output ).to.instanceof( ModelDocumentFragment );
  43. expect( stringify( output ) ).to.equal( 'foobar' );
  44. } );
  45. it( 'should set paragraph', () => {
  46. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  47. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  48. const output = data.parse( '<p>foo<b>bar</b></p>' );
  49. expect( output ).to.instanceof( ModelDocumentFragment );
  50. expect( stringify( output ) ).to.equal( '<paragraph>foobar</paragraph>' );
  51. } );
  52. it( 'should set two paragraphs', () => {
  53. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  54. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  55. const output = data.parse( '<p>foo</p><p>bar</p>' );
  56. expect( output ).to.instanceof( ModelDocumentFragment );
  57. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  58. } );
  59. it( 'should set paragraphs with bold', () => {
  60. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  61. schema.extend( '$text', {
  62. allowAttributes: [ 'bold' ]
  63. } );
  64. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  65. upcastHelpers.elementToAttribute( { view: 'strong', model: 'bold' } );
  66. const output = data.parse( '<p>foo<strong>bar</strong></p>' );
  67. expect( output ).to.instanceof( ModelDocumentFragment );
  68. expect( stringify( output ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  69. } );
  70. it( 'should parse in the root context by default', () => {
  71. const output = data.parse( 'foo' );
  72. expect( stringify( output ) ).to.equal( '' );
  73. } );
  74. it( 'should accept parsing context', () => {
  75. const output = data.parse( 'foo', [ '$block' ] );
  76. expect( stringify( output ) ).to.equal( 'foo' );
  77. } );
  78. } );
  79. describe( 'toModel()', () => {
  80. beforeEach( () => {
  81. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  82. upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } );
  83. } );
  84. it( 'should convert content of an element #1', () => {
  85. const viewElement = parseView( '<p>foo</p>' );
  86. const output = data.toModel( viewElement );
  87. expect( output ).to.instanceof( ModelDocumentFragment );
  88. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph>' );
  89. } );
  90. it( 'should convert content of an element #2', () => {
  91. const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
  92. const output = data.toModel( viewFragment );
  93. expect( output ).to.be.instanceOf( ModelDocumentFragment );
  94. expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  95. } );
  96. it( 'should accept parsing context', () => {
  97. modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
  98. schema.register( 'inlineRoot' );
  99. schema.extend( '$text', { allowIn: 'inlineRoot' } );
  100. const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
  101. // Model fragment in root.
  102. expect( stringify( data.toModel( viewFragment ) ) ).to.equal( '' );
  103. // Model fragment in inline root.
  104. expect( stringify( data.toModel( viewFragment, [ 'inlineRoot' ] ) ) ).to.equal( 'foo' );
  105. } );
  106. } );
  107. describe( 'init()', () => {
  108. it( 'should be decorated', () => {
  109. const spy = sinon.spy();
  110. data.on( 'init', spy );
  111. data.init( 'foo bar' );
  112. sinon.assert.calledWithExactly( spy, sinon.match.any, [ 'foo bar' ] );
  113. } );
  114. it( 'should throw an error when document data is already initialized', () => {
  115. data.init( '<p>Foo</p>' );
  116. expect( () => {
  117. data.init( '<p>Bar</p>' );
  118. } ).to.throw(
  119. CKEditorError,
  120. 'datacontroller-init-document-not-empty: Trying to set initial data to not empty document.'
  121. );
  122. } );
  123. it( 'should set data to default main root', () => {
  124. schema.extend( '$text', { allowIn: '$root' } );
  125. data.init( 'foo' );
  126. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  127. } );
  128. it( 'should get root name as a parameter', () => {
  129. schema.extend( '$text', { allowIn: '$root' } );
  130. data.init( 'foo', 'title' );
  131. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  132. } );
  133. it( 'should create a batch', () => {
  134. schema.extend( '$text', { allowIn: '$root' } );
  135. data.init( 'foo' );
  136. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  137. } );
  138. it( 'should cause firing change event', () => {
  139. const spy = sinon.spy();
  140. schema.extend( '$text', { allowIn: '$root' } );
  141. model.document.on( 'change', spy );
  142. data.init( 'foo' );
  143. expect( spy.calledOnce ).to.be.true;
  144. } );
  145. it( 'should return a resolved Promise', () => {
  146. const promise = data.init( '<p>Foo</p>' );
  147. expect( promise ).to.be.instanceof( Promise );
  148. return promise;
  149. } );
  150. } );
  151. describe( 'set()', () => {
  152. it( 'should set data to default main root', () => {
  153. schema.extend( '$text', { allowIn: '$root' } );
  154. data.set( 'foo' );
  155. expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
  156. } );
  157. it( 'should create a batch', () => {
  158. schema.extend( '$text', { allowIn: '$root' } );
  159. data.set( 'foo' );
  160. expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 );
  161. } );
  162. it( 'should cause firing change event', () => {
  163. const spy = sinon.spy();
  164. schema.extend( '$text', { allowIn: '$root' } );
  165. model.document.on( 'change', spy );
  166. data.set( 'foo' );
  167. expect( spy.calledOnce ).to.be.true;
  168. } );
  169. it( 'should get root name as a parameter', () => {
  170. schema.extend( '$text', { allowIn: '$root' } );
  171. data.set( 'foo', 'main' );
  172. data.set( 'Bar', 'title' );
  173. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  174. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  175. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  176. } );
  177. it( 'should parse given data before set in a context of correct root', () => {
  178. schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } );
  179. data.set( 'foo', 'main' );
  180. data.set( 'Bar', 'title' );
  181. expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
  182. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  183. expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 );
  184. } );
  185. // This case was added when order of params was different and it really didn't work. Let's keep it
  186. // if anyone will ever try to change this.
  187. it( 'should allow setting empty data', () => {
  188. schema.extend( '$text', { allowIn: '$root' } );
  189. data.set( 'foo', 'title' );
  190. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
  191. data.set( '', 'title' );
  192. expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
  193. } );
  194. } );
  195. describe( 'get()', () => {
  196. it( 'should get paragraph with text', () => {
  197. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  198. setData( model, '<paragraph>foo</paragraph>' );
  199. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  200. expect( data.get() ).to.equal( '<p>foo</p>' );
  201. } );
  202. it( 'should get empty paragraph', () => {
  203. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  204. setData( model, '<paragraph></paragraph>' );
  205. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  206. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  207. } );
  208. it( 'should get two paragraphs', () => {
  209. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  210. setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  211. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  212. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  213. } );
  214. it( 'should get text directly in root', () => {
  215. schema.extend( '$text', { allowIn: '$root' } );
  216. setData( model, 'foo' );
  217. expect( data.get() ).to.equal( 'foo' );
  218. } );
  219. it( 'should get paragraphs without bold', () => {
  220. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  221. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  222. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  223. expect( data.get() ).to.equal( '<p>foobar</p>' );
  224. } );
  225. it( 'should get paragraphs with bold', () => {
  226. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  227. setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
  228. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  229. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  230. expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
  231. } );
  232. it( 'should get root name as a parameter', () => {
  233. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  234. schema.extend( '$text', { allowIn: '$root' } );
  235. setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  236. setData( model, 'Bar', { rootName: 'title' } );
  237. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  238. downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );
  239. expect( data.get() ).to.equal( '<p>foo</p>' );
  240. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  241. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  242. } );
  243. } );
  244. describe( 'stringify()', () => {
  245. beforeEach( () => {
  246. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  247. schema.register( 'div' );
  248. schema.extend( '$block', { allowIn: 'div' } );
  249. schema.extend( 'div', { allowIn: '$root' } );
  250. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  251. } );
  252. it( 'should stringify a content of an element', () => {
  253. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  254. expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
  255. } );
  256. it( 'should stringify a content of a document fragment', () => {
  257. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  258. expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
  259. } );
  260. } );
  261. describe( 'toView()', () => {
  262. beforeEach( () => {
  263. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  264. schema.register( 'div' );
  265. schema.extend( '$block', { allowIn: 'div' } );
  266. schema.extend( 'div', { allowIn: '$root' } );
  267. downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
  268. } );
  269. it( 'should convert a content of an element', () => {
  270. const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
  271. const viewDocumentFragment = data.toView( modelElement );
  272. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  273. const viewElement = viewDocumentFragment.getChild( 0 );
  274. expect( viewElement.name ).to.equal( 'p' );
  275. expect( viewElement.childCount ).to.equal( 1 );
  276. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  277. } );
  278. it( 'should correctly convert document markers #1', () => {
  279. const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
  280. const modelRoot = model.document.getRoot();
  281. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  282. model.change( writer => {
  283. writer.insert( modelElement, modelRoot, 0 );
  284. const range = writer.createRange( writer.createPositionAt( modelRoot, 0 ), writer.createPositionAt( modelRoot, 1 ) );
  285. writer.addMarker( 'marker:a', { range, usingOperation: true } );
  286. } );
  287. const viewDocumentFragment = data.toView( modelElement );
  288. const viewElement = viewDocumentFragment.getChild( 0 );
  289. expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
  290. } );
  291. it( 'should correctly convert document markers #2', () => {
  292. const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
  293. const modelRoot = model.document.getRoot();
  294. downcastHelpers.markerToHighlight( { model: 'marker:a', view: { classes: 'a' } } );
  295. downcastHelpers.markerToHighlight( { model: 'marker:b', view: { classes: 'b' } } );
  296. const modelP1 = modelElement.getChild( 0 );
  297. const modelP2 = modelElement.getChild( 1 );
  298. model.change( writer => {
  299. writer.insert( modelElement, modelRoot, 0 );
  300. const rangeA = writer.createRange( writer.createPositionAt( modelP1, 1 ), writer.createPositionAt( modelP1, 3 ) );
  301. const rangeB = writer.createRange( writer.createPositionAt( modelP2, 0 ), writer.createPositionAt( modelP2, 2 ) );
  302. writer.addMarker( 'marker:a', { range: rangeA, usingOperation: true } );
  303. writer.addMarker( 'marker:b', { range: rangeB, usingOperation: true } );
  304. } );
  305. const viewDocumentFragment = data.toView( modelP1 );
  306. expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
  307. } );
  308. it( 'should convert a document fragment', () => {
  309. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  310. const viewDocumentFragment = data.toView( modelDocumentFragment );
  311. expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
  312. expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
  313. const viewElement = viewDocumentFragment.getChild( 0 );
  314. expect( viewElement.name ).to.equal( 'p' );
  315. expect( viewElement.childCount ).to.equal( 1 );
  316. expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
  317. } );
  318. it( 'should keep view-model mapping', () => {
  319. const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
  320. const viewDocumentFragment = data.toView( modelDocumentFragment );
  321. const firstModelElement = modelDocumentFragment.getChild( 0 );
  322. const firstViewElement = viewDocumentFragment.getChild( 0 );
  323. const modelRange = ModelRange._createOn( firstModelElement );
  324. const viewRange = ViewRange._createOn( firstViewElement );
  325. const mappedModelRange = data.mapper.toModelRange( viewRange );
  326. const mappedViewRange = data.mapper.toViewRange( modelRange );
  327. expect( mappedModelRange ).to.be.instanceOf( ModelRange );
  328. expect( mappedViewRange ).to.be.instanceOf( ViewRange );
  329. expect( mappedModelRange.end.nodeBefore ).to.equal( firstModelElement );
  330. expect( mappedModelRange.end.nodeAfter ).to.equal( modelDocumentFragment.getChild( 1 ) );
  331. expect( mappedViewRange.end.nodeBefore ).to.equal( firstViewElement );
  332. expect( mappedViewRange.end.nodeAfter ).to.equal( viewDocumentFragment.getChild( 1 ) );
  333. } );
  334. } );
  335. describe( 'destroy()', () => {
  336. it( 'should be there for you', () => {
  337. // Should not throw.
  338. data.destroy();
  339. expect( data ).to.respondTo( 'destroy' );
  340. } );
  341. } );
  342. } );