restore-from-optimizing.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.unused) {
  16. continue;
  17. }
  18. if (!property.dirty && !property.important && !property.hack) {
  19. continue;
  20. }
  21. if (restoreCallback) {
  22. restored = restoreCallback(property);
  23. property.value = restored;
  24. } else {
  25. restored = property.value;
  26. }
  27. if (property.important) {
  28. restoreImportant(property);
  29. }
  30. if (property.hack) {
  31. restoreHack(property);
  32. }
  33. if ('all' in property) {
  34. current = property.all[property.position];
  35. current[1][1] = property.name;
  36. current.splice(2, current.length - 1);
  37. Array.prototype.push.apply(current, restored);
  38. }
  39. }
  40. }
  41. function restoreImportant(property) {
  42. property.value[property.value.length - 1][1] += IMPORTANT_TOKEN;
  43. }
  44. function restoreHack(property) {
  45. if (property.hack[0] == Hack.UNDERSCORE) {
  46. property.name = UNDERSCORE_HACK + property.name;
  47. } else if (property.hack[0] == Hack.ASTERISK) {
  48. property.name = ASTERISK_HACK + property.name;
  49. } else if (property.hack[0] == Hack.BACKSLASH) {
  50. property.value[property.value.length - 1][1] += BACKSLASH_HACK + property.hack[1];
  51. } else if (property.hack[0] == Hack.BANG) {
  52. property.value[property.value.length - 1][1] += Marker.SPACE + BANG_HACK;
  53. }
  54. }
  55. module.exports = restoreFromOptimizing;