8
0

utils.js 14 KB

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