wrap-for-optimizing.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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, includeVariable) {
  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 (!includeVariable && someVariableReferences(property)) {
  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 && !lastValue[1].match(Match.IMPORTANT_WORD_PATTERN) && Match.BANG_SUFFIX_PATTERN.test(lastValue[1])) {
  77. match = [Hack.BANG];
  78. } else if (lastValue[1].indexOf(Match.BACKSLASH) > 0 && lastValue[1].indexOf(Match.BACKSLASH) == lastValue[1].length - Match.BACKSLASH.length - 1) {
  79. match = [Hack.BACKSLASH, lastValue[1].substring(lastValue[1].indexOf(Match.BACKSLASH) + 1)];
  80. } else if (lastValue[1].indexOf(Match.BACKSLASH) === 0 && lastValue[1].length == 2) {
  81. match = [Hack.BACKSLASH, lastValue[1].substring(1)];
  82. }
  83. return match;
  84. }
  85. function isImportant(property) {
  86. if (property.length < 3)
  87. return false;
  88. var lastValue = property[property.length - 1];
  89. if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
  90. return true;
  91. } else if (Match.IMPORTANT_WORD_PATTERN.test(lastValue[1]) && Match.SUFFIX_BANG_PATTERN.test(property[property.length - 2][1])) {
  92. return true;
  93. }
  94. return false;
  95. }
  96. function stripImportant(property) {
  97. var lastValue = property[property.length - 1];
  98. var oneButLastValue = property[property.length - 2];
  99. if (Match.IMPORTANT_TOKEN_PATTERN.test(lastValue[1])) {
  100. lastValue[1] = lastValue[1].replace(Match.IMPORTANT_TOKEN_PATTERN, '');
  101. } else {
  102. lastValue[1] = lastValue[1].replace(Match.IMPORTANT_WORD_PATTERN, '');
  103. oneButLastValue[1] = oneButLastValue[1].replace(Match.SUFFIX_BANG_PATTERN, '');
  104. }
  105. if (lastValue[1].length === 0) {
  106. property.pop();
  107. }
  108. if (oneButLastValue[1].length === 0) {
  109. property.pop();
  110. }
  111. }
  112. function stripPrefixHack(property) {
  113. property[1][1] = property[1][1].substring(1);
  114. }
  115. function stripSuffixHack(property, hackFrom) {
  116. var lastValue = property[property.length - 1];
  117. lastValue[1] = lastValue[1]
  118. .substring(0, lastValue[1].indexOf(hackFrom[0] == Hack.BACKSLASH ? Match.BACKSLASH : Match.BANG))
  119. .trim();
  120. if (lastValue[1].length === 0) {
  121. property.pop();
  122. }
  123. }
  124. function wrapSingle(property) {
  125. var importantProperty = isImportant(property);
  126. if (importantProperty) {
  127. stripImportant(property);
  128. }
  129. var whichHack = hackFrom(property);
  130. if (whichHack[0] == Hack.ASTERISK || whichHack[0] == Hack.UNDERSCORE) {
  131. stripPrefixHack(property);
  132. } else if (whichHack[0] == Hack.BACKSLASH || whichHack[0] == Hack.BANG) {
  133. stripSuffixHack(property, whichHack);
  134. }
  135. return {
  136. block: property[2] && property[2][0] == Token.PROPERTY_BLOCK,
  137. components: [],
  138. dirty: false,
  139. hack: whichHack,
  140. important: importantProperty,
  141. name: property[1][1],
  142. multiplex: property.length > 3 ? isMultiplex(property) : false,
  143. position: 0,
  144. shorthand: false,
  145. unused: false,
  146. value: property.slice(2)
  147. };
  148. }
  149. module.exports = {
  150. all: wrapAll,
  151. single: wrapSingle
  152. };