test_flexbox_flex_shorthand.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=696253
  5. -->
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Test for Bug 696253</title>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <script type="text/javascript" src="property_database.js"></script>
  11. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  12. </head>
  13. <body>
  14. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=696253">Mozilla Bug 696253</a>
  15. <div id="display">
  16. <div id="content">
  17. </div>
  18. </div>
  19. <pre id="test">
  20. <script type="application/javascript;version=1.7">
  21. "use strict";
  22. /** Test for Bug 696253 **/
  23. /* (Testing the 'flex' CSS shorthand property) */
  24. // The CSS property name for the shorthand we're testing:
  25. const gFlexPropName = "flex";
  26. // Info from property_database.js on this property:
  27. const gFlexPropInfo = gCSSProperties[gFlexPropName];
  28. // The name of the property in the DOM (i.e. in elem.style):
  29. // (NOTE: In this case it's actually the same as the CSS property name --
  30. // "flex" -- but that's not guaranteed in general.)
  31. const gFlexDOMName = gFlexPropInfo.domProp;
  32. // Default values for shorthand subproperties, when they're not specified
  33. // explicitly in a testcase. This lets the testcases be more concise.
  34. //
  35. // The values here are from the flexbox spec on the 'flex' shorthand:
  36. // "When omitted, [flex-grow and flex-shrink are] set to '1'."
  37. // "When omitted [..., flex-basis's] specified value is '0%'."
  38. let gFlexShorthandDefaults = {
  39. "flex-grow": "1",
  40. "flex-shrink": "1",
  41. "flex-basis": "0%"
  42. };
  43. let gFlexShorthandTestcases = [
  44. /*
  45. {
  46. "flex": "SPECIFIED value for flex shorthand",
  47. // Expected Computed Values of Subproperties
  48. // Semi-optional -- if unspecified, the expected value is taken
  49. // from gFlexShorthandDefaults.
  50. "flex-grow": "EXPECTED computed value for flex-grow property",
  51. "flex-shrink": "EXPECTED computed value for flex-shrink property",
  52. "flex-basis": "EXPECTED computed value for flex-basis property",
  53. },
  54. */
  55. // Initial values of subproperties:
  56. // --------------------------------
  57. // (checked by another test that uses property_database.js, too, but
  58. // might as well check here, too, for thoroughness).
  59. {
  60. "flex": "",
  61. "flex-grow": "0",
  62. "flex-shrink": "1",
  63. "flex-basis": "auto",
  64. },
  65. {
  66. "flex": "initial",
  67. "flex-grow": "0",
  68. "flex-shrink": "1",
  69. "flex-basis": "auto",
  70. },
  71. // Special keyword "none" --> "0 0 auto"
  72. // -------------------------------------
  73. {
  74. "flex": "none",
  75. "flex-grow": "0",
  76. "flex-shrink": "0",
  77. "flex-basis": "auto",
  78. },
  79. // One Value (numeric) --> sets flex-grow
  80. // --------------------------------------
  81. {
  82. "flex": "0",
  83. "flex-grow": "0",
  84. },
  85. {
  86. "flex": "5",
  87. "flex-grow": "5",
  88. },
  89. {
  90. "flex": "1000",
  91. "flex-grow": "1000",
  92. },
  93. {
  94. "flex": "0.0000001",
  95. "flex-grow": "1e-7"
  96. },
  97. {
  98. "flex": "20000000",
  99. "flex-grow": "2e+7"
  100. },
  101. // One Value (length or other nonnumeric) --> sets flex-basis
  102. // ----------------------------------------------------------
  103. {
  104. "flex": "0px",
  105. "flex-basis": "0px",
  106. },
  107. {
  108. "flex": "0%",
  109. "flex-basis": "0%",
  110. },
  111. {
  112. "flex": "25px",
  113. "flex-basis": "25px",
  114. },
  115. {
  116. "flex": "5%",
  117. "flex-basis": "5%",
  118. },
  119. {
  120. "flex": "auto",
  121. "flex-basis": "auto",
  122. },
  123. {
  124. "flex": "-moz-fit-content",
  125. "flex-basis": "-moz-fit-content",
  126. },
  127. {
  128. "flex": "calc(5px + 6px)",
  129. "flex-basis": "11px",
  130. },
  131. {
  132. "flex": "calc(15% + 30px)",
  133. "flex-basis": "calc(30px + 15%)",
  134. },
  135. // Two Values (numeric) --> sets flex-grow, flex-shrink
  136. // ----------------------------------------------------
  137. {
  138. "flex": "0 0",
  139. "flex-grow": "0",
  140. "flex-shrink": "0",
  141. },
  142. {
  143. "flex": "0 2",
  144. "flex-grow": "0",
  145. "flex-shrink": "2",
  146. },
  147. {
  148. "flex": "3 0",
  149. "flex-grow": "3",
  150. "flex-shrink": "0",
  151. },
  152. {
  153. "flex": "0.5000 2.03",
  154. "flex-grow": "0.5",
  155. "flex-shrink": "2.03",
  156. },
  157. {
  158. "flex": "300.0 500.0",
  159. "flex-grow": "300",
  160. "flex-shrink": "500",
  161. },
  162. // Two Values (numeric & length-ish) --> sets flex-grow, flex-basis
  163. // ----------------------------------------------------------------
  164. {
  165. "flex": "0 0px",
  166. "flex-grow": "0",
  167. "flex-basis": "0px",
  168. },
  169. {
  170. "flex": "0 0%",
  171. "flex-grow": "0",
  172. "flex-basis": "0%",
  173. },
  174. {
  175. "flex": "10 30px",
  176. "flex-grow": "10",
  177. "flex-basis": "30px",
  178. },
  179. {
  180. "flex": "99px 2.3",
  181. "flex-grow": "2.3",
  182. "flex-basis": "99px",
  183. },
  184. {
  185. "flex": "99% 6",
  186. "flex-grow": "6",
  187. "flex-basis": "99%",
  188. },
  189. {
  190. "flex": "auto 5",
  191. "flex-grow": "5",
  192. "flex-basis": "auto",
  193. },
  194. {
  195. "flex": "5 -moz-fit-content",
  196. "flex-grow": "5",
  197. "flex-basis": "-moz-fit-content",
  198. },
  199. {
  200. "flex": "calc(5% + 10px) 3",
  201. "flex-grow": "3",
  202. "flex-basis": "calc(10px + 5%)",
  203. },
  204. // Three Values --> Sets all three subproperties
  205. // ---------------------------------------------
  206. {
  207. "flex": "0 0 0",
  208. "flex-grow": "0",
  209. "flex-shrink": "0",
  210. "flex-basis": "0px",
  211. },
  212. {
  213. "flex": "0.0 0.00 0px",
  214. "flex-grow": "0",
  215. "flex-shrink": "0",
  216. "flex-basis": "0px",
  217. },
  218. {
  219. "flex": "0% 0 0",
  220. "flex-grow": "0",
  221. "flex-shrink": "0",
  222. "flex-basis": "0%",
  223. },
  224. {
  225. "flex": "10px 3 2",
  226. "flex-grow": "3",
  227. "flex-shrink": "2",
  228. "flex-basis": "10px",
  229. },
  230. ];
  231. function runFlexShorthandTest(aFlexShorthandTestcase)
  232. {
  233. let content = document.getElementById("content");
  234. let elem = document.createElement("div");
  235. elem.style[gFlexDOMName] = aFlexShorthandTestcase[gFlexPropName];
  236. content.appendChild(elem);
  237. gFlexPropInfo.subproperties.forEach(function(aSubPropName) {
  238. var expectedVal = aSubPropName in aFlexShorthandTestcase ?
  239. aFlexShorthandTestcase[aSubPropName] :
  240. gFlexShorthandDefaults[aSubPropName];
  241. // Compare computed value against expected computed value (from testcase)
  242. is(window.getComputedStyle(elem, null).getPropertyValue(aSubPropName),
  243. expectedVal,
  244. "Computed value of subproperty \"" + aSubPropName + "\" when we set \"" +
  245. gFlexPropName + ": " + aFlexShorthandTestcase[gFlexPropName] + "\"");
  246. });
  247. // Clean up
  248. content.removeChild(elem);
  249. }
  250. function main() {
  251. gFlexShorthandTestcases.forEach(runFlexShorthandTest);
  252. }
  253. main();
  254. </script>
  255. </pre>
  256. </body>
  257. </html>