datacontroller.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view */
  6. 'use strict';
  7. import ModelDocument from '/ckeditor5/engine/model/document.js';
  8. import DataController from '/ckeditor5/engine/datacontroller.js';
  9. import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/HtmlDataProcessor.js';
  10. import BuildViewConverterFor from '/ckeditor5/engine/conversion/view-converter-builder.js';
  11. import BuildModelConverterFor from '/ckeditor5/engine/conversion/model-converter-builder.js';
  12. import { getData, setData, stringify } from '/tests/engine/_utils/model.js';
  13. import count from '/ckeditor5/utils/count.js';
  14. describe( 'DataController', () => {
  15. let modelDocument, htmlDataProcessor, data, schema;
  16. beforeEach( () => {
  17. modelDocument = new ModelDocument();
  18. modelDocument.createRoot( 'main' );
  19. modelDocument.createRoot( 'title' );
  20. htmlDataProcessor = new HtmlDataProcessor();
  21. data = new DataController( modelDocument, htmlDataProcessor );
  22. schema = modelDocument.schema;
  23. } );
  24. describe( 'parse', () => {
  25. it( 'should set text', () => {
  26. schema.allow( { name: '$text', inside: '$root' } );
  27. const model = data.parse( '<p>foo<b>bar</b></p>' );
  28. expect( stringify( model ) ).to.equal( 'foobar' );
  29. } );
  30. it( 'should set paragraph', () => {
  31. schema.registerItem( 'paragraph', '$block' );
  32. BuildViewConverterFor( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  33. const model = data.parse( '<p>foo<b>bar</b></p>' );
  34. expect( stringify( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
  35. } );
  36. it( 'should set two paragraphs', () => {
  37. schema.registerItem( 'paragraph', '$block' );
  38. BuildViewConverterFor( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  39. const model = data.parse( '<p>foo</p><p>bar</p>' );
  40. expect( stringify( model ) ).to.equal(
  41. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  42. } );
  43. it( 'should set paragraphs with bold', () => {
  44. schema.registerItem( 'paragraph', '$block' );
  45. schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
  46. BuildViewConverterFor( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
  47. BuildViewConverterFor( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  48. const model = data.parse( '<p>foo<b>bar</b></p>' );
  49. expect( stringify( model ) ).to.equal(
  50. '<paragraph>foo<$text bold=true>bar</$text></paragraph>' );
  51. } );
  52. } );
  53. describe( 'set', () => {
  54. it( 'should set data to root', () => {
  55. schema.allow( { name: '$text', inside: '$root' } );
  56. data.set( 'foo' );
  57. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
  58. } );
  59. it( 'should create a batch', () => {
  60. schema.allow( { name: '$text', inside: '$root' } );
  61. data.set( 'foo' );
  62. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
  63. } );
  64. it( 'should get root name as a parameter', () => {
  65. schema.allow( { name: '$text', inside: '$root' } );
  66. data.set( 'main', 'foo' );
  67. data.set( 'title', 'Bar' );
  68. expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
  69. expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
  70. expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
  71. } );
  72. } );
  73. describe( 'get', () => {
  74. it( 'should get paragraph with text', () => {
  75. setData( modelDocument, '<paragraph>foo</paragraph>' );
  76. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  77. expect( data.get() ).to.equal( '<p>foo</p>' );
  78. } );
  79. it( 'should get empty paragraph', () => {
  80. setData( modelDocument, '<paragraph></paragraph>' );
  81. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  82. expect( data.get() ).to.equal( '<p>&nbsp;</p>' );
  83. } );
  84. it( 'should get two paragraphs', () => {
  85. setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  86. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  87. expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
  88. } );
  89. it( 'should get text directly in root', () => {
  90. setData( modelDocument, 'foo' );
  91. expect( data.get() ).to.equal( 'foo' );
  92. } );
  93. it( 'should get paragraphs without bold', () => {
  94. setData( modelDocument, '<paragraph>foo<$text bold=true>bar</$text></paragraph>' );
  95. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  96. expect( data.get() ).to.equal( '<p>foobar</p>' );
  97. } );
  98. it( 'should get paragraphs with bold', () => {
  99. setData( modelDocument, '<paragraph>foo<$text bold=true>bar</$text></paragraph>' );
  100. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  101. BuildModelConverterFor( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  102. expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
  103. } );
  104. it( 'should get root name as a parameter', () => {
  105. setData( modelDocument, '<paragraph>foo</paragraph>', { rootName: 'main' } );
  106. setData( modelDocument, 'Bar', { rootName: 'title' } );
  107. BuildModelConverterFor( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
  108. BuildModelConverterFor( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  109. expect( data.get() ).to.equal( '<p>foo</p>' );
  110. expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
  111. expect( data.get( 'title' ) ).to.equal( 'Bar' );
  112. } );
  113. } );
  114. describe( 'destroy', () => {
  115. it( 'should be there for you', () => {
  116. // Should not throw.
  117. data.destroy();
  118. expect( data ).to.respondTo( 'destroy' );
  119. } );
  120. } );
  121. } );