override-compactor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. var canOverride = require('./can-override');
  2. var compactable = require('./compactable');
  3. var deepClone = require('./clone').deep;
  4. var shallowClone = require('./clone').shallow;
  5. var hasInherit = require('./has-inherit');
  6. var restoreFromOptimizing = require('./restore-from-optimizing');
  7. var everyCombination = require('./every-combination');
  8. var sameVendorPrefixesIn = require('./vendor-prefixes').same;
  9. var stringifyProperty = require('../stringifier/one-time').property;
  10. var MULTIPLEX_SEPARATOR = ',';
  11. // Used when searching for a component that matches property
  12. function nameMatchFilter(to) {
  13. return function (property) {
  14. return to.name === property.name;
  15. };
  16. }
  17. function wouldBreakCompatibility(property, validator) {
  18. for (var i = 0; i < property.components.length; i++) {
  19. var component = property.components[i];
  20. var descriptor = compactable[component.name];
  21. var canOverride = descriptor && descriptor.canOverride || canOverride.sameValue;
  22. var _component = shallowClone(component);
  23. _component.value = [[descriptor.defaultValue]];
  24. if (!canOverride(_component, component, validator))
  25. return true;
  26. }
  27. return false;
  28. }
  29. function isComponentOf(shorthand, longhand) {
  30. return compactable[shorthand.name].components.indexOf(longhand.name) > -1;
  31. }
  32. function overrideIntoMultiplex(property, by) {
  33. by.unused = true;
  34. turnIntoMultiplex(by, multiplexSize(property));
  35. property.value = by.value;
  36. }
  37. function overrideByMultiplex(property, by) {
  38. by.unused = true;
  39. property.multiplex = true;
  40. property.value = by.value;
  41. }
  42. function overrideSimple(property, by) {
  43. by.unused = true;
  44. property.value = by.value;
  45. }
  46. function override(property, by) {
  47. if (by.multiplex)
  48. overrideByMultiplex(property, by);
  49. else if (property.multiplex)
  50. overrideIntoMultiplex(property, by);
  51. else
  52. overrideSimple(property, by);
  53. }
  54. function overrideShorthand(property, by) {
  55. by.unused = true;
  56. for (var i = 0, l = property.components.length; i < l; i++) {
  57. override(property.components[i], by.components[i], property.multiplex);
  58. }
  59. }
  60. function turnIntoMultiplex(property, size) {
  61. property.multiplex = true;
  62. for (var i = 0, l = property.components.length; i < l; i++) {
  63. var component = property.components[i];
  64. if (component.multiplex)
  65. continue;
  66. var value = component.value.slice(0);
  67. for (var j = 1; j < size; j++) {
  68. component.value.push([MULTIPLEX_SEPARATOR]);
  69. Array.prototype.push.apply(component.value, value);
  70. }
  71. }
  72. }
  73. function multiplexSize(component) {
  74. var size = 0;
  75. for (var i = 0, l = component.value.length; i < l; i++) {
  76. if (component.value[i][0] == MULTIPLEX_SEPARATOR)
  77. size++;
  78. }
  79. return size + 1;
  80. }
  81. function lengthOf(property) {
  82. var fakeAsArray = [[property.name]].concat(property.value);
  83. return stringifyProperty([fakeAsArray], 0).length;
  84. }
  85. function moreSameShorthands(properties, startAt, name) {
  86. // Since we run the main loop in `compactOverrides` backwards, at this point some
  87. // properties may not be marked as unused.
  88. // We should consider reverting the order if possible
  89. var count = 0;
  90. for (var i = startAt; i >= 0; i--) {
  91. if (properties[i].name == name && !properties[i].unused)
  92. count++;
  93. if (count > 1)
  94. break;
  95. }
  96. return count > 1;
  97. }
  98. function overridingFunction(shorthand, validator) {
  99. for (var i = 0, l = shorthand.components.length; i < l; i++) {
  100. if (anyValue(validator.isValidFunction, shorthand.components[i]))
  101. return true;
  102. }
  103. return false;
  104. }
  105. function anyValue(fn, property) {
  106. for (var i = 0, l = property.value.length; i < l; i++) {
  107. if (property.value[i][0] == MULTIPLEX_SEPARATOR)
  108. continue;
  109. if (fn(property.value[i][0]))
  110. return true;
  111. }
  112. return false;
  113. }
  114. function wouldResultInLongerValue(left, right) {
  115. if (!left.multiplex && !right.multiplex || left.multiplex && right.multiplex)
  116. return false;
  117. var multiplex = left.multiplex ? left : right;
  118. var simple = left.multiplex ? right : left;
  119. var component;
  120. var multiplexClone = deepClone(multiplex);
  121. restoreFromOptimizing([multiplexClone]);
  122. var simpleClone = deepClone(simple);
  123. restoreFromOptimizing([simpleClone]);
  124. var lengthBefore = lengthOf(multiplexClone) + 1 + lengthOf(simpleClone);
  125. if (left.multiplex) {
  126. component = multiplexClone.components.filter(nameMatchFilter(simpleClone))[0];
  127. overrideIntoMultiplex(component, simpleClone);
  128. } else {
  129. component = simpleClone.components.filter(nameMatchFilter(multiplexClone))[0];
  130. turnIntoMultiplex(simpleClone, multiplexSize(multiplexClone));
  131. overrideByMultiplex(component, multiplexClone);
  132. }
  133. restoreFromOptimizing([simpleClone]);
  134. var lengthAfter = lengthOf(simpleClone);
  135. return lengthBefore < lengthAfter;
  136. }
  137. function isCompactable(property) {
  138. return property.name in compactable;
  139. }
  140. function noneOverrideHack(left, right) {
  141. return !left.multiplex &&
  142. (left.name == 'background' || left.name == 'background-image') &&
  143. right.multiplex &&
  144. (right.name == 'background' || right.name == 'background-image') &&
  145. anyLayerIsNone(right.value);
  146. }
  147. function anyLayerIsNone(values) {
  148. var layers = intoLayers(values);
  149. for (var i = 0, l = layers.length; i < l; i++) {
  150. if (layers[i].length == 1 && layers[i][0][0] == 'none')
  151. return true;
  152. }
  153. return false;
  154. }
  155. function intoLayers(values) {
  156. var layers = [];
  157. for (var i = 0, layer = [], l = values.length; i < l; i++) {
  158. var value = values[i];
  159. if (value[0] == MULTIPLEX_SEPARATOR) {
  160. layers.push(layer);
  161. layer = [];
  162. } else {
  163. layer.push(value);
  164. }
  165. }
  166. layers.push(layer);
  167. return layers;
  168. }
  169. function compactOverrides(properties, compatibility, validator) {
  170. var mayOverride, right, left, component;
  171. var i, j, k;
  172. propertyLoop:
  173. for (i = properties.length - 1; i >= 0; i--) {
  174. right = properties[i];
  175. if (!isCompactable(right))
  176. continue;
  177. if (right.variable)
  178. continue;
  179. mayOverride = compactable[right.name].canOverride || canOverride.sameValue;
  180. for (j = i - 1; j >= 0; j--) {
  181. left = properties[j];
  182. if (!isCompactable(left))
  183. continue;
  184. if (left.variable)
  185. continue;
  186. if (left.unused || right.unused)
  187. continue;
  188. if (left.hack && !right.hack || !left.hack && right.hack)
  189. continue;
  190. if (hasInherit(right))
  191. continue;
  192. if (noneOverrideHack(left, right))
  193. continue;
  194. if (!left.shorthand && right.shorthand && isComponentOf(right, left)) {
  195. // maybe `left` can be overridden by `right` which is a shorthand?
  196. if (!right.important && left.important)
  197. continue;
  198. if (!sameVendorPrefixesIn([left], right.components))
  199. continue;
  200. if (!anyValue(validator.isValidFunction, left) && overridingFunction(right, validator))
  201. continue;
  202. component = right.components.filter(nameMatchFilter(left))[0];
  203. mayOverride = (compactable[left.name] && compactable[left.name].canOverride) || canOverride.sameValue;
  204. if (everyCombination(mayOverride, left, component, validator)) {
  205. left.unused = true;
  206. }
  207. } else if (left.shorthand && !right.shorthand && isComponentOf(left, right)) {
  208. // maybe `right` can be pulled into `left` which is a shorthand?
  209. if (right.important && !left.important)
  210. continue;
  211. if (!right.important && left.important) {
  212. right.unused = true;
  213. continue;
  214. }
  215. // Pending more clever algorithm in #527
  216. if (moreSameShorthands(properties, i - 1, left.name))
  217. continue;
  218. if (overridingFunction(left, validator))
  219. continue;
  220. component = left.components.filter(nameMatchFilter(right))[0];
  221. if (everyCombination(mayOverride, component, right, validator)) {
  222. var disabledBackgroundMerging =
  223. !compatibility.properties.backgroundClipMerging && component.name.indexOf('background-clip') > -1 ||
  224. !compatibility.properties.backgroundOriginMerging && component.name.indexOf('background-origin') > -1 ||
  225. !compatibility.properties.backgroundSizeMerging && component.name.indexOf('background-size') > -1;
  226. var nonMergeableValue = compactable[right.name].nonMergeableValue === right.value[0][0];
  227. if (disabledBackgroundMerging || nonMergeableValue)
  228. continue;
  229. if (!compatibility.properties.merging && wouldBreakCompatibility(left, validator))
  230. continue;
  231. if (component.value[0][0] != right.value[0][0] && (hasInherit(left) || hasInherit(right)))
  232. continue;
  233. if (wouldResultInLongerValue(left, right))
  234. continue;
  235. if (!left.multiplex && right.multiplex)
  236. turnIntoMultiplex(left, multiplexSize(right));
  237. override(component, right);
  238. left.dirty = true;
  239. }
  240. } else if (left.shorthand && right.shorthand && left.name == right.name) {
  241. // merge if all components can be merged
  242. if (!left.multiplex && right.multiplex)
  243. continue;
  244. if (!right.important && left.important) {
  245. right.unused = true;
  246. continue propertyLoop;
  247. }
  248. if (right.important && !left.important) {
  249. left.unused = true;
  250. continue;
  251. }
  252. for (k = left.components.length - 1; k >= 0; k--) {
  253. var leftComponent = left.components[k];
  254. var rightComponent = right.components[k];
  255. mayOverride = compactable[leftComponent.name].canOverride || canOverride.sameValue;
  256. if (!everyCombination(mayOverride, leftComponent, rightComponent, validator))
  257. continue propertyLoop;
  258. if (!everyCombination(canOverride.twoOptionalFunctions, leftComponent, rightComponent, validator) && validator.isValidFunction(rightComponent))
  259. continue propertyLoop;
  260. }
  261. overrideShorthand(left, right);
  262. left.dirty = true;
  263. } else if (left.shorthand && right.shorthand && isComponentOf(left, right)) {
  264. // border is a shorthand but any of its components is a shorthand too
  265. if (!left.important && right.important)
  266. continue;
  267. component = left.components.filter(nameMatchFilter(right))[0];
  268. mayOverride = compactable[right.name].canOverride || canOverride.sameValue;
  269. if (!everyCombination(mayOverride, component, right, validator))
  270. continue;
  271. if (left.important && !right.important) {
  272. right.unused = true;
  273. continue;
  274. }
  275. var rightRestored = compactable[right.name].restore(right, compactable);
  276. if (rightRestored.length > 1)
  277. continue;
  278. component = left.components.filter(nameMatchFilter(right))[0];
  279. override(component, right);
  280. right.dirty = true;
  281. } else if (left.name == right.name) {
  282. // two non-shorthands should be merged based on understandability
  283. if (left.important && !right.important) {
  284. right.unused = true;
  285. continue;
  286. }
  287. mayOverride = compactable[right.name].canOverride || canOverride.sameValue;
  288. if (!everyCombination(mayOverride, left, right, validator))
  289. continue;
  290. left.unused = true;
  291. }
  292. }
  293. }
  294. }
  295. module.exports = compactOverrides;