flex.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const flexGrow = require("./flexGrow");
  4. const flexShrink = require("./flexShrink");
  5. const flexBasis = require("./flexBasis");
  6. const shorthandFor = new Map([
  7. ["flex-grow", flexGrow],
  8. ["flex-shrink", flexShrink],
  9. ["flex-basis", flexBasis]
  10. ]);
  11. module.exports.parse = function parse(v) {
  12. const key = parsers.parseKeyword(v, ["auto", "none"]);
  13. if (key) {
  14. if (key === "auto") {
  15. return "1 1 auto";
  16. }
  17. if (key === "none") {
  18. return "0 0 auto";
  19. }
  20. if (key === "initial") {
  21. return "0 1 auto";
  22. }
  23. return;
  24. }
  25. const obj = parsers.parseShorthand(v, shorthandFor);
  26. if (obj) {
  27. const flex = {
  28. "flex-grow": "1",
  29. "flex-shrink": "1",
  30. "flex-basis": "0%"
  31. };
  32. const items = Object.entries(obj);
  33. for (const [property, value] of items) {
  34. flex[property] = value;
  35. }
  36. return [...Object.values(flex)].join(" ");
  37. }
  38. };
  39. module.exports.isValid = function isValid(v) {
  40. if (v === "") {
  41. return true;
  42. }
  43. return typeof module.exports.parse(v) === "string";
  44. };
  45. module.exports.definition = {
  46. set(v) {
  47. v = parsers.prepareValue(v, this._global);
  48. if (parsers.hasVarFunc(v)) {
  49. this._shorthandSetter("flex", "", shorthandFor);
  50. this._setProperty("flex", v);
  51. } else {
  52. this._shorthandSetter("flex", module.exports.parse(v), shorthandFor);
  53. }
  54. },
  55. get() {
  56. let val = this.getPropertyValue("flex");
  57. if (parsers.hasVarFunc(val)) {
  58. return val;
  59. }
  60. val = this._shorthandGetter("flex", shorthandFor);
  61. if (parsers.hasVarFunc(val)) {
  62. return "";
  63. }
  64. return val;
  65. },
  66. enumerable: true,
  67. configurable: true
  68. };