| OLD | NEW |
| 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) && calculate(); |
| 16 if (!parser.calcToReversePolishNotation(start, end)) | 19 } |
| 17 return false; | 20 |
| 18 return parser.calculate(result); | 21 unsigned SizesCalcParser::result() const |
| 22 { |
| 23 ASSERT(m_isValid); |
| 24 return m_result; |
| 19 } | 25 } |
| 20 | 26 |
| 21 static bool operatorPriority(UChar cc, bool& highPriority) | 27 static bool operatorPriority(UChar cc, bool& highPriority) |
| 22 { | 28 { |
| 23 if (cc == '+' || cc == '-') | 29 if (cc == '+' || cc == '-') |
| 24 highPriority = false; | 30 highPriority = false; |
| 25 else if (cc == '*' || cc == '/') | 31 else if (cc == '*' || cc == '/') |
| 26 highPriority = true; | 32 highPriority = true; |
| 27 else | 33 else |
| 28 return false; | 34 return false; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // http://en.wikipedia.org/wiki/Shunting-yard_algorithm | 92 // http://en.wikipedia.org/wiki/Shunting-yard_algorithm |
| 87 | 93 |
| 88 Vector<MediaQueryToken> stack; | 94 Vector<MediaQueryToken> stack; |
| 89 for (MediaQueryTokenIterator it = start; it != end; ++it) { | 95 for (MediaQueryTokenIterator it = start; it != end; ++it) { |
| 90 MediaQueryTokenType type = it->type(); | 96 MediaQueryTokenType type = it->type(); |
| 91 switch (type) { | 97 switch (type) { |
| 92 case NumberToken: | 98 case NumberToken: |
| 93 appendNumber(*it); | 99 appendNumber(*it); |
| 94 break; | 100 break; |
| 95 case DimensionToken: | 101 case DimensionToken: |
| 102 m_viewportDependant = m_viewportDependant || CSSPrimitiveValue::isVi
ewportPercentageLength(it->unitType()); |
| 96 if (!CSSPrimitiveValue::isLength(it->unitType()) || !appendLength(*i
t)) | 103 if (!CSSPrimitiveValue::isLength(it->unitType()) || !appendLength(*i
t)) |
| 97 return false; | 104 return false; |
| 98 break; | 105 break; |
| 99 case DelimiterToken: | 106 case DelimiterToken: |
| 100 if (!handleOperator(stack, *it)) | 107 if (!handleOperator(stack, *it)) |
| 101 return false; | 108 return false; |
| 102 break; | 109 break; |
| 103 case FunctionToken: | 110 case FunctionToken: |
| 104 if (it->value() != "calc") | 111 if (it->value() != "calc") |
| 105 return false; | 112 return false; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 if (rightOperand.isLength || rightOperand.value == 0) | 193 if (rightOperand.isLength || rightOperand.value == 0) |
| 187 return false; | 194 return false; |
| 188 stack.append(SizesCalcValue(leftOperand.value / rightOperand.value, left
Operand.isLength)); | 195 stack.append(SizesCalcValue(leftOperand.value / rightOperand.value, left
Operand.isLength)); |
| 189 break; | 196 break; |
| 190 default: | 197 default: |
| 191 return false; | 198 return false; |
| 192 } | 199 } |
| 193 return true; | 200 return true; |
| 194 } | 201 } |
| 195 | 202 |
| 196 bool SizesCalcParser::calculate(unsigned& result) | 203 bool SizesCalcParser::calculate() |
| 197 { | 204 { |
| 198 Vector<SizesCalcValue> stack; | 205 Vector<SizesCalcValue> stack; |
| 199 for (Vector<SizesCalcValue>::iterator it = m_valueList.begin(); it != m_valu
eList.end(); ++it) { | 206 for (Vector<SizesCalcValue>::iterator it = m_valueList.begin(); it != m_valu
eList.end(); ++it) { |
| 200 if (it->operation == 0) { | 207 if (it->operation == 0) { |
| 201 stack.append(*it); | 208 stack.append(*it); |
| 202 } else { | 209 } else { |
| 203 if (!operateOnStack(stack, it->operation)) | 210 if (!operateOnStack(stack, it->operation)) |
| 204 return false; | 211 return false; |
| 205 } | 212 } |
| 206 } | 213 } |
| 207 if (stack.size() == 1 && stack.last().isLength) { | 214 if (stack.size() == 1 && stack.last().isLength) { |
| 208 result = clampTo<unsigned>(stack.last().value); | 215 m_result = clampTo<unsigned>(stack.last().value); |
| 209 return true; | 216 return true; |
| 210 } | 217 } |
| 211 return false; | 218 return false; |
| 212 } | 219 } |
| 213 | 220 |
| 214 } // namespace blink | 221 } // namespace blink |
| OLD | NEW |