utils.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. it( 'should throw when attempting to create a widget out of anything but ContainerElement', () => {
  100. expect( () => {
  101. toWidget( writer.createRawElement( 'div' ), writer );
  102. }, 'raw element' ).to.throw( /^widget-to-widget-wrong-element-type/ );
  103. expect( () => {
  104. toWidget( writer.createEmptyElement( 'img' ), writer );
  105. }, 'empty element' ).to.throw( /^widget-to-widget-wrong-element-type/ );
  106. expect( () => {
  107. toWidget( writer.createAttributeElement( 'a' ), writer );
  108. }, 'attribute element' ).to.throw( /^widget-to-widget-wrong-element-type/ );
  109. expect( () => {
  110. toWidget( writer.createUIElement( 'span' ), writer );
  111. }, 'UI element' ).to.throw( /^widget-to-widget-wrong-element-type/ );
  112. } );
  113. } );
  114. describe( 'isWidget()', () => {
  115. it( 'should return true for widgetized elements', () => {
  116. expect( isWidget( element ) ).to.be.true;
  117. } );
  118. it( 'should return false for non-widgetized elements', () => {
  119. expect( isWidget( new ViewElement( viewDocument, 'p' ) ) ).to.be.false;
  120. } );
  121. it( 'should return false for text node', () => {
  122. expect( isWidget( new ViewText( viewDocument, 'p' ) ) ).to.be.false;
  123. } );
  124. } );
  125. describe( 'label utils', () => {
  126. it( 'should allow to set label for element', () => {
  127. const element = new ViewElement( viewDocument, 'p' );
  128. setLabel( element, 'foo bar baz', writer );
  129. expect( getLabel( element ) ).to.equal( 'foo bar baz' );
  130. } );
  131. it( 'should return empty string for elements without label', () => {
  132. const element = new ViewElement( viewDocument, 'div' );
  133. expect( getLabel( element ) ).to.equal( '' );
  134. } );
  135. it( 'should allow to use a function as label creator', () => {
  136. const element = new ViewElement( viewDocument, 'p' );
  137. let caption = 'foo';
  138. setLabel( element, () => caption, writer );
  139. expect( getLabel( element ) ).to.equal( 'foo' );
  140. caption = 'bar';
  141. expect( getLabel( element ) ).to.equal( 'bar' );
  142. } );
  143. } );
  144. describe( 'toWidgetEditable()', () => {
  145. let viewDocument, element;
  146. beforeEach( () => {
  147. viewDocument = new ViewDocument();
  148. element = new ViewEditableElement( viewDocument, 'div' );
  149. toWidgetEditable( element, writer );
  150. } );
  151. it( 'should be created in context of proper document', () => {
  152. expect( element.document ).to.equal( viewDocument );
  153. } );
  154. it( 'should add proper class', () => {
  155. expect( element.hasClass( 'ck-editor__editable', 'ck-editor__nested-editable' ) ).to.be.true;
  156. } );
  157. it( 'should add proper contenteditable value when element is read-only - initialization', () => {
  158. const element = new ViewEditableElement( viewDocument, 'div' );
  159. element.isReadOnly = true;
  160. toWidgetEditable( element, writer );
  161. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  162. } );
  163. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  164. element.isReadOnly = true;
  165. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  166. element.isReadOnly = false;
  167. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  168. } );
  169. it( 'should add proper class when element is focused', () => {
  170. element.isFocused = true;
  171. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.true;
  172. element.isFocused = false;
  173. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.false;
  174. } );
  175. } );
  176. describe( 'addHighlightHandling()', () => {
  177. let element, addSpy, removeSpy, set, remove;
  178. beforeEach( () => {
  179. element = new ViewElement( viewDocument, 'p' );
  180. addSpy = sinon.spy();
  181. removeSpy = sinon.spy();
  182. setHighlightHandling( element, writer, addSpy, removeSpy );
  183. set = element.getCustomProperty( 'addHighlight' );
  184. remove = element.getCustomProperty( 'removeHighlight' );
  185. } );
  186. it( 'should set highlight handling methods', () => {
  187. expect( typeof set ).to.equal( 'function' );
  188. expect( typeof remove ).to.equal( 'function' );
  189. } );
  190. it( 'should call highlight methods when descriptor is added and removed', () => {
  191. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight' };
  192. set( element, descriptor, writer );
  193. remove( element, descriptor.id, writer );
  194. sinon.assert.calledOnce( addSpy );
  195. sinon.assert.calledWithExactly( addSpy, element, descriptor, writer );
  196. sinon.assert.calledOnce( removeSpy );
  197. sinon.assert.calledWithExactly( removeSpy, element, descriptor, writer );
  198. } );
  199. it( 'should call highlight methods when next descriptor is added', () => {
  200. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  201. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  202. set( element, descriptor );
  203. set( element, secondDescriptor );
  204. sinon.assert.calledTwice( addSpy );
  205. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  206. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  207. } );
  208. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  209. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  210. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  211. set( element, descriptor );
  212. set( element, secondDescriptor );
  213. sinon.assert.calledOnce( addSpy );
  214. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  215. } );
  216. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  217. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  218. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  219. set( element, descriptor );
  220. set( element, secondDescriptor );
  221. remove( element, secondDescriptor.id );
  222. sinon.assert.calledThrice( addSpy );
  223. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  224. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  225. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  226. sinon.assert.calledTwice( removeSpy );
  227. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  228. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  229. } );
  230. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  231. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  232. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  233. set( element, descriptor );
  234. set( element, secondDescriptor );
  235. remove( element, secondDescriptor );
  236. sinon.assert.calledOnce( addSpy );
  237. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  238. sinon.assert.notCalled( removeSpy );
  239. } );
  240. it( 'should call highlight methods - CSS class array', () => {
  241. const descriptor = { priority: 10, classes: [ 'highlight', 'a' ], id: 'highlight-1' };
  242. const secondDescriptor = { priority: 10, classes: [ 'highlight', 'b' ], id: 'highlight-2' };
  243. set( element, descriptor );
  244. set( element, secondDescriptor );
  245. sinon.assert.calledTwice( addSpy );
  246. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  247. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  248. } );
  249. } );
  250. describe( 'findOptimalInsertionPosition()', () => {
  251. let model, doc;
  252. beforeEach( () => {
  253. model = new Model();
  254. doc = model.document;
  255. doc.createRoot();
  256. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  257. model.schema.register( 'image' );
  258. model.schema.register( 'span' );
  259. model.schema.extend( 'image', {
  260. allowIn: '$root',
  261. isObject: true,
  262. isBlock: true
  263. } );
  264. model.schema.register( 'horizontalLine', {
  265. isObject: true,
  266. allowWhere: '$block'
  267. } );
  268. model.schema.extend( 'span', { allowIn: 'paragraph' } );
  269. model.schema.extend( '$text', { allowIn: 'span' } );
  270. } );
  271. it( 'returns position after selected element', () => {
  272. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  273. const pos = findOptimalInsertionPosition( doc.selection, model );
  274. expect( pos.path ).to.deep.equal( [ 2 ] );
  275. } );
  276. it( 'returns position before parent block if an inline object is selected', () => {
  277. model.schema.register( 'placeholder', {
  278. allowWhere: '$text',
  279. isInline: true,
  280. isObject: true
  281. } );
  282. setData( model, '<paragraph>x</paragraph><paragraph>f[<placeholder></placeholder>]oo</paragraph><paragraph>y</paragraph>' );
  283. const pos = findOptimalInsertionPosition( doc.selection, model );
  284. expect( pos.path ).to.deep.equal( [ 1 ] );
  285. } );
  286. it( 'returns position inside empty block', () => {
  287. setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  288. const pos = findOptimalInsertionPosition( doc.selection, model );
  289. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  290. } );
  291. it( 'returns position before block if at the beginning of that block', () => {
  292. setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  293. const pos = findOptimalInsertionPosition( doc.selection, model );
  294. expect( pos.path ).to.deep.equal( [ 1 ] );
  295. } );
  296. it( 'returns position before block if in the middle of that block (collapsed selection)', () => {
  297. setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  298. const pos = findOptimalInsertionPosition( doc.selection, model );
  299. expect( pos.path ).to.deep.equal( [ 1 ] );
  300. } );
  301. it( 'returns position before block if in the middle of that block (non-collapsed selection)', () => {
  302. setData( model, '<paragraph>x</paragraph><paragraph>f[o]o</paragraph><paragraph>y</paragraph>' );
  303. const pos = findOptimalInsertionPosition( doc.selection, model );
  304. expect( pos.path ).to.deep.equal( [ 1 ] );
  305. } );
  306. it( 'returns position after block if at the end of that block', () => {
  307. setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  308. const pos = findOptimalInsertionPosition( doc.selection, model );
  309. expect( pos.path ).to.deep.equal( [ 2 ] );
  310. } );
  311. // Checking if isTouching() was used.
  312. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  313. setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  314. const pos = findOptimalInsertionPosition( doc.selection, model );
  315. expect( pos.path ).to.deep.equal( [ 2 ] );
  316. } );
  317. it( 'returns selection focus if not in a block', () => {
  318. model.schema.extend( '$text', { allowIn: '$root' } );
  319. setData( model, 'foo[]bar' );
  320. const pos = findOptimalInsertionPosition( doc.selection, model );
  321. expect( pos.path ).to.deep.equal( [ 3 ] );
  322. } );
  323. // https://github.com/ckeditor/ckeditor5/issues/7438
  324. describe( 'integration with the WidgetTypeAround feature ("widget-type-around" model selection attribute)', () => {
  325. it( 'should respect the attribute value when a widget (block and an object) is selected ("fake caret" before a widget)', () => {
  326. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  327. model.change( writer => {
  328. writer.setSelectionAttribute( 'widget-type-around', 'before' );
  329. } );
  330. const pos = findOptimalInsertionPosition( doc.selection, model );
  331. expect( pos.path ).to.deep.equal( [ 1 ] );
  332. } );
  333. it( 'should respect the attribute value when a widget (block and an object) is selected ("fake caret" after a widget)', () => {
  334. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  335. model.change( writer => {
  336. writer.setSelectionAttribute( 'widget-type-around', 'after' );
  337. } );
  338. const pos = findOptimalInsertionPosition( doc.selection, model );
  339. expect( pos.path ).to.deep.equal( [ 2 ] );
  340. } );
  341. it( 'should return a position after a selected widget (block and an object) ("fake caret" not displayed)', () => {
  342. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  343. const pos = findOptimalInsertionPosition( doc.selection, model );
  344. expect( pos.path ).to.deep.equal( [ 2 ] );
  345. } );
  346. it( 'should respect the attribute value when a widget (an object) is selected ("fake caret" before a widget)', () => {
  347. setData( model, '<paragraph>x</paragraph>[<horizontalLine></horizontalLine>]<paragraph>y</paragraph>' );
  348. model.change( writer => {
  349. writer.setSelectionAttribute( 'widget-type-around', 'before' );
  350. } );
  351. const pos = findOptimalInsertionPosition( doc.selection, model );
  352. expect( pos.path ).to.deep.equal( [ 1 ] );
  353. } );
  354. it( 'should respect the attribute value when a widget (an object) is selected ("fake caret" after a widget)', () => {
  355. setData( model, '<paragraph>x</paragraph>[<horizontalLine></horizontalLine>]<paragraph>y</paragraph>' );
  356. model.change( writer => {
  357. writer.setSelectionAttribute( 'widget-type-around', 'after' );
  358. } );
  359. const pos = findOptimalInsertionPosition( doc.selection, model );
  360. expect( pos.path ).to.deep.equal( [ 2 ] );
  361. } );
  362. it( 'should return a position after a selected widget (an object) ("fake caret" not displayed)', () => {
  363. setData( model, '<paragraph>x</paragraph>[<horizontalLine></horizontalLine>]<paragraph>y</paragraph>' );
  364. const pos = findOptimalInsertionPosition( doc.selection, model );
  365. expect( pos.path ).to.deep.equal( [ 2 ] );
  366. } );
  367. } );
  368. } );
  369. describe( 'viewToModelPositionOutsideModelElement()', () => {
  370. let mapper, model, modelP, viewP, viewXyz, modelSpan, viewSpan;
  371. beforeEach( () => {
  372. mapper = new Mapper();
  373. model = new Model();
  374. // MODEL: <p>foo<span></span>bar</p>
  375. const modelFoo = new ModelText( 'foo' );
  376. modelSpan = new ModelElement( 'span' );
  377. const modelBar = new ModelText( 'bar' );
  378. modelP = new ModelElement( 'p', null, [ modelFoo, modelSpan, modelBar ] );
  379. // VIEW: <p>foo<span>xyz</span>bar</p>
  380. const viewFoo = new ViewText( viewDocument, 'foo' );
  381. viewXyz = new ViewText( viewDocument, 'xyz' );
  382. viewSpan = new ViewElement( viewDocument, 'span', null, viewXyz );
  383. const viewBar = new ViewText( viewDocument, 'bar' );
  384. viewP = new ViewElement( viewDocument, 'p', null, [ viewFoo, viewSpan, viewBar ] );
  385. mapper.bindElements( modelP, viewP );
  386. mapper.bindElements( modelSpan, viewSpan );
  387. } );
  388. it( 'should map view position that is at the beginning of the view element to a position before the model element', () => {
  389. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  390. // View:
  391. // <p>foo<span>|xyz</span>bar</p>.
  392. const viewPosition = new ViewPosition( viewXyz, 0 );
  393. // Model:
  394. // <p>foo|<span></span>bar</p>.
  395. const modelPosition = mapper.toModelPosition( viewPosition );
  396. expect( modelPosition.path ).to.deep.equal( [ 3 ] );
  397. } );
  398. it( 'should map view position that is in the middle of the view element to a position after the model element', () => {
  399. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  400. // View:
  401. // <p>foo<span>x|yz</span>bar</p>.
  402. const viewPosition = new ViewPosition( viewXyz, 1 );
  403. // Model:
  404. // <p>foo|<span></span>bar</p>.
  405. const modelPosition = mapper.toModelPosition( viewPosition );
  406. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  407. } );
  408. it( 'should map view position that is at the end of the view element to a position after the model element', () => {
  409. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  410. // View:
  411. // <p>foo<span>xyz|</span>bar</p>.
  412. const viewPosition = new ViewPosition( viewXyz, 3 );
  413. // Model:
  414. // <p>foo<span></span>|bar</p>.
  415. const modelPosition = mapper.toModelPosition( viewPosition );
  416. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  417. } );
  418. it( 'should not fire if view element is not matched', () => {
  419. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, () => false ) );
  420. // View:
  421. // <p>foo<span>x|yz</span>bar</p>.
  422. const viewPosition = new ViewPosition( viewXyz, 1 );
  423. // Model:
  424. // <p>foo<span>x|yz</span>bar</p>.
  425. modelSpan._appendChild( new ModelText( 'xyz' ) );
  426. const modelPosition = mapper.toModelPosition( viewPosition );
  427. expect( modelPosition.path ).to.deep.equal( [ 3, 1 ] );
  428. } );
  429. } );
  430. describe( 'centeredBalloonPositionForLongWidgets()', () => {
  431. const arrowVerticalOffset = BalloonPanelView.arrowVerticalOffset;
  432. // Balloon is a 10x10 rect.
  433. const balloonRect = new Rect( {
  434. top: 0,
  435. left: 0,
  436. right: 10,
  437. bottom: 10,
  438. width: 10,
  439. height: 10
  440. } );
  441. beforeEach( () => {
  442. testUtils.sinon.stub( global.window, 'innerWidth' ).value( 100 );
  443. testUtils.sinon.stub( global.window, 'innerHeight' ).value( 100 );
  444. } );
  445. it( 'should return null if there is enough space above the widget', () => {
  446. // Widget is a 50x150 rect, translated (25,25) from viewport's beginning (0,0).
  447. const widgetRect = new Rect( {
  448. top: 25,
  449. left: 25,
  450. right: 75,
  451. bottom: 175,
  452. width: 50,
  453. height: 150
  454. } );
  455. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  456. expect( position ).to.equal( null );
  457. } );
  458. it( 'should return null if there is enough space below the widget', () => {
  459. // Widget is a 50x150 rect, translated (25,-125) from viewport's beginning (0,0).
  460. const widgetRect = new Rect( {
  461. top: -125,
  462. left: 25,
  463. right: 75,
  464. bottom: 25,
  465. width: 50,
  466. height: 150
  467. } );
  468. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  469. expect( position ).to.equal( null );
  470. } );
  471. it( 'should position the balloon inside a widget – at the top + in the middle', () => {
  472. // Widget is a 50x150 rect, translated (25,5) from viewport's beginning (0,0).
  473. const widgetRect = new Rect( {
  474. top: 5,
  475. left: 25,
  476. right: 75,
  477. bottom: 155,
  478. width: 50,
  479. height: 150
  480. } );
  481. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  482. expect( position ).to.deep.equal( {
  483. top: 5 + arrowVerticalOffset,
  484. left: 45,
  485. name: 'arrow_n'
  486. } );
  487. } );
  488. it( 'should stick the balloon to the top of the viewport when the top of a widget is off-screen', () => {
  489. // Widget is a 50x150 rect, translated (25,-25) from viewport's beginning (0,0).
  490. const widgetRect = new Rect( {
  491. top: -25,
  492. left: 25,
  493. right: 75,
  494. bottom: 150,
  495. width: 50,
  496. height: 150
  497. } );
  498. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  499. expect( position ).to.deep.equal( {
  500. top: arrowVerticalOffset,
  501. left: 45,
  502. name: 'arrow_n'
  503. } );
  504. } );
  505. it( 'should horizontally center the balloon in the visible area when the widget is cropped by the viewport', () => {
  506. // Widget is a 50x150 rect, translated (-25,5) from viewport's beginning (0,0).
  507. const widgetRect = new Rect( {
  508. top: 5,
  509. left: -25,
  510. right: 25,
  511. bottom: 155,
  512. width: 50,
  513. height: 150
  514. } );
  515. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  516. expect( position ).to.deep.equal( {
  517. top: 5 + arrowVerticalOffset,
  518. left: 7.5,
  519. name: 'arrow_n'
  520. } );
  521. } );
  522. it( 'should horizontally center the balloon in the widget when the widget is completely off the viewport', () => {
  523. // Widget is a 50x150 rect, translated (0,-100) from viewport's beginning (0,0).
  524. const widgetRect = new Rect( {
  525. top: 0,
  526. left: -100,
  527. right: -50,
  528. bottom: 150,
  529. width: 50,
  530. height: 150
  531. } );
  532. const position = centeredBalloonPositionForLongWidgets( widgetRect, balloonRect );
  533. expect( position ).to.deep.equal( {
  534. top: 0 + arrowVerticalOffset,
  535. left: -80,
  536. name: 'arrow_n'
  537. } );
  538. } );
  539. } );
  540. } );