restore-from-optimizing.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var Hack = require('./hack');
  2. var Marker = require('../tokenizer/marker');
  3. var ASTERISK_HACK = '*';
  4. var BACKSLASH_HACK = '\\';
  5. var IMPORTANT_TOKEN = '!important';
  6. var UNDERSCORE_HACK = '_';
  7. var BANG_HACK = '!ie';
  8. function restoreFromOptimizing(properties, restoreCallback) {
  9. var property;
  10. var restored;
  11. var current;
  12. var i;
  13. for (i = properties.length - 1; i >= 0; i--) {
  14. property = properties[i];
  15. if (property.dynamic && property.important) {
  16. restoreImportant(property);
  17. continue;
  18. }
  19. if (property.dynamic) {
  20. continue;
  21. }
  22. if (property.unused) {
  23. continue;
  24. }
  25. if (!property.dirty && !property.important && !property.hack) {
  26. continue;
  27. }
  28. if (property.optimizable && restoreCallback) {
  29. restored = restoreCallback(property);
  30. property.value = restored;
  31. } else {
  32. restored = property.value;
  33. }
  34. if (property.important) {
  35. restoreImportant(property);
  36. }
  37. if (property.hack) {
  38. restoreHack(property);
  39. }
  40. if ('all' in property) {
  41. current = property.all[property.position];
  42. current[1][1] = property.name;
  43. current.splice(2, current.length - 1);
  44. Array.prototype.push.apply(current, restored);
  45. }
  46. }
  47. }
  48. function restoreImportant(property) {
  49. property.value[property.value.length - 1][1] += IMPORTANT_TOKEN;
  50. }
  51. function restoreHack(property) {
  52. if (property.hack[0] == Hack.UNDERSCORE) {
  53. property.name = UNDERSCORE_HACK + property.name;
  54. } else if (property.hack[0] == Hack.ASTERISK) {
  55. property.name = ASTERISK_HACK + property.name;
  56. } else if (property.hack[0] == Hack.BACKSLASH) {
  57. property.value[property.value.length - 1][1] += BACKSLASH_HACK + property.hack[1];
  58. } else if (property.hack[0] == Hack.BANG) {
  59. property.value[property.value.length - 1][1] += Marker.SPACE + BANG_HACK;
  60. }
  61. }
  62. module.exports = restoreFromOptimizing;