1 var hasInherit = require('./has-inherit');
2 var everyValuesPair = require('./every-values-pair');
3 var findComponentIn = require('./find-component-in');
4 var isComponentOf = require('./is-component-of');
5 var isMergeableShorthand = require('./is-mergeable-shorthand');
6 var overridesNonComponentShorthand = require('./overrides-non-component-shorthand');
7 var sameVendorPrefixesIn = require('./vendor-prefixes').same;
9 var compactable = require('../compactable');
10 var deepClone = require('../clone').deep;
11 var deepClone = require('../clone').deep;
12 var restoreWithComponents = require('../restore-with-components');
13 var shallowClone = require('../clone').shallow;
15 var restoreFromOptimizing = require('../../restore-from-optimizing');
17 var Token = require('../../../tokenizer/token');
18 var Marker = require('../../../tokenizer/marker');
20 var serializeProperty = require('../../../writer/one-time').property;
22 function wouldBreakCompatibility(property, validator) {
23 for (var i = 0; i < property.components.length; i++) {
24 var component = property.components[i];
25 var descriptor = compactable[component.name];
26 var canOverride = descriptor && descriptor.canOverride || canOverride.sameValue;
28 var _component = shallowClone(component);
29 _component.value = [[Token.PROPERTY_VALUE, descriptor.defaultValue]];
31 if (!everyValuesPair(canOverride.bind(null, validator), _component, component)) {
39 function overrideIntoMultiplex(property, by) {
42 turnIntoMultiplex(by, multiplexSize(property));
43 property.value = by.value;
46 function overrideByMultiplex(property, by) {
48 property.multiplex = true;
49 property.value = by.value;
52 function overrideSimple(property, by) {
54 property.value = by.value;
57 function override(property, by) {
59 overrideByMultiplex(property, by);
60 else if (property.multiplex)
61 overrideIntoMultiplex(property, by);
63 overrideSimple(property, by);
66 function overrideShorthand(property, by) {
69 for (var i = 0, l = property.components.length; i < l; i++) {
70 override(property.components[i], by.components[i], property.multiplex);
74 function turnIntoMultiplex(property, size) {
75 property.multiplex = true;
77 if (compactable[property.name].shorthand) {
78 turnShorthandValueIntoMultiplex(property, size);
80 turnLonghandValueIntoMultiplex(property, size);
84 function turnShorthandValueIntoMultiplex(property, size) {
88 for (i = 0, l = property.components.length; i < l; i++) {
89 component = property.components[i];
91 if (!component.multiplex) {
92 turnLonghandValueIntoMultiplex(component, size);
97 function turnLonghandValueIntoMultiplex(property, size) {
98 var withRealValue = compactable[property.name].intoMultiplexMode == 'real';
99 var withValue = withRealValue ?
100 property.value.slice(0) :
101 compactable[property.name].defaultValue;
102 var i = multiplexSize(property);
104 var m = withValue.length;
106 for (; i < size; i++) {
107 property.value.push([Token.PROPERTY_VALUE, Marker.COMMA]);
109 if (Array.isArray(withValue)) {
110 for (j = 0; j < m; j++) {
111 property.value.push(withRealValue ? withValue[j] : [Token.PROPERTY_VALUE, withValue[j]]);
114 property.value.push(withRealValue ? withValue : [Token.PROPERTY_VALUE, withValue]);
119 function multiplexSize(component) {
122 for (var i = 0, l = component.value.length; i < l; i++) {
123 if (component.value[i][1] == Marker.COMMA)
130 function lengthOf(property) {
133 [Token.PROPERTY_NAME, property.name]
134 ].concat(property.value);
135 return serializeProperty([fakeAsArray], 0).length;
138 function moreSameShorthands(properties, startAt, name) {
139 // Since we run the main loop in `compactOverrides` backwards, at this point some
140 // properties may not be marked as unused.
141 // We should consider reverting the order if possible
144 for (var i = startAt; i >= 0; i--) {
145 if (properties[i].name == name && !properties[i].unused)
154 function overridingFunction(shorthand, validator) {
155 for (var i = 0, l = shorthand.components.length; i < l; i++) {
156 if (!anyValue(validator.isUrl, shorthand.components[i]) && anyValue(validator.isFunction, shorthand.components[i])) {
164 function anyValue(fn, property) {
165 for (var i = 0, l = property.value.length; i < l; i++) {
166 if (property.value[i][1] == Marker.COMMA)
169 if (fn(property.value[i][1]))
176 function wouldResultInLongerValue(left, right) {
177 if (!left.multiplex && !right.multiplex || left.multiplex && right.multiplex)
180 var multiplex = left.multiplex ? left : right;
181 var simple = left.multiplex ? right : left;
184 var multiplexClone = deepClone(multiplex);
185 restoreFromOptimizing([multiplexClone], restoreWithComponents);
187 var simpleClone = deepClone(simple);
188 restoreFromOptimizing([simpleClone], restoreWithComponents);
190 var lengthBefore = lengthOf(multiplexClone) + 1 + lengthOf(simpleClone);
192 if (left.multiplex) {
193 component = findComponentIn(multiplexClone, simpleClone);
194 overrideIntoMultiplex(component, simpleClone);
196 component = findComponentIn(simpleClone, multiplexClone);
197 turnIntoMultiplex(simpleClone, multiplexSize(multiplexClone));
198 overrideByMultiplex(component, multiplexClone);
201 restoreFromOptimizing([simpleClone], restoreWithComponents);
203 var lengthAfter = lengthOf(simpleClone);
205 return lengthBefore <= lengthAfter;
208 function isCompactable(property) {
209 return property.name in compactable;
212 function noneOverrideHack(left, right) {
213 return !left.multiplex &&
214 (left.name == 'background' || left.name == 'background-image') &&
216 (right.name == 'background' || right.name == 'background-image') &&
217 anyLayerIsNone(right.value);
220 function anyLayerIsNone(values) {
221 var layers = intoLayers(values);
223 for (var i = 0, l = layers.length; i < l; i++) {
224 if (layers[i].length == 1 && layers[i][0][1] == 'none')
231 function intoLayers(values) {
234 for (var i = 0, layer = [], l = values.length; i < l; i++) {
235 var value = values[i];
236 if (value[1] == Marker.COMMA) {
248 function overrideProperties(properties, withMerging, compatibility, validator) {
249 var mayOverride, right, left, component;
250 var overriddenComponents;
251 var overriddenComponent;
252 var overridingComponent;
257 for (i = properties.length - 1; i >= 0; i--) {
258 right = properties[i];
260 if (!isCompactable(right))
266 mayOverride = compactable[right.name].canOverride;
269 for (j = i - 1; j >= 0; j--) {
270 left = properties[j];
272 if (!isCompactable(left))
278 if (left.unused || right.unused)
281 if (left.hack && !right.hack && !right.important || !left.hack && !left.important && right.hack)
284 if (left.important == right.important && left.hack[0] != right.hack[0])
287 if (left.important == right.important && (left.hack[0] != right.hack[0] || (left.hack[1] && left.hack[1] != right.hack[1])))
290 if (hasInherit(right))
293 if (noneOverrideHack(left, right))
296 if (right.shorthand && isComponentOf(right, left)) {
297 // maybe `left` can be overridden by `right` which is a shorthand?
298 if (!right.important && left.important)
301 if (!sameVendorPrefixesIn([left], right.components))
304 if (!anyValue(validator.isFunction, left) && overridingFunction(right, validator))
307 if (!isMergeableShorthand(right)) {
312 component = findComponentIn(right, left);
313 mayOverride = compactable[left.name].canOverride;
314 if (everyValuesPair(mayOverride.bind(null, validator), left, component)) {
317 } else if (right.shorthand && overridesNonComponentShorthand(right, left)) {
318 // `right` is a shorthand while `left` can be overriden by it, think `border` and `border-top`
319 if (!right.important && left.important) {
323 if (!sameVendorPrefixesIn([left], right.components)) {
327 if (!anyValue(validator.isFunction, left) && overridingFunction(right, validator)) {
331 overriddenComponents = left.shorthand ?
335 for (k = overriddenComponents.length - 1; k >= 0; k--) {
336 overriddenComponent = overriddenComponents[k];
337 overridingComponent = findComponentIn(right, overriddenComponent);
338 mayOverride = compactable[overriddenComponent.name].canOverride;
340 if (!everyValuesPair(mayOverride.bind(null, validator), left, overridingComponent)) {
341 continue traverseLoop;
346 } else if (withMerging && left.shorthand && !right.shorthand && isComponentOf(left, right, true)) {
347 // maybe `right` can be pulled into `left` which is a shorthand?
348 if (right.important && !left.important)
351 if (!right.important && left.important) {
356 // Pending more clever algorithm in #527
357 if (moreSameShorthands(properties, i - 1, left.name))
360 if (overridingFunction(left, validator))
363 if (!isMergeableShorthand(left))
366 component = findComponentIn(left, right);
367 if (everyValuesPair(mayOverride.bind(null, validator), component, right)) {
368 var disabledBackgroundMerging =
369 !compatibility.properties.backgroundClipMerging && component.name.indexOf('background-clip') > -1 ||
370 !compatibility.properties.backgroundOriginMerging && component.name.indexOf('background-origin') > -1 ||
371 !compatibility.properties.backgroundSizeMerging && component.name.indexOf('background-size') > -1;
372 var nonMergeableValue = compactable[right.name].nonMergeableValue === right.value[0][1];
374 if (disabledBackgroundMerging || nonMergeableValue)
377 if (!compatibility.properties.merging && wouldBreakCompatibility(left, validator))
380 if (component.value[0][1] != right.value[0][1] && (hasInherit(left) || hasInherit(right)))
383 if (wouldResultInLongerValue(left, right))
386 if (!left.multiplex && right.multiplex)
387 turnIntoMultiplex(left, multiplexSize(right));
389 override(component, right);
392 } else if (withMerging && left.shorthand && right.shorthand && left.name == right.name) {
393 // merge if all components can be merged
395 if (!left.multiplex && right.multiplex)
398 if (!right.important && left.important) {
400 continue propertyLoop;
403 if (right.important && !left.important) {
408 if (!isMergeableShorthand(right)) {
413 for (k = left.components.length - 1; k >= 0; k--) {
414 var leftComponent = left.components[k];
415 var rightComponent = right.components[k];
417 mayOverride = compactable[leftComponent.name].canOverride;
418 if (!everyValuesPair(mayOverride.bind(null, validator), leftComponent, rightComponent))
419 continue propertyLoop;
422 overrideShorthand(left, right);
424 } else if (withMerging && left.shorthand && right.shorthand && isComponentOf(left, right)) {
425 // border is a shorthand but any of its components is a shorthand too
427 if (!left.important && right.important)
430 component = findComponentIn(left, right);
431 mayOverride = compactable[right.name].canOverride;
432 if (!everyValuesPair(mayOverride.bind(null, validator), component, right))
435 if (left.important && !right.important) {
440 var rightRestored = compactable[right.name].restore(right, compactable);
441 if (rightRestored.length > 1)
444 component = findComponentIn(left, right);
445 override(component, right);
447 } else if (left.name == right.name) {
448 // two non-shorthands should be merged based on understandability
451 if (right.shorthand) {
452 for (k = right.components.length - 1; k >= 0 && overridable; k--) {
453 overriddenComponent = left.components[k];
454 overridingComponent = right.components[k];
455 mayOverride = compactable[overridingComponent.name].canOverride;
457 overridable = overridable && everyValuesPair(mayOverride.bind(null, validator), overriddenComponent, overridingComponent);
460 mayOverride = compactable[right.name].canOverride;
461 overridable = everyValuesPair(mayOverride.bind(null, validator), left, right);
464 if (left.important && !right.important && overridable) {
469 if (!left.important && right.important && overridable) {
484 module.exports = overrideProperties;