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.
15 (function(scope, testing) {
17 function numberToString(x) {
18 return x.toFixed(3).replace('.000', '');
21 function clamp(min, max, x) {
22 return Math.min(max, Math.max(min, x));
25 function parseNumber(string) {
26 if (/^\s*[-+]?(\d*\.)?\d+\s*$/.test(string))
27 return Number(string);
30 function mergeNumbers(left, right) {
31 return [left, right, numberToString];
34 // FIXME: This should probably go in it's own handler.
35 function mergeFlex(left, right) {
38 return clampedMergeNumbers(0, Infinity)(left, right);
41 function mergePositiveIntegers(left, right) {
42 return [left, right, function(x) {
43 return Math.round(clamp(1, Infinity, x));
47 function clampedMergeNumbers(min, max) {
48 return function(left, right) {
49 return [left, right, function(x) {
50 return numberToString(clamp(min, max, x));
55 function round(left, right) {
56 return [left, right, Math.round];
60 scope.addPropertiesHandler(parseNumber, clampedMergeNumbers(0, Infinity), ['border-image-width', 'line-height']);
61 scope.addPropertiesHandler(parseNumber, clampedMergeNumbers(0, 1), ['opacity', 'shape-image-threshold']);
62 scope.addPropertiesHandler(parseNumber, clampedMergeNumbers(0.01, Infinity), ['zoom']);
63 scope.addPropertiesHandler(parseNumber, mergeFlex, ['flex-grow', 'flex-shrink']);
64 scope.addPropertiesHandler(parseNumber, mergeNumbers, ['zoom']);
65 scope.addPropertiesHandler(parseNumber, mergePositiveIntegers, ['orphans', 'widows']);
66 scope.addPropertiesHandler(parseNumber, round, ['z-index']);
68 scope.parseNumber = parseNumber;
69 scope.mergeNumbers = mergeNumbers;
70 scope.numberToString = numberToString;
72 })(webAnimations1, webAnimationsTesting);