styles.js 26 KB

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