8
0

styles.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. import encodedImage from './_utils/encodedimage.txt';
  7. describe( 'Styles', () => {
  8. let styles;
  9. beforeEach( () => {
  10. styles = new StyleProxy();
  11. } );
  12. describe( 'size getter', () => {
  13. it( 'should return 0 if no styles are set', () => {
  14. expect( styles.size ).to.equal( 0 );
  15. } );
  16. it( 'should return number of set styles', () => {
  17. styles.setStyle( 'color:blue' );
  18. expect( styles.size ).to.equal( 1 );
  19. styles.setStyle( 'margin:1px;' );
  20. expect( styles.size ).to.equal( 1 );
  21. styles.setStyle( 'margin-top:1px;margin-bottom:1px;' );
  22. expect( styles.size ).to.equal( 2 );
  23. } );
  24. } );
  25. describe( 'setStyle()', () => {
  26. it( 'should reset styles to a new value', () => {
  27. styles.setStyle( 'color:red;margin-top:1px;' );
  28. expect( styles.getNormalized() ).to.deep.equal( { color: 'red', margin: { top: '1px' } } );
  29. styles.setStyle( 'margin-bottom:2em;' );
  30. expect( styles.getNormalized() ).to.deep.equal( { margin: { bottom: '2em' } } );
  31. } );
  32. describe( 'styles parsing edge cases and incorrect styles', () => {
  33. it( 'should not crash and not add any styles if styles attribute was empty', () => {
  34. styles.setStyle( '' );
  35. expect( styles.getStyleNames() ).to.deep.equal( [] );
  36. } );
  37. it( 'should be able to parse big styles definition', () => {
  38. expect( () => {
  39. styles.setStyle( `background-image:url('data:image/jpeg;base64,${ encodedImage }')` );
  40. } ).not.to.throw();
  41. } );
  42. it( 'should work with both types of quotes and ignore values inside quotes', () => {
  43. styles.setStyle( 'background-image:url("im;color:g.jpg")' );
  44. expect( styles.getInlineProperty( 'background-image' ) ).to.equal( 'url("im;color:g.jpg")' );
  45. styles.setStyle( 'background-image:url(\'im;color:g.jpg\')' );
  46. expect( styles.getInlineProperty( 'background-image' ) ).to.equal( 'url(\'im;color:g.jpg\')' );
  47. } );
  48. it( 'should not be confused by whitespaces', () => {
  49. styles.setStyle( '\ncolor:\n red ' );
  50. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  51. } );
  52. it( 'should not be confused by duplicated semicolon', () => {
  53. styles.setStyle( 'color: red;; display: inline' );
  54. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  55. expect( styles.getInlineProperty( 'display' ) ).to.equal( 'inline' );
  56. } );
  57. it( 'should not throw when value is missing', () => {
  58. styles.setStyle( 'color:; display: inline' );
  59. expect( styles.getInlineProperty( 'color' ) ).to.equal( '' );
  60. expect( styles.getInlineProperty( 'display' ) ).to.equal( 'inline' );
  61. } );
  62. it( 'should not throw when colon is duplicated', () => {
  63. styles.setStyle( 'color:: red; display: inline' );
  64. // The result makes no sense, but here we just check that the algorithm doesn't crash.
  65. expect( styles.getInlineProperty( 'color' ) ).to.equal( ': red' );
  66. expect( styles.getInlineProperty( 'display' ) ).to.equal( 'inline' );
  67. } );
  68. it( 'should not throw when random stuff passed', () => {
  69. styles.setStyle( 'color: red;:; ;;" ": display: inline; \'aaa;:' );
  70. // The result makes no sense, but here we just check that the algorithm doesn't crash.
  71. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  72. expect( styles.getInlineProperty( 'display' ) ).to.be.undefined;
  73. } );
  74. } );
  75. } );
  76. describe( 'getInlineStyle()', () => {
  77. it( 'should return undefined for empty styles', () => {
  78. expect( styles.getInlineStyle() ).to.be.undefined;
  79. } );
  80. it( 'should return sorted styles string if styles are set', () => {
  81. styles.setStyle( 'margin-top:1px;color:blue;' );
  82. expect( styles.getInlineStyle() ).to.equal( 'color:blue;margin-top:1px;' );
  83. } );
  84. } );
  85. describe( 'getInlineProperty', () => {
  86. it( 'should return empty string for missing shorthand', () => {
  87. styles.setStyle( 'margin-top:1px' );
  88. expect( styles.getInlineProperty( 'margin' ) ).to.be.undefined;
  89. } );
  90. } );
  91. describe( 'hasProperty()', () => {
  92. it( 'should return false if property is not set', () => {
  93. expect( styles.hasProperty( 'foo' ) ).to.be.false;
  94. } );
  95. it( 'should return false if normalized property is not set', () => {
  96. styles.setStyle( 'margin-top:1px' );
  97. // TODO
  98. // expect( styles.hasProperty( 'margin' ) ).to.be.false;
  99. expect( styles.hasProperty( 'margin' ) ).to.be.true;
  100. } );
  101. it( 'should return true if property is set', () => {
  102. styles.setStyle( 'color:deeppink' );
  103. expect( styles.hasProperty( 'color' ) ).to.be.true;
  104. } );
  105. it( 'should return true if normalized shorthanded property is set', () => {
  106. styles.setStyle( 'margin:1px 2px 3px 4px' );
  107. expect( styles.hasProperty( 'margin-top' ) ).to.be.true;
  108. } );
  109. } );
  110. describe( 'insertProperty()', () => {
  111. it( 'should insert new property (empty styles)', () => {
  112. styles.insertProperty( 'color', 'blue' );
  113. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  114. } );
  115. it( 'should insert new property (other properties are set)', () => {
  116. styles.setStyle( 'margin: 1px;' );
  117. styles.insertProperty( 'color', 'blue' );
  118. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  119. } );
  120. it( 'should overwrite property', () => {
  121. styles.setStyle( 'color: red;' );
  122. styles.insertProperty( 'color', 'blue' );
  123. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  124. } );
  125. it( 'should set multiple styles by providing an object', () => {
  126. styles.setStyle( 'color: red;' );
  127. styles.insertProperty( { color: 'blue', margin: '1px' } );
  128. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  129. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  130. } );
  131. } );
  132. describe( 'removeProperty()', () => {
  133. it( 'should do nothing if property is not set', () => {
  134. styles.removeProperty( 'color' );
  135. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  136. } );
  137. it( 'should insert new property (other properties are set)', () => {
  138. styles.setStyle( 'color:blue' );
  139. styles.removeProperty( 'color' );
  140. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  141. } );
  142. it( 'should remove normalized property', () => {
  143. styles.setStyle( 'margin:1px' );
  144. styles.removeProperty( 'margin-top' );
  145. expect( styles.getInlineProperty( 'margin-top' ) ).to.be.undefined;
  146. } );
  147. } );
  148. describe( 'getStyleNames()', () => {
  149. it( 'should output empty array for empty styles', () => {
  150. expect( styles.getStyleNames() ).to.deep.equal( [] );
  151. } );
  152. it( 'should output custom style names', () => {
  153. styles.setStyle( 'foo: 2;bar: baz;foo-bar-baz:none;' );
  154. expect( styles.getStyleNames() ).to.deep.equal( [ 'bar', 'foo', 'foo-bar-baz' ] );
  155. } );
  156. it( 'should output full names for known style names', () => {
  157. styles.setStyle( 'margin: 1px;margin-left: 2em;' );
  158. expect( styles.getStyleNames() ).to.deep.equal( [ 'margin' ] );
  159. } );
  160. } );
  161. describe( 'styles rules', () => {
  162. describe( 'border', () => {
  163. it( 'should parse border shorthand', () => {
  164. styles.setStyle( 'border:1px solid blue;' );
  165. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  166. color: { top: 'blue', right: 'blue', bottom: 'blue', left: 'blue' },
  167. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  168. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  169. } );
  170. } );
  171. it( 'should parse border shorthand with other shorthands', () => {
  172. styles.setStyle( 'border:1px solid blue;border-left:#665511 dashed 2.7em;border-top:7px dotted #ccc;' );
  173. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  174. color: { top: '#ccc', right: 'blue', bottom: 'blue', left: '#665511' },
  175. style: { top: 'dotted', right: 'solid', bottom: 'solid', left: 'dashed' },
  176. width: { top: '7px', right: '1px', bottom: '1px', left: '2.7em' }
  177. } );
  178. } );
  179. it( 'should output inline shorthand rules #1', () => {
  180. styles.setStyle( 'border:1px solid blue;' );
  181. expect( styles.getInlineStyle() ).to.equal( 'border-color:blue;border-style:solid;border-width:1px;' );
  182. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( 'blue' );
  183. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'solid' );
  184. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '1px' );
  185. } );
  186. it( 'should output inline shorthand rules #2', () => {
  187. styles.setStyle( 'border:1px solid blue;border-left:#665511 dashed 2.7em;border-top:7px dotted #ccc;' );
  188. expect( styles.getInlineStyle() ).to.equal(
  189. 'border-color:#ccc blue blue #665511;border-style:dotted solid solid dashed;border-width:7px 1px 1px 2.7em;'
  190. );
  191. // todo expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  192. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( '#ccc blue blue #665511' );
  193. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'dotted solid solid dashed' );
  194. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '7px 1px 1px 2.7em' );
  195. } );
  196. it( 'should merge rules on insert other shorthand', () => {
  197. styles.setStyle( 'border:1px solid blue;' );
  198. styles.insertProperty( 'border-left', '#665511 dashed 2.7em' );
  199. styles.insertProperty( 'border-top', '7px dotted #ccc' );
  200. expect( styles.getInlineStyle() ).to.equal(
  201. 'border-color:#ccc blue blue #665511;border-style:dotted solid solid dashed;border-width:7px 1px 1px 2.7em;'
  202. );
  203. // expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  204. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( '#ccc blue blue #665511' );
  205. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'dotted solid solid dashed' );
  206. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '7px 1px 1px 2.7em' );
  207. } );
  208. it( 'should output', () => {
  209. styles.setStyle( 'border:1px solid blue;' );
  210. styles.removeProperty( 'border-color' );
  211. expect( styles.getInlineStyle() ).to.equal(
  212. 'border-style:solid;border-width:1px;'
  213. );
  214. // expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  215. expect( styles.getInlineProperty( 'border-color' ) ).to.be.undefined;
  216. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'solid' );
  217. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '1px' );
  218. } );
  219. describe( 'border-color', () => {
  220. it( 'should set all border colors (1 value defined)', () => {
  221. styles.setStyle( 'border-color:cyan;' );
  222. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  223. color: {
  224. top: 'cyan',
  225. right: 'cyan',
  226. bottom: 'cyan',
  227. left: 'cyan'
  228. }
  229. } );
  230. } );
  231. it( 'should set all border colors (2 values defined)', () => {
  232. styles.setStyle( 'border-color:cyan magenta;' );
  233. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  234. color: {
  235. top: 'cyan',
  236. right: 'magenta',
  237. bottom: 'cyan',
  238. left: 'magenta'
  239. }
  240. } );
  241. } );
  242. it( 'should set all border colors (3 values defined)', () => {
  243. styles.setStyle( 'border-color:cyan magenta pink;' );
  244. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  245. color: {
  246. top: 'cyan',
  247. right: 'magenta',
  248. bottom: 'pink',
  249. left: 'magenta'
  250. }
  251. } );
  252. } );
  253. it( 'should set all border colors (4 values defined)', () => {
  254. styles.setStyle( 'border-color:cyan magenta pink beige;' );
  255. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  256. color: {
  257. top: 'cyan',
  258. right: 'magenta',
  259. bottom: 'pink',
  260. left: 'beige'
  261. }
  262. } );
  263. } );
  264. it( 'should merge with border shorthand', () => {
  265. styles.setStyle( 'border:1px solid blue;border-color:cyan black;' );
  266. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  267. color: { top: 'cyan', right: 'black', bottom: 'cyan', left: 'black' },
  268. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  269. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  270. } );
  271. } );
  272. } );
  273. describe( 'border-style', () => {
  274. it( 'should set all border styles (1 value defined)', () => {
  275. styles.setStyle( 'border-style:solid;' );
  276. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  277. style: {
  278. top: 'solid',
  279. right: 'solid',
  280. bottom: 'solid',
  281. left: 'solid'
  282. }
  283. } );
  284. } );
  285. it( 'should set all border styles (2 values defined)', () => {
  286. styles.setStyle( 'border-style:solid dotted;' );
  287. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  288. style: {
  289. top: 'solid',
  290. right: 'dotted',
  291. bottom: 'solid',
  292. left: 'dotted'
  293. }
  294. } );
  295. } );
  296. it( 'should set all border styles (3 values defined)', () => {
  297. styles.setStyle( 'border-style:solid dotted dashed;' );
  298. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  299. style: {
  300. top: 'solid',
  301. right: 'dotted',
  302. bottom: 'dashed',
  303. left: 'dotted'
  304. }
  305. } );
  306. } );
  307. it( 'should set all border styles (4 values defined)', () => {
  308. styles.setStyle( 'border-style:solid dotted dashed ridge;' );
  309. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  310. style: {
  311. top: 'solid',
  312. right: 'dotted',
  313. bottom: 'dashed',
  314. left: 'ridge'
  315. }
  316. } );
  317. } );
  318. } );
  319. describe( 'border-width', () => {
  320. it( 'should set all border widths (1 value defined)', () => {
  321. styles.setStyle( 'border-width:1px;' );
  322. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  323. width: {
  324. top: '1px',
  325. right: '1px',
  326. bottom: '1px',
  327. left: '1px'
  328. }
  329. } );
  330. } );
  331. it( 'should set all border widths (2 values defined)', () => {
  332. styles.setStyle( 'border-width:1px .34cm;' );
  333. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  334. width: {
  335. top: '1px',
  336. right: '.34cm',
  337. bottom: '1px',
  338. left: '.34cm'
  339. }
  340. } );
  341. } );
  342. it( 'should set all border widths (3 values defined)', () => {
  343. styles.setStyle( 'border-width:1px .34cm 90.1rem;' );
  344. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  345. width: {
  346. top: '1px',
  347. right: '.34cm',
  348. bottom: '90.1rem',
  349. left: '.34cm'
  350. }
  351. } );
  352. } );
  353. it( 'should set all border widths (4 values defined)', () => {
  354. styles.setStyle( 'border-width:1px .34cm 90.1rem thick;' );
  355. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  356. width: {
  357. top: '1px',
  358. right: '.34cm',
  359. bottom: '90.1rem',
  360. left: 'thick'
  361. }
  362. } );
  363. } );
  364. } );
  365. } );
  366. describe( 'margin', () => {
  367. it( 'should set all margins (1 value defined)', () => {
  368. styles.setStyle( 'margin:1px;' );
  369. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  370. top: '1px',
  371. right: '1px',
  372. bottom: '1px',
  373. left: '1px'
  374. } );
  375. } );
  376. it( 'should set all margins (2 values defined)', () => {
  377. styles.setStyle( 'margin:1px .34cm;' );
  378. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  379. top: '1px',
  380. right: '.34cm',
  381. bottom: '1px',
  382. left: '.34cm'
  383. } );
  384. } );
  385. it( 'should set all margins (3 values defined)', () => {
  386. styles.setStyle( 'margin:1px .34cm 90.1rem;' );
  387. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  388. top: '1px',
  389. right: '.34cm',
  390. bottom: '90.1rem',
  391. left: '.34cm'
  392. } );
  393. } );
  394. it( 'should set all margins (4 values defined)', () => {
  395. styles.setStyle( 'margin:1px .34cm 90.1rem thick;' );
  396. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  397. top: '1px',
  398. right: '.34cm',
  399. bottom: '90.1rem',
  400. left: 'thick'
  401. } );
  402. } );
  403. it( 'should output inline style (1 value defined)', () => {
  404. styles.setStyle( 'margin:1px;' );
  405. expect( styles.getInlineStyle() ).to.equal( 'margin:1px;' );
  406. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px' );
  407. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  408. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  409. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  410. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  411. } );
  412. it( 'should output inline style (2 values defined)', () => {
  413. styles.setStyle( 'margin:1px .34cm;' );
  414. expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm;' );
  415. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm' );
  416. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  417. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  418. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  419. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '.34cm' );
  420. } );
  421. it( 'should output inline style (3 values defined)', () => {
  422. styles.setStyle( 'margin:1px .34cm 90.1rem;' );
  423. expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm 90.1rem;' );
  424. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem' );
  425. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  426. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  427. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  428. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '.34cm' );
  429. } );
  430. it( 'should output inline style (3 values defined, only last different)', () => {
  431. styles.setStyle( 'margin:1px 1px 90.1rem;' );
  432. expect( styles.getInlineStyle() ).to.equal( 'margin:1px 1px 90.1rem;' );
  433. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 90.1rem' );
  434. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  435. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  436. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  437. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  438. } );
  439. it( 'should output inline style (4 values defined)', () => {
  440. styles.setStyle( 'margin:1px .34cm 90.1rem thick;' );
  441. expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm 90.1rem thick;' );
  442. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem thick' );
  443. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  444. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  445. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  446. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( 'thick' );
  447. } );
  448. it( 'should output inline style (4 values defined, only last different)', () => {
  449. styles.setStyle( 'margin:1px 1px 1px thick;' );
  450. expect( styles.getInlineStyle() ).to.equal( 'margin:1px 1px 1px thick;' );
  451. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 1px thick' );
  452. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  453. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  454. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  455. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( 'thick' );
  456. } );
  457. describe( 'margin-*', () => {
  458. it( 'should set proper margin', () => {
  459. styles.setStyle( 'margin-top:1px;' );
  460. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( { top: '1px' } );
  461. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  462. } );
  463. it( 'should merge margin with margin shorthand', () => {
  464. styles.setStyle( 'margin: 2em;margin-top:1px;' );
  465. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  466. top: '1px',
  467. right: '2em',
  468. bottom: '2em',
  469. left: '2em'
  470. } );
  471. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  472. expect( styles.getNormalized( 'margin-right' ) ).to.equal( '2em' );
  473. expect( styles.getNormalized( 'margin-bottom' ) ).to.equal( '2em' );
  474. expect( styles.getNormalized( 'margin-left' ) ).to.equal( '2em' );
  475. } );
  476. it( 'should output margin-top', () => {
  477. styles.setStyle( 'margin-top:1px;' );
  478. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;' );
  479. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  480. } );
  481. it( 'should output margin-right', () => {
  482. styles.setStyle( 'margin-right:1px;' );
  483. expect( styles.getInlineStyle() ).to.equal( 'margin-right:1px;' );
  484. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  485. } );
  486. it( 'should output margin-bottom', () => {
  487. styles.setStyle( 'margin-bottom:1px;' );
  488. expect( styles.getInlineStyle() ).to.equal( 'margin-bottom:1px;' );
  489. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  490. } );
  491. it( 'should output margin-left', () => {
  492. styles.setStyle( 'margin-left:1px;' );
  493. expect( styles.getInlineStyle() ).to.equal( 'margin-left:1px;' );
  494. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  495. } );
  496. } );
  497. } );
  498. describe( 'padding', () => {
  499. it( 'should set all paddings (1 value defined)', () => {
  500. styles.setStyle( 'padding:1px;' );
  501. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  502. top: '1px',
  503. right: '1px',
  504. bottom: '1px',
  505. left: '1px'
  506. } );
  507. } );
  508. it( 'should set all paddings (2 values defined)', () => {
  509. styles.setStyle( 'padding:1px .34cm;' );
  510. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  511. top: '1px',
  512. right: '.34cm',
  513. bottom: '1px',
  514. left: '.34cm'
  515. } );
  516. } );
  517. it( 'should set all paddings (3 values defined)', () => {
  518. styles.setStyle( 'padding:1px .34cm 90.1rem;' );
  519. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  520. top: '1px',
  521. right: '.34cm',
  522. bottom: '90.1rem',
  523. left: '.34cm'
  524. } );
  525. } );
  526. it( 'should set all paddings (4 values defined)', () => {
  527. styles.setStyle( 'padding:1px .34cm 90.1rem thick;' );
  528. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  529. top: '1px',
  530. right: '.34cm',
  531. bottom: '90.1rem',
  532. left: 'thick'
  533. } );
  534. } );
  535. describe( 'padding-*', () => {
  536. it( 'should set proper padding', () => {
  537. styles.setStyle( 'padding-top:1px;' );
  538. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  539. top: '1px'
  540. } );
  541. } );
  542. it( 'should set proper padding with padding shorthand', () => {
  543. styles.setStyle( 'padding: 2em;padding-top:1px;' );
  544. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  545. top: '1px',
  546. right: '2em',
  547. bottom: '2em',
  548. left: '2em'
  549. } );
  550. } );
  551. } );
  552. } );
  553. describe( 'unknown rules', () => {
  554. it( 'should left rules untouched', () => {
  555. styles.setStyle( 'foo-bar:baz 1px abc;baz: 2px 3em;' );
  556. expect( styles.getInlineStyle() ).to.equal( 'baz:2px 3em;foo-bar:baz 1px abc;' );
  557. expect( styles.getInlineProperty( 'foo-bar' ) ).to.equal( 'baz 1px abc' );
  558. expect( styles.getInlineProperty( 'baz' ) ).to.equal( '2px 3em' );
  559. } );
  560. } );
  561. describe( 'background', () => {
  562. it( 'should normalize background', () => {
  563. // TODO: border-box given only for coverage test.
  564. styles.setStyle( 'background:url("example.jpg") center #f00 repeat-y fixed border-box;' );
  565. expect( styles.getNormalized( 'background' ) ).to.deep.equal( {
  566. attachment: 'fixed',
  567. image: 'url("example.jpg")',
  568. position: [ 'center' ],
  569. repeat: [ 'repeat-y' ],
  570. color: '#f00'
  571. } );
  572. } );
  573. // TODO: define what should happen with layers
  574. it.skip( 'should normalize background with layers', () => {
  575. styles.setStyle( 'background:url("test.jpg") repeat-y,#f00;' );
  576. expect( styles.getNormalized( 'background' ) ).to.deep.equal( { color: '#f00' } );
  577. } );
  578. it( 'should normalize background-color', () => {
  579. styles.setStyle( 'background-color:#f00;' );
  580. expect( styles.getNormalized( 'background' ) ).to.deep.equal( { color: '#f00' } );
  581. } );
  582. it( 'should output inline background-color style', () => {
  583. styles.setStyle( 'background:#f00;' );
  584. expect( styles.getInlineStyle() ).to.equal( 'background-color:#f00;' );
  585. expect( styles.getInlineProperty( 'background-color' ) ).to.equal( '#f00' );
  586. } );
  587. } );
  588. } );
  589. } );