1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
// Copyright 2014-2017 The Chromium Authors. All rights reserved.
// Copyright (C) 2016 Apple Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "config.h"
#include "SizesCalcParser.h"
#include "CSSParserToken.h"
#include "RenderView.h"
#include "SizesAttributeParser.h"
namespace WebCore {
SizesCalcParser::SizesCalcParser(CSSParserTokenRange range, const Document& document)
: m_result(0)
, m_document(document)
{
m_isValid = calcToReversePolishNotation(range) && calculate();
}
float SizesCalcParser::result() const
{
ASSERT(m_isValid);
return m_result;
}
static bool operatorPriority(UChar cc, bool& highPriority)
{
if (cc == '+' || cc == '-')
highPriority = false;
else if (cc == '*' || cc == '/')
highPriority = true;
else
return false;
return true;
}
bool SizesCalcParser::handleOperator(Vector<CSSParserToken>& stack, const CSSParserToken& token)
{
// If the token is an operator, o1, then:
// while there is an operator token, o2, at the top of the stack, and
// either o1 is left-associative and its precedence is equal to that of o2,
// or o1 has precedence less than that of o2,
// pop o2 off the stack, onto the output queue;
// push o1 onto the stack.
bool stackOperatorPriority;
bool incomingOperatorPriority;
if (!operatorPriority(token.delimiter(), incomingOperatorPriority))
return false;
if (!stack.isEmpty() && stack.last().type() == DelimiterToken) {
if (!operatorPriority(stack.last().delimiter(), stackOperatorPriority))
return false;
if (!incomingOperatorPriority || stackOperatorPriority) {
appendOperator(stack.last());
stack.removeLast();
}
}
stack.append(token);
return true;
}
void SizesCalcParser::appendNumber(const CSSParserToken& token)
{
SizesCalcValue value;
value.value = token.numericValue();
m_valueList.append(value);
}
bool SizesCalcParser::appendLength(const CSSParserToken& token)
{
SizesCalcValue value;
double result = SizesAttributeParser::computeLength(token.numericValue(), token.unitType(), m_document);
value.value = result;
value.isLength = true;
m_valueList.append(value);
return true;
}
void SizesCalcParser::appendOperator(const CSSParserToken& token)
{
SizesCalcValue value;
value.operation = token.delimiter();
m_valueList.append(value);
}
bool SizesCalcParser::calcToReversePolishNotation(CSSParserTokenRange range)
{
// This method implements the shunting yard algorithm, to turn the calc syntax into a reverse polish notation.
// http://en.wikipedia.org/wiki/Shunting-yard_algorithm
Vector<CSSParserToken> stack;
while (!range.atEnd()) {
const CSSParserToken& token = range.consume();
switch (token.type()) {
case NumberToken:
appendNumber(token);
break;
case DimensionToken:
if (!CSSPrimitiveValue::isLength(token.unitType()) || !appendLength(token))
return false;
break;
case DelimiterToken:
if (!handleOperator(stack, token))
return false;
break;
case FunctionToken:
if (!equalLettersIgnoringASCIICase(token.value(), "calc"_s))
return false;
// "calc(" is the same as "("
FALLTHROUGH;
case LeftParenthesisToken:
// If the token is a left parenthesis, then push it onto the stack.
stack.append(token);
break;
case RightParenthesisToken:
// If the token is a right parenthesis:
// Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue.
while (!stack.isEmpty() && stack.last().type() != LeftParenthesisToken && stack.last().type() != FunctionToken) {
appendOperator(stack.last());
stack.removeLast();
}
// If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
if (stack.isEmpty())
return false;
// Pop the left parenthesis from the stack, but not onto the output queue.
stack.removeLast();
break;
case WhitespaceToken:
case EOFToken:
break;
case CDOToken:
case CDCToken:
case AtKeywordToken:
case HashToken:
case UrlToken:
case BadUrlToken:
case PercentageToken:
case IncludeMatchToken:
case DashMatchToken:
case PrefixMatchToken:
case SuffixMatchToken:
case SubstringMatchToken:
case ColumnToken:
case IdentToken:
case CommaToken:
case ColonToken:
case SemicolonToken:
case LeftBraceToken:
case LeftBracketToken:
case RightBraceToken:
case RightBracketToken:
case StringToken:
case BadStringToken:
return false;
case CommentToken:
ASSERT_NOT_REACHED();
return false;
}
}
// When there are no more tokens to read:
// While there are still operator tokens in the stack:
while (!stack.isEmpty()) {
// If the operator token on the top of the stack is a parenthesis, then there are unclosed parentheses.
CSSParserTokenType type = stack.last().type();
if (type != LeftParenthesisToken && type != FunctionToken) {
// Pop the operator onto the output queue.
appendOperator(stack.last());
}
stack.removeLast();
}
return true;
}
static bool operateOnStack(Vector<SizesCalcValue>& stack, UChar operation)
{
if (stack.size() < 2)
return false;
SizesCalcValue rightOperand = stack.last();
stack.removeLast();
SizesCalcValue leftOperand = stack.last();
stack.removeLast();
bool isLength;
switch (operation) {
case '+':
if (rightOperand.isLength != leftOperand.isLength)
return false;
isLength = (rightOperand.isLength && leftOperand.isLength);
stack.append(SizesCalcValue(leftOperand.value + rightOperand.value, isLength));
break;
case '-':
if (rightOperand.isLength != leftOperand.isLength)
return false;
isLength = (rightOperand.isLength && leftOperand.isLength);
stack.append(SizesCalcValue(leftOperand.value - rightOperand.value, isLength));
break;
case '*':
if (rightOperand.isLength && leftOperand.isLength)
return false;
isLength = (rightOperand.isLength || leftOperand.isLength);
stack.append(SizesCalcValue(leftOperand.value * rightOperand.value, isLength));
break;
case '/':
if (rightOperand.isLength || !rightOperand.value)
return false;
stack.append(SizesCalcValue(leftOperand.value / rightOperand.value, leftOperand.isLength));
break;
default:
return false;
}
return true;
}
bool SizesCalcParser::calculate()
{
Vector<SizesCalcValue> stack;
for (const auto& value : m_valueList) {
if (!value.operation)
stack.append(value);
else {
if (!operateOnStack(stack, value.operation))
return false;
}
}
if (stack.size() == 1 && stack.last().isLength) {
m_result = std::max(clampTo<float>(stack.last().value), (float)0.0);
return true;
}
return false;
}
} // namespace WebCore
|