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 negateDimension(dimension) {
19 for (var k in dimension) {
20 result[k] = -dimension[k];
25 function consumeOffset(string) {
26 return scope.consumeToken(/^(left|center|right|top|bottom)\b/i, string) || scope.consumeLengthOrPercent(string);
37 function parseOrigin(slots, string) {
38 var result = scope.consumeRepeated(consumeOffset, /^/, string);
39 if (!result || result[1] != '') return;
40 var tokens = result[0];
41 tokens[0] = tokens[0] || 'center';
42 tokens[1] = tokens[1] || 'center';
44 tokens[2] = tokens[2] || {px: 0};
46 if (tokens.length != slots) {
49 // Reorder so that the horizontal axis comes first.
50 if (/top|bottom/.test(tokens[0]) || /left|right/.test(tokens[1])) {
52 tokens[0] = tokens[1];
55 // Invalid if not horizontal then vertical.
56 if (!/left|right|center|Object/.test(tokens[0]))
58 if (!/top|bottom|center|Object/.test(tokens[1]))
60 return tokens.map(function(position) {
61 return typeof position == 'object' ? position : offsetMap[position];
65 var mergeOffsetList = scope.mergeNestedRepeated.bind(null, scope.mergeDimensions, ' ');
66 scope.addPropertiesHandler(parseOrigin.bind(null, 3), mergeOffsetList, ['transform-origin']);
67 scope.addPropertiesHandler(parseOrigin.bind(null, 2), mergeOffsetList, ['perspective-origin']);
69 function consumePosition(string) {
70 var result = scope.consumeRepeated(consumeOffset, /^/, string);
75 var tokens = result[0];
76 var out = [{'%': 50}, {'%': 50}];
78 var bottomOrRight = false;
80 for (var i = 0; i < tokens.length; i++) {
81 var token = tokens[i];
82 if (typeof token == 'string') {
83 bottomOrRight = /bottom|right/.test(token);
84 pos = {left: 0, right: 0, center: pos, top: 1, bottom: 1}[token];
85 out[pos] = offsetMap[token];
86 if (token == 'center') {
87 // Center doesn't accept a length offset.
92 // If bottom or right we need to subtract the length from 100%
93 token = negateDimension(token);
94 token['%'] = (token['%'] || 0) + 100;
98 bottomOrRight = false;
101 return [out, result[1]];
104 function parsePositionList(string) {
105 var result = scope.consumeRepeated(consumePosition, /^,/, string);
106 if (result && result[1] == '') {
111 scope.consumePosition = consumePosition;
112 scope.mergeOffsetList = mergeOffsetList;
114 var mergePositionList = scope.mergeNestedRepeated.bind(null, mergeOffsetList, ', ');
115 scope.addPropertiesHandler(parsePositionList, mergePositionList, ['background-position', 'object-position']);