8
0

utils.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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.register( 'horizontalLine', {
  251. isObject: true,
  252. allowWhere: '$block'
  253. } );
  254. model.schema.extend( 'span', { allowIn: 'paragraph' } );
  255. model.schema.extend( '$text', { allowIn: 'span' } );
  256. } );
  257. it( 'returns position after selected element', () => {
  258. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  259. const pos = findOptimalInsertionPosition( doc.selection, model );
  260. expect( pos.path ).to.deep.equal( [ 2 ] );
  261. } );
  262. it( 'returns position before parent block if an inline object is selected', () => {
  263. model.schema.register( 'placeholder', {
  264. allowWhere: '$text',
  265. isInline: true,
  266. isObject: true
  267. } );
  268. setData( model, '<paragraph>x</paragraph><paragraph>f[<placeholder></placeholder>]oo</paragraph><paragraph>y</paragraph>' );
  269. const pos = findOptimalInsertionPosition( doc.selection, model );
  270. expect( pos.path ).to.deep.equal( [ 1 ] );
  271. } );
  272. it( 'returns position inside empty block', () => {
  273. setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  274. const pos = findOptimalInsertionPosition( doc.selection, model );
  275. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  276. } );
  277. it( 'returns position before block if at the beginning of that block', () => {
  278. setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  279. const pos = findOptimalInsertionPosition( doc.selection, model );
  280. expect( pos.path ).to.deep.equal( [ 1 ] );
  281. } );
  282. it( 'returns position before block if in the middle of that block (collapsed selection)', () => {
  283. setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  284. const pos = findOptimalInsertionPosition( doc.selection, model );
  285. expect( pos.path ).to.deep.equal( [ 1 ] );
  286. } );
  287. it( 'returns position before block if in the middle of that block (non-collapsed selection)', () => {
  288. setData( model, '<paragraph>x</paragraph><paragraph>f[o]o</paragraph><paragraph>y</paragraph>' );
  289. const pos = findOptimalInsertionPosition( doc.selection, model );
  290. expect( pos.path ).to.deep.equal( [ 1 ] );
  291. } );
  292. it( 'returns position after block if at the end of that block', () => {
  293. setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  294. const pos = findOptimalInsertionPosition( doc.selection, model );
  295. expect( pos.path ).to.deep.equal( [ 2 ] );
  296. } );
  297. // Checking if isTouching() was used.
  298. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  299. setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  300. const pos = findOptimalInsertionPosition( doc.selection, model );
  301. expect( pos.path ).to.deep.equal( [ 2 ] );
  302. } );
  303. it( 'returns selection focus if not in a block', () => {
  304. model.schema.extend( '$text', { allowIn: '$root' } );
  305. setData( model, 'foo[]bar' );
  306. const pos = findOptimalInsertionPosition( doc.selection, model );
  307. expect( pos.path ).to.deep.equal( [ 3 ] );
  308. } );
  309. // https://github.com/ckeditor/ckeditor5/issues/7438
  310. describe( 'integration with the WidgetTypeAround feature ("widget-type-around" model selection attribute)', () => {
  311. it( 'should respect the attribute value when a widget (block and an object) is selected ("fake caret" before a widget)', () => {
  312. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  313. model.change( writer => {
  314. writer.setSelectionAttribute( 'widget-type-around', 'before' );
  315. } );
  316. const pos = findOptimalInsertionPosition( doc.selection, model );
  317. expect( pos.path ).to.deep.equal( [ 1 ] );
  318. } );
  319. it( 'should respect the attribute value when a widget (block and an object) is selected ("fake caret" after a widget)', () => {
  320. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  321. model.change( writer => {
  322. writer.setSelectionAttribute( 'widget-type-around', 'after' );
  323. } );
  324. const pos = findOptimalInsertionPosition( doc.selection, model );
  325. expect( pos.path ).to.deep.equal( [ 2 ] );
  326. } );
  327. it( 'should return a position after a selected widget (block and an object) ("fake caret" not displayed)', () => {
  328. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  329. const pos = findOptimalInsertionPosition( doc.selection, model );
  330. expect( pos.path ).to.deep.equal( [ 2 ] );
  331. } );
  332. it( 'should respect the attribute value when a widget (an object) is selected ("fake caret" before a widget)', () => {
  333. setData( model, '<paragraph>x</paragraph>[<horizontalLine></horizontalLine>]<paragraph>y</paragraph>' );
  334. model.change( writer => {
  335. writer.setSelectionAttribute( 'widget-type-around', 'before' );
  336. } );
  337. const pos = findOptimalInsertionPosition( doc.selection, model );
  338. expect( pos.path ).to.deep.equal( [ 1 ] );
  339. } );
  340. it( 'should respect the attribute value when a widget (an object) is selected ("fake caret" after a widget)', () => {
  341. setData( model, '<paragraph>x</paragraph>[<horizontalLine></horizontalLine>]<paragraph>y</paragraph>' );
  342. model.change( writer => {
  343. writer.setSelectionAttribute( 'widget-type-around', 'after' );
  344. } );
  345. const pos = findOptimalInsertionPosition( doc.selection, model );
  346. expect( pos.path ).to.deep.equal( [ 2 ] );
  347. } );
  348. it( 'should return a position after a selected widget (an object) ("fake caret" not displayed)', () => {
  349. setData( model, '<paragraph>x</paragraph>[<horizontalLine></horizontalLine>]<paragraph>y</paragraph>' );
  350. const pos = findOptimalInsertionPosition( doc.selection, model );
  351. expect( pos.path ).to.deep.equal( [ 2 ] );
  352. } );
  353. } );
  354. } );
  355. describe( 'viewToModelPositionOutsideModelElement()', () => {
  356. let mapper, model, modelP, viewP, viewXyz, modelSpan, viewSpan;
  357. beforeEach( () => {
  358. mapper = new Mapper();
  359. model = new Model();
  360. // MODEL: <p>foo<span></span>bar</p>
  361. const modelFoo = new ModelText( 'foo' );
  362. modelSpan = new ModelElement( 'span' );
  363. const modelBar = new ModelText( 'bar' );
  364. modelP = new ModelElement( 'p', null, [ modelFoo, modelSpan, modelBar ] );
  365. // VIEW: <p>foo<span>xyz</span>bar</p>
  366. const viewFoo = new ViewText( viewDocument, 'foo' );
  367. viewXyz = new ViewText( viewDocument, 'xyz' );
  368. viewSpan = new ViewElement( viewDocument, 'span', null, viewXyz );
  369. const viewBar = new ViewText( viewDocument, 'bar' );
  370. viewP = new ViewElement( viewDocument, 'p', null, [ viewFoo, viewSpan, viewBar ] );
  371. mapper.bindElements( modelP, viewP );
  372. mapper.bindElements( modelSpan, viewSpan );
  373. } );
  374. it( 'should map view position that is at the beginning of the view element to a position before the model element', () => {
  375. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  376. // View:
  377. // <p>foo<span>|xyz</span>bar</p>.
  378. const viewPosition = new ViewPosition( viewXyz, 0 );
  379. // Model:
  380. // <p>foo|<span></span>bar</p>.
  381. const modelPosition = mapper.toModelPosition( viewPosition );
  382. expect( modelPosition.path ).to.deep.equal( [ 3 ] );
  383. } );
  384. it( 'should map view position that is in the middle of the view element to a position after the model element', () => {
  385. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  386. // View:
  387. // <p>foo<span>x|yz</span>bar</p>.
  388. const viewPosition = new ViewPosition( viewXyz, 1 );
  389. // Model:
  390. // <p>foo|<span></span>bar</p>.
  391. const modelPosition = mapper.toModelPosition( viewPosition );
  392. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  393. } );
  394. it( 'should map view position that is at the end of the view element to a position after the model element', () => {
  395. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  396. // View:
  397. // <p>foo<span>xyz|</span>bar</p>.
  398. const viewPosition = new ViewPosition( viewXyz, 3 );
  399. // Model:
  400. // <p>foo<span></span>|bar</p>.
  401. const modelPosition = mapper.toModelPosition( viewPosition );
  402. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  403. } );
  404. it( 'should not fire if view element is not matched', () => {
  405. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, () => false ) );
  406. // View:
  407. // <p>foo<span>x|yz</span>bar</p>.
  408. const viewPosition = new ViewPosition( viewXyz, 1 );
  409. // Model:
  410. // <p>foo<span>x|yz</span>bar</p>.
  411. modelSpan._appendChild( new ModelText( 'xyz' ) );
  412. const modelPosition = mapper.toModelPosition( viewPosition );
  413. expect( modelPosition.path ).to.deep.equal( [ 3, 1 ] );
  414. } );
  415. } );
  416. describe( 'centeredBalloonPositionForLongWidgets()', () => {
  417. const arrowVerticalOffset = BalloonPanelView.arrowVerticalOffset;
  418. // Balloon is a 10x10 rect.
  419. const balloonRect = new Rect( {
  420. top: 0,
  421. left: 0,
  422. right: 10,
  423. bottom: 10,
  424. width: 10,
  425. height: 10
  426. } );
  427. beforeEach( () => {
  428. testUtils.sinon.stub( global.window, 'innerWidth' ).value( 100 );
  429. testUtils.sinon.stub( global.window, 'innerHeight' ).value( 100 );
  430. } );
  431. it( 'should return null if there is enough space above the widget', () => {
  432. // Widget is a 50x150 rect, translated (25,25) from viewport's beginning (0,0).
  433. const widgetRect = new Rect( {
  434. top: 25,
  435. left: 25,
  436. right: 75,
  437. bottom: 175,
  438. width: 50,
  439. height: 150
  440. } );
  441. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  442. expect( position ).to.equal( null );
  443. } );
  444. it( 'should return null if there is enough space below the widget', () => {
  445. // Widget is a 50x150 rect, translated (25,-125) from viewport's beginning (0,0).
  446. const widgetRect = new Rect( {
  447. top: -125,
  448. left: 25,
  449. right: 75,
  450. bottom: 25,
  451. width: 50,
  452. height: 150
  453. } );
  454. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  455. expect( position ).to.equal( null );
  456. } );
  457. it( 'should position the balloon inside a widget – at the top + in the middle', () => {
  458. // Widget is a 50x150 rect, translated (25,5) from viewport's beginning (0,0).
  459. const widgetRect = new Rect( {
  460. top: 5,
  461. left: 25,
  462. right: 75,
  463. bottom: 155,
  464. width: 50,
  465. height: 150
  466. } );
  467. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  468. expect( position ).to.deep.equal( {
  469. top: 5 + arrowVerticalOffset,
  470. left: 45,
  471. name: 'arrow_n'
  472. } );
  473. } );
  474. it( 'should stick the balloon to the top of the viewport when the top of a widget is off-screen', () => {
  475. // Widget is a 50x150 rect, translated (25,-25) from viewport's beginning (0,0).
  476. const widgetRect = new Rect( {
  477. top: -25,
  478. left: 25,
  479. right: 75,
  480. bottom: 150,
  481. width: 50,
  482. height: 150
  483. } );
  484. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  485. expect( position ).to.deep.equal( {
  486. top: arrowVerticalOffset,
  487. left: 45,
  488. name: 'arrow_n'
  489. } );
  490. } );
  491. it( 'should horizontally center the balloon in the visible area when the widget is cropped by the viewport', () => {
  492. // Widget is a 50x150 rect, translated (-25,5) from viewport's beginning (0,0).
  493. const widgetRect = new Rect( {
  494. top: 5,
  495. left: -25,
  496. right: 25,
  497. bottom: 155,
  498. width: 50,
  499. height: 150
  500. } );
  501. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  502. expect( position ).to.deep.equal( {
  503. top: 5 + arrowVerticalOffset,
  504. left: 7.5,
  505. name: 'arrow_n'
  506. } );
  507. } );
  508. it( 'should horizontally center the balloon in the widget when the widget is completely off the viewport', () => {
  509. // Widget is a 50x150 rect, translated (0,-100) from viewport's beginning (0,0).
  510. const widgetRect = new Rect( {
  511. top: 0,
  512. left: -100,
  513. right: -50,
  514. bottom: 150,
  515. width: 50,
  516. height: 150
  517. } );
  518. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  519. expect( position ).to.deep.equal( {
  520. top: 0 + arrowVerticalOffset,
  521. left: -80,
  522. name: 'arrow_n'
  523. } );
  524. } );
  525. } );
  526. } );