8
0

styles.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import StyleProxy from '../../src/view/styles';
  6. describe( 'Styles', () => {
  7. let styles;
  8. beforeEach( () => {
  9. styles = new StyleProxy();
  10. } );
  11. describe( 'getStyleNames()', () => {
  12. it( 'should output custom style names', () => {
  13. styles.setStyle( 'foo: 2;bar: baz;foo-bar-baz:none;' );
  14. expect( styles.getStyleNames() ).to.deep.equal( [ 'bar', 'foo', 'foo-bar-baz' ] );
  15. } );
  16. it( 'should output full names for known style names', () => {
  17. styles.setStyle( 'margin: 1px;margin-left: 2em;' );
  18. expect( styles.getStyleNames() ).to.deep.equal( [ 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ] );
  19. } );
  20. } );
  21. describe( 'styles rules', () => {
  22. describe( 'border', () => {
  23. it( 'should parse border shorthand', () => {
  24. styles.setStyle( 'border:1px solid blue;' );
  25. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  26. color: { top: 'blue', right: 'blue', bottom: 'blue', left: 'blue' },
  27. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  28. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  29. } );
  30. } );
  31. it( 'should parse border shorthand with other shorthands', () => {
  32. styles.setStyle( 'border:1px solid blue;border-left:#665511 dashed 2.7em;border-top:7px dotted #ccc;' );
  33. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  34. color: { top: '#ccc', right: 'blue', bottom: 'blue', left: '#665511' },
  35. style: { top: 'dotted', right: 'solid', bottom: 'solid', left: 'dashed' },
  36. width: { top: '7px', right: '1px', bottom: '1px', left: '2.7em' }
  37. } );
  38. } );
  39. it( 'should output inline shorthand rules', () => {
  40. styles.setStyle( 'border:1px solid blue;' );
  41. expect( styles.getInlineStyle() ).to.equal( 'border-color:blue;border-style:solid;border-width:1px;' );
  42. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( 'blue' );
  43. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'solid' );
  44. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '1px' );
  45. } );
  46. it( 'should output inline shorthand rules', () => {
  47. styles.setStyle( 'border:1px solid blue;border-left:#665511 dashed 2.7em;border-top:7px dotted #ccc;' );
  48. expect( styles.getInlineStyle() ).to.equal(
  49. 'border-color:#ccc blue blue #665511;border-style:dotted solid solid dashed;border-width:7px 1px 1px 2.7em;'
  50. );
  51. // todo expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  52. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( '#ccc blue blue #665511' );
  53. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'dotted solid solid dashed' );
  54. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '7px 1px 1px 2.7em' );
  55. } );
  56. it( 'should merge rules on insert other shorthand', () => {
  57. styles.setStyle( 'border:1px solid blue;' );
  58. styles.insertProperty( 'border-left', '#665511 dashed 2.7em' );
  59. styles.insertProperty( 'border-top', '7px dotted #ccc' );
  60. expect( styles.getInlineStyle() ).to.equal(
  61. 'border-color:#ccc blue blue #665511;border-style:dotted solid solid dashed;border-width:7px 1px 1px 2.7em;'
  62. );
  63. // expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  64. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( '#ccc blue blue #665511' );
  65. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'dotted solid solid dashed' );
  66. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '7px 1px 1px 2.7em' );
  67. } );
  68. it( 'should output', () => {
  69. styles.setStyle( 'border:1px solid blue;' );
  70. styles.removeProperty( 'border-color' );
  71. expect( styles.getInlineStyle() ).to.equal(
  72. 'border-style:solid;border-width:1px;'
  73. );
  74. // expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  75. expect( styles.getInlineProperty( 'border-color' ) ).to.be.undefined;
  76. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'solid' );
  77. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '1px' );
  78. } );
  79. describe( 'border-color', () => {
  80. it( 'should set all border colors (1 value defined)', () => {
  81. styles.setStyle( 'border-color:cyan;' );
  82. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  83. color: {
  84. top: 'cyan',
  85. right: 'cyan',
  86. bottom: 'cyan',
  87. left: 'cyan'
  88. }
  89. } );
  90. } );
  91. it( 'should set all border colors (2 values defined)', () => {
  92. styles.setStyle( 'border-color:cyan magenta;' );
  93. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  94. color: {
  95. top: 'cyan',
  96. right: 'magenta',
  97. bottom: 'cyan',
  98. left: 'magenta'
  99. }
  100. } );
  101. } );
  102. it( 'should set all border colors (3 values defined)', () => {
  103. styles.setStyle( 'border-color:cyan magenta pink;' );
  104. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  105. color: {
  106. top: 'cyan',
  107. right: 'magenta',
  108. bottom: 'pink',
  109. left: 'magenta'
  110. }
  111. } );
  112. } );
  113. it( 'should set all border colors (4 values defined)', () => {
  114. styles.setStyle( 'border-color:cyan magenta pink beige;' );
  115. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  116. color: {
  117. top: 'cyan',
  118. right: 'magenta',
  119. bottom: 'pink',
  120. left: 'beige'
  121. }
  122. } );
  123. } );
  124. it( 'should merge with border shorthand', () => {
  125. styles.setStyle( 'border:1px solid blue;border-color:cyan black;' );
  126. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  127. color: { top: 'cyan', right: 'black', bottom: 'cyan', left: 'black' },
  128. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  129. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  130. } );
  131. } );
  132. } );
  133. describe( 'border-style', () => {
  134. it( 'should set all border styles (1 value defined)', () => {
  135. styles.setStyle( 'border-style:solid;' );
  136. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  137. style: {
  138. top: 'solid',
  139. right: 'solid',
  140. bottom: 'solid',
  141. left: 'solid'
  142. }
  143. } );
  144. } );
  145. it( 'should set all border styles (2 values defined)', () => {
  146. styles.setStyle( 'border-style:solid dotted;' );
  147. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  148. style: {
  149. top: 'solid',
  150. right: 'dotted',
  151. bottom: 'solid',
  152. left: 'dotted'
  153. }
  154. } );
  155. } );
  156. it( 'should set all border styles (3 values defined)', () => {
  157. styles.setStyle( 'border-style:solid dotted dashed;' );
  158. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  159. style: {
  160. top: 'solid',
  161. right: 'dotted',
  162. bottom: 'dashed',
  163. left: 'dotted'
  164. }
  165. } );
  166. } );
  167. it( 'should set all border styles (4 values defined)', () => {
  168. styles.setStyle( 'border-style:solid dotted dashed ridge;' );
  169. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  170. style: {
  171. top: 'solid',
  172. right: 'dotted',
  173. bottom: 'dashed',
  174. left: 'ridge'
  175. }
  176. } );
  177. } );
  178. } );
  179. describe( 'border-width', () => {
  180. it( 'should set all border widths (1 value defined)', () => {
  181. styles.setStyle( 'border-width:1px;' );
  182. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  183. width: {
  184. top: '1px',
  185. right: '1px',
  186. bottom: '1px',
  187. left: '1px'
  188. }
  189. } );
  190. } );
  191. it( 'should set all border widths (2 values defined)', () => {
  192. styles.setStyle( 'border-width:1px .34cm;' );
  193. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  194. width: {
  195. top: '1px',
  196. right: '.34cm',
  197. bottom: '1px',
  198. left: '.34cm'
  199. }
  200. } );
  201. } );
  202. it( 'should set all border widths (3 values defined)', () => {
  203. styles.setStyle( 'border-width:1px .34cm 90.1rem;' );
  204. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  205. width: {
  206. top: '1px',
  207. right: '.34cm',
  208. bottom: '90.1rem',
  209. left: '.34cm'
  210. }
  211. } );
  212. } );
  213. it( 'should set all border widths (4 values defined)', () => {
  214. styles.setStyle( 'border-width:1px .34cm 90.1rem thick;' );
  215. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  216. width: {
  217. top: '1px',
  218. right: '.34cm',
  219. bottom: '90.1rem',
  220. left: 'thick'
  221. }
  222. } );
  223. } );
  224. } );
  225. } );
  226. describe( 'margin', () => {
  227. it( 'should set all margins (1 value defined)', () => {
  228. styles.setStyle( 'margin:1px;' );
  229. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  230. top: '1px',
  231. right: '1px',
  232. bottom: '1px',
  233. left: '1px'
  234. } );
  235. } );
  236. it( 'should set all margins (2 values defined)', () => {
  237. styles.setStyle( 'margin:1px .34cm;' );
  238. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  239. top: '1px',
  240. right: '.34cm',
  241. bottom: '1px',
  242. left: '.34cm'
  243. } );
  244. } );
  245. it( 'should set all margins (3 values defined)', () => {
  246. styles.setStyle( 'margin:1px .34cm 90.1rem;' );
  247. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  248. top: '1px',
  249. right: '.34cm',
  250. bottom: '90.1rem',
  251. left: '.34cm'
  252. } );
  253. } );
  254. it( 'should set all margins (4 values defined)', () => {
  255. styles.setStyle( 'margin:1px .34cm 90.1rem thick;' );
  256. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  257. top: '1px',
  258. right: '.34cm',
  259. bottom: '90.1rem',
  260. left: 'thick'
  261. } );
  262. } );
  263. it( 'should output inline style (1 value defined)', () => {
  264. styles.setStyle( 'margin:1px;' );
  265. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;margin-right:1px;margin-bottom:1px;margin-left:1px;' );
  266. // expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px' );
  267. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  268. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  269. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  270. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  271. } );
  272. it( 'should output inline style (2 values defined)', () => {
  273. styles.setStyle( 'margin:1px .34cm;' );
  274. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;margin-right:.34cm;margin-bottom:1px;margin-left:.34cm;' );
  275. // expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm' );
  276. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  277. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  278. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  279. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '.34cm' );
  280. } );
  281. it( 'should output inline style (3 values defined)', () => {
  282. styles.setStyle( 'margin:1px .34cm 90.1rem;' );
  283. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;margin-right:.34cm;margin-bottom:90.1rem;margin-left:.34cm;' );
  284. // expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem' );
  285. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  286. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  287. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  288. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '.34cm' );
  289. } );
  290. it( 'should output inline style (3 values defined, only last different)', () => {
  291. styles.setStyle( 'margin:1px 1px 90.1rem;' );
  292. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;margin-right:1px;margin-bottom:90.1rem;margin-left:1px;' );
  293. // expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 90.1rem' );
  294. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  295. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  296. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  297. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  298. } );
  299. it( 'should output inline style (4 values defined)', () => {
  300. styles.setStyle( 'margin:1px .34cm 90.1rem thick;' );
  301. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;margin-right:.34cm;margin-bottom:90.1rem;margin-left:thick;' );
  302. // expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem thick' );
  303. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  304. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  305. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  306. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( 'thick' );
  307. } );
  308. it( 'should output inline style (4 values defined, only last different)', () => {
  309. styles.setStyle( 'margin:1px 1px 1px thick;' );
  310. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;margin-right:1px;margin-bottom:1px;margin-left:thick;' );
  311. // expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 1px thick' );
  312. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  313. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  314. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  315. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( 'thick' );
  316. } );
  317. describe( 'margin-*', () => {
  318. it( 'should set proper margin', () => {
  319. styles.setStyle( 'margin-top:1px;' );
  320. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( { top: '1px' } );
  321. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  322. } );
  323. it( 'should set proper margin with margin shorthand', () => {
  324. styles.setStyle( 'margin: 2em;margin-top:1px;' );
  325. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  326. top: '1px',
  327. right: '2em',
  328. bottom: '2em',
  329. left: '2em'
  330. } );
  331. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  332. expect( styles.getNormalized( 'margin-right' ) ).to.equal( '2em' );
  333. expect( styles.getNormalized( 'margin-bottom' ) ).to.equal( '2em' );
  334. expect( styles.getNormalized( 'margin-left' ) ).to.equal( '2em' );
  335. } );
  336. } );
  337. } );
  338. describe( 'padding', () => {
  339. it( 'should set all paddings (1 value defined)', () => {
  340. styles.setStyle( 'padding:1px;' );
  341. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  342. top: '1px',
  343. right: '1px',
  344. bottom: '1px',
  345. left: '1px'
  346. } );
  347. } );
  348. it( 'should set all paddings (2 values defined)', () => {
  349. styles.setStyle( 'padding:1px .34cm;' );
  350. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  351. top: '1px',
  352. right: '.34cm',
  353. bottom: '1px',
  354. left: '.34cm'
  355. } );
  356. } );
  357. it( 'should set all paddings (3 values defined)', () => {
  358. styles.setStyle( 'padding:1px .34cm 90.1rem;' );
  359. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  360. top: '1px',
  361. right: '.34cm',
  362. bottom: '90.1rem',
  363. left: '.34cm'
  364. } );
  365. } );
  366. it( 'should set all paddings (4 values defined)', () => {
  367. styles.setStyle( 'padding:1px .34cm 90.1rem thick;' );
  368. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  369. top: '1px',
  370. right: '.34cm',
  371. bottom: '90.1rem',
  372. left: 'thick'
  373. } );
  374. } );
  375. describe( 'padding-*', () => {
  376. it( 'should set proper padding', () => {
  377. styles.setStyle( 'padding-top:1px;' );
  378. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  379. top: '1px'
  380. } );
  381. } );
  382. it( 'should set proper padding with padding shorthand', () => {
  383. styles.setStyle( 'padding: 2em;padding-top:1px;' );
  384. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  385. top: '1px',
  386. right: '2em',
  387. bottom: '2em',
  388. left: '2em'
  389. } );
  390. } );
  391. } );
  392. } );
  393. describe( 'unknown rules', () => {
  394. it( 'should left rules untouched', () => {
  395. styles.setStyle( 'foo-bar:baz 1px abc;baz: 2px 3em;' );
  396. expect( styles.getInlineStyle() ).to.equal( 'baz:2px 3em;foo-bar:baz 1px abc;' );
  397. expect( styles.getInlineProperty( 'foo-bar' ) ).to.equal( 'baz 1px abc' );
  398. expect( styles.getInlineProperty( 'baz' ) ).to.equal( '2px 3em' );
  399. } );
  400. } );
  401. } );
  402. } );