8
0

styles.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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 Styles from '../../src/view/styles';
  6. import encodedImage from './_utils/encodedimage.txt';
  7. describe( 'Styles', () => {
  8. let styles;
  9. beforeEach( () => {
  10. styles = new Styles();
  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. it( 'should set object property', () => {
  132. styles.setStyle( 'margin:1px;' );
  133. styles.insertProperty( 'margin', { right: '2px' } );
  134. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  135. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '2px' );
  136. } );
  137. } );
  138. describe( 'removeProperty()', () => {
  139. it( 'should do nothing if property is not set', () => {
  140. styles.removeProperty( 'color' );
  141. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  142. } );
  143. it( 'should insert new property (other properties are set)', () => {
  144. styles.setStyle( 'color:blue' );
  145. styles.removeProperty( 'color' );
  146. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  147. } );
  148. it( 'should remove normalized property', () => {
  149. styles.setStyle( 'margin:1px' );
  150. styles.removeProperty( 'margin-top' );
  151. expect( styles.getInlineProperty( 'margin-top' ) ).to.be.undefined;
  152. } );
  153. } );
  154. describe( 'getStyleNames()', () => {
  155. it( 'should output empty array for empty styles', () => {
  156. expect( styles.getStyleNames() ).to.deep.equal( [] );
  157. } );
  158. it( 'should output custom style names', () => {
  159. styles.setStyle( 'foo: 2;bar: baz;foo-bar-baz:none;' );
  160. expect( styles.getStyleNames() ).to.deep.equal( [ 'bar', 'foo', 'foo-bar-baz' ] );
  161. } );
  162. it( 'should output full names for known style names', () => {
  163. styles.setStyle( 'margin: 1px;margin-left: 2em;' );
  164. expect( styles.getStyleNames() ).to.deep.equal( [ 'margin' ] );
  165. } );
  166. } );
  167. describe( 'styles rules', () => {
  168. describe( 'border', () => {
  169. it( 'should parse border shorthand', () => {
  170. styles.setStyle( 'border:1px solid blue;' );
  171. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  172. color: { top: 'blue', right: 'blue', bottom: 'blue', left: 'blue' },
  173. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  174. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  175. } );
  176. } );
  177. it( 'should parse border shorthand with other shorthands', () => {
  178. styles.setStyle( 'border:1px solid blue;border-left:#665511 dashed 2.7em;border-top:7px dotted #ccc;' );
  179. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  180. color: { top: '#ccc', right: 'blue', bottom: 'blue', left: '#665511' },
  181. style: { top: 'dotted', right: 'solid', bottom: 'solid', left: 'dashed' },
  182. width: { top: '7px', right: '1px', bottom: '1px', left: '2.7em' }
  183. } );
  184. } );
  185. it( 'should output inline shorthand rules #1', () => {
  186. styles.setStyle( 'border:1px solid blue;' );
  187. expect( styles.getInlineStyle() ).to.equal( 'border-color:blue;border-style:solid;border-width:1px;' );
  188. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( 'blue' );
  189. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'solid' );
  190. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '1px' );
  191. } );
  192. it( 'should output inline shorthand rules #2', () => {
  193. styles.setStyle( 'border:1px solid blue;border-left:#665511 dashed 2.7em;border-top:7px dotted #ccc;' );
  194. expect( styles.getInlineStyle() ).to.equal(
  195. 'border-color:#ccc blue blue #665511;border-style:dotted solid solid dashed;border-width:7px 1px 1px 2.7em;'
  196. );
  197. expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  198. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( '#ccc blue blue #665511' );
  199. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'dotted solid solid dashed' );
  200. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '7px 1px 1px 2.7em' );
  201. } );
  202. it( 'should parse border + border-position(only color defined)', () => {
  203. styles.setStyle( 'border:1px solid blue;border-left:#665511;' );
  204. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  205. color: { top: 'blue', right: 'blue', bottom: 'blue', left: '#665511' },
  206. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  207. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  208. } );
  209. } );
  210. it( 'should parse border + border-position(only style defined)', () => {
  211. styles.setStyle( 'border:1px solid blue;border-left:ridge;' );
  212. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  213. color: { top: 'blue', right: 'blue', bottom: 'blue', left: 'blue' },
  214. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'ridge' },
  215. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  216. } );
  217. } );
  218. it( 'should parse border + border-position(only width defined)', () => {
  219. styles.setStyle( 'border:1px solid blue;border-left:1337px' );
  220. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  221. color: { top: 'blue', right: 'blue', bottom: 'blue', left: 'blue' },
  222. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  223. width: { top: '1px', right: '1px', bottom: '1px', left: '1337px' }
  224. } );
  225. } );
  226. it( 'should merge rules on insert other shorthand', () => {
  227. styles.setStyle( 'border:1px solid blue;' );
  228. styles.insertProperty( 'border-left', '#665511 dashed 2.7em' );
  229. styles.insertProperty( 'border-top', '7px dotted #ccc' );
  230. expect( styles.getInlineStyle() ).to.equal(
  231. 'border-color:#ccc blue blue #665511;border-style:dotted solid solid dashed;border-width:7px 1px 1px 2.7em;'
  232. );
  233. expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  234. expect( styles.getInlineProperty( 'border-color' ) ).to.equal( '#ccc blue blue #665511' );
  235. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'dotted solid solid dashed' );
  236. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '7px 1px 1px 2.7em' );
  237. } );
  238. it( 'should output', () => {
  239. styles.setStyle( 'border:1px solid blue;' );
  240. styles.removeProperty( 'border-color' );
  241. expect( styles.getInlineStyle() ).to.equal(
  242. 'border-style:solid;border-width:1px;'
  243. );
  244. expect( styles.getInlineProperty( 'border' ) ).to.be.undefined;
  245. expect( styles.getInlineProperty( 'border-color' ) ).to.be.undefined;
  246. expect( styles.getInlineProperty( 'border-style' ) ).to.equal( 'solid' );
  247. expect( styles.getInlineProperty( 'border-width' ) ).to.equal( '1px' );
  248. } );
  249. describe( 'border-color', () => {
  250. it( 'should set all border colors (1 value defined)', () => {
  251. styles.setStyle( 'border-color:cyan;' );
  252. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  253. color: {
  254. top: 'cyan',
  255. right: 'cyan',
  256. bottom: 'cyan',
  257. left: 'cyan'
  258. }
  259. } );
  260. } );
  261. it( 'should set all border colors (2 values defined)', () => {
  262. styles.setStyle( 'border-color:cyan magenta;' );
  263. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  264. color: {
  265. top: 'cyan',
  266. right: 'magenta',
  267. bottom: 'cyan',
  268. left: 'magenta'
  269. }
  270. } );
  271. } );
  272. it( 'should set all border colors (3 values defined)', () => {
  273. styles.setStyle( 'border-color:cyan magenta pink;' );
  274. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  275. color: {
  276. top: 'cyan',
  277. right: 'magenta',
  278. bottom: 'pink',
  279. left: 'magenta'
  280. }
  281. } );
  282. } );
  283. it( 'should set all border colors (4 values defined)', () => {
  284. styles.setStyle( 'border-color:cyan magenta pink beige;' );
  285. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  286. color: {
  287. top: 'cyan',
  288. right: 'magenta',
  289. bottom: 'pink',
  290. left: 'beige'
  291. }
  292. } );
  293. } );
  294. it( 'should merge with border shorthand', () => {
  295. styles.setStyle( 'border:1px solid blue;border-color:cyan black;' );
  296. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  297. color: { top: 'cyan', right: 'black', bottom: 'cyan', left: 'black' },
  298. style: { top: 'solid', right: 'solid', bottom: 'solid', left: 'solid' },
  299. width: { top: '1px', right: '1px', bottom: '1px', left: '1px' }
  300. } );
  301. } );
  302. } );
  303. describe( 'border-style', () => {
  304. it( 'should set all border styles (1 value defined)', () => {
  305. styles.setStyle( 'border-style:solid;' );
  306. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  307. style: {
  308. top: 'solid',
  309. right: 'solid',
  310. bottom: 'solid',
  311. left: 'solid'
  312. }
  313. } );
  314. } );
  315. it( 'should set all border styles (2 values defined)', () => {
  316. styles.setStyle( 'border-style:solid dotted;' );
  317. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  318. style: {
  319. top: 'solid',
  320. right: 'dotted',
  321. bottom: 'solid',
  322. left: 'dotted'
  323. }
  324. } );
  325. } );
  326. it( 'should set all border styles (3 values defined)', () => {
  327. styles.setStyle( 'border-style:solid dotted dashed;' );
  328. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  329. style: {
  330. top: 'solid',
  331. right: 'dotted',
  332. bottom: 'dashed',
  333. left: 'dotted'
  334. }
  335. } );
  336. } );
  337. it( 'should set all border styles (4 values defined)', () => {
  338. styles.setStyle( 'border-style:solid dotted dashed ridge;' );
  339. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  340. style: {
  341. top: 'solid',
  342. right: 'dotted',
  343. bottom: 'dashed',
  344. left: 'ridge'
  345. }
  346. } );
  347. } );
  348. } );
  349. describe( 'border-width', () => {
  350. it( 'should set all border widths (1 value defined)', () => {
  351. styles.setStyle( 'border-width:1px;' );
  352. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  353. width: {
  354. top: '1px',
  355. right: '1px',
  356. bottom: '1px',
  357. left: '1px'
  358. }
  359. } );
  360. } );
  361. it( 'should set all border widths (2 values defined)', () => {
  362. styles.setStyle( 'border-width:1px .34cm;' );
  363. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  364. width: {
  365. top: '1px',
  366. right: '.34cm',
  367. bottom: '1px',
  368. left: '.34cm'
  369. }
  370. } );
  371. } );
  372. it( 'should set all border widths (3 values defined)', () => {
  373. styles.setStyle( 'border-width:1px .34cm 90.1rem;' );
  374. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  375. width: {
  376. top: '1px',
  377. right: '.34cm',
  378. bottom: '90.1rem',
  379. left: '.34cm'
  380. }
  381. } );
  382. } );
  383. it( 'should set all border widths (4 values defined)', () => {
  384. styles.setStyle( 'border-width:1px .34cm 90.1rem thick;' );
  385. expect( styles.getNormalized( 'border' ) ).to.deep.equal( {
  386. width: {
  387. top: '1px',
  388. right: '.34cm',
  389. bottom: '90.1rem',
  390. left: 'thick'
  391. }
  392. } );
  393. } );
  394. } );
  395. } );
  396. describe( 'margin', () => {
  397. it( 'should set all margins (1 value defined)', () => {
  398. styles.setStyle( 'margin:1px;' );
  399. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  400. top: '1px',
  401. right: '1px',
  402. bottom: '1px',
  403. left: '1px'
  404. } );
  405. } );
  406. it( 'should set all margins (2 values defined)', () => {
  407. styles.setStyle( 'margin:1px .34cm;' );
  408. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  409. top: '1px',
  410. right: '.34cm',
  411. bottom: '1px',
  412. left: '.34cm'
  413. } );
  414. } );
  415. it( 'should set all margins (3 values defined)', () => {
  416. styles.setStyle( 'margin:1px .34cm 90.1rem;' );
  417. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  418. top: '1px',
  419. right: '.34cm',
  420. bottom: '90.1rem',
  421. left: '.34cm'
  422. } );
  423. } );
  424. it( 'should set all margins (4 values defined)', () => {
  425. styles.setStyle( 'margin:1px .34cm 90.1rem thick;' );
  426. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  427. top: '1px',
  428. right: '.34cm',
  429. bottom: '90.1rem',
  430. left: 'thick'
  431. } );
  432. } );
  433. it( 'should output inline style (1 value defined)', () => {
  434. styles.setStyle( 'margin:1px;' );
  435. expect( styles.getInlineStyle() ).to.equal( 'margin:1px;' );
  436. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px' );
  437. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  438. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  439. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  440. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  441. } );
  442. it( 'should output inline style (2 values defined)', () => {
  443. styles.setStyle( 'margin:1px .34cm;' );
  444. expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm;' );
  445. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm' );
  446. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  447. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  448. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  449. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '.34cm' );
  450. } );
  451. it( 'should output inline style (3 values defined)', () => {
  452. styles.setStyle( 'margin:1px .34cm 90.1rem;' );
  453. expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm 90.1rem;' );
  454. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem' );
  455. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  456. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  457. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  458. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '.34cm' );
  459. } );
  460. it( 'should output inline style (3 values defined, only last different)', () => {
  461. styles.setStyle( 'margin:1px 1px 90.1rem;' );
  462. expect( styles.getInlineStyle() ).to.equal( 'margin:1px 1px 90.1rem;' );
  463. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 90.1rem' );
  464. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  465. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  466. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  467. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  468. } );
  469. it( 'should output inline style (4 values defined)', () => {
  470. styles.setStyle( 'margin:1px .34cm 90.1rem thick;' );
  471. expect( styles.getInlineStyle() ).to.equal( 'margin:1px .34cm 90.1rem thick;' );
  472. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px .34cm 90.1rem thick' );
  473. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  474. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '.34cm' );
  475. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '90.1rem' );
  476. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( 'thick' );
  477. } );
  478. it( 'should output inline style (4 values defined, only last different)', () => {
  479. styles.setStyle( 'margin:1px 1px 1px thick;' );
  480. expect( styles.getInlineStyle() ).to.equal( 'margin:1px 1px 1px thick;' );
  481. expect( styles.getInlineProperty( 'margin' ) ).to.equal( '1px 1px 1px thick' );
  482. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  483. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  484. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  485. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( 'thick' );
  486. } );
  487. describe( 'margin-*', () => {
  488. it( 'should set proper margin', () => {
  489. styles.setStyle( 'margin-top:1px;' );
  490. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( { top: '1px' } );
  491. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  492. } );
  493. it( 'should merge margin with margin shorthand', () => {
  494. styles.setStyle( 'margin: 2em;margin-top:1px;' );
  495. expect( styles.getNormalized( 'margin' ) ).to.deep.equal( {
  496. top: '1px',
  497. right: '2em',
  498. bottom: '2em',
  499. left: '2em'
  500. } );
  501. expect( styles.getNormalized( 'margin-top' ) ).to.equal( '1px' );
  502. expect( styles.getNormalized( 'margin-right' ) ).to.equal( '2em' );
  503. expect( styles.getNormalized( 'margin-bottom' ) ).to.equal( '2em' );
  504. expect( styles.getNormalized( 'margin-left' ) ).to.equal( '2em' );
  505. } );
  506. it( 'should output margin-top', () => {
  507. styles.setStyle( 'margin-top:1px;' );
  508. expect( styles.getInlineStyle() ).to.equal( 'margin-top:1px;' );
  509. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  510. } );
  511. it( 'should output margin-right', () => {
  512. styles.setStyle( 'margin-right:1px;' );
  513. expect( styles.getInlineStyle() ).to.equal( 'margin-right:1px;' );
  514. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '1px' );
  515. } );
  516. it( 'should output margin-bottom', () => {
  517. styles.setStyle( 'margin-bottom:1px;' );
  518. expect( styles.getInlineStyle() ).to.equal( 'margin-bottom:1px;' );
  519. expect( styles.getInlineProperty( 'margin-bottom' ) ).to.equal( '1px' );
  520. } );
  521. it( 'should output margin-left', () => {
  522. styles.setStyle( 'margin-left:1px;' );
  523. expect( styles.getInlineStyle() ).to.equal( 'margin-left:1px;' );
  524. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  525. } );
  526. } );
  527. } );
  528. describe( 'padding', () => {
  529. it( 'should set all paddings (1 value defined)', () => {
  530. styles.setStyle( 'padding:1px;' );
  531. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  532. top: '1px',
  533. right: '1px',
  534. bottom: '1px',
  535. left: '1px'
  536. } );
  537. } );
  538. it( 'should set all paddings (2 values defined)', () => {
  539. styles.setStyle( 'padding:1px .34cm;' );
  540. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  541. top: '1px',
  542. right: '.34cm',
  543. bottom: '1px',
  544. left: '.34cm'
  545. } );
  546. } );
  547. it( 'should set all paddings (3 values defined)', () => {
  548. styles.setStyle( 'padding:1px .34cm 90.1rem;' );
  549. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  550. top: '1px',
  551. right: '.34cm',
  552. bottom: '90.1rem',
  553. left: '.34cm'
  554. } );
  555. } );
  556. it( 'should set all paddings (4 values defined)', () => {
  557. styles.setStyle( 'padding:1px .34cm 90.1rem thick;' );
  558. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  559. top: '1px',
  560. right: '.34cm',
  561. bottom: '90.1rem',
  562. left: 'thick'
  563. } );
  564. } );
  565. describe( 'padding-*', () => {
  566. it( 'should set proper padding', () => {
  567. styles.setStyle( 'padding-top:1px;' );
  568. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  569. top: '1px'
  570. } );
  571. } );
  572. it( 'should set proper padding with padding shorthand', () => {
  573. styles.setStyle( 'padding: 2em;padding-top:1px;' );
  574. expect( styles.getNormalized( 'padding' ) ).to.deep.equal( {
  575. top: '1px',
  576. right: '2em',
  577. bottom: '2em',
  578. left: '2em'
  579. } );
  580. } );
  581. } );
  582. } );
  583. describe( 'unknown rules', () => {
  584. it( 'should left rules untouched', () => {
  585. styles.setStyle( 'foo-bar:baz 1px abc;baz: 2px 3em;' );
  586. expect( styles.getInlineStyle() ).to.equal( 'baz:2px 3em;foo-bar:baz 1px abc;' );
  587. expect( styles.getInlineProperty( 'foo-bar' ) ).to.equal( 'baz 1px abc' );
  588. expect( styles.getInlineProperty( 'baz' ) ).to.equal( '2px 3em' );
  589. } );
  590. } );
  591. describe( 'background', () => {
  592. it( 'should normalize background', () => {
  593. // TODO: border-box given only for coverage test.
  594. styles.setStyle( 'background:url("example.jpg") center #f00 repeat-y fixed border-box;' );
  595. expect( styles.getNormalized( 'background' ) ).to.deep.equal( {
  596. attachment: 'fixed',
  597. image: 'url("example.jpg")',
  598. position: [ 'center' ],
  599. repeat: [ 'repeat-y' ],
  600. color: '#f00'
  601. } );
  602. } );
  603. // TODO: define what should happen with layers
  604. it.skip( 'should normalize background with layers', () => {
  605. styles.setStyle( 'background:url("test.jpg") repeat-y,#f00;' );
  606. expect( styles.getNormalized( 'background' ) ).to.deep.equal( { color: '#f00' } );
  607. } );
  608. it( 'should normalize background-color', () => {
  609. styles.setStyle( 'background-color:#f00;' );
  610. expect( styles.getNormalized( 'background' ) ).to.deep.equal( { color: '#f00' } );
  611. } );
  612. it( 'should output inline background-color style', () => {
  613. styles.setStyle( 'background:#f00;' );
  614. expect( styles.getInlineStyle() ).to.equal( 'background-color:#f00;' );
  615. expect( styles.getInlineProperty( 'background-color' ) ).to.equal( '#f00' );
  616. } );
  617. } );
  618. } );
  619. } );