mapper-helpers.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import { setData as modelSetData } from '../../src/dev-utils/model';
  7. import View from '../../src/view/view';
  8. import createViewRoot from '../view/_utils/createroot';
  9. import { setData as viewSetData } from '../../src/dev-utils/view';
  10. import Mapper from '../../src/conversion/mapper';
  11. import ViewPosition from '../../src/view/position';
  12. import { mapModelPositionOnInlineElement, mapViewPositionInsideInlineElement } from '../../src/conversion/mapper-helpers';
  13. describe( 'mapper-helpers', () => {
  14. let model, view, viewDocument, mapper, modelRoot, viewRoot, viewInlineWidget, viewP, modelParagraph, inlineWidget;
  15. beforeEach( () => {
  16. model = new Model();
  17. modelRoot = model.document.createRoot();
  18. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  19. model.schema.register( 'inline-widget', { isInline: true, isObject: true, allowWhere: '$text' } );
  20. modelSetData( model, '<paragraph>foo[<inline-widget></inline-widget>]bar</paragraph>' );
  21. view = new View();
  22. viewDocument = view.document;
  23. viewRoot = createViewRoot( viewDocument, 'div', 'main' );
  24. viewSetData( view, '<p>foo<placeholder>widget description</placeholder>bar</p>' );
  25. viewP = viewRoot.getChild( 0 );
  26. viewInlineWidget = viewP.getChild( 1 );
  27. modelParagraph = modelRoot.getChild( 0 );
  28. inlineWidget = modelParagraph.getChild( 1 );
  29. mapper = new Mapper();
  30. mapper.bindElements( modelRoot, viewRoot );
  31. mapper.bindElements( modelParagraph, viewRoot.getChild( 0 ) );
  32. mapper.bindElements( inlineWidget, viewInlineWidget );
  33. } );
  34. describe( 'mapViewPositionInsideInlineElement()', () => {
  35. beforeEach( () => {
  36. mapper.on( 'viewToModelPosition', mapViewPositionInsideInlineElement( model ) );
  37. } );
  38. it( 'should return position after inline-widget when view position is at start of inline-widget', () => {
  39. const viewPosition = new ViewPosition( viewInlineWidget, 0 );
  40. const modelPosition = mapper.toModelPosition( viewPosition );
  41. expect( modelPosition.parent ).to.equal( modelParagraph );
  42. // The offset in <paragraph> should be after the inline-widget:
  43. // PARAGRAPH
  44. // |- foo before: [ 0 ] after: [ 3 ]
  45. // |- INLINE-WIDGET before: [ 3 ] after: [ 4 ]
  46. // |- bar before: [ 4 ] after: [ 7 ]
  47. expect( modelPosition.offset ).to.equal( 4 );
  48. } );
  49. it( 'should return position before inline-widget when view position is not at start of inline-widget', () => {
  50. const viewPosition = new ViewPosition( viewInlineWidget, 2 ); // in real-case scenarios is 1 even if placeholder has text.
  51. const modelPosition = mapper.toModelPosition( viewPosition );
  52. expect( modelPosition.parent ).to.equal( modelParagraph );
  53. // The offset in <paragraph> should be before the inline-widget:
  54. // PARAGRAPH
  55. // |- foo before: [ 0 ] after: [ 3 ]
  56. // |- INLINE-WIDGET before: [ 3 ] after: [ 4 ]
  57. // |- bar before: [ 4 ] after: [ 7 ]
  58. expect( modelPosition.offset ).to.equal( 3 );
  59. } );
  60. it( 'should do nothing if position maps to text node', () => {
  61. const viewPosition = new ViewPosition( viewP.getChild( 0 ), 1 );
  62. const data = { viewPosition, mapper };
  63. const mapperHelper = mapViewPositionInsideInlineElement( model );
  64. mapperHelper( {}, data );
  65. expect( data.modelPosition ).to.be.undefined;
  66. } );
  67. it( 'should do nothing if position maps to a non-inline element', () => {
  68. const viewPosition = new ViewPosition( viewP, 0 );
  69. const data = { viewPosition, mapper };
  70. const mapperHelper = mapViewPositionInsideInlineElement( model );
  71. mapperHelper( {}, data );
  72. expect( data.modelPosition ).to.be.undefined;
  73. } );
  74. } );
  75. describe( 'mapModelPositionOnInlineElement()', () => {
  76. beforeEach( () => {
  77. mapper.on( 'modelToViewPosition', mapModelPositionOnInlineElement( model, view ) );
  78. } );
  79. it( 'should return position after inline-widget when view position is at start of inline-widget', () => {
  80. const modelPosition = model.createPositionFromPath( modelRoot, [ 0, 4 ], 'toPrevious' );
  81. const viewPosition = mapper.toViewPosition( modelPosition );
  82. expect( viewPosition.parent ).to.equal( viewP );
  83. expect( viewPosition.offset ).to.equal( 2 );
  84. expect( viewPosition.nodeBefore ).to.equal( viewInlineWidget );
  85. } );
  86. it( 'should return position before inline-widget when view position is not at start of inline-widget', () => {
  87. const modelPosition = model.createPositionFromPath( modelRoot, [ 0, 3 ], 'toNext' );
  88. const viewPosition = mapper.toViewPosition( modelPosition );
  89. expect( viewPosition.parent ).to.equal( viewP );
  90. expect( viewPosition.offset ).to.equal( 1 );
  91. expect( viewPosition.nodeAfter ).to.equal( viewInlineWidget );
  92. } );
  93. it( 'should do nothing if position is phantom', () => {
  94. const modelPosition = model.createPositionFromPath( modelRoot, [ 0, 4 ], 'toPrevious' );
  95. const data = { isPhantom: true };
  96. mapper.toViewPosition( modelPosition, data );
  97. expect( data.modelPosition ).to.be.undefined;
  98. } );
  99. it( 'should do nothing if position\'s parent is not mapped', () => {
  100. mapper.unbindViewElement( viewInlineWidget );
  101. const modelPosition = model.createPositionFromPath( modelRoot, [ 0, 4 ], 'toPrevious' );
  102. const data = {};
  103. mapper.toViewPosition( modelPosition, data );
  104. expect( data.modelPosition ).to.be.undefined;
  105. } );
  106. it( 'should do nothing if position maps to a non-inline element', () => {
  107. const modelPosition = model.createPositionFromPath( modelRoot, [ 0 ], 'toPrevious' );
  108. const data = {};
  109. mapper.toViewPosition( modelPosition, data );
  110. expect( data.modelPosition ).to.be.undefined;
  111. } );
  112. } );
  113. } );