unit.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var WHOLE_PIXEL_VALUE = /(?:^|\s|\()(-?\d+)px/;
  2. var plugin = {
  3. level1: {
  4. value: function unit(_name, value, options) {
  5. if (!WHOLE_PIXEL_VALUE.test(value)) {
  6. return value;
  7. }
  8. return value.replace(WHOLE_PIXEL_VALUE, function(match, val) {
  9. var newValue;
  10. var intVal = parseInt(val);
  11. if (intVal === 0) {
  12. return match;
  13. }
  14. if (options.compatibility.properties.shorterLengthUnits
  15. && options.compatibility.units.pt
  16. && intVal * 3 % 4 === 0) {
  17. newValue = intVal * 3 / 4 + 'pt';
  18. }
  19. if (options.compatibility.properties.shorterLengthUnits
  20. && options.compatibility.units.pc
  21. && intVal % 16 === 0) {
  22. newValue = intVal / 16 + 'pc';
  23. }
  24. if (options.compatibility.properties.shorterLengthUnits
  25. && options.compatibility.units.in
  26. && intVal % 96 === 0) {
  27. newValue = intVal / 96 + 'in';
  28. }
  29. if (newValue) {
  30. newValue = match.substring(0, match.indexOf(val)) + newValue;
  31. }
  32. return newValue && newValue.length < match.length ? newValue : match;
  33. });
  34. }
  35. }
  36. };
  37. module.exports = plugin;