utils.js 13 KB

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