view-to-model-converters.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
  6. import ViewContainerElement from '../../src/view/containerelement';
  7. import ViewDocumentFragment from '../../src/view/documentfragment';
  8. import ViewText from '../../src/view/text';
  9. import Model from '../../src/model/model';
  10. import ModelDocumentFragment from '../../src/model/documentfragment';
  11. import ModelElement from '../../src/model/element';
  12. import ModelText from '../../src/model/text';
  13. import { convertToModelFragment, convertText } from '../../src/conversion/view-to-model-converters';
  14. describe( 'view-to-model-converters', () => {
  15. let dispatcher, schema, additionalData, model;
  16. beforeEach( () => {
  17. model = new Model();
  18. schema = model.schema;
  19. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  20. schema.extend( '$text', { allowIn: '$root' } );
  21. additionalData = { context: [ '$root' ] };
  22. dispatcher = new ViewConversionDispatcher( model, { schema } );
  23. } );
  24. describe( 'convertText', () => {
  25. it( 'should return converter converting ViewText to ModelText', () => {
  26. const viewText = new ViewText( 'foobar' );
  27. dispatcher.on( 'text', convertText() );
  28. const conversionResult = dispatcher.convert( viewText, additionalData );
  29. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  30. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  31. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  32. } );
  33. it( 'should not convert already consumed texts', () => {
  34. const viewText = new ViewText( 'foofuckbafuckr' );
  35. // Default converter for elements. Returns just converted children. Added with lowest priority.
  36. dispatcher.on( 'text', convertText(), { priority: 'lowest' } );
  37. // Added with normal priority. Should make the above converter not fire.
  38. dispatcher.on( 'text', ( evt, data, consumable ) => {
  39. if ( consumable.consume( data.input ) ) {
  40. data.output = new ModelText( data.input.data.replace( /fuck/gi, '****' ) );
  41. }
  42. } );
  43. const conversionResult = dispatcher.convert( viewText, additionalData );
  44. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  45. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  46. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foo****ba****r' );
  47. } );
  48. it( 'should not convert text if it is wrong with schema', () => {
  49. schema.on( 'checkChild', ( evt, args ) => {
  50. const ctx = args[ 0 ];
  51. const child = args[ 1 ];
  52. const childRule = schema.getRule( child );
  53. if ( childRule.name == '$text' && ctx.matchEnd( '$root' ) ) {
  54. evt.stop();
  55. evt.return = false;
  56. }
  57. }, { priority: 'high' } );
  58. const viewText = new ViewText( 'foobar' );
  59. dispatcher.on( 'text', convertText() );
  60. let conversionResult = dispatcher.convert( viewText, additionalData );
  61. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  62. expect( conversionResult.childCount ).to.equal( 0 );
  63. conversionResult = dispatcher.convert( viewText, { context: [ '$block' ] } );
  64. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  65. expect( conversionResult.childCount ).to.equal( 1 );
  66. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  67. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  68. } );
  69. it( 'should support unicode', () => {
  70. const viewText = new ViewText( 'நிலைக்கு' );
  71. dispatcher.on( 'text', convertText() );
  72. const conversionResult = dispatcher.convert( viewText, additionalData );
  73. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  74. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  75. expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
  76. } );
  77. } );
  78. describe( 'convertToModelFragment', () => {
  79. it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
  80. const viewFragment = new ViewDocumentFragment( [
  81. new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ),
  82. new ViewText( 'bar' )
  83. ] );
  84. // To get any meaningful results we have to actually convert something.
  85. dispatcher.on( 'text', convertText() );
  86. // This way P element won't be converted per-se but will fire converting it's children.
  87. dispatcher.on( 'element', convertToModelFragment() );
  88. dispatcher.on( 'documentFragment', convertToModelFragment() );
  89. const conversionResult = dispatcher.convert( viewFragment, additionalData );
  90. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  91. expect( conversionResult.maxOffset ).to.equal( 6 );
  92. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  93. } );
  94. it( 'should not convert already consumed (converted) changes', () => {
  95. const viewP = new ViewContainerElement( 'p', null, new ViewText( 'foo' ) );
  96. // To get any meaningful results we have to actually convert something.
  97. dispatcher.on( 'text', convertText() );
  98. // Default converter for elements. Returns just converted children. Added with lowest priority.
  99. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  100. // Added with normal priority. Should make the above converter not fire.
  101. dispatcher.on( 'element:p', ( evt, data, consumable, conversionApi ) => {
  102. if ( consumable.consume( data.input, { name: true } ) ) {
  103. data.output = new ModelElement( 'paragraph' );
  104. data.context.push( data.output );
  105. data.output.appendChildren( conversionApi.convertChildren( data.input, consumable, data ) );
  106. data.context.pop();
  107. }
  108. } );
  109. const conversionResult = dispatcher.convert( viewP, additionalData );
  110. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  111. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
  112. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  113. expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
  114. expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  115. } );
  116. } );
  117. } );