utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 passe for an empty value', () => {
  180. expect( colorFieldValidator( '' ) ).to.be.true;
  181. } );
  182. it( 'should passe 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 passe for an empty value', () => {
  198. expect( lengthFieldValidator( '' ) ).to.be.true;
  199. } );
  200. it( 'should passe 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 lengths surrounded by white spaces', () => {
  209. expect( lengthFieldValidator( '3px ' ) ).to.be.true;
  210. expect( lengthFieldValidator( ' 12em ' ) ).to.be.true;
  211. } );
  212. } );
  213. describe( 'getBorderStyleDefinitions()', () => {
  214. let view, locale, definitions;
  215. beforeEach( () => {
  216. locale = { t: val => val };
  217. view = new View( locale );
  218. view.set( 'borderStyle', 'none' );
  219. definitions = getBorderStyleDefinitions( view );
  220. } );
  221. it( 'should return a collection', () => {
  222. expect( definitions ).to.be.instanceOf( Collection );
  223. } );
  224. it( 'should create a button definition for each style', () => {
  225. expect( definitions.map( ( { type } ) => type ).every( item => item === 'button' ) ).to.be.true;
  226. } );
  227. it( 'should set label of a button for each style', () => {
  228. expect( definitions.map( ( { model: { label } } ) => label ) ).to.have.ordered.members( [
  229. 'None',
  230. 'Solid',
  231. 'Dotted',
  232. 'Dashed',
  233. 'Double',
  234. 'Groove',
  235. 'Ridge',
  236. 'Inset',
  237. 'Outset',
  238. ] );
  239. } );
  240. it( 'should set type of a button for each style', () => {
  241. expect( definitions.map( ( { model: { withText } } ) => withText ).every( item => item === true ) ).to.be.true;
  242. } );
  243. it( 'should bind button\'s #isOn to the view #borderStyle property', () => {
  244. view.borderStyle = 'dotted';
  245. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  246. false,
  247. false,
  248. true,
  249. false,
  250. false,
  251. false,
  252. false,
  253. false,
  254. false,
  255. ] );
  256. view.borderStyle = 'inset';
  257. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  258. false,
  259. false,
  260. false,
  261. false,
  262. false,
  263. false,
  264. false,
  265. true,
  266. false,
  267. ] );
  268. } );
  269. } );
  270. describe( 'fillToolbar()', () => {
  271. let view, locale, toolbar;
  272. const labels = {
  273. first: 'Do something',
  274. second: 'Do something else'
  275. };
  276. const icons = {
  277. first: '<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  278. second: '<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path /></svg>'
  279. };
  280. beforeEach( () => {
  281. locale = { t: val => val };
  282. view = new View( locale );
  283. view.set( 'someProperty', 'foo' );
  284. toolbar = new ToolbarView( locale );
  285. fillToolbar( {
  286. view, toolbar, icons, labels,
  287. propertyName: 'someProperty'
  288. } );
  289. } );
  290. afterEach( () => {
  291. view.destroy();
  292. } );
  293. it( 'should create buttons', () => {
  294. expect( toolbar.items ).to.have.length( 2 );
  295. expect( toolbar.items.first ).to.be.instanceOf( ButtonView );
  296. expect( toolbar.items.last ).to.be.instanceOf( ButtonView );
  297. } );
  298. it( 'should set button labels', () => {
  299. expect( toolbar.items.first.label ).to.equal( 'Do something' );
  300. expect( toolbar.items.last.label ).to.equal( 'Do something else' );
  301. } );
  302. it( 'should set button icons', () => {
  303. expect( toolbar.items.first.icon ).to.equal( icons.first );
  304. expect( toolbar.items.last.icon ).to.equal( icons.second );
  305. } );
  306. it( 'should bind button #isOn to an observable property', () => {
  307. expect( toolbar.items.first.isOn ).to.be.false;
  308. expect( toolbar.items.last.isOn ).to.be.false;
  309. view.someProperty = 'first';
  310. expect( toolbar.items.first.isOn ).to.be.true;
  311. expect( toolbar.items.last.isOn ).to.be.false;
  312. view.someProperty = 'second';
  313. expect( toolbar.items.first.isOn ).to.be.false;
  314. expect( toolbar.items.last.isOn ).to.be.true;
  315. } );
  316. it( 'should make the buttons change the property value upon execution', () => {
  317. toolbar.items.first.fire( 'execute' );
  318. expect( view.someProperty ).to.equal( 'first' );
  319. toolbar.items.last.fire( 'execute' );
  320. expect( view.someProperty ).to.equal( 'second' );
  321. } );
  322. } );
  323. } );