view-to-model-converters.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ModelSchema from '../../src/model/schema';
  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, objWithContext;
  16. beforeEach( () => {
  17. schema = new ModelSchema();
  18. schema.registerItem( 'paragraph', '$block' );
  19. schema.allow( { name: '$text', inside: '$root' } );
  20. objWithContext = { context: [ '$root' ] };
  21. dispatcher = new ViewConversionDispatcher( { schema } );
  22. } );
  23. describe( 'convertText', () => {
  24. it( 'should return converter converting ViewText to ModelText', () => {
  25. const viewText = new ViewText( 'foobar' );
  26. dispatcher.on( 'text', convertText() );
  27. const conversionResult = dispatcher.convert( viewText, objWithContext );
  28. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  29. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  30. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  31. } );
  32. it( 'should not convert already consumed texts', () => {
  33. const viewText = new ViewText( 'foofuckbafuckr' );
  34. // Default converter for elements. Returns just converted children. Added with lowest priority.
  35. dispatcher.on( 'text', convertText(), { priority: 'lowest' } );
  36. // Added with normal priority. Should make the above converter not fire.
  37. dispatcher.on( 'text', ( evt, data, consumable ) => {
  38. if ( consumable.consume( data.input ) ) {
  39. data.output = new ModelText( data.input.data.replace( /fuck/gi, '****' ) );
  40. }
  41. } );
  42. const conversionResult = dispatcher.convert( viewText, objWithContext );
  43. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  44. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  45. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foo****ba****r' );
  46. } );
  47. it( 'should not convert text if it is wrong with schema', () => {
  48. schema.disallow( { name: '$text', inside: '$root' } );
  49. const viewText = new ViewText( 'foobar' );
  50. dispatcher.on( 'text', convertText() );
  51. let conversionResult = dispatcher.convert( viewText, objWithContext );
  52. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  53. expect( conversionResult.childCount ).to.equal( 0 );
  54. conversionResult = dispatcher.convert( viewText, { context: [ '$block' ] } );
  55. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  56. expect( conversionResult.childCount ).to.equal( 1 );
  57. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  58. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  59. } );
  60. it( 'should support unicode', () => {
  61. const viewText = new ViewText( 'நிலைக்கு' );
  62. dispatcher.on( 'text', convertText() );
  63. const conversionResult = dispatcher.convert( viewText, objWithContext );
  64. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  65. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
  66. expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
  67. } );
  68. } );
  69. describe( 'convertToModelFragment', () => {
  70. it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
  71. const viewFragment = new ViewDocumentFragment( [
  72. new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ),
  73. new ViewText( 'bar' )
  74. ] );
  75. // To get any meaningful results we have to actually convert something.
  76. dispatcher.on( 'text', convertText() );
  77. // This way P element won't be converted per-se but will fire converting it's children.
  78. dispatcher.on( 'element', convertToModelFragment() );
  79. dispatcher.on( 'documentFragment', convertToModelFragment() );
  80. const conversionResult = dispatcher.convert( viewFragment, objWithContext );
  81. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  82. expect( conversionResult.maxOffset ).to.equal( 6 );
  83. expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
  84. } );
  85. it( 'should not convert already consumed (converted) changes', () => {
  86. const viewP = new ViewContainerElement( 'p', null, new ViewText( 'foo' ) );
  87. // To get any meaningful results we have to actually convert something.
  88. dispatcher.on( 'text', convertText() );
  89. // Default converter for elements. Returns just converted children. Added with lowest priority.
  90. dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  91. // Added with normal priority. Should make the above converter not fire.
  92. dispatcher.on( 'element:p', ( evt, data, consumable, conversionApi ) => {
  93. if ( consumable.consume( data.input, { name: true } ) ) {
  94. data.output = new ModelElement( 'paragraph' );
  95. data.context.push( data.output );
  96. data.output.appendChildren( conversionApi.convertChildren( data.input, consumable, data ) );
  97. data.context.pop();
  98. }
  99. } );
  100. const conversionResult = dispatcher.convert( viewP, objWithContext );
  101. expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
  102. expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
  103. expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
  104. expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
  105. expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  106. } );
  107. } );
  108. } );