table-properties.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 LabeledFieldView from '@ckeditor/ckeditor5-ui/src/labeledfield/labeledfieldview';
  13. import ColorInputView from '../../../src/ui/colorinputview';
  14. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  15. import {
  16. getBorderStyleDefinitions,
  17. getBorderStyleLabels,
  18. getLocalizedColorErrorText,
  19. getLocalizedLengthErrorText,
  20. lengthFieldValidator,
  21. lineWidthFieldValidator,
  22. colorFieldValidator,
  23. fillToolbar,
  24. getLabeledColorInputCreator
  25. } from '../../../src/utils/ui/table-properties';
  26. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  27. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  28. describe( 'table utils', () => {
  29. let editor, editorElement;
  30. testUtils.createSinonSandbox();
  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. } );
  41. } );
  42. afterEach( () => {
  43. editorElement.remove();
  44. return editor.destroy();
  45. } );
  46. describe( 'ui - table properties', () => {
  47. describe( 'getBorderStyleLabels()', () => {
  48. it( 'should return labels for different border styles', () => {
  49. const t = string => string;
  50. expect( getBorderStyleLabels( t ) ).to.deep.equal( {
  51. none: 'None',
  52. solid: 'Solid',
  53. dotted: 'Dotted',
  54. dashed: 'Dashed',
  55. double: 'Double',
  56. groove: 'Groove',
  57. ridge: 'Ridge',
  58. inset: 'Inset',
  59. outset: 'Outset'
  60. } );
  61. } );
  62. } );
  63. describe( 'getLocalizedColorErrorText()', () => {
  64. it( 'should return the error text', () => {
  65. const t = string => string;
  66. expect( getLocalizedColorErrorText( t ) ).to.match( /^The color is invalid/ );
  67. } );
  68. } );
  69. describe( 'getLocalizedLengthErrorText()', () => {
  70. it( 'should return the error text', () => {
  71. const t = string => string;
  72. expect( getLocalizedLengthErrorText( t ) ).to.match( /^The value is invalid/ );
  73. } );
  74. } );
  75. describe( 'colorFieldValidator()', () => {
  76. it( 'should pass for an empty value', () => {
  77. expect( colorFieldValidator( '' ) ).to.be.true;
  78. } );
  79. it( 'should pass for white spaces', () => {
  80. expect( colorFieldValidator( ' ' ) ).to.be.true;
  81. } );
  82. it( 'should pass for colors', () => {
  83. expect( colorFieldValidator( '#FFF' ) ).to.be.true;
  84. expect( colorFieldValidator( '#FFAA11' ) ).to.be.true;
  85. expect( colorFieldValidator( 'rgb(255,123,100)' ) ).to.be.true;
  86. expect( colorFieldValidator( 'red' ) ).to.be.true;
  87. } );
  88. it( 'should pass for colors surrounded by white spaces', () => {
  89. expect( colorFieldValidator( ' #AAA ' ) ).to.be.true;
  90. expect( colorFieldValidator( ' rgb(255,123,100) ' ) ).to.be.true;
  91. } );
  92. } );
  93. describe( 'lengthFieldValidator()', () => {
  94. it( 'should pass for an empty value', () => {
  95. expect( lengthFieldValidator( '' ) ).to.be.true;
  96. } );
  97. it( 'should pass for white spaces', () => {
  98. expect( lengthFieldValidator( ' ' ) ).to.be.true;
  99. } );
  100. it( 'should pass for lengths', () => {
  101. expect( lengthFieldValidator( '1px' ) ).to.be.true;
  102. expect( lengthFieldValidator( '12em' ) ).to.be.true;
  103. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  104. expect( lengthFieldValidator( '45%' ) ).to.be.true;
  105. } );
  106. it( 'should pass for number without unit', () => {
  107. expect( lengthFieldValidator( '1' ) ).to.be.true;
  108. expect( lengthFieldValidator( '12.1' ) ).to.be.true;
  109. expect( lengthFieldValidator( '0.125 ' ) ).to.be.true;
  110. } );
  111. it( 'should not pass for invalid number values', () => {
  112. expect( lengthFieldValidator( '.1 ' ) ).to.be.false;
  113. expect( lengthFieldValidator( '45. ' ) ).to.be.false;
  114. expect( lengthFieldValidator( '45.1.1 ' ) ).to.be.false;
  115. } );
  116. it( 'should pass for lengths surrounded by white spaces', () => {
  117. expect( lengthFieldValidator( '3px ' ) ).to.be.true;
  118. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  119. } );
  120. } );
  121. describe( 'lineWidthFieldValidator()', () => {
  122. it( 'should pass for an empty value', () => {
  123. expect( lineWidthFieldValidator( '' ) ).to.be.true;
  124. } );
  125. it( 'should pass for white spaces', () => {
  126. expect( lineWidthFieldValidator( ' ' ) ).to.be.true;
  127. } );
  128. it( 'should pass for lengths', () => {
  129. expect( lineWidthFieldValidator( '1px' ) ).to.be.true;
  130. expect( lineWidthFieldValidator( '12em' ) ).to.be.true;
  131. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  132. } );
  133. it( 'should pass for number without unit', () => {
  134. expect( lineWidthFieldValidator( '1' ) ).to.be.true;
  135. expect( lineWidthFieldValidator( '12.1' ) ).to.be.true;
  136. expect( lineWidthFieldValidator( '0.125 ' ) ).to.be.true;
  137. } );
  138. it( 'should not pass for invalid number values', () => {
  139. expect( lineWidthFieldValidator( '.1 ' ) ).to.be.false;
  140. expect( lineWidthFieldValidator( '45. ' ) ).to.be.false;
  141. expect( lineWidthFieldValidator( '45.1.1 ' ) ).to.be.false;
  142. expect( lineWidthFieldValidator( '45%' ) ).to.be.false;
  143. } );
  144. it( 'should pass for lengths surrounded by white spaces', () => {
  145. expect( lineWidthFieldValidator( '3px ' ) ).to.be.true;
  146. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  147. } );
  148. } );
  149. describe( 'getBorderStyleDefinitions()', () => {
  150. let view, locale, definitions;
  151. beforeEach( () => {
  152. locale = { t: val => val };
  153. view = new View( locale );
  154. view.set( 'borderStyle', 'none' );
  155. definitions = getBorderStyleDefinitions( view );
  156. } );
  157. it( 'should return a collection', () => {
  158. expect( definitions ).to.be.instanceOf( Collection );
  159. } );
  160. it( 'should create a button definition for each style', () => {
  161. expect( definitions.map( ( { type } ) => type ).every( item => item === 'button' ) ).to.be.true;
  162. } );
  163. it( 'should set label of a button for each style', () => {
  164. expect( definitions.map( ( { model: { label } } ) => label ) ).to.have.ordered.members( [
  165. 'None',
  166. 'Solid',
  167. 'Dotted',
  168. 'Dashed',
  169. 'Double',
  170. 'Groove',
  171. 'Ridge',
  172. 'Inset',
  173. 'Outset'
  174. ] );
  175. } );
  176. it( 'should set type of a button for each style', () => {
  177. expect( definitions.map( ( { model: { withText } } ) => withText ).every( item => item === true ) ).to.be.true;
  178. } );
  179. it( 'should bind button\'s #isOn to the view #borderStyle property', () => {
  180. view.borderStyle = 'dotted';
  181. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  182. false,
  183. false,
  184. true,
  185. false,
  186. false,
  187. false,
  188. false,
  189. false,
  190. false
  191. ] );
  192. view.borderStyle = 'inset';
  193. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  194. false,
  195. false,
  196. false,
  197. false,
  198. false,
  199. false,
  200. false,
  201. true,
  202. false
  203. ] );
  204. } );
  205. } );
  206. describe( 'fillToolbar()', () => {
  207. let view, locale, toolbar;
  208. const labels = {
  209. first: 'Do something',
  210. second: 'Do something else',
  211. third: 'Be default'
  212. };
  213. const icons = {
  214. first: '<svg viewBox="0 0 21 21" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  215. second: '<svg viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  216. third: '<svg viewBox="0 0 23 23" xmlns="http://www.w3.org/2000/svg"><path /></svg>'
  217. };
  218. beforeEach( () => {
  219. locale = { t: val => val };
  220. view = new View( locale );
  221. view.set( 'someProperty', 'foo' );
  222. toolbar = new ToolbarView( locale );
  223. fillToolbar( {
  224. view, toolbar, icons, labels,
  225. propertyName: 'someProperty',
  226. nameToValue: name => name === 'third' ? '' : name
  227. } );
  228. } );
  229. afterEach( () => {
  230. view.destroy();
  231. } );
  232. it( 'should create buttons', () => {
  233. expect( toolbar.items ).to.have.length( 3 );
  234. expect( toolbar.items.first ).to.be.instanceOf( ButtonView );
  235. expect( toolbar.items.get( 1 ) ).to.be.instanceOf( ButtonView );
  236. expect( toolbar.items.last ).to.be.instanceOf( ButtonView );
  237. } );
  238. it( 'should set button labels', () => {
  239. expect( toolbar.items.first.label ).to.equal( 'Do something' );
  240. expect( toolbar.items.get( 1 ).label ).to.equal( 'Do something else' );
  241. expect( toolbar.items.last.label ).to.equal( 'Be default' );
  242. } );
  243. it( 'should set button icons', () => {
  244. expect( toolbar.items.first.icon ).to.equal( icons.first );
  245. expect( toolbar.items.get( 1 ).icon ).to.equal( icons.second );
  246. expect( toolbar.items.last.icon ).to.equal( icons.third );
  247. } );
  248. it( 'should set button tooltips', () => {
  249. expect( toolbar.items.first.tooltip ).to.equal( labels.first );
  250. expect( toolbar.items.get( 1 ).tooltip ).to.equal( labels.second );
  251. expect( toolbar.items.last.tooltip ).to.equal( labels.third );
  252. } );
  253. it( 'should bind button #isOn to an observable property', () => {
  254. expect( toolbar.items.first.isOn ).to.be.false;
  255. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  256. expect( toolbar.items.last.isOn ).to.be.false;
  257. view.someProperty = 'first';
  258. expect( toolbar.items.first.isOn ).to.be.true;
  259. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  260. expect( toolbar.items.last.isOn ).to.be.false;
  261. view.someProperty = 'second';
  262. expect( toolbar.items.first.isOn ).to.be.false;
  263. expect( toolbar.items.get( 1 ).isOn ).to.be.true;
  264. expect( toolbar.items.last.isOn ).to.be.false;
  265. view.someProperty = '';
  266. expect( toolbar.items.first.isOn ).to.be.false;
  267. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  268. expect( toolbar.items.last.isOn ).to.be.true;
  269. } );
  270. it( 'should make the buttons change the property value upon execution', () => {
  271. toolbar.items.first.fire( 'execute' );
  272. expect( view.someProperty ).to.equal( 'first' );
  273. toolbar.items.get( 1 ).fire( 'execute' );
  274. expect( view.someProperty ).to.equal( 'second' );
  275. toolbar.items.last.fire( 'execute' );
  276. expect( view.someProperty ).to.equal( '' );
  277. } );
  278. } );
  279. describe( 'getLabeledColorInputCreator()', () => {
  280. let creator, labeledField;
  281. const colorConfig = [
  282. {
  283. color: 'hsl(180, 75%, 60%)',
  284. label: 'Turquoise'
  285. },
  286. {
  287. color: 'hsl(210, 75%, 60%)',
  288. label: 'Light blue'
  289. }
  290. ];
  291. beforeEach( () => {
  292. creator = getLabeledColorInputCreator( {
  293. colorConfig,
  294. columns: 3
  295. } );
  296. labeledField = new LabeledFieldView( { t: () => {} }, creator );
  297. } );
  298. afterEach( () => {
  299. labeledField.destroy();
  300. } );
  301. it( 'should return a function', () => {
  302. expect( creator ).to.be.a( 'function' );
  303. } );
  304. it( 'should pass the options.colorConfig on', () => {
  305. expect( labeledField.fieldView.options.colorDefinitions ).to.have.length( 2 );
  306. } );
  307. it( 'should pass the options.columns on', () => {
  308. expect( labeledField.fieldView.options.columns ).to.equal( 3 );
  309. } );
  310. it( 'should return a ColorInputView instance', () => {
  311. expect( labeledField.fieldView ).to.be.instanceOf( ColorInputView );
  312. } );
  313. it( 'should set ColorInputView#id', () => {
  314. expect( labeledField.fieldView.id ).to.match( /^ck-labeled-field-view-.+/ );
  315. } );
  316. it( 'should set ColorInputView#ariaDescribedById', () => {
  317. expect( labeledField.fieldView.ariaDescribedById ).to.match( /^ck-labeled-field-view-status-.+/ );
  318. } );
  319. it( 'should bind ColorInputView#isReadOnly to LabeledFieldView#isEnabled', () => {
  320. labeledField.isEnabled = true;
  321. expect( labeledField.fieldView.isReadOnly ).to.be.false;
  322. labeledField.isEnabled = false;
  323. expect( labeledField.fieldView.isReadOnly ).to.be.true;
  324. } );
  325. it( 'should bind ColorInputView#hasError to LabeledFieldView#errorText', () => {
  326. labeledField.errorText = 'foo';
  327. expect( labeledField.fieldView.hasError ).to.be.true;
  328. labeledField.errorText = null;
  329. expect( labeledField.fieldView.hasError ).to.be.false;
  330. } );
  331. it( 'should clear labeld field view #errorText upon #input event', () => {
  332. labeledField.errorText = 'foo';
  333. labeledField.fieldView.fire( 'input' );
  334. expect( labeledField.errorText ).to.be.null;
  335. } );
  336. it( 'should bind LabeledFieldView#isEmpty to the ColorInputView instance', () => {
  337. labeledField.fieldView.isEmpty = true;
  338. expect( labeledField.isEmpty ).to.be.true;
  339. labeledField.fieldView.isEmpty = false;
  340. expect( labeledField.isEmpty ).to.be.false;
  341. } );
  342. it( 'should bind LabeledFieldView#isFocused to the ColorInputView instance', () => {
  343. labeledField.fieldView.isFocused = true;
  344. expect( labeledField.isFocused ).to.be.true;
  345. labeledField.fieldView.isFocused = false;
  346. expect( labeledField.isFocused ).to.be.false;
  347. } );
  348. } );
  349. } );
  350. } );