Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: Source/core/css/parser/SizesCalcParser.cpp

Issue 252743004: A thread safe CSS calc parser for sizes (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Revert last patch, will include it in a separate commit Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/css/parser/SizesCalcParser.h"
7
8 #include "core/css/MediaValues.h"
9 #include "core/css/parser/MediaQueryToken.h"
10
11 namespace WebCore {
12
13 bool SizesCalcParser::parse(MediaQueryTokenIterator start,
14 MediaQueryTokenIterator end,
15 PassRefPtr<MediaValues> mediaValues,
16 unsigned& result)
17 {
18 SizesCalcParser parser(mediaValues);
19 if (!parser.calcToReversePolishNotation(start, end))
20 return false;
21 return parser.calculate(result);
22 }
23
24 static bool operatorPriority(UChar cc, bool& highPriority)
25 {
26 if (cc == '+' || cc == '-')
27 highPriority = false;
28 else if (cc == '*' || cc == '/')
29 highPriority = true;
30 else
31 return false;
32 return true;
33 }
34
35 bool SizesCalcParser::handleOperator(Vector<MediaQueryToken>& stack, const Media QueryToken& token)
36 {
37 // If the token is an operator, o1, then:
38 // while there is an operator token, o2, at the top of the stack, and
39 // either o1 is left-associative and its precedence is equal to that of o2,
40 // or o1 has precedence less than that of o2,
41 // pop o2 off the stack, onto the output queue;
42 // push o1 onto the stack.
43 bool stackOperatorPriority;
44 bool incomingOperatorPriority;
45
46 if (!operatorPriority(token.delimiter(), incomingOperatorPriority))
47 return false;
48 if (!stack.isEmpty() && stack.last().type() == DelimiterToken) {
49 if (!operatorPriority(stack.last().delimiter(), stackOperatorPriority))
50 return false;
51 if (!incomingOperatorPriority || stackOperatorPriority) {
52 appendOperator(stack.last());
53 stack.removeLast();
54 }
55 }
56 stack.append(token);
57 return true;
58 }
59
60 void SizesCalcParser::appendNumber(const MediaQueryToken& token)
61 {
62 SizesCalcValue value;
63 value.value = token.numericValue();
64 m_valueList.append(value);
65 }
66
67 bool SizesCalcParser::appendLength(const MediaQueryToken& token)
68 {
69 SizesCalcValue value;
70 int result = 0;
71 if (!m_mediaValues->computeLength(token.numericValue(), token.unitType(), re sult))
72 return false;
73 value.value = result;
74 value.isLength = true;
75 m_valueList.append(value);
76 return true;
77 }
78
79 void SizesCalcParser::appendOperator(const MediaQueryToken& token)
80 {
81 SizesCalcValue value;
82 value.operation = token.delimiter();
83 m_valueList.append(value);
84 }
85
86 bool SizesCalcParser::calcToReversePolishNotation(MediaQueryTokenIterator start, MediaQueryTokenIterator end)
87 {
88 // This method implements the shunting yard algorithm, to turn the calc synt ax into a reverse polish notation.
89 // http://en.wikipedia.org/wiki/Shunting-yard_algorithm
90
91 Vector<MediaQueryToken> stack;
92 for (MediaQueryTokenIterator it = start; it != end; ++it) {
93 MediaQueryTokenType type = it->type();
94 switch (type) {
95 case NumberToken:
96 appendNumber(*it);
97 break;
98 case DimensionToken:
99 if (!CSSPrimitiveValue::isLength(it->unitType()) || !appendLength(*i t))
100 return false;
101 break;
102 case DelimiterToken:
103 if (!handleOperator(stack, *it))
104 return false;
105 break;
106 case FunctionToken:
107 if (it->value() != "calc")
108 return false;
109 // "calc(" is the same as "("
110 case LeftParenthesisToken:
111 // If the token is a left parenthesis, then push it onto the stack.
112 stack.append(*it);
113 break;
114 case RightParenthesisToken:
115 // If the token is a right parenthesis:
116 // Until the token at the top of the stack is a left parenthesis, po p operators off the stack onto the output queue.
117 while (!stack.isEmpty() && stack.last().type() != LeftParenthesisTok en && stack.last().type() != FunctionToken) {
118 appendOperator(stack.last());
119 stack.removeLast();
120 }
121 // If the stack runs out without finding a left parenthesis, then th ere are mismatched parentheses.
122 if (stack.isEmpty())
123 return false;
124 // Pop the left parenthesis from the stack, but not onto the output queue.
125 stack.removeLast();
126 break;
127 case CommentToken:
128 case WhitespaceToken:
129 case EOFToken:
130 break;
131 case PercentageToken:
132 case IdentToken:
133 case CommaToken:
134 case ColonToken:
135 case SemicolonToken:
136 case LeftBraceToken:
137 case LeftBracketToken:
138 case RightBraceToken:
139 case RightBracketToken:
140 case StringToken:
141 case BadStringToken:
142 return false;
143 }
144 }
145
146 // When there are no more tokens to read:
147 // While there are still operator tokens in the stack:
148 while (!stack.isEmpty()) {
149 // If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses.
150 MediaQueryTokenType type = stack.last().type();
151 if (type == LeftParenthesisToken || type == FunctionToken)
152 return false;
153 // Pop the operator onto the output queue.
154 appendOperator(stack.last());
155 stack.removeLast();
156 }
157 return true;
158 }
159
160 static bool operateOnStack(Vector<SizesCalcValue>& stack, UChar operation)
161 {
162 if (stack.size() < 2)
163 return false;
164 SizesCalcValue rightOperand = stack.last();
165 stack.removeLast();
166 SizesCalcValue leftOperand = stack.last();
167 stack.removeLast();
168 switch (operation) {
169 case '+':
170 if (!rightOperand.isLength || !leftOperand.isLength)
171 return false;
172 stack.append(SizesCalcValue(leftOperand.value + rightOperand.value));
173 break;
174 case '-':
175 if (!rightOperand.isLength || !leftOperand.isLength)
176 return false;
177 stack.append(SizesCalcValue(leftOperand.value - rightOperand.value));
178 break;
179 case '*':
180 if (rightOperand.isLength && leftOperand.isLength)
181 return false;
182 stack.append(SizesCalcValue(leftOperand.value * rightOperand.value));
183 break;
184 case '/':
185 if (!leftOperand.isLength || rightOperand.isLength || rightOperand.value == 0)
186 return false;
187 stack.append(SizesCalcValue(leftOperand.value / rightOperand.value));
188 break;
189 default:
190 return false;
191 }
192 return true;
193 }
194
195 bool SizesCalcParser::calculate(unsigned& result)
196 {
197 Vector<SizesCalcValue> stack;
198 for (Vector<SizesCalcValue>::iterator it = m_valueList.begin(); it != m_valu eList.end(); ++it) {
199 if (it->operation == 0) {
200 stack.append(*it);
201 } else {
202 if (!operateOnStack(stack, it->operation))
203 return false;
204 }
205 }
206 if (stack.size() == 1) {
207 result = clampTo<unsigned>(stack.last().value);
208 return true;
209 }
210 return false;
211 }
212
213 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698