utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. describe( 'UI Utils', () => {
  31. let editor, editingView, balloon, editorElement;
  32. beforeEach( () => {
  33. editorElement = global.document.createElement( 'div' );
  34. global.document.body.appendChild( editorElement );
  35. return ClassicEditor
  36. .create( editorElement, {
  37. plugins: [ Table, TableCellProperties, Paragraph ]
  38. } )
  39. .then( newEditor => {
  40. editor = newEditor;
  41. editingView = editor.editing.view;
  42. balloon = editor.plugins.get( 'ContextualBalloon' );
  43. } );
  44. } );
  45. afterEach( () => {
  46. editorElement.remove();
  47. return editor.destroy();
  48. } );
  49. describe( 'repositionContextualBalloon()', () => {
  50. describe( 'with respect to the table cell', () => {
  51. it( 'should re-position the ContextualBalloon when the table cell is selected', () => {
  52. const spy = sinon.spy( balloon, 'updatePosition' );
  53. const defaultPositions = BalloonPanelView.defaultPositions;
  54. const view = new View();
  55. view.element = global.document.createElement( 'div' );
  56. balloon.add( {
  57. view,
  58. position: {
  59. target: global.document.body
  60. }
  61. } );
  62. setData( editor.model,
  63. '<table><tableRow>' +
  64. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  65. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  66. '</tableRow></table>' );
  67. repositionContextualBalloon( editor, 'cell' );
  68. const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
  69. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  70. sinon.assert.calledWithExactly( spy, {
  71. target: editingView.domConverter.viewToDom( viewCell ),
  72. positions: [
  73. defaultPositions.northArrowSouth,
  74. defaultPositions.northArrowSouthWest,
  75. defaultPositions.northArrowSouthEast,
  76. defaultPositions.southArrowNorth,
  77. defaultPositions.southArrowNorthWest,
  78. defaultPositions.southArrowNorthEast
  79. ]
  80. } );
  81. } );
  82. it( 'should not engage with no table is selected', () => {
  83. const spy = sinon.spy( balloon, 'updatePosition' );
  84. setData( editor.model, '<paragraph>foo</paragraph>' );
  85. repositionContextualBalloon( editor, 'cell' );
  86. sinon.assert.notCalled( spy );
  87. } );
  88. } );
  89. describe( 'with respect to the entire table', () => {
  90. it( 'should re-position the ContextualBalloon when the table is selected', () => {
  91. const spy = sinon.spy( balloon, 'updatePosition' );
  92. const defaultPositions = BalloonPanelView.defaultPositions;
  93. const view = new View();
  94. view.element = global.document.createElement( 'div' );
  95. balloon.add( {
  96. view,
  97. position: {
  98. target: global.document.body
  99. }
  100. } );
  101. setData( editor.model,
  102. '<table><tableRow>' +
  103. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  104. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  105. '</tableRow></table>' );
  106. repositionContextualBalloon( editor, 'table' );
  107. const modelTable = findAncestor( 'table', editor.model.document.selection.getFirstPosition() );
  108. const viewTable = editor.editing.mapper.toViewElement( modelTable );
  109. sinon.assert.calledWithExactly( spy, {
  110. target: editingView.domConverter.viewToDom( viewTable ),
  111. positions: [
  112. defaultPositions.northArrowSouth,
  113. defaultPositions.northArrowSouthWest,
  114. defaultPositions.northArrowSouthEast,
  115. defaultPositions.southArrowNorth,
  116. defaultPositions.southArrowNorthWest,
  117. defaultPositions.southArrowNorthEast
  118. ]
  119. } );
  120. } );
  121. it( 'should not engage with no table is selected', () => {
  122. const spy = sinon.spy( balloon, 'updatePosition' );
  123. setData( editor.model, '<paragraph>foo</paragraph>' );
  124. repositionContextualBalloon( editor, 'table' );
  125. sinon.assert.notCalled( spy );
  126. } );
  127. } );
  128. } );
  129. describe( 'getBalloonCellPositionData()', () => {
  130. it( 'returns the position data', () => {
  131. const defaultPositions = BalloonPanelView.defaultPositions;
  132. setData( editor.model, '<table><tableRow>' +
  133. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  134. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  135. '</tableRow></table>' );
  136. const data = getBalloonCellPositionData( editor );
  137. const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
  138. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  139. expect( data ).to.deep.equal( {
  140. target: editingView.domConverter.viewToDom( viewCell ),
  141. positions: [
  142. defaultPositions.northArrowSouth,
  143. defaultPositions.northArrowSouthWest,
  144. defaultPositions.northArrowSouthEast,
  145. defaultPositions.southArrowNorth,
  146. defaultPositions.southArrowNorthWest,
  147. defaultPositions.southArrowNorthEast
  148. ]
  149. } );
  150. } );
  151. } );
  152. describe( 'getBorderStyleLabels()', () => {
  153. it( 'should return labels for different border styles', () => {
  154. const t = string => string;
  155. expect( getBorderStyleLabels( t ) ).to.deep.equal( {
  156. none: 'None',
  157. solid: 'Solid',
  158. dotted: 'Dotted',
  159. dashed: 'Dashed',
  160. double: 'Double',
  161. groove: 'Groove',
  162. ridge: 'Ridge',
  163. inset: 'Inset',
  164. outset: 'Outset'
  165. } );
  166. } );
  167. } );
  168. describe( 'getLocalizedColorErrorText()', () => {
  169. it( 'should return the error text', () => {
  170. const t = string => string;
  171. expect( getLocalizedColorErrorText( t ) ).to.match( /^The color is invalid/ );
  172. } );
  173. } );
  174. describe( 'getLocalizedLengthErrorText()', () => {
  175. it( 'should return the error text', () => {
  176. const t = string => string;
  177. expect( getLocalizedLengthErrorText( t ) ).to.match( /^The value is invalid/ );
  178. } );
  179. } );
  180. describe( 'colorFieldValidator()', () => {
  181. it( 'should pass for an empty value', () => {
  182. expect( colorFieldValidator( '' ) ).to.be.true;
  183. } );
  184. it( 'should pass for white spaces', () => {
  185. expect( colorFieldValidator( ' ' ) ).to.be.true;
  186. } );
  187. it( 'should pass for colors', () => {
  188. expect( colorFieldValidator( '#FFF' ) ).to.be.true;
  189. expect( colorFieldValidator( '#FFAA11' ) ).to.be.true;
  190. expect( colorFieldValidator( 'rgb(255,123,100)' ) ).to.be.true;
  191. expect( colorFieldValidator( 'red' ) ).to.be.true;
  192. } );
  193. it( 'should pass for colors surrounded by white spaces', () => {
  194. expect( colorFieldValidator( ' #AAA ' ) ).to.be.true;
  195. expect( colorFieldValidator( ' rgb(255,123,100) ' ) ).to.be.true;
  196. } );
  197. } );
  198. describe( 'lengthFieldValidator()', () => {
  199. it( 'should pass for an empty value', () => {
  200. expect( lengthFieldValidator( '' ) ).to.be.true;
  201. } );
  202. it( 'should pass for white spaces', () => {
  203. expect( lengthFieldValidator( ' ' ) ).to.be.true;
  204. } );
  205. it( 'should pass for lengths', () => {
  206. expect( lengthFieldValidator( '1px' ) ).to.be.true;
  207. expect( lengthFieldValidator( '12em' ) ).to.be.true;
  208. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  209. expect( lengthFieldValidator( '45%' ) ).to.be.true;
  210. } );
  211. it( 'should pass for number without unit', () => {
  212. expect( lengthFieldValidator( '1' ) ).to.be.true;
  213. expect( lengthFieldValidator( '12.1' ) ).to.be.true;
  214. expect( lengthFieldValidator( '0.125 ' ) ).to.be.true;
  215. } );
  216. it( 'should not pass for invalid number values', () => {
  217. expect( lengthFieldValidator( '.1 ' ) ).to.be.false;
  218. expect( lengthFieldValidator( '45. ' ) ).to.be.false;
  219. expect( lengthFieldValidator( '45.1.1 ' ) ).to.be.false;
  220. } );
  221. it( 'should pass for lengths surrounded by white spaces', () => {
  222. expect( lengthFieldValidator( '3px ' ) ).to.be.true;
  223. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  224. } );
  225. } );
  226. describe( 'lineWidthFieldValidator()', () => {
  227. it( 'should pass for an empty value', () => {
  228. expect( lineWidthFieldValidator( '' ) ).to.be.true;
  229. } );
  230. it( 'should pass for white spaces', () => {
  231. expect( lineWidthFieldValidator( ' ' ) ).to.be.true;
  232. } );
  233. it( 'should pass for lengths', () => {
  234. expect( lineWidthFieldValidator( '1px' ) ).to.be.true;
  235. expect( lineWidthFieldValidator( '12em' ) ).to.be.true;
  236. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  237. } );
  238. it( 'should pass for number without unit', () => {
  239. expect( lineWidthFieldValidator( '1' ) ).to.be.true;
  240. expect( lineWidthFieldValidator( '12.1' ) ).to.be.true;
  241. expect( lineWidthFieldValidator( '0.125 ' ) ).to.be.true;
  242. } );
  243. it( 'should not pass for invalid number values', () => {
  244. expect( lineWidthFieldValidator( '.1 ' ) ).to.be.false;
  245. expect( lineWidthFieldValidator( '45. ' ) ).to.be.false;
  246. expect( lineWidthFieldValidator( '45.1.1 ' ) ).to.be.false;
  247. expect( lineWidthFieldValidator( '45%' ) ).to.be.false;
  248. } );
  249. it( 'should pass for lengths surrounded by white spaces', () => {
  250. expect( lineWidthFieldValidator( '3px ' ) ).to.be.true;
  251. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  252. } );
  253. } );
  254. describe( 'getBorderStyleDefinitions()', () => {
  255. let view, locale, definitions;
  256. beforeEach( () => {
  257. locale = { t: val => val };
  258. view = new View( locale );
  259. view.set( 'borderStyle', 'none' );
  260. definitions = getBorderStyleDefinitions( view );
  261. } );
  262. it( 'should return a collection', () => {
  263. expect( definitions ).to.be.instanceOf( Collection );
  264. } );
  265. it( 'should create a button definition for each style', () => {
  266. expect( definitions.map( ( { type } ) => type ).every( item => item === 'button' ) ).to.be.true;
  267. } );
  268. it( 'should set label of a button for each style', () => {
  269. expect( definitions.map( ( { model: { label } } ) => label ) ).to.have.ordered.members( [
  270. 'None',
  271. 'Solid',
  272. 'Dotted',
  273. 'Dashed',
  274. 'Double',
  275. 'Groove',
  276. 'Ridge',
  277. 'Inset',
  278. 'Outset'
  279. ] );
  280. } );
  281. it( 'should set type of a button for each style', () => {
  282. expect( definitions.map( ( { model: { withText } } ) => withText ).every( item => item === true ) ).to.be.true;
  283. } );
  284. it( 'should bind button\'s #isOn to the view #borderStyle property', () => {
  285. view.borderStyle = 'dotted';
  286. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  287. false,
  288. false,
  289. true,
  290. false,
  291. false,
  292. false,
  293. false,
  294. false,
  295. false
  296. ] );
  297. view.borderStyle = 'inset';
  298. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  299. false,
  300. false,
  301. false,
  302. false,
  303. false,
  304. false,
  305. false,
  306. true,
  307. false
  308. ] );
  309. } );
  310. } );
  311. describe( 'fillToolbar()', () => {
  312. let view, locale, toolbar;
  313. const labels = {
  314. first: 'Do something',
  315. second: 'Do something else',
  316. third: 'Be default'
  317. };
  318. const icons = {
  319. first: '<svg viewBox="0 0 21 21" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  320. second: '<svg viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  321. third: '<svg viewBox="0 0 23 23" xmlns="http://www.w3.org/2000/svg"><path /></svg>'
  322. };
  323. beforeEach( () => {
  324. locale = { t: val => val };
  325. view = new View( locale );
  326. view.set( 'someProperty', 'foo' );
  327. toolbar = new ToolbarView( locale );
  328. fillToolbar( {
  329. view, toolbar, icons, labels,
  330. propertyName: 'someProperty',
  331. nameToValue: name => name === 'third' ? '' : name
  332. } );
  333. } );
  334. afterEach( () => {
  335. view.destroy();
  336. } );
  337. it( 'should create buttons', () => {
  338. expect( toolbar.items ).to.have.length( 3 );
  339. expect( toolbar.items.first ).to.be.instanceOf( ButtonView );
  340. expect( toolbar.items.get( 1 ) ).to.be.instanceOf( ButtonView );
  341. expect( toolbar.items.last ).to.be.instanceOf( ButtonView );
  342. } );
  343. it( 'should set button labels', () => {
  344. expect( toolbar.items.first.label ).to.equal( 'Do something' );
  345. expect( toolbar.items.get( 1 ).label ).to.equal( 'Do something else' );
  346. expect( toolbar.items.last.label ).to.equal( 'Be default' );
  347. } );
  348. it( 'should set button icons', () => {
  349. expect( toolbar.items.first.icon ).to.equal( icons.first );
  350. expect( toolbar.items.get( 1 ).icon ).to.equal( icons.second );
  351. expect( toolbar.items.last.icon ).to.equal( icons.third );
  352. } );
  353. it( 'should bind button #isOn to an observable property', () => {
  354. expect( toolbar.items.first.isOn ).to.be.false;
  355. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  356. expect( toolbar.items.last.isOn ).to.be.false;
  357. view.someProperty = 'first';
  358. expect( toolbar.items.first.isOn ).to.be.true;
  359. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  360. expect( toolbar.items.last.isOn ).to.be.false;
  361. view.someProperty = 'second';
  362. expect( toolbar.items.first.isOn ).to.be.false;
  363. expect( toolbar.items.get( 1 ).isOn ).to.be.true;
  364. expect( toolbar.items.last.isOn ).to.be.false;
  365. view.someProperty = '';
  366. expect( toolbar.items.first.isOn ).to.be.false;
  367. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  368. expect( toolbar.items.last.isOn ).to.be.true;
  369. } );
  370. it( 'should make the buttons change the property value upon execution', () => {
  371. toolbar.items.first.fire( 'execute' );
  372. expect( view.someProperty ).to.equal( 'first' );
  373. toolbar.items.get( 1 ).fire( 'execute' );
  374. expect( view.someProperty ).to.equal( 'second' );
  375. toolbar.items.last.fire( 'execute' );
  376. expect( view.someProperty ).to.equal( '' );
  377. } );
  378. } );
  379. } );