styles.js 31 KB

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