styles.js 30 KB

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