• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(scope, testing) {
16
17  function numberToString(x) {
18    return x.toFixed(3).replace(/0+$/, '').replace(/\.$/, '');
19  }
20
21  function clamp(min, max, x) {
22    return Math.min(max, Math.max(min, x));
23  }
24
25  function parseNumber(string) {
26    if (/^\s*[-+]?(\d*\.)?\d+\s*$/.test(string))
27      return Number(string);
28  }
29
30  function mergeNumbers(left, right) {
31    return [left, right, numberToString];
32  }
33
34  // FIXME: This should probably go in it's own handler.
35  function mergeFlex(left, right) {
36    if (left == 0)
37      return;
38    return clampedMergeNumbers(0, Infinity)(left, right);
39  }
40
41  function mergePositiveIntegers(left, right) {
42    return [left, right, function(x) {
43      return Math.round(clamp(1, Infinity, x));
44    }];
45  }
46
47  function clampedMergeNumbers(min, max) {
48    return function(left, right) {
49      return [left, right, function(x) {
50        return numberToString(clamp(min, max, x));
51      }];
52    };
53  }
54
55  function parseNumberList(string) {
56    var items = string.trim().split(/\s*[\s,]\s*/);
57    if (items.length === 0) {
58      return;
59    }
60    var result = [];
61    for (var i = 0; i < items.length; i++) {
62      var number = parseNumber(items[i]);
63      if (number === undefined) {
64        return;
65      }
66      result.push(number);
67    }
68    return result;
69  }
70
71  function mergeNumberLists(left, right) {
72    if (left.length != right.length) {
73      return;
74    }
75    return [left, right, function(numberList) {
76      return numberList.map(numberToString).join(' ');
77    }];
78  }
79
80  function round(left, right) {
81    return [left, right, Math.round];
82  }
83
84  scope.clamp = clamp;
85  scope.addPropertiesHandler(parseNumberList, mergeNumberLists, ['stroke-dasharray']);
86  scope.addPropertiesHandler(parseNumber, clampedMergeNumbers(0, Infinity), ['border-image-width', 'line-height']);
87  scope.addPropertiesHandler(parseNumber, clampedMergeNumbers(0, 1), ['opacity', 'shape-image-threshold']);
88  scope.addPropertiesHandler(parseNumber, mergeFlex, ['flex-grow', 'flex-shrink']);
89  scope.addPropertiesHandler(parseNumber, mergePositiveIntegers, ['orphans', 'widows']);
90  scope.addPropertiesHandler(parseNumber, round, ['z-index']);
91
92  scope.parseNumber = parseNumber;
93  scope.parseNumberList = parseNumberList;
94  scope.mergeNumbers = mergeNumbers;
95  scope.numberToString = numberToString;
96
97})(webAnimations1, webAnimationsTesting);
98