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

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

Issue 369423002: Have srcset respond to viewport changes (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Self review nits Created 6 years, 5 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/css/parser/SizesCalcParser.h" 6 #include "core/css/parser/SizesCalcParser.h"
7 7
8 #include "core/css/MediaValues.h" 8 #include "core/css/MediaValues.h"
9 #include "core/css/parser/MediaQueryToken.h" 9 #include "core/css/parser/MediaQueryToken.h"
10 10
11 namespace blink { 11 namespace blink {
12 12
13 bool SizesCalcParser::parse(MediaQueryTokenIterator start, MediaQueryTokenIterat or end, PassRefPtr<MediaValues> mediaValues, unsigned& result) 13 SizesCalcParser::SizesCalcParser(MediaQueryTokenIterator start, MediaQueryTokenI terator end, PassRefPtr<MediaValues> mediaValues)
14 : m_mediaValues(mediaValues)
15 , m_viewportDependant(false)
16 , m_result(0)
14 { 17 {
15 SizesCalcParser parser(mediaValues); 18 m_isValid = calcToReversePolishNotation(start, end);
16 if (!parser.calcToReversePolishNotation(start, end)) 19 if (m_isValid)
17 return false; 20 m_isValid = calculate();
18 return parser.calculate(result); 21 }
22
23 unsigned SizesCalcParser::result()
24 {
25 ASSERT(m_isValid);
26 return m_result;
19 } 27 }
20 28
21 static bool operatorPriority(UChar cc, bool& highPriority) 29 static bool operatorPriority(UChar cc, bool& highPriority)
22 { 30 {
23 if (cc == '+' || cc == '-') 31 if (cc == '+' || cc == '-')
24 highPriority = false; 32 highPriority = false;
25 else if (cc == '*' || cc == '/') 33 else if (cc == '*' || cc == '/')
26 highPriority = true; 34 highPriority = true;
27 else 35 else
28 return false; 36 return false;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 // http://en.wikipedia.org/wiki/Shunting-yard_algorithm 94 // http://en.wikipedia.org/wiki/Shunting-yard_algorithm
87 95
88 Vector<MediaQueryToken> stack; 96 Vector<MediaQueryToken> stack;
89 for (MediaQueryTokenIterator it = start; it != end; ++it) { 97 for (MediaQueryTokenIterator it = start; it != end; ++it) {
90 MediaQueryTokenType type = it->type(); 98 MediaQueryTokenType type = it->type();
91 switch (type) { 99 switch (type) {
92 case NumberToken: 100 case NumberToken:
93 appendNumber(*it); 101 appendNumber(*it);
94 break; 102 break;
95 case DimensionToken: 103 case DimensionToken:
104 m_viewportDependant = m_viewportDependant || CSSPrimitiveValue::isVi ewportPercentageLength(it->unitType());
96 if (!CSSPrimitiveValue::isLength(it->unitType()) || !appendLength(*i t)) 105 if (!CSSPrimitiveValue::isLength(it->unitType()) || !appendLength(*i t))
97 return false; 106 return false;
98 break; 107 break;
99 case DelimiterToken: 108 case DelimiterToken:
100 if (!handleOperator(stack, *it)) 109 if (!handleOperator(stack, *it))
101 return false; 110 return false;
102 break; 111 break;
103 case FunctionToken: 112 case FunctionToken:
104 if (it->value() != "calc") 113 if (it->value() != "calc")
105 return false; 114 return false;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 if (rightOperand.isLength || rightOperand.value == 0) 195 if (rightOperand.isLength || rightOperand.value == 0)
187 return false; 196 return false;
188 stack.append(SizesCalcValue(leftOperand.value / rightOperand.value, left Operand.isLength)); 197 stack.append(SizesCalcValue(leftOperand.value / rightOperand.value, left Operand.isLength));
189 break; 198 break;
190 default: 199 default:
191 return false; 200 return false;
192 } 201 }
193 return true; 202 return true;
194 } 203 }
195 204
196 bool SizesCalcParser::calculate(unsigned& result) 205 bool SizesCalcParser::calculate()
197 { 206 {
198 Vector<SizesCalcValue> stack; 207 Vector<SizesCalcValue> stack;
199 for (Vector<SizesCalcValue>::iterator it = m_valueList.begin(); it != m_valu eList.end(); ++it) { 208 for (Vector<SizesCalcValue>::iterator it = m_valueList.begin(); it != m_valu eList.end(); ++it) {
200 if (it->operation == 0) { 209 if (it->operation == 0) {
201 stack.append(*it); 210 stack.append(*it);
202 } else { 211 } else {
203 if (!operateOnStack(stack, it->operation)) 212 if (!operateOnStack(stack, it->operation))
204 return false; 213 return false;
205 } 214 }
206 } 215 }
207 if (stack.size() == 1 && stack.last().isLength) { 216 if (stack.size() == 1 && stack.last().isLength) {
208 result = clampTo<unsigned>(stack.last().value); 217 m_result = clampTo<unsigned>(stack.last().value);
209 return true; 218 return true;
210 } 219 }
211 return false; 220 return false;
212 } 221 }
213 222
214 } // namespace blink 223 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698