Commit 9f50b2a0 authored by andersca@apple.com's avatar andersca@apple.com

Clean up XPathExpressionNode and XPath::Function

https://bugs.webkit.org/show_bug.cgi?id=120411

Reviewed by Antti Koivisto.

Rename the subexpression and function argument getters to be more descriptive,
remove the non-const overloads (they were never used) and change the getters to return
references since they can never be null.

* xml/XPathExpressionNode.cpp:
* xml/XPathExpressionNode.h:
(WebCore::XPath::ParseNode::~ParseNode):
(WebCore::XPath::Expression::addSubExpression):
(WebCore::XPath::Expression::isContextNodeSensitive):
(WebCore::XPath::Expression::setIsContextNodeSensitive):
(WebCore::XPath::Expression::isContextPositionSensitive):
(WebCore::XPath::Expression::setIsContextPositionSensitive):
(WebCore::XPath::Expression::isContextSizeSensitive):
(WebCore::XPath::Expression::setIsContextSizeSensitive):
(WebCore::XPath::Expression::subExpressionCount):
(WebCore::XPath::Expression::subExpression):
* xml/XPathFunctions.cpp:
(WebCore::XPath::Function::setArguments):
(WebCore::XPath::FunId::evaluate):
(WebCore::XPath::FunLocalName::evaluate):
(WebCore::XPath::FunNamespaceURI::evaluate):
(WebCore::XPath::FunName::evaluate):
(WebCore::XPath::FunCount::evaluate):
(WebCore::XPath::FunString::evaluate):
(WebCore::XPath::FunConcat::evaluate):
(WebCore::XPath::FunStartsWith::evaluate):
(WebCore::XPath::FunContains::evaluate):
(WebCore::XPath::FunSubstringBefore::evaluate):
(WebCore::XPath::FunSubstringAfter::evaluate):
(WebCore::XPath::FunSubstring::evaluate):
(WebCore::XPath::FunStringLength::evaluate):
(WebCore::XPath::FunNormalizeSpace::evaluate):
(WebCore::XPath::FunTranslate::evaluate):
(WebCore::XPath::FunBoolean::evaluate):
(WebCore::XPath::FunNot::evaluate):
(WebCore::XPath::FunLang::evaluate):
(WebCore::XPath::FunNumber::evaluate):
(WebCore::XPath::FunSum::evaluate):
(WebCore::XPath::FunFloor::evaluate):
(WebCore::XPath::FunCeiling::evaluate):
(WebCore::XPath::FunRound::evaluate):
* xml/XPathFunctions.h:
(WebCore::XPath::Function::setName):
(WebCore::XPath::Function::argumentCount):
(WebCore::XPath::Function::argument):
(WebCore::XPath::Function::name):
* xml/XPathPredicate.cpp:
(WebCore::XPath::Negative::evaluate):
(WebCore::XPath::NumericOp::evaluate):
(WebCore::XPath::EqTestOp::evaluate):
(WebCore::XPath::LogicalOp::evaluate):
(WebCore::XPath::Union::evaluate):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@154745 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 42e42c67
2013-08-28 Anders Carlsson <andersca@apple.com>
Clean up XPathExpressionNode and XPath::Function
https://bugs.webkit.org/show_bug.cgi?id=120411
Reviewed by Antti Koivisto.
Rename the subexpression and function argument getters to be more descriptive,
remove the non-const overloads (they were never used) and change the getters to return
references since they can never be null.
* xml/XPathExpressionNode.cpp:
* xml/XPathExpressionNode.h:
(WebCore::XPath::ParseNode::~ParseNode):
(WebCore::XPath::Expression::addSubExpression):
(WebCore::XPath::Expression::isContextNodeSensitive):
(WebCore::XPath::Expression::setIsContextNodeSensitive):
(WebCore::XPath::Expression::isContextPositionSensitive):
(WebCore::XPath::Expression::setIsContextPositionSensitive):
(WebCore::XPath::Expression::isContextSizeSensitive):
(WebCore::XPath::Expression::setIsContextSizeSensitive):
(WebCore::XPath::Expression::subExpressionCount):
(WebCore::XPath::Expression::subExpression):
* xml/XPathFunctions.cpp:
(WebCore::XPath::Function::setArguments):
(WebCore::XPath::FunId::evaluate):
(WebCore::XPath::FunLocalName::evaluate):
(WebCore::XPath::FunNamespaceURI::evaluate):
(WebCore::XPath::FunName::evaluate):
(WebCore::XPath::FunCount::evaluate):
(WebCore::XPath::FunString::evaluate):
(WebCore::XPath::FunConcat::evaluate):
(WebCore::XPath::FunStartsWith::evaluate):
(WebCore::XPath::FunContains::evaluate):
(WebCore::XPath::FunSubstringBefore::evaluate):
(WebCore::XPath::FunSubstringAfter::evaluate):
(WebCore::XPath::FunSubstring::evaluate):
(WebCore::XPath::FunStringLength::evaluate):
(WebCore::XPath::FunNormalizeSpace::evaluate):
(WebCore::XPath::FunTranslate::evaluate):
(WebCore::XPath::FunBoolean::evaluate):
(WebCore::XPath::FunNot::evaluate):
(WebCore::XPath::FunLang::evaluate):
(WebCore::XPath::FunNumber::evaluate):
(WebCore::XPath::FunSum::evaluate):
(WebCore::XPath::FunFloor::evaluate):
(WebCore::XPath::FunCeiling::evaluate):
(WebCore::XPath::FunRound::evaluate):
* xml/XPathFunctions.h:
(WebCore::XPath::Function::setName):
(WebCore::XPath::Function::argumentCount):
(WebCore::XPath::Function::argument):
(WebCore::XPath::Function::name):
* xml/XPathPredicate.cpp:
(WebCore::XPath::Negative::evaluate):
(WebCore::XPath::NumericOp::evaluate):
(WebCore::XPath::EqTestOp::evaluate):
(WebCore::XPath::LogicalOp::evaluate):
(WebCore::XPath::Union::evaluate):
2013-08-28 Andreas Kling <akling@apple.com>
Page::pluginData() should return a reference.
/*
* Copyright 2005 Frerich Raabe <raabe@kde.org>
* Copyright (C) 2006 Apple Computer, Inc.
* Copyright (C) 2006, 2013 Apple Computer, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -51,5 +51,5 @@ Expression::~Expression()
deleteAllValues(m_subExpressions);
}
}
}
} // namespace XPath
} // namespace WebCore
/*
* Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
* Copyright (C) 2006, 2009 Apple Inc.
* Copyright (C) 2006, 2009, 2013 Apple Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -34,68 +34,67 @@
#include <wtf/text/StringHash.h>
namespace WebCore {
namespace XPath {
struct EvaluationContext {
WTF_MAKE_FAST_ALLOCATED;
public:
RefPtr<Node> node;
unsigned long size;
unsigned long position;
HashMap<String, String> variableBindings;
bool hadTypeConversionError;
};
class ParseNode {
public:
virtual ~ParseNode() { }
};
class Expression : public ParseNode {
WTF_MAKE_NONCOPYABLE(Expression); WTF_MAKE_FAST_ALLOCATED;
public:
static EvaluationContext& evaluationContext();
virtual ~Expression();
void addSubExpression(Expression* expr)
{
m_subExpressions.append(expr);
m_isContextNodeSensitive |= expr->m_isContextNodeSensitive;
m_isContextPositionSensitive |= expr->m_isContextPositionSensitive;
m_isContextSizeSensitive |= expr->m_isContextSizeSensitive;
}
namespace XPath {
struct EvaluationContext {
WTF_MAKE_FAST_ALLOCATED;
public:
RefPtr<Node> node;
unsigned long size;
unsigned long position;
HashMap<String, String> variableBindings;
bool hadTypeConversionError;
};
class ParseNode {
public:
virtual ~ParseNode() { }
};
class Expression : public ParseNode {
WTF_MAKE_NONCOPYABLE(Expression); WTF_MAKE_FAST_ALLOCATED;
public:
static EvaluationContext& evaluationContext();
Expression();
virtual ~Expression();
virtual Value evaluate() const = 0;
void addSubExpression(Expression* expr)
{
m_subExpressions.append(expr);
m_isContextNodeSensitive |= expr->m_isContextNodeSensitive;
m_isContextPositionSensitive |= expr->m_isContextPositionSensitive;
m_isContextSizeSensitive |= expr->m_isContextSizeSensitive;
}
bool isContextNodeSensitive() const { return m_isContextNodeSensitive; }
bool isContextPositionSensitive() const { return m_isContextPositionSensitive; }
bool isContextSizeSensitive() const { return m_isContextSizeSensitive; }
void setIsContextNodeSensitive(bool value) { m_isContextNodeSensitive = value; }
void setIsContextPositionSensitive(bool value) { m_isContextPositionSensitive = value; }
void setIsContextSizeSensitive(bool value) { m_isContextSizeSensitive = value; }
virtual Value::Type resultType() const = 0;
protected:
unsigned subExprCount() const { return m_subExpressions.size(); }
Expression* subExpr(unsigned i) { return m_subExpressions[i]; }
const Expression* subExpr(unsigned i) const { return m_subExpressions[i]; }
private:
Vector<Expression*> m_subExpressions;
// Evaluation details that can be used for optimization.
bool m_isContextNodeSensitive;
bool m_isContextPositionSensitive;
bool m_isContextSizeSensitive;
};
bool isContextNodeSensitive() const { return m_isContextNodeSensitive; }
void setIsContextNodeSensitive(bool value) { m_isContextNodeSensitive = value; }
}
bool isContextPositionSensitive() const { return m_isContextPositionSensitive; }
void setIsContextPositionSensitive(bool value) { m_isContextPositionSensitive = value; }
bool isContextSizeSensitive() const { return m_isContextSizeSensitive; }
void setIsContextSizeSensitive(bool value) { m_isContextSizeSensitive = value; }
virtual Value evaluate() const = 0;
virtual Value::Type resultType() const = 0;
protected:
Expression();
unsigned subExpressionCount() const { return m_subExpressions.size(); }
const Expression& subExpression(unsigned i) const { return *m_subExpressions[i]; }
private:
Vector<Expression*> m_subExpressions;
// Evaluation details that can be used for optimization.
bool m_isContextNodeSensitive;
bool m_isContextPositionSensitive;
bool m_isContextSizeSensitive;
};
}
} // namespace XPath
} // namespace WebCore
#endif // EXPRESSION_H
#endif // XPathExpressionNode_h
......@@ -292,7 +292,7 @@ inline bool Interval::contains(int value) const
void Function::setArguments(const Vector<Expression*>& args)
{
ASSERT(!subExprCount());
ASSERT(!subExpressionCount());
// Some functions use context node as implicit argument, so when explicit arguments are added, they may no longer be context node sensitive.
if (m_name != "lang" && !args.isEmpty())
......@@ -315,7 +315,7 @@ Value FunPosition::evaluate() const
Value FunId::evaluate() const
{
Value a = arg(0)->evaluate();
Value a = argument(0).evaluate();
StringBuilder idList; // A whitespace-separated list of IDs
if (a.isNodeSet()) {
......@@ -378,8 +378,8 @@ static inline String expandedName(Node* node)
Value FunLocalName::evaluate() const
{
if (argCount() > 0) {
Value a = arg(0)->evaluate();
if (argumentCount() > 0) {
Value a = argument(0).evaluate();
if (!a.isNodeSet())
return "";
......@@ -392,8 +392,8 @@ Value FunLocalName::evaluate() const
Value FunNamespaceURI::evaluate() const
{
if (argCount() > 0) {
Value a = arg(0)->evaluate();
if (argumentCount() > 0) {
Value a = argument(0).evaluate();
if (!a.isNodeSet())
return "";
......@@ -406,8 +406,8 @@ Value FunNamespaceURI::evaluate() const
Value FunName::evaluate() const
{
if (argCount() > 0) {
Value a = arg(0)->evaluate();
if (argumentCount() > 0) {
Value a = argument(0).evaluate();
if (!a.isNodeSet())
return "";
......@@ -420,16 +420,16 @@ Value FunName::evaluate() const
Value FunCount::evaluate() const
{
Value a = arg(0)->evaluate();
Value a = argument(0).evaluate();
return double(a.toNodeSet().size());
}
Value FunString::evaluate() const
{
if (!argCount())
if (!argumentCount())
return Value(Expression::evaluationContext().node.get()).toString();
return arg(0)->evaluate().toString();
return argument(0).evaluate().toString();
}
Value FunConcat::evaluate() const
......@@ -437,9 +437,8 @@ Value FunConcat::evaluate() const
StringBuilder result;
result.reserveCapacity(1024);
unsigned count = argCount();
for (unsigned i = 0; i < count; ++i) {
String str(arg(i)->evaluate().toString());
for (unsigned i = 0, count = argumentCount(); i < count; ++i) {
String str(argument(i).evaluate().toString());
result.append(str);
}
......@@ -448,8 +447,8 @@ Value FunConcat::evaluate() const
Value FunStartsWith::evaluate() const
{
String s1 = arg(0)->evaluate().toString();
String s2 = arg(1)->evaluate().toString();
String s1 = argument(0).evaluate().toString();
String s2 = argument(1).evaluate().toString();
if (s2.isEmpty())
return true;
......@@ -459,8 +458,8 @@ Value FunStartsWith::evaluate() const
Value FunContains::evaluate() const
{
String s1 = arg(0)->evaluate().toString();
String s2 = arg(1)->evaluate().toString();
String s1 = argument(0).evaluate().toString();
String s2 = argument(1).evaluate().toString();
if (s2.isEmpty())
return true;
......@@ -470,8 +469,8 @@ Value FunContains::evaluate() const
Value FunSubstringBefore::evaluate() const
{
String s1 = arg(0)->evaluate().toString();
String s2 = arg(1)->evaluate().toString();
String s1 = argument(0).evaluate().toString();
String s2 = argument(1).evaluate().toString();
if (s2.isEmpty())
return "";
......@@ -486,8 +485,8 @@ Value FunSubstringBefore::evaluate() const
Value FunSubstringAfter::evaluate() const
{
String s1 = arg(0)->evaluate().toString();
String s2 = arg(1)->evaluate().toString();
String s1 = argument(0).evaluate().toString();
String s2 = argument(1).evaluate().toString();
size_t i = s1.find(s2);
if (i == notFound)
......@@ -498,15 +497,15 @@ Value FunSubstringAfter::evaluate() const
Value FunSubstring::evaluate() const
{
String s = arg(0)->evaluate().toString();
double doublePos = arg(1)->evaluate().toNumber();
String s = argument(0).evaluate().toString();
double doublePos = argument(1).evaluate().toNumber();
if (std::isnan(doublePos))
return "";
long pos = static_cast<long>(FunRound::round(doublePos));
bool haveLength = argCount() == 3;
bool haveLength = argumentCount() == 3;
long len = -1;
if (haveLength) {
double doubleLen = arg(2)->evaluate().toNumber();
double doubleLen = argument(2).evaluate().toNumber();
if (std::isnan(doubleLen))
return "";
len = static_cast<long>(FunRound::round(doubleLen));
......@@ -529,27 +528,27 @@ Value FunSubstring::evaluate() const
Value FunStringLength::evaluate() const
{
if (!argCount())
if (!argumentCount())
return Value(Expression::evaluationContext().node.get()).toString().length();
return arg(0)->evaluate().toString().length();
return argument(0).evaluate().toString().length();
}
Value FunNormalizeSpace::evaluate() const
{
if (!argCount()) {
if (!argumentCount()) {
String s = Value(Expression::evaluationContext().node.get()).toString();
return s.simplifyWhiteSpace();
}
String s = arg(0)->evaluate().toString();
String s = argument(0).evaluate().toString();
return s.simplifyWhiteSpace();
}
Value FunTranslate::evaluate() const
{
String s1 = arg(0)->evaluate().toString();
String s2 = arg(1)->evaluate().toString();
String s3 = arg(2)->evaluate().toString();
String s1 = argument(0).evaluate().toString();
String s2 = argument(1).evaluate().toString();
String s3 = argument(2).evaluate().toString();
StringBuilder result;
for (unsigned i1 = 0; i1 < s1.length(); ++i1) {
......@@ -567,12 +566,12 @@ Value FunTranslate::evaluate() const
Value FunBoolean::evaluate() const
{
return arg(0)->evaluate().toBoolean();
return argument(0).evaluate().toBoolean();
}
Value FunNot::evaluate() const
{
return !arg(0)->evaluate().toBoolean();
return !argument(0).evaluate().toBoolean();
}
Value FunTrue::evaluate() const
......@@ -582,7 +581,7 @@ Value FunTrue::evaluate() const
Value FunLang::evaluate() const
{
String lang = arg(0)->evaluate().toString();
String lang = argument(0).evaluate().toString();
const Attribute* languageAttribute = 0;
Node* node = evaluationContext().node.get();
......@@ -622,14 +621,14 @@ Value FunFalse::evaluate() const
Value FunNumber::evaluate() const
{
if (!argCount())
if (!argumentCount())
return Value(Expression::evaluationContext().node.get()).toNumber();
return arg(0)->evaluate().toNumber();
return argument(0).evaluate().toNumber();
}
Value FunSum::evaluate() const
{
Value a = arg(0)->evaluate();
Value a = argument(0).evaluate();
if (!a.isNodeSet())
return 0.0;
......@@ -646,12 +645,12 @@ Value FunSum::evaluate() const
Value FunFloor::evaluate() const
{
return floor(arg(0)->evaluate().toNumber());
return floor(argument(0).evaluate().toNumber());
}
Value FunCeiling::evaluate() const
{
return ceil(arg(0)->evaluate().toNumber());
return ceil(argument(0).evaluate().toNumber());
}
double FunRound::round(double val)
......@@ -667,7 +666,7 @@ double FunRound::round(double val)
Value FunRound::evaluate() const
{
return round(arg(0)->evaluate().toNumber());
return round(argument(0).evaluate().toNumber());
}
struct FunctionMapping {
......
......@@ -30,27 +30,26 @@
#include "XPathExpressionNode.h"
namespace WebCore {
namespace XPath {
namespace XPath {
class Function : public Expression {
public:
void setArguments(const Vector<Expression*>&);
void setName(const String& name) { m_name = name; }
class Function : public Expression {
public:
void setArguments(const Vector<Expression*>&);
void setName(const String& name) { m_name = name; }
protected:
Expression* arg(int pos) { return subExpr(pos); }
const Expression* arg(int pos) const { return subExpr(pos); }
unsigned int argCount() const { return subExprCount(); }
String name() const { return m_name; }
protected:
unsigned argumentCount() const { return subExpressionCount(); }
const Expression& argument(int pos) const { return subExpression(pos); }
private:
String m_name;
};
String name() const { return m_name; }
Function* createFunction(const String& name, const Vector<Expression*>& args = Vector<Expression*>());
private:
String m_name;
};
} // namespace XPath
Function* createFunction(const String& name, const Vector<Expression*>& args = Vector<Expression*>());
} // namespace XPath
} // namespace WebCore
#endif // XPathFunctions_h
......@@ -60,7 +60,7 @@ Value StringExpression::evaluate() const
Value Negative::evaluate() const
{
Value p(subExpr(0)->evaluate());
Value p(subExpression(0).evaluate());
return -p.toNumber();
}
......@@ -73,8 +73,8 @@ NumericOp::NumericOp(Opcode opcode, Expression* lhs, Expression* rhs)
Value NumericOp::evaluate() const
{
Value lhs(subExpr(0)->evaluate());
Value rhs(subExpr(1)->evaluate());
Value lhs(subExpression(0).evaluate());
Value rhs(subExpression(1).evaluate());
double leftVal = lhs.toNumber();
double rightVal = rhs.toNumber();
......@@ -192,8 +192,8 @@ bool EqTestOp::compare(const Value& lhs, const Value& rhs) const
Value EqTestOp::evaluate() const
{
Value lhs(subExpr(0)->evaluate());
Value rhs(subExpr(1)->evaluate());
Value lhs(subExpression(0).evaluate());
Value rhs(subExpression(1).evaluate());
return compare(lhs, rhs);
}
......@@ -215,7 +215,7 @@ bool LogicalOp::shortCircuitOn() const
Value LogicalOp::evaluate() const
{
Value lhs(subExpr(0)->evaluate());
Value lhs(subExpression(0).evaluate());
// This is not only an optimization, http://www.w3.org/TR/xpath
// dictates that we must do short-circuit evaluation
......@@ -223,13 +223,13 @@ Value LogicalOp::evaluate() const
if (lhsBool == shortCircuitOn())
return lhsBool;
return subExpr(1)->evaluate().toBoolean();
return subExpression(1).evaluate().toBoolean();
}
Value Union::evaluate() const
{
Value lhsResult = subExpr(0)->evaluate();
Value rhs = subExpr(1)->evaluate();
Value lhsResult = subExpression(0).evaluate();
Value rhs = subExpression(1).evaluate();
NodeSet& resultSet = lhsResult.modifiableNodeSet();
const NodeSet& rhsNodes = rhs.toNodeSet();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment