opacity.js 920 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. const parsers = require("../parsers");
  3. module.exports.parse = function parse(v) {
  4. let num = parsers.parseNumber(v);
  5. if (num) {
  6. num = parseFloat(num);
  7. if (num < 0) {
  8. return "0";
  9. } else if (num > 1) {
  10. return "1";
  11. }
  12. return `${num}`;
  13. }
  14. let pct = parsers.parsePercent(v);
  15. if (pct) {
  16. pct = parseFloat(pct);
  17. if (pct < 0) {
  18. return "0%";
  19. } else if (pct > 100) {
  20. return "100%";
  21. }
  22. return `${pct}%`;
  23. }
  24. return parsers.parseKeyword(v);
  25. };
  26. module.exports.isValid = function isValid(v) {
  27. if (v === "") {
  28. return true;
  29. }
  30. return typeof module.exports.parse(v) === "string";
  31. };
  32. module.exports.definition = {
  33. set(v) {
  34. v = parsers.prepareValue(v, this._global);
  35. this._setProperty("opacity", module.exports.parse(v));
  36. },
  37. get() {
  38. return this.getPropertyValue("opacity");
  39. },
  40. enumerable: true,
  41. configurable: true
  42. };