utils.js 17 KB

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