table-properties.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  13. import {
  14. getBorderStyleDefinitions,
  15. getBorderStyleLabels,
  16. getLocalizedColorErrorText,
  17. getLocalizedLengthErrorText,
  18. lengthFieldValidator,
  19. lineWidthFieldValidator,
  20. colorFieldValidator,
  21. fillToolbar
  22. } from '../../../src/utils/ui/table-properties';
  23. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  24. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  25. describe( 'table utils', () => {
  26. let editor, editorElement;
  27. testUtils.createSinonSandbox();
  28. beforeEach( () => {
  29. editorElement = global.document.createElement( 'div' );
  30. global.document.body.appendChild( editorElement );
  31. return ClassicEditor
  32. .create( editorElement, {
  33. plugins: [ Table, TableCellProperties, Paragraph ]
  34. } )
  35. .then( newEditor => {
  36. editor = newEditor;
  37. } );
  38. } );
  39. afterEach( () => {
  40. editorElement.remove();
  41. return editor.destroy();
  42. } );
  43. describe( 'ui - table properties', () => {
  44. describe( 'getBorderStyleLabels()', () => {
  45. it( 'should return labels for different border styles', () => {
  46. const t = string => string;
  47. expect( getBorderStyleLabels( t ) ).to.deep.equal( {
  48. none: 'None',
  49. solid: 'Solid',
  50. dotted: 'Dotted',
  51. dashed: 'Dashed',
  52. double: 'Double',
  53. groove: 'Groove',
  54. ridge: 'Ridge',
  55. inset: 'Inset',
  56. outset: 'Outset'
  57. } );
  58. } );
  59. } );
  60. describe( 'getLocalizedColorErrorText()', () => {
  61. it( 'should return the error text', () => {
  62. const t = string => string;
  63. expect( getLocalizedColorErrorText( t ) ).to.match( /^The color is invalid/ );
  64. } );
  65. } );
  66. describe( 'getLocalizedLengthErrorText()', () => {
  67. it( 'should return the error text', () => {
  68. const t = string => string;
  69. expect( getLocalizedLengthErrorText( t ) ).to.match( /^The value is invalid/ );
  70. } );
  71. } );
  72. describe( 'colorFieldValidator()', () => {
  73. it( 'should pass for an empty value', () => {
  74. expect( colorFieldValidator( '' ) ).to.be.true;
  75. } );
  76. it( 'should pass for white spaces', () => {
  77. expect( colorFieldValidator( ' ' ) ).to.be.true;
  78. } );
  79. it( 'should pass for colors', () => {
  80. expect( colorFieldValidator( '#FFF' ) ).to.be.true;
  81. expect( colorFieldValidator( '#FFAA11' ) ).to.be.true;
  82. expect( colorFieldValidator( 'rgb(255,123,100)' ) ).to.be.true;
  83. expect( colorFieldValidator( 'red' ) ).to.be.true;
  84. } );
  85. it( 'should pass for colors surrounded by white spaces', () => {
  86. expect( colorFieldValidator( ' #AAA ' ) ).to.be.true;
  87. expect( colorFieldValidator( ' rgb(255,123,100) ' ) ).to.be.true;
  88. } );
  89. } );
  90. describe( 'lengthFieldValidator()', () => {
  91. it( 'should pass for an empty value', () => {
  92. expect( lengthFieldValidator( '' ) ).to.be.true;
  93. } );
  94. it( 'should pass for white spaces', () => {
  95. expect( lengthFieldValidator( ' ' ) ).to.be.true;
  96. } );
  97. it( 'should pass for lengths', () => {
  98. expect( lengthFieldValidator( '1px' ) ).to.be.true;
  99. expect( lengthFieldValidator( '12em' ) ).to.be.true;
  100. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  101. expect( lengthFieldValidator( '45%' ) ).to.be.true;
  102. } );
  103. it( 'should pass for number without unit', () => {
  104. expect( lengthFieldValidator( '1' ) ).to.be.true;
  105. expect( lengthFieldValidator( '12.1' ) ).to.be.true;
  106. expect( lengthFieldValidator( '0.125 ' ) ).to.be.true;
  107. } );
  108. it( 'should not pass for invalid number values', () => {
  109. expect( lengthFieldValidator( '.1 ' ) ).to.be.false;
  110. expect( lengthFieldValidator( '45. ' ) ).to.be.false;
  111. expect( lengthFieldValidator( '45.1.1 ' ) ).to.be.false;
  112. } );
  113. it( 'should pass for lengths surrounded by white spaces', () => {
  114. expect( lengthFieldValidator( '3px ' ) ).to.be.true;
  115. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  116. } );
  117. } );
  118. describe( 'lineWidthFieldValidator()', () => {
  119. it( 'should pass for an empty value', () => {
  120. expect( lineWidthFieldValidator( '' ) ).to.be.true;
  121. } );
  122. it( 'should pass for white spaces', () => {
  123. expect( lineWidthFieldValidator( ' ' ) ).to.be.true;
  124. } );
  125. it( 'should pass for lengths', () => {
  126. expect( lineWidthFieldValidator( '1px' ) ).to.be.true;
  127. expect( lineWidthFieldValidator( '12em' ) ).to.be.true;
  128. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  129. } );
  130. it( 'should pass for number without unit', () => {
  131. expect( lineWidthFieldValidator( '1' ) ).to.be.true;
  132. expect( lineWidthFieldValidator( '12.1' ) ).to.be.true;
  133. expect( lineWidthFieldValidator( '0.125 ' ) ).to.be.true;
  134. } );
  135. it( 'should not pass for invalid number values', () => {
  136. expect( lineWidthFieldValidator( '.1 ' ) ).to.be.false;
  137. expect( lineWidthFieldValidator( '45. ' ) ).to.be.false;
  138. expect( lineWidthFieldValidator( '45.1.1 ' ) ).to.be.false;
  139. expect( lineWidthFieldValidator( '45%' ) ).to.be.false;
  140. } );
  141. it( 'should pass for lengths surrounded by white spaces', () => {
  142. expect( lineWidthFieldValidator( '3px ' ) ).to.be.true;
  143. expect( lineWidthFieldValidator( ' 12em ' ) ).to.be.true;
  144. } );
  145. } );
  146. describe( 'getBorderStyleDefinitions()', () => {
  147. let view, locale, definitions;
  148. beforeEach( () => {
  149. locale = { t: val => val };
  150. view = new View( locale );
  151. view.set( 'borderStyle', 'none' );
  152. definitions = getBorderStyleDefinitions( view );
  153. } );
  154. it( 'should return a collection', () => {
  155. expect( definitions ).to.be.instanceOf( Collection );
  156. } );
  157. it( 'should create a button definition for each style', () => {
  158. expect( definitions.map( ( { type } ) => type ).every( item => item === 'button' ) ).to.be.true;
  159. } );
  160. it( 'should set label of a button for each style', () => {
  161. expect( definitions.map( ( { model: { label } } ) => label ) ).to.have.ordered.members( [
  162. 'None',
  163. 'Solid',
  164. 'Dotted',
  165. 'Dashed',
  166. 'Double',
  167. 'Groove',
  168. 'Ridge',
  169. 'Inset',
  170. 'Outset'
  171. ] );
  172. } );
  173. it( 'should set type of a button for each style', () => {
  174. expect( definitions.map( ( { model: { withText } } ) => withText ).every( item => item === true ) ).to.be.true;
  175. } );
  176. it( 'should bind button\'s #isOn to the view #borderStyle property', () => {
  177. view.borderStyle = 'dotted';
  178. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  179. false,
  180. false,
  181. true,
  182. false,
  183. false,
  184. false,
  185. false,
  186. false,
  187. false
  188. ] );
  189. view.borderStyle = 'inset';
  190. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  191. false,
  192. false,
  193. false,
  194. false,
  195. false,
  196. false,
  197. false,
  198. true,
  199. false
  200. ] );
  201. } );
  202. } );
  203. describe( 'fillToolbar()', () => {
  204. let view, locale, toolbar;
  205. const labels = {
  206. first: 'Do something',
  207. second: 'Do something else',
  208. third: 'Be default'
  209. };
  210. const icons = {
  211. first: '<svg viewBox="0 0 21 21" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  212. second: '<svg viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  213. third: '<svg viewBox="0 0 23 23" xmlns="http://www.w3.org/2000/svg"><path /></svg>'
  214. };
  215. beforeEach( () => {
  216. locale = { t: val => val };
  217. view = new View( locale );
  218. view.set( 'someProperty', 'foo' );
  219. toolbar = new ToolbarView( locale );
  220. fillToolbar( {
  221. view, toolbar, icons, labels,
  222. propertyName: 'someProperty',
  223. nameToValue: name => name === 'third' ? '' : name
  224. } );
  225. } );
  226. afterEach( () => {
  227. view.destroy();
  228. } );
  229. it( 'should create buttons', () => {
  230. expect( toolbar.items ).to.have.length( 3 );
  231. expect( toolbar.items.first ).to.be.instanceOf( ButtonView );
  232. expect( toolbar.items.get( 1 ) ).to.be.instanceOf( ButtonView );
  233. expect( toolbar.items.last ).to.be.instanceOf( ButtonView );
  234. } );
  235. it( 'should set button labels', () => {
  236. expect( toolbar.items.first.label ).to.equal( 'Do something' );
  237. expect( toolbar.items.get( 1 ).label ).to.equal( 'Do something else' );
  238. expect( toolbar.items.last.label ).to.equal( 'Be default' );
  239. } );
  240. it( 'should set button icons', () => {
  241. expect( toolbar.items.first.icon ).to.equal( icons.first );
  242. expect( toolbar.items.get( 1 ).icon ).to.equal( icons.second );
  243. expect( toolbar.items.last.icon ).to.equal( icons.third );
  244. } );
  245. it( 'should set button tooltips', () => {
  246. expect( toolbar.items.first.tooltip ).to.equal( labels.first );
  247. expect( toolbar.items.get( 1 ).tooltip ).to.equal( labels.second );
  248. expect( toolbar.items.last.tooltip ).to.equal( labels.third );
  249. } );
  250. it( 'should bind button #isOn to an observable property', () => {
  251. expect( toolbar.items.first.isOn ).to.be.false;
  252. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  253. expect( toolbar.items.last.isOn ).to.be.false;
  254. view.someProperty = 'first';
  255. expect( toolbar.items.first.isOn ).to.be.true;
  256. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  257. expect( toolbar.items.last.isOn ).to.be.false;
  258. view.someProperty = 'second';
  259. expect( toolbar.items.first.isOn ).to.be.false;
  260. expect( toolbar.items.get( 1 ).isOn ).to.be.true;
  261. expect( toolbar.items.last.isOn ).to.be.false;
  262. view.someProperty = '';
  263. expect( toolbar.items.first.isOn ).to.be.false;
  264. expect( toolbar.items.get( 1 ).isOn ).to.be.false;
  265. expect( toolbar.items.last.isOn ).to.be.true;
  266. } );
  267. it( 'should make the buttons change the property value upon execution', () => {
  268. toolbar.items.first.fire( 'execute' );
  269. expect( view.someProperty ).to.equal( 'first' );
  270. toolbar.items.get( 1 ).fire( 'execute' );
  271. expect( view.someProperty ).to.equal( 'second' );
  272. toolbar.items.last.fire( 'execute' );
  273. expect( view.someProperty ).to.equal( '' );
  274. } );
  275. } );
  276. } );
  277. } );