1 // Copyright 2014 Google Inc. All rights reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
17 function consumeShadow(string) {
23 function consumePart(string) {
24 var result = scope.consumeToken(/^inset/i, string);
29 var result = scope.consumeLengthOrPercent(string);
31 shadow.lengths.push(result[0]);
34 var result = scope.consumeColor(string);
36 shadow.color = result[0];
40 var result = scope.consumeRepeated(consumePart, /^/, string);
41 if (result && result[0].length) {
42 return [shadow, result[1]];
46 function parseShadowList(string) {
47 var result = scope.consumeRepeated(consumeShadow, /^,/, string);
48 if (result && result[1] == '') {
53 function mergeShadow(left, right) {
54 while (left.lengths.length < Math.max(left.lengths.length, right.lengths.length))
55 left.lengths.push({px: 0});
56 while (right.lengths.length < Math.max(left.lengths.length, right.lengths.length))
57 right.lengths.push({px: 0});
59 if (left.inset != right.inset || !!left.color != !!right.color) {
62 var lengthReconstitution = [];
63 var colorReconstitution;
64 var matchingLeft = [[], 0];
65 var matchingRight = [[], 0];
66 for (var i = 0; i < left.lengths.length; i++) {
67 var mergedDimensions = scope.mergeDimensions(left.lengths[i], right.lengths[i], i == 2);
68 matchingLeft[0].push(mergedDimensions[0]);
69 matchingRight[0].push(mergedDimensions[1]);
70 lengthReconstitution.push(mergedDimensions[2]);
72 if (left.color && right.color) {
73 var mergedColor = scope.mergeColors(left.color, right.color);
74 matchingLeft[1] = mergedColor[0];
75 matchingRight[1] = mergedColor[1];
76 colorReconstitution = mergedColor[2];
78 return [matchingLeft, matchingRight, function(value) {
79 var result = left.inset ? 'inset ' : ' ';
80 for (var i = 0; i < lengthReconstitution.length; i++) {
81 result += lengthReconstitution[i](value[0][i]) + ' ';
83 if (colorReconstitution) {
84 result += colorReconstitution(value[1]);
90 function mergeNestedRepeatedShadow(nestedMerge, separator, left, right) {
93 function defaultShadow(inset) {
94 return {inset: inset, color: [0, 0, 0, 0], lengths: [{px: 0}, {px: 0}, {px: 0}, {px: 0}]};
96 for (var i = 0; i < left.length || i < right.length; i++) {
97 var l = left[i] || defaultShadow(right[i].inset);
98 var r = right[i] || defaultShadow(left[i].inset);
102 return scope.mergeNestedRepeated(nestedMerge, separator, leftCopy, rightCopy);
105 var mergeShadowList = mergeNestedRepeatedShadow.bind(null, mergeShadow, ', ');
106 scope.addPropertiesHandler(parseShadowList, mergeShadowList, ['box-shadow', 'text-shadow']);