utils.js 17 KB

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