datacontroller.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import ModelDocument from '/ckeditor5/engine/treemodel/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/treecontroller/view-converter-builder.js';
  11. import { getData } from '/tests/engine/_utils/model.js';
  12. describe( 'DataController', () => {
  13. let modelDocument, htmlDataProcessor, data, schema;
  14. beforeEach( () => {
  15. modelDocument = new ModelDocument();
  16. modelDocument.createRoot( 'main' );
  17. htmlDataProcessor = new HtmlDataProcessor();
  18. data = new DataController( modelDocument, htmlDataProcessor );
  19. schema = modelDocument.schema;
  20. } );
  21. describe( 'set', () => {
  22. it( 'should set text', () => {
  23. schema.allow( { name: '$text', inside: '$root' } );
  24. data.set( '<p>foo<b>bar</b></p>' );
  25. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foobar' );
  26. } );
  27. it( 'should set paragraph', () => {
  28. schema.registerItem( 'paragraph', '$block' );
  29. BuildViewConverterFor( data.toModel ).fromElement( 'p' ).toElement( 'paragraph' );
  30. data.set( '<p>foo<b>bar</b></p>' );
  31. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  32. } );
  33. it( 'should set two paragraphs', () => {
  34. schema.registerItem( 'paragraph', '$block' );
  35. BuildViewConverterFor( data.toModel ).fromElement( 'p' ).toElement( 'paragraph' );
  36. data.set( '<p>foo</p><p>bar</p>' );
  37. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal(
  38. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  39. } );
  40. it( 'should set paragraphs with bold', () => {
  41. schema.registerItem( 'paragraph', '$block' );
  42. schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
  43. BuildViewConverterFor( data.toModel ).fromElement( 'p' ).toElement( 'paragraph' );
  44. BuildViewConverterFor( data.toModel ).fromElement( 'b' ).toAttribute( 'bold', true );
  45. data.set( '<p>foo<b>bar</b></p>' );
  46. expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal(
  47. '<paragraph>foo<$text bold=true>bar</$text></paragraph>' );
  48. } );
  49. } );
  50. } );