zero.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var split = require('../../../utils/split');
  2. var ANY_FUNCTION_PATTERN = /^(-(?:moz|ms|o|webkit)-[a-z-]+|[a-z-]+)\((.+)\)$/;
  3. var SKIP_FUNCTION_PATTERN = /^(?:-moz-calc|-webkit-calc|calc|rgb|hsl|rgba|hsla|min|max|clamp|expression)\(/;
  4. var TOKEN_SEPARATOR_PATTERN = /([\s,/])/;
  5. function removeRecursively(value, options) {
  6. var functionTokens;
  7. var tokens;
  8. if (SKIP_FUNCTION_PATTERN.test(value)) {
  9. return value;
  10. }
  11. functionTokens = ANY_FUNCTION_PATTERN.exec(value);
  12. if (!functionTokens) {
  13. return removeZeros(value, options);
  14. }
  15. tokens = split(functionTokens[2], TOKEN_SEPARATOR_PATTERN)
  16. .map(function(token) { return removeRecursively(token, options); });
  17. return functionTokens[1] + '(' + tokens.join('') + ')';
  18. }
  19. function removeZeros(value, options) {
  20. return value
  21. .replace(options.unitsRegexp, '$10$2')
  22. .replace(options.unitsRegexp, '$10$2');
  23. }
  24. var plugin = {
  25. level1: {
  26. value: function zero(name, value, options) {
  27. if (!options.compatibility.properties.zeroUnits) {
  28. return value;
  29. }
  30. if (value.indexOf('%') > 0 && (name == 'height' || name == 'max-height' || name == 'width' || name == 'max-width')) {
  31. return value;
  32. }
  33. return removeRecursively(value, options);
  34. }
  35. }
  36. };
  37. module.exports = plugin;