utils.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import DowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
  7. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  8. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  9. import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
  10. import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
  11. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  12. import {
  13. toWidget,
  14. isWidget,
  15. setLabel,
  16. getLabel,
  17. toWidgetEditable,
  18. setHighlightHandling,
  19. findOptimalInsertionPosition,
  20. viewToModelPositionOutsideModelElement,
  21. WIDGET_CLASS_NAME,
  22. centeredBalloonPositionForLongWidgets
  23. } from '../src/utils';
  24. import UIElement from '@ckeditor/ckeditor5-engine/src/view/uielement';
  25. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  26. import Model from '@ckeditor/ckeditor5-engine/src/model/model';
  27. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  28. import Mapper from '@ckeditor/ckeditor5-engine/src/conversion/mapper';
  29. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  30. import ModelText from '@ckeditor/ckeditor5-engine/src/model/text';
  31. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  32. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  33. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  34. describe( 'widget utils', () => {
  35. let element, writer, viewDocument;
  36. testUtils.createSinonSandbox();
  37. beforeEach( () => {
  38. viewDocument = new ViewDocument();
  39. writer = new DowncastWriter( viewDocument );
  40. element = writer.createContainerElement( 'div' );
  41. toWidget( element, writer );
  42. } );
  43. describe( 'toWidget()', () => {
  44. it( 'should set contenteditable to "false"', () => {
  45. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  46. } );
  47. it( 'should define getFillerOffset method', () => {
  48. expect( element.getFillerOffset ).to.be.a( 'function' );
  49. expect( element.getFillerOffset() ).to.be.null;
  50. } );
  51. it( 'should add proper CSS class', () => {
  52. expect( element.hasClass( WIDGET_CLASS_NAME ) ).to.be.true;
  53. } );
  54. it( 'should add element\'s label if one is provided', () => {
  55. toWidget( element, writer, { label: 'foo bar baz label' } );
  56. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  57. } );
  58. it( 'should add element\'s label if one is provided as function', () => {
  59. toWidget( element, writer, { label: () => 'foo bar baz label' } );
  60. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  61. } );
  62. it( 'should set default highlight handling methods', () => {
  63. toWidget( element, writer );
  64. const set = element.getCustomProperty( 'addHighlight' );
  65. const remove = element.getCustomProperty( 'removeHighlight' );
  66. expect( typeof set ).to.equal( 'function' );
  67. expect( typeof remove ).to.equal( 'function' );
  68. set( element, { priority: 1, classes: 'highlight', id: 'highlight' }, writer );
  69. expect( element.hasClass( 'highlight' ) ).to.be.true;
  70. remove( element, 'highlight', writer );
  71. expect( element.hasClass( 'highlight' ) ).to.be.false;
  72. } );
  73. it( 'should set default highlight handling methods - CSS classes array', () => {
  74. toWidget( element, writer );
  75. const set = element.getCustomProperty( 'addHighlight' );
  76. const remove = element.getCustomProperty( 'removeHighlight' );
  77. expect( typeof set ).to.equal( 'function' );
  78. expect( typeof remove ).to.equal( 'function' );
  79. set( element, { priority: 1, classes: [ 'highlight', 'foo' ], id: 'highlight' }, writer );
  80. expect( element.hasClass( 'highlight' ) ).to.be.true;
  81. expect( element.hasClass( 'foo' ) ).to.be.true;
  82. remove( element, 'highlight', writer );
  83. expect( element.hasClass( 'highlight' ) ).to.be.false;
  84. expect( element.hasClass( 'foo' ) ).to.be.false;
  85. } );
  86. it( 'should add element a selection handle to widget if hasSelectionHandle=true is passed', () => {
  87. toWidget( element, writer, { hasSelectionHandle: true } );
  88. expect( element.hasClass( 'ck-widget_with-selection-handle' ) ).to.be.true;
  89. const selectionHandle = element.getChild( 0 );
  90. expect( selectionHandle ).to.be.instanceof( UIElement );
  91. const domSelectionHandle = selectionHandle.render( document );
  92. expect( domSelectionHandle.classList.contains( 'ck' ) ).to.be.true;
  93. expect( domSelectionHandle.classList.contains( 'ck-widget__selection-handle' ) ).to.be.true;
  94. const icon = domSelectionHandle.firstChild;
  95. expect( icon.nodeName ).to.equal( 'svg' );
  96. expect( icon.classList.contains( 'ck' ) ).to.be.true;
  97. expect( icon.classList.contains( 'ck-icon' ) ).to.be.true;
  98. } );
  99. } );
  100. describe( 'isWidget()', () => {
  101. it( 'should return true for widgetized elements', () => {
  102. expect( isWidget( element ) ).to.be.true;
  103. } );
  104. it( 'should return false for non-widgetized elements', () => {
  105. expect( isWidget( new ViewElement( viewDocument, 'p' ) ) ).to.be.false;
  106. } );
  107. it( 'should return false for text node', () => {
  108. expect( isWidget( new ViewText( viewDocument, 'p' ) ) ).to.be.false;
  109. } );
  110. } );
  111. describe( 'label utils', () => {
  112. it( 'should allow to set label for element', () => {
  113. const element = new ViewElement( viewDocument, 'p' );
  114. setLabel( element, 'foo bar baz', writer );
  115. expect( getLabel( element ) ).to.equal( 'foo bar baz' );
  116. } );
  117. it( 'should return empty string for elements without label', () => {
  118. const element = new ViewElement( viewDocument, 'div' );
  119. expect( getLabel( element ) ).to.equal( '' );
  120. } );
  121. it( 'should allow to use a function as label creator', () => {
  122. const element = new ViewElement( viewDocument, 'p' );
  123. let caption = 'foo';
  124. setLabel( element, () => caption, writer );
  125. expect( getLabel( element ) ).to.equal( 'foo' );
  126. caption = 'bar';
  127. expect( getLabel( element ) ).to.equal( 'bar' );
  128. } );
  129. } );
  130. describe( 'toWidgetEditable()', () => {
  131. let viewDocument, element;
  132. beforeEach( () => {
  133. viewDocument = new ViewDocument();
  134. element = new ViewEditableElement( viewDocument, 'div' );
  135. toWidgetEditable( element, writer );
  136. } );
  137. it( 'should be created in context of proper document', () => {
  138. expect( element.document ).to.equal( viewDocument );
  139. } );
  140. it( 'should add proper class', () => {
  141. expect( element.hasClass( 'ck-editor__editable', 'ck-editor__nested-editable' ) ).to.be.true;
  142. } );
  143. it( 'should add proper contenteditable value when element is read-only - initialization', () => {
  144. const element = new ViewEditableElement( viewDocument, 'div' );
  145. element.isReadOnly = true;
  146. toWidgetEditable( element, writer );
  147. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  148. } );
  149. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  150. element.isReadOnly = true;
  151. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  152. element.isReadOnly = false;
  153. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  154. } );
  155. it( 'should add proper class when element is focused', () => {
  156. element.isFocused = true;
  157. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.true;
  158. element.isFocused = false;
  159. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.false;
  160. } );
  161. } );
  162. describe( 'addHighlightHandling()', () => {
  163. let element, addSpy, removeSpy, set, remove;
  164. beforeEach( () => {
  165. element = new ViewElement( viewDocument, 'p' );
  166. addSpy = sinon.spy();
  167. removeSpy = sinon.spy();
  168. setHighlightHandling( element, writer, addSpy, removeSpy );
  169. set = element.getCustomProperty( 'addHighlight' );
  170. remove = element.getCustomProperty( 'removeHighlight' );
  171. } );
  172. it( 'should set highlight handling methods', () => {
  173. expect( typeof set ).to.equal( 'function' );
  174. expect( typeof remove ).to.equal( 'function' );
  175. } );
  176. it( 'should call highlight methods when descriptor is added and removed', () => {
  177. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight' };
  178. set( element, descriptor, writer );
  179. remove( element, descriptor.id, writer );
  180. sinon.assert.calledOnce( addSpy );
  181. sinon.assert.calledWithExactly( addSpy, element, descriptor, writer );
  182. sinon.assert.calledOnce( removeSpy );
  183. sinon.assert.calledWithExactly( removeSpy, element, descriptor, writer );
  184. } );
  185. it( 'should call highlight methods when next descriptor is added', () => {
  186. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  187. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  188. set( element, descriptor );
  189. set( element, secondDescriptor );
  190. sinon.assert.calledTwice( addSpy );
  191. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  192. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  193. } );
  194. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  195. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  196. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  197. set( element, descriptor );
  198. set( element, secondDescriptor );
  199. sinon.assert.calledOnce( addSpy );
  200. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  201. } );
  202. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  203. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  204. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  205. set( element, descriptor );
  206. set( element, secondDescriptor );
  207. remove( element, secondDescriptor.id );
  208. sinon.assert.calledThrice( addSpy );
  209. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  210. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  211. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  212. sinon.assert.calledTwice( removeSpy );
  213. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  214. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  215. } );
  216. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  217. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  218. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  219. set( element, descriptor );
  220. set( element, secondDescriptor );
  221. remove( element, secondDescriptor );
  222. sinon.assert.calledOnce( addSpy );
  223. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  224. sinon.assert.notCalled( removeSpy );
  225. } );
  226. it( 'should call highlight methods - CSS class array', () => {
  227. const descriptor = { priority: 10, classes: [ 'highlight', 'a' ], id: 'highlight-1' };
  228. const secondDescriptor = { priority: 10, classes: [ 'highlight', 'b' ], id: 'highlight-2' };
  229. set( element, descriptor );
  230. set( element, secondDescriptor );
  231. sinon.assert.calledTwice( addSpy );
  232. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  233. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  234. } );
  235. } );
  236. describe( 'findOptimalInsertionPosition()', () => {
  237. let model, doc;
  238. beforeEach( () => {
  239. model = new Model();
  240. doc = model.document;
  241. doc.createRoot();
  242. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  243. model.schema.register( 'image' );
  244. model.schema.register( 'span' );
  245. model.schema.extend( 'image', {
  246. allowIn: '$root',
  247. isObject: true,
  248. isBlock: true
  249. } );
  250. model.schema.extend( 'span', { allowIn: 'paragraph' } );
  251. model.schema.extend( '$text', { allowIn: 'span' } );
  252. } );
  253. it( 'returns position after selected element', () => {
  254. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  255. const pos = findOptimalInsertionPosition( doc.selection, model );
  256. expect( pos.path ).to.deep.equal( [ 2 ] );
  257. } );
  258. it( 'returns position before parent block if an inline object is selected', () => {
  259. model.schema.register( 'placeholder', {
  260. allowWhere: '$text',
  261. isInline: true,
  262. isObject: true
  263. } );
  264. setData( model, '<paragraph>x</paragraph><paragraph>f[<placeholder></placeholder>]oo</paragraph><paragraph>y</paragraph>' );
  265. const pos = findOptimalInsertionPosition( doc.selection, model );
  266. expect( pos.path ).to.deep.equal( [ 1 ] );
  267. } );
  268. it( 'returns position inside empty block', () => {
  269. setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  270. const pos = findOptimalInsertionPosition( doc.selection, model );
  271. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  272. } );
  273. it( 'returns position before block if at the beginning of that block', () => {
  274. setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  275. const pos = findOptimalInsertionPosition( doc.selection, model );
  276. expect( pos.path ).to.deep.equal( [ 1 ] );
  277. } );
  278. it( 'returns position before block if in the middle of that block (collapsed selection)', () => {
  279. setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  280. const pos = findOptimalInsertionPosition( doc.selection, model );
  281. expect( pos.path ).to.deep.equal( [ 1 ] );
  282. } );
  283. it( 'returns position before block if in the middle of that block (non-collapsed selection)', () => {
  284. setData( model, '<paragraph>x</paragraph><paragraph>f[o]o</paragraph><paragraph>y</paragraph>' );
  285. const pos = findOptimalInsertionPosition( doc.selection, model );
  286. expect( pos.path ).to.deep.equal( [ 1 ] );
  287. } );
  288. it( 'returns position after block if at the end of that block', () => {
  289. setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  290. const pos = findOptimalInsertionPosition( doc.selection, model );
  291. expect( pos.path ).to.deep.equal( [ 2 ] );
  292. } );
  293. // Checking if isTouching() was used.
  294. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  295. setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  296. const pos = findOptimalInsertionPosition( doc.selection, model );
  297. expect( pos.path ).to.deep.equal( [ 2 ] );
  298. } );
  299. it( 'returns selection focus if not in a block', () => {
  300. model.schema.extend( '$text', { allowIn: '$root' } );
  301. setData( model, 'foo[]bar' );
  302. const pos = findOptimalInsertionPosition( doc.selection, model );
  303. expect( pos.path ).to.deep.equal( [ 3 ] );
  304. } );
  305. } );
  306. describe( 'viewToModelPositionOutsideModelElement()', () => {
  307. let mapper, model, modelP, viewP, viewXyz, modelSpan, viewSpan;
  308. beforeEach( () => {
  309. mapper = new Mapper();
  310. model = new Model();
  311. // MODEL: <p>foo<span></span>bar</p>
  312. const modelFoo = new ModelText( 'foo' );
  313. modelSpan = new ModelElement( 'span' );
  314. const modelBar = new ModelText( 'bar' );
  315. modelP = new ModelElement( 'p', null, [ modelFoo, modelSpan, modelBar ] );
  316. // VIEW: <p>foo<span>xyz</span>bar</p>
  317. const viewFoo = new ViewText( viewDocument, 'foo' );
  318. viewXyz = new ViewText( viewDocument, 'xyz' );
  319. viewSpan = new ViewElement( viewDocument, 'span', null, viewXyz );
  320. const viewBar = new ViewText( viewDocument, 'bar' );
  321. viewP = new ViewElement( viewDocument, 'p', null, [ viewFoo, viewSpan, viewBar ] );
  322. mapper.bindElements( modelP, viewP );
  323. mapper.bindElements( modelSpan, viewSpan );
  324. } );
  325. it( 'should map view position that is at the beginning of the view element to a position before the model element', () => {
  326. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  327. // View:
  328. // <p>foo<span>|xyz</span>bar</p>.
  329. const viewPosition = new ViewPosition( viewXyz, 0 );
  330. // Model:
  331. // <p>foo|<span></span>bar</p>.
  332. const modelPosition = mapper.toModelPosition( viewPosition );
  333. expect( modelPosition.path ).to.deep.equal( [ 3 ] );
  334. } );
  335. it( 'should map view position that is in the middle of the view element to a position after the model element', () => {
  336. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  337. // View:
  338. // <p>foo<span>x|yz</span>bar</p>.
  339. const viewPosition = new ViewPosition( viewXyz, 1 );
  340. // Model:
  341. // <p>foo|<span></span>bar</p>.
  342. const modelPosition = mapper.toModelPosition( viewPosition );
  343. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  344. } );
  345. it( 'should map view position that is at the end of the view element to a position after the model element', () => {
  346. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  347. // View:
  348. // <p>foo<span>xyz|</span>bar</p>.
  349. const viewPosition = new ViewPosition( viewXyz, 3 );
  350. // Model:
  351. // <p>foo<span></span>|bar</p>.
  352. const modelPosition = mapper.toModelPosition( viewPosition );
  353. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  354. } );
  355. it( 'should not fire if view element is not matched', () => {
  356. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, () => false ) );
  357. // View:
  358. // <p>foo<span>x|yz</span>bar</p>.
  359. const viewPosition = new ViewPosition( viewXyz, 1 );
  360. // Model:
  361. // <p>foo<span>x|yz</span>bar</p>.
  362. modelSpan._appendChild( new ModelText( 'xyz' ) );
  363. const modelPosition = mapper.toModelPosition( viewPosition );
  364. expect( modelPosition.path ).to.deep.equal( [ 3, 1 ] );
  365. } );
  366. } );
  367. describe( 'centeredBalloonPositionForLongWidgets()', () => {
  368. const arrowVerticalOffset = BalloonPanelView.arrowVerticalOffset;
  369. // Balloon is a 10x10 rect.
  370. const balloonRect = new Rect( {
  371. top: 0,
  372. left: 0,
  373. right: 10,
  374. bottom: 10,
  375. width: 10,
  376. height: 10
  377. } );
  378. beforeEach( () => {
  379. testUtils.sinon.stub( global.window, 'innerWidth' ).value( 100 );
  380. testUtils.sinon.stub( global.window, 'innerHeight' ).value( 100 );
  381. } );
  382. it( 'should return null if there is enough space above the widget', () => {
  383. // Widget is a 50x150 rect, translated (25,25) from viewport's beginning (0,0).
  384. const widgetRect = new Rect( {
  385. top: 25,
  386. left: 25,
  387. right: 75,
  388. bottom: 175,
  389. width: 50,
  390. height: 150
  391. } );
  392. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  393. expect( position ).to.equal( null );
  394. } );
  395. it( 'should return null if there is enough space below the widget', () => {
  396. // Widget is a 50x150 rect, translated (25,-125) from viewport's beginning (0,0).
  397. const widgetRect = new Rect( {
  398. top: -125,
  399. left: 25,
  400. right: 75,
  401. bottom: 25,
  402. width: 50,
  403. height: 150
  404. } );
  405. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  406. expect( position ).to.equal( null );
  407. } );
  408. it( 'should position the balloon inside a widget – at the top + in the middle', () => {
  409. // Widget is a 50x150 rect, translated (25,5) from viewport's beginning (0,0).
  410. const widgetRect = new Rect( {
  411. top: 5,
  412. left: 25,
  413. right: 75,
  414. bottom: 155,
  415. width: 50,
  416. height: 150
  417. } );
  418. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  419. expect( position ).to.deep.equal( {
  420. top: 5 + arrowVerticalOffset,
  421. left: 45,
  422. name: 'arrow_n'
  423. } );
  424. } );
  425. it( 'should stick the balloon to the top of the viewport when the top of a widget is off-screen', () => {
  426. // Widget is a 50x150 rect, translated (25,-25) from viewport's beginning (0,0).
  427. const widgetRect = new Rect( {
  428. top: -25,
  429. left: 25,
  430. right: 75,
  431. bottom: 150,
  432. width: 50,
  433. height: 150
  434. } );
  435. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  436. expect( position ).to.deep.equal( {
  437. top: arrowVerticalOffset,
  438. left: 45,
  439. name: 'arrow_n'
  440. } );
  441. } );
  442. it( 'should horizontally center the balloon in the visible area when the widget is cropped by the viewport', () => {
  443. // Widget is a 50x150 rect, translated (-25,5) from viewport's beginning (0,0).
  444. const widgetRect = new Rect( {
  445. top: 5,
  446. left: -25,
  447. right: 25,
  448. bottom: 155,
  449. width: 50,
  450. height: 150
  451. } );
  452. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  453. expect( position ).to.deep.equal( {
  454. top: 5 + arrowVerticalOffset,
  455. left: 7.5,
  456. name: 'arrow_n'
  457. } );
  458. } );
  459. it( 'should horizontally center the balloon in the widget when the widget is completely off the viewport', () => {
  460. // Widget is a 50x150 rect, translated (0,-100) from viewport's beginning (0,0).
  461. const widgetRect = new Rect( {
  462. top: 0,
  463. left: -100,
  464. right: -50,
  465. bottom: 150,
  466. width: 50,
  467. height: 150
  468. } );
  469. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  470. expect( position ).to.deep.equal( {
  471. top: 0 + arrowVerticalOffset,
  472. left: -80,
  473. name: 'arrow_n'
  474. } );
  475. } );
  476. } );
  477. } );