wrap-for-optimizing.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. var Hack = require('./hack');
  2. var Marker = require('../tokenizer/marker');
  3. var Token = require('../tokenizer/token');
  4. var Match = {
  5. ASTERISK: '*',
  6. BACKSLASH: '\\',
  7. BANG: '!',
  8. BANG_SUFFIX_PATTERN: /!\w+$/,
  9. IMPORTANT_TOKEN: '!important',
  10. IMPORTANT_TOKEN_PATTERN: new RegExp('!important$', 'i'),
  11. IMPORTANT_WORD: 'important',
  12. IMPORTANT_WORD_PATTERN: new RegExp('important$', 'i'),
  13. SUFFIX_BANG_PATTERN: /!$/,
  14. UNDERSCORE: '_',
  15. VARIABLE_REFERENCE_PATTERN: /var\(--.+\)$/
  16. };
  17. function wrapAll(properties, skipProperties) {
  18. var wrapped = [];
  19. var single;
  20. var property;
  21. var i;
  22. for (i = properties.length - 1; i >= 0; i--) {
  23. property = properties[i];
  24. if (property[0] != Token.PROPERTY) {
  25. continue;
  26. }
  27. if (skipProperties && skipProperties.indexOf(property[1][1]) > -1) {
  28. continue;
  29. }
  30. single = wrapSingle(property);
  31. single.all = properties;
  32. single.position = i;
  33. wrapped.unshift(single);
  34. }
  35. return wrapped;
  36. }
  37. function someVariableReferences(property) {
  38. var i, l;
  39. var value;
  40. // skipping `property` and property name tokens
  41. for (i = 2, l = property.length; i < l; i++) {
  42. value = property[i];
  43. if (value[0] != Token.PROPERTY_VALUE) {
  44. continue;
  45. }
  46. if (isVariableReference(value[1])) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. function isVariableReference(value) {
  53. return Match.VARIABLE_REFERENCE_PATTERN.test(value);
  54. }
  55. function isMultiplex(property) {
  56. var value;
  57. var i, l;
  58. for (i = 3, l = property.length; i < l; i++) {
  59. value = property[i];
  60. if (value[0] == Token.PROPERTY_VALUE && (value[1] == Marker.COMMA || value[1] == Marker.FORWARD_SLASH)) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. function hackFrom(property) {
  67. var match = false;
  68. var name = property[1][1];
  69. var lastValue = property[property.length - 1];
  70. if (name[0] == Match.UNDERSCORE) {
  71. match = [Hack.UNDERSCORE];
  72. } else if (name[0] == Match.ASTERISK) {
  73. match = [Hack.ASTERISK];
  74. } else if (lastValue[1][0] == Match.BANG && !lastValue[1].match(Match.IMPORTANT_WORD_PATTERN)) {
  75. match = [Hack.BANG];
  76. } else if (lastValue[1].indexOf(Match.BANG) > 0
  77. && !lastValue[1].match(Match.IMPORTANT_WORD_PATTERN)
  78. && Match.BANG_SUFFIX_PATTERN.test(lastValue[1])) {
  79. match = [Hack.BANG];
  80. } else if (lastValue[1].indexOf(Match.BACKSLASH) > 0
  81. && lastValue[1].indexOf(Match.BACKSLASH) == lastValue[1].length - Match.BACKSLASH.length - 1) {
  82. match = [Hack.BACKSLASH, lastValue[1].substring(lastValue[1].indexOf(Match.BACKSLASH) + 1)];
  83. } else if (lastValue[1].indexOf(Match.BACKSLASH) === 0 && lastValue[1].length == 2) {
  84. match = [Hack.BACKSLASH, lastValue[1].substring(1)];
  85. }
  86. return match;
  87. }
  88. function isImportant(property) {
  89. if (property.length < 3) { return false; }
  90. var lastValue = property[property.length - 1];
  91. if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
  92. return true;
  93. } if (Match.IMPORTANT_WORD_PATTERN.test(lastValue[1])
  94. && Match.SUFFIX_BANG_PATTERN.test(property[property.length - 2][1])) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. function stripImportant(property) {
  100. var lastValue = property[property.length - 1];
  101. var oneButLastValue = property[property.length - 2];
  102. if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
  103. lastValue[1] = lastValue[1].replace(Match.IMPORTANT_TOKEN_PATTERN, '');
  104. } else {
  105. lastValue[1] = lastValue[1].replace(Match.IMPORTANT_WORD_PATTERN, '');
  106. oneButLastValue[1] = oneButLastValue[1].replace(Match.SUFFIX_BANG_PATTERN, '');
  107. }
  108. if (lastValue[1].length === 0) {
  109. property.pop();
  110. }
  111. if (oneButLastValue[1].length === 0) {
  112. property.pop();
  113. }
  114. }
  115. function stripPrefixHack(property) {
  116. property[1][1] = property[1][1].substring(1);
  117. }
  118. function stripSuffixHack(property, hackFrom) {
  119. var lastValue = property[property.length - 1];
  120. lastValue[1] = lastValue[1]
  121. .substring(0, lastValue[1].indexOf(hackFrom[0] == Hack.BACKSLASH ? Match.BACKSLASH : Match.BANG))
  122. .trim();
  123. if (lastValue[1].length === 0) {
  124. property.pop();
  125. }
  126. }
  127. function wrapSingle(property) {
  128. var importantProperty = isImportant(property);
  129. if (importantProperty) {
  130. stripImportant(property);
  131. }
  132. var whichHack = hackFrom(property);
  133. if (whichHack[0] == Hack.ASTERISK || whichHack[0] == Hack.UNDERSCORE) {
  134. stripPrefixHack(property);
  135. } else if (whichHack[0] == Hack.BACKSLASH || whichHack[0] == Hack.BANG) {
  136. stripSuffixHack(property, whichHack);
  137. }
  138. return {
  139. block: property[2] && property[2][0] == Token.PROPERTY_BLOCK,
  140. components: [],
  141. dirty: false,
  142. dynamic: someVariableReferences(property),
  143. hack: whichHack,
  144. important: importantProperty,
  145. name: property[1][1],
  146. multiplex: property.length > 3 ? isMultiplex(property) : false,
  147. optimizable: true,
  148. position: 0,
  149. shorthand: false,
  150. unused: false,
  151. value: property.slice(2)
  152. };
  153. }
  154. module.exports = {
  155. all: wrapAll,
  156. single: wrapSingle
  157. };