8
0

utils.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  6. import Table from '../../src/table';
  7. import TableCellProperties from '../../src/tablecellproperties';
  8. import { findAncestor } from '../../src/commands/utils';
  9. import { getTableCellsContainingSelection } from '../../src/utils/utils';
  10. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import View from '@ckeditor/ckeditor5-ui/src/view';
  13. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  14. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  15. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  16. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. import {
  18. repositionContextualBalloon,
  19. getBalloonCellPositionData,
  20. getBorderStyleDefinitions,
  21. getBorderStyleLabels,
  22. getLocalizedColorErrorText,
  23. getLocalizedLengthErrorText,
  24. lengthFieldValidator,
  25. lineWidthFieldValidator,
  26. colorFieldValidator,
  27. fillToolbar
  28. } from '../../src/ui/utils';
  29. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  30. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  31. import { centeredBalloonPositionForLongWidgets } from '@ckeditor/ckeditor5-widget/src/utils';
  32. import { modelTable } from '../_utils/utils';
  33. describe( 'UI Utils', () => {
  34. let editor, editingView, balloon, editorElement;
  35. testUtils.createSinonSandbox();
  36. beforeEach( () => {
  37. editorElement = global.document.createElement( 'div' );
  38. global.document.body.appendChild( editorElement );
  39. return ClassicEditor
  40. .create( editorElement, {
  41. plugins: [ Table, TableCellProperties, Paragraph ]
  42. } )
  43. .then( newEditor => {
  44. editor = newEditor;
  45. editingView = editor.editing.view;
  46. balloon = editor.plugins.get( 'ContextualBalloon' );
  47. } );
  48. } );
  49. afterEach( () => {
  50. editorElement.remove();
  51. return editor.destroy();
  52. } );
  53. describe( 'repositionContextualBalloon()', () => {
  54. describe( 'with respect to the table cell', () => {
  55. it( 'should re-position the ContextualBalloon when the table cell is selected', () => {
  56. const spy = sinon.spy( balloon, 'updatePosition' );
  57. const defaultPositions = BalloonPanelView.defaultPositions;
  58. const view = new View();
  59. view.element = global.document.createElement( 'div' );
  60. balloon.add( {
  61. view,
  62. position: {
  63. target: global.document.body
  64. }
  65. } );
  66. setData( editor.model,
  67. '<table><tableRow>' +
  68. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  69. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  70. '</tableRow></table>' );
  71. repositionContextualBalloon( editor, 'cell' );
  72. const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
  73. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  74. sinon.assert.calledWithExactly( spy, {
  75. target: editingView.domConverter.viewToDom( viewCell ),
  76. positions: [
  77. defaultPositions.northArrowSouth,
  78. defaultPositions.northArrowSouthWest,
  79. defaultPositions.northArrowSouthEast,
  80. defaultPositions.southArrowNorth,
  81. defaultPositions.southArrowNorthWest,
  82. defaultPositions.southArrowNorthEast
  83. ]
  84. } );
  85. } );
  86. it( 'should not engage with no table is selected', () => {
  87. const spy = sinon.spy( balloon, 'updatePosition' );
  88. setData( editor.model, '<paragraph>foo</paragraph>' );
  89. repositionContextualBalloon( editor, 'cell' );
  90. sinon.assert.notCalled( spy );
  91. } );
  92. } );
  93. describe( 'with respect to the entire table', () => {
  94. it( 'should re-position the ContextualBalloon when the table is selected', () => {
  95. const spy = sinon.spy( balloon, 'updatePosition' );
  96. const defaultPositions = BalloonPanelView.defaultPositions;
  97. const view = new View();
  98. view.element = global.document.createElement( 'div' );
  99. balloon.add( {
  100. view,
  101. position: {
  102. target: global.document.body
  103. }
  104. } );
  105. setData( editor.model,
  106. '<table><tableRow>' +
  107. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  108. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  109. '</tableRow></table>' );
  110. repositionContextualBalloon( editor, 'table' );
  111. const modelTable = findAncestor( 'table', editor.model.document.selection.getFirstPosition() );
  112. const viewTable = editor.editing.mapper.toViewElement( modelTable );
  113. sinon.assert.calledWithExactly( spy, {
  114. target: editingView.domConverter.viewToDom( viewTable ),
  115. positions: [
  116. defaultPositions.northArrowSouth,
  117. defaultPositions.northArrowSouthWest,
  118. defaultPositions.northArrowSouthEast,
  119. defaultPositions.southArrowNorth,
  120. defaultPositions.southArrowNorthWest,
  121. defaultPositions.southArrowNorthEast,
  122. centeredBalloonPositionForLongWidgets
  123. ]
  124. } );
  125. } );
  126. it( 'should not engage with no table is selected', () => {
  127. const spy = sinon.spy( balloon, 'updatePosition' );
  128. setData( editor.model, '<paragraph>foo</paragraph>' );
  129. repositionContextualBalloon( editor, 'table' );
  130. sinon.assert.notCalled( spy );
  131. } );
  132. } );
  133. } );
  134. describe( 'getBalloonCellPositionData()', () => {
  135. let modelRoot;
  136. beforeEach( () => {
  137. setData( editor.model, modelTable( [
  138. [ '11[]', '12', '13' ],
  139. [ '21', '22', '23' ],
  140. [ '31', '32', '33' ]
  141. ] ) );
  142. modelRoot = editor.model.document.getRoot();
  143. for ( let row = 0; row < 3; row++ ) {
  144. for ( let col = 0; col < 3; col++ ) {
  145. const modelCell = modelRoot.getNodeByPath( [ 0, row, col ] );
  146. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  147. const cellDomElement = editingView.domConverter.viewToDom( viewCell );
  148. mockBoundingBox( cellDomElement, {
  149. top: 100 + row * 10,
  150. left: 100 + col * 10,
  151. height: 10,
  152. width: 10
  153. } );
  154. }
  155. }
  156. } );
  157. it( 'returns the position data', () => {
  158. const defaultPositions = BalloonPanelView.defaultPositions;
  159. const data = getBalloonCellPositionData( editor );
  160. const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
  161. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  162. expect( data ).to.deep.equal( {
  163. target: editingView.domConverter.viewToDom( viewCell ),
  164. positions: [
  165. defaultPositions.northArrowSouth,
  166. defaultPositions.northArrowSouthWest,
  167. defaultPositions.northArrowSouthEast,
  168. defaultPositions.southArrowNorth,
  169. defaultPositions.southArrowNorthWest,
  170. defaultPositions.southArrowNorthEast
  171. ]
  172. } );
  173. } );
  174. it( 'returns the position data for multiple cells selected horizontally', () => {
  175. selectTableCells( [
  176. [ 0, 0 ],
  177. [ 0, 1 ]
  178. ] );
  179. const data = getBalloonCellPositionData( editor );
  180. const targetData = data.target();
  181. expect( targetData ).to.deep.equal( {
  182. top: 100,
  183. left: 100,
  184. right: 120,
  185. bottom: 110,
  186. width: 20,
  187. height: 10
  188. } );
  189. } );
  190. it( 'returns the position data for multiple cells selected vertically', () => {
  191. selectTableCells( [
  192. [ 0, 1 ],
  193. [ 1, 1 ]
  194. ] );
  195. const data = getBalloonCellPositionData( editor );
  196. const targetData = data.target();
  197. expect( targetData ).to.deep.equal( {
  198. top: 100,
  199. left: 110,
  200. right: 120,
  201. bottom: 120,
  202. width: 10,
  203. height: 20
  204. } );
  205. } );
  206. it( 'returns the position data for multiple cells selected', () => {
  207. selectTableCells( [
  208. [ 0, 1 ],
  209. [ 1, 0 ],
  210. [ 1, 1 ]
  211. ] );
  212. const data = getBalloonCellPositionData( editor );
  213. const targetData = data.target();
  214. expect( targetData ).to.deep.equal( {
  215. top: 100,
  216. left: 100,
  217. right: 120,
  218. bottom: 120,
  219. width: 20,
  220. height: 20
  221. } );
  222. } );
  223. function selectTableCells( paths ) {
  224. editor.model.change( writer => {
  225. writer.setSelection( paths.map( path => writer.createRangeOn( modelRoot.getNodeByPath( [ 0, ...path ] ) ) ) );
  226. } );
  227. }
  228. function mockBoundingBox( element, data ) {
  229. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  230. ...data,
  231. right: data.left + data.width,
  232. bottom: data.top + data.height
  233. } );
  234. }
  235. } );
  236. describe( 'getBorderStyleLabels()', () => {
  237. it( 'should return labels for different border styles', () => {
  238. const t = string => string;
  239. expect( getBorderStyleLabels( t ) ).to.deep.equal( {
  240. none: 'None',
  241. solid: 'Solid',
  242. dotted: 'Dotted',
  243. dashed: 'Dashed',
  244. double: 'Double',
  245. groove: 'Groove',
  246. ridge: 'Ridge',
  247. inset: 'Inset',
  248. outset: 'Outset'
  249. } );
  250. } );
  251. } );
  252. describe( 'getLocalizedColorErrorText()', () => {
  253. it( 'should return the error text', () => {
  254. const t = string => string;
  255. expect( getLocalizedColorErrorText( t ) ).to.match( /^The color is invalid/ );
  256. } );
  257. } );
  258. describe( 'getLocalizedLengthErrorText()', () => {
  259. it( 'should return the error text', () => {
  260. const t = string => string;
  261. expect( getLocalizedLengthErrorText( t ) ).to.match( /^The value is invalid/ );
  262. } );
  263. } );
  264. describe( 'colorFieldValidator()', () => {
  265. it( 'should pass for an empty value', () => {
  266. expect( colorFieldValidator( '' ) ).to.be.true;
  267. } );
  268. it( 'should pass for white spaces', () => {
  269. expect( colorFieldValidator( ' ' ) ).to.be.true;
  270. } );
  271. it( 'should pass for colors', () => {
  272. expect( colorFieldValidator( '#FFF' ) ).to.be.true;
  273. expect( colorFieldValidator( '#FFAA11' ) ).to.be.true;
  274. expect( colorFieldValidator( 'rgb(255,123,100)' ) ).to.be.true;
  275. expect( colorFieldValidator( 'red' ) ).to.be.true;
  276. } );
  277. it( 'should pass for colors surrounded by white spaces', () => {
  278. expect( colorFieldValidator( ' #AAA ' ) ).to.be.true;
  279. expect( colorFieldValidator( ' rgb(255,123,100) ' ) ).to.be.true;
  280. } );
  281. } );
  282. describe( 'lengthFieldValidator()', () => {
  283. it( 'should pass for an empty value', () => {
  284. expect( lengthFieldValidator( '' ) ).to.be.true;
  285. } );
  286. it( 'should pass for white spaces', () => {
  287. expect( lengthFieldValidator( ' ' ) ).to.be.true;
  288. } );
  289. it( 'should pass for lengths', () => {
  290. expect( lengthFieldValidator( '1px' ) ).to.be.true;
  291. expect( lengthFieldValidator( '12em' ) ).to.be.true;
  292. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  293. expect( lengthFieldValidator( '45%' ) ).to.be.true;
  294. } );
  295. it( 'should pass for number without unit', () => {
  296. expect( lengthFieldValidator( '1' ) ).to.be.true;
  297. expect( lengthFieldValidator( '12.1' ) ).to.be.true;
  298. expect( lengthFieldValidator( '0.125 ' ) ).to.be.true;
  299. } );
  300. it( 'should not pass for invalid number values', () => {
  301. expect( lengthFieldValidator( '.1 ' ) ).to.be.false;
  302. expect( lengthFieldValidator( '45. ' ) ).to.be.false;
  303. expect( lengthFieldValidator( '45.1.1 ' ) ).to.be.false;
  304. } );
  305. it( 'should pass for lengths surrounded by white spaces', () => {
  306. expect( lengthFieldValidator( '3px ' ) ).to.be.true;
  307. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  308. } );
  309. } );
  310. describe( 'lineWidthFieldValidator()', () => {
  311. it( 'should pass for an empty value', () => {
  312. expect( lineWidthFieldValidator( '' ) ).to.be.true;
  313. } );
  314. it( 'should pass for white spaces', () => {
  315. expect( lineWidthFieldValidator( ' ' ) ).to.be.true;
  316. } );
  317. it( 'should pass for lengths', () => {
  318. expect( lineWidthFieldValidator( '1px' ) ).to.be.true;
  319. expect( lineWidthFieldValidator( '12em' ) ).to.be.true;
  320. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  321. } );
  322. it( 'should pass for number without unit', () => {
  323. expect( lineWidthFieldValidator( '1' ) ).to.be.true;
  324. expect( lineWidthFieldValidator( '12.1' ) ).to.be.true;
  325. expect( lineWidthFieldValidator( '0.125 ' ) ).to.be.true;
  326. } );
  327. it( 'should not pass for invalid number values', () => {
  328. expect( lineWidthFieldValidator( '.1 ' ) ).to.be.false;
  329. expect( lineWidthFieldValidator( '45. ' ) ).to.be.false;
  330. expect( lineWidthFieldValidator( '45.1.1 ' ) ).to.be.false;
  331. expect( lineWidthFieldValidator( '45%' ) ).to.be.false;
  332. } );
  333. it( 'should pass for lengths surrounded by white spaces', () => {
  334. expect( lineWidthFieldValidator( '3px ' ) ).to.be.true;
  335. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  336. } );
  337. } );
  338. describe( 'getBorderStyleDefinitions()', () => {
  339. let view, locale, definitions;
  340. beforeEach( () => {
  341. locale = { t: val => val };
  342. view = new View( locale );
  343. view.set( 'borderStyle', 'none' );
  344. definitions = getBorderStyleDefinitions( view );
  345. } );
  346. it( 'should return a collection', () => {
  347. expect( definitions ).to.be.instanceOf( Collection );
  348. } );
  349. it( 'should create a button definition for each style', () => {
  350. expect( definitions.map( ( { type } ) => type ).every( item => item === 'button' ) ).to.be.true;
  351. } );
  352. it( 'should set label of a button for each style', () => {
  353. expect( definitions.map( ( { model: { label } } ) => label ) ).to.have.ordered.members( [
  354. 'None',
  355. 'Solid',
  356. 'Dotted',
  357. 'Dashed',
  358. 'Double',
  359. 'Groove',
  360. 'Ridge',
  361. 'Inset',
  362. 'Outset'
  363. ] );
  364. } );
  365. it( 'should set type of a button for each style', () => {
  366. expect( definitions.map( ( { model: { withText } } ) => withText ).every( item => item === true ) ).to.be.true;
  367. } );
  368. it( 'should bind button\'s #isOn to the view #borderStyle property', () => {
  369. view.borderStyle = 'dotted';
  370. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  371. false,
  372. false,
  373. true,
  374. false,
  375. false,
  376. false,
  377. false,
  378. false,
  379. false
  380. ] );
  381. view.borderStyle = 'inset';
  382. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  383. false,
  384. false,
  385. false,
  386. false,
  387. false,
  388. false,
  389. false,
  390. true,
  391. false
  392. ] );
  393. } );
  394. } );
  395. describe( 'fillToolbar()', () => {
  396. let view, locale, toolbar;
  397. const labels = {
  398. first: 'Do something',
  399. second: 'Do something else',
  400. third: 'Be default'
  401. };
  402. const icons = {
  403. first: '<svg viewBox="0 0 21 21" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  404. second: '<svg viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  405. third: '<svg viewBox="0 0 23 23" xmlns="http://www.w3.org/2000/svg"><path /></svg>'
  406. };
  407. beforeEach( () => {
  408. locale = { t: val => val };
  409. view = new View( locale );
  410. view.set( 'someProperty', 'foo' );
  411. toolbar = new ToolbarView( locale );
  412. fillToolbar( {
  413. view, toolbar, icons, labels,
  414. propertyName: 'someProperty',
  415. nameToValue: name => name === 'third' ? '' : name
  416. } );
  417. } );
  418. afterEach( () => {
  419. view.destroy();
  420. } );
  421. it( 'should create buttons', () => {
  422. expect( toolbar.items ).to.have.length( 3 );
  423. expect( toolbar.items.first ).to.be.instanceOf( ButtonView );
  424. expect( toolbar.items.get( 1 ) ).to.be.instanceOf( ButtonView );
  425. expect( toolbar.items.last ).to.be.instanceOf( ButtonView );
  426. } );
  427. it( 'should set button labels', () => {
  428. expect( toolbar.items.first.label ).to.equal( 'Do something' );
  429. expect( toolbar.items.get( 1 ).label ).to.equal( 'Do something else' );
  430. expect( toolbar.items.last.label ).to.equal( 'Be default' );
  431. } );
  432. it( 'should set button icons', () => {
  433. expect( toolbar.items.first.icon ).to.equal( icons.first );
  434. expect( toolbar.items.get( 1 ).icon ).to.equal( icons.second );
  435. expect( toolbar.items.last.icon ).to.equal( icons.third );
  436. } );
  437. it( 'should set button tooltips', () => {
  438. expect( toolbar.items.first.tooltip ).to.equal( labels.first );
  439. expect( toolbar.items.get( 1 ).tooltip ).to.equal( labels.second );
  440. expect( toolbar.items.last.tooltip ).to.equal( labels.third );
  441. } );
  442. it( 'should bind button #isOn to an observable property', () => {
  443. expect( toolbar.items.first.isOn ).to.be.false;
  444. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  445. expect( toolbar.items.last.isOn ).to.be.false;
  446. view.someProperty = 'first';
  447. expect( toolbar.items.first.isOn ).to.be.true;
  448. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  449. expect( toolbar.items.last.isOn ).to.be.false;
  450. view.someProperty = 'second';
  451. expect( toolbar.items.first.isOn ).to.be.false;
  452. expect( toolbar.items.get( 1 ).isOn ).to.be.true;
  453. expect( toolbar.items.last.isOn ).to.be.false;
  454. view.someProperty = '';
  455. expect( toolbar.items.first.isOn ).to.be.false;
  456. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  457. expect( toolbar.items.last.isOn ).to.be.true;
  458. } );
  459. it( 'should make the buttons change the property value upon execution', () => {
  460. toolbar.items.first.fire( 'execute' );
  461. expect( view.someProperty ).to.equal( 'first' );
  462. toolbar.items.get( 1 ).fire( 'execute' );
  463. expect( view.someProperty ).to.equal( 'second' );
  464. toolbar.items.last.fire( 'execute' );
  465. expect( view.someProperty ).to.equal( '' );
  466. } );
  467. } );
  468. } );