1// Copyright 2014 Google Inc. All rights reserved.
2//
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
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
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.
14
15(function(shared, scope, testing) {
16
17  var propertyHandlers = {};
18
19  function toCamelCase(property) {
20    return property.replace(/-(.)/g, function(_, c) {
21      return c.toUpperCase();
22    });
23  }
24
25  function addPropertyHandler(parser, merger, property) {
26    propertyHandlers[property] = propertyHandlers[property] || [];
27    propertyHandlers[property].push([parser, merger]);
28  }
29  function addPropertiesHandler(parser, merger, properties) {
30    for (var i = 0; i < properties.length; i++) {
31      var property = properties[i];
32      WEB_ANIMATIONS_TESTING && console.assert(property.toLowerCase() === property);
33      addPropertyHandler(parser, merger, toCamelCase(property));
34    }
35  }
36  scope.addPropertiesHandler = addPropertiesHandler;
37
38  var initialValues = {
39    backgroundColor: 'transparent',
40    backgroundPosition: '0% 0%',
41    borderBottomColor: 'currentColor',
42    borderBottomLeftRadius: '0px',
43    borderBottomRightRadius: '0px',
44    borderBottomWidth: '3px',
45    borderLeftColor: 'currentColor',
46    borderLeftWidth: '3px',
47    borderRightColor: 'currentColor',
48    borderRightWidth: '3px',
49    // Spec says this should be 0 but in practise it is 2px.
50    borderSpacing: '2px',
51    borderTopColor: 'currentColor',
52    borderTopLeftRadius: '0px',
53    borderTopRightRadius: '0px',
54    borderTopWidth: '3px',
55    bottom: 'auto',
56    clip: 'rect(0px, 0px, 0px, 0px)',
57    color: 'black', // Depends on user agent.
58    fontSize: '100%',
59    fontWeight: '400',
60    height: 'auto',
61    left: 'auto',
62    letterSpacing: 'normal',
63    lineHeight: '120%',
64    marginBottom: '0px',
65    marginLeft: '0px',
66    marginRight: '0px',
67    marginTop: '0px',
68    maxHeight: 'none',
69    maxWidth: 'none',
70    minHeight: '0px',
71    minWidth: '0px',
72    opacity: '1.0',
73    outlineColor: 'invert',
74    outlineOffset: '0px',
75    outlineWidth: '3px',
76    paddingBottom: '0px',
77    paddingLeft: '0px',
78    paddingRight: '0px',
79    paddingTop: '0px',
80    right: 'auto',
81    strokeDasharray: 'none',
82    strokeDashoffset: '0px',
83    textIndent: '0px',
84    textShadow: '0px 0px 0px transparent',
85    top: 'auto',
86    transform: '',
87    verticalAlign: '0px',
88    visibility: 'visible',
89    width: 'auto',
90    wordSpacing: 'normal',
91    zIndex: 'auto'
92  };
93
94  function propertyInterpolation(property, left, right) {
95    var ucProperty = property;
96    if (/-/.test(property) && !shared.isDeprecated('Hyphenated property names', '2016-03-22', 'Use camelCase instead.', true)) {
97      ucProperty = toCamelCase(property);
98    }
99    if (left == 'initial' || right == 'initial') {
100      if (left == 'initial')
101        left = initialValues[ucProperty];
102      if (right == 'initial')
103        right = initialValues[ucProperty];
104    }
105    var handlers = left == right ? [] : propertyHandlers[ucProperty];
106    for (var i = 0; handlers && i < handlers.length; i++) {
107      var parsedLeft = handlers[i][0](left);
108      var parsedRight = handlers[i][0](right);
109      if (parsedLeft !== undefined && parsedRight !== undefined) {
110        var interpolationArgs = handlers[i][1](parsedLeft, parsedRight);
111        if (interpolationArgs) {
112          var interp = scope.Interpolation.apply(null, interpolationArgs);
113          return function(t) {
114            if (t == 0) return left;
115            if (t == 1) return right;
116            return interp(t);
117          };
118        }
119      }
120    }
121    return scope.Interpolation(false, true, function(bool) {
122      return bool ? right : left;
123    });
124  }
125  scope.propertyInterpolation = propertyInterpolation;
126
127})(webAnimationsShared, webAnimations1, webAnimationsTesting);
128