Changeset 154745 in webkit


Ignore:
Timestamp:
Aug 28, 2013 7:31:16 AM (11 years ago)
Author:
andersca@apple.com
Message:

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):

Location:
trunk/Source/WebCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r154743 r154745  
     12013-08-28  Anders Carlsson  <andersca@apple.com>
     2
     3        Clean up XPathExpressionNode and XPath::Function
     4        https://bugs.webkit.org/show_bug.cgi?id=120411
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Rename the subexpression and function argument getters to be more descriptive,
     9        remove the non-const overloads (they were never used) and change the getters to return
     10        references since they can never be null.
     11
     12        * xml/XPathExpressionNode.cpp:
     13        * xml/XPathExpressionNode.h:
     14        (WebCore::XPath::ParseNode::~ParseNode):
     15        (WebCore::XPath::Expression::addSubExpression):
     16        (WebCore::XPath::Expression::isContextNodeSensitive):
     17        (WebCore::XPath::Expression::setIsContextNodeSensitive):
     18        (WebCore::XPath::Expression::isContextPositionSensitive):
     19        (WebCore::XPath::Expression::setIsContextPositionSensitive):
     20        (WebCore::XPath::Expression::isContextSizeSensitive):
     21        (WebCore::XPath::Expression::setIsContextSizeSensitive):
     22        (WebCore::XPath::Expression::subExpressionCount):
     23        (WebCore::XPath::Expression::subExpression):
     24        * xml/XPathFunctions.cpp:
     25        (WebCore::XPath::Function::setArguments):
     26        (WebCore::XPath::FunId::evaluate):
     27        (WebCore::XPath::FunLocalName::evaluate):
     28        (WebCore::XPath::FunNamespaceURI::evaluate):
     29        (WebCore::XPath::FunName::evaluate):
     30        (WebCore::XPath::FunCount::evaluate):
     31        (WebCore::XPath::FunString::evaluate):
     32        (WebCore::XPath::FunConcat::evaluate):
     33        (WebCore::XPath::FunStartsWith::evaluate):
     34        (WebCore::XPath::FunContains::evaluate):
     35        (WebCore::XPath::FunSubstringBefore::evaluate):
     36        (WebCore::XPath::FunSubstringAfter::evaluate):
     37        (WebCore::XPath::FunSubstring::evaluate):
     38        (WebCore::XPath::FunStringLength::evaluate):
     39        (WebCore::XPath::FunNormalizeSpace::evaluate):
     40        (WebCore::XPath::FunTranslate::evaluate):
     41        (WebCore::XPath::FunBoolean::evaluate):
     42        (WebCore::XPath::FunNot::evaluate):
     43        (WebCore::XPath::FunLang::evaluate):
     44        (WebCore::XPath::FunNumber::evaluate):
     45        (WebCore::XPath::FunSum::evaluate):
     46        (WebCore::XPath::FunFloor::evaluate):
     47        (WebCore::XPath::FunCeiling::evaluate):
     48        (WebCore::XPath::FunRound::evaluate):
     49        * xml/XPathFunctions.h:
     50        (WebCore::XPath::Function::setName):
     51        (WebCore::XPath::Function::argumentCount):
     52        (WebCore::XPath::Function::argument):
     53        (WebCore::XPath::Function::name):
     54        * xml/XPathPredicate.cpp:
     55        (WebCore::XPath::Negative::evaluate):
     56        (WebCore::XPath::NumericOp::evaluate):
     57        (WebCore::XPath::EqTestOp::evaluate):
     58        (WebCore::XPath::LogicalOp::evaluate):
     59        (WebCore::XPath::Union::evaluate):
     60
    1612013-08-28  Andreas Kling  <akling@apple.com>
    262
  • trunk/Source/WebCore/xml/XPathExpressionNode.cpp

    r97771 r154745  
    11/*
    22 * Copyright 2005 Frerich Raabe <raabe@kde.org>
    3  * Copyright (C) 2006 Apple Computer, Inc.
     3 * Copyright (C) 2006, 2013 Apple Computer, Inc.
    44 *
    55 * Redistribution and use in source and binary forms, with or without
     
    5252}
    5353
    54 }
    55 }
     54} // namespace XPath
     55} // namespace WebCore
  • trunk/Source/WebCore/xml/XPathExpressionNode.h

    r97771 r154745  
    11/*
    22 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
    3  * Copyright (C) 2006, 2009 Apple Inc.
     3 * Copyright (C) 2006, 2009, 2013 Apple Inc.
    44 *
    55 * Redistribution and use in source and binary forms, with or without
     
    3535
    3636namespace WebCore {
     37namespace XPath {
    3738
    38     namespace XPath {
    39        
    40         struct EvaluationContext {
    41             WTF_MAKE_FAST_ALLOCATED;
    42         public:
    43             RefPtr<Node> node;
    44             unsigned long size;
    45             unsigned long position;
    46             HashMap<String, String> variableBindings;
     39struct EvaluationContext {
     40    WTF_MAKE_FAST_ALLOCATED;
     41public:
     42    RefPtr<Node> node;
     43    unsigned long size;
     44    unsigned long position;
     45    HashMap<String, String> variableBindings;
    4746
    48             bool hadTypeConversionError;
    49         };
     47    bool hadTypeConversionError;
     48};
    5049
    51         class ParseNode {
    52         public:
    53             virtual ~ParseNode() { }
    54         };
     50class ParseNode {
     51public:
     52    virtual ~ParseNode() { }
     53};
    5554
    56         class Expression : public ParseNode {
    57             WTF_MAKE_NONCOPYABLE(Expression); WTF_MAKE_FAST_ALLOCATED;
    58         public:
    59             static EvaluationContext& evaluationContext();
     55class Expression : public ParseNode {
     56    WTF_MAKE_NONCOPYABLE(Expression); WTF_MAKE_FAST_ALLOCATED;
     57public:
     58    static EvaluationContext& evaluationContext();
    6059
    61             Expression();
    62             virtual ~Expression();
     60    virtual ~Expression();
    6361
    64             virtual Value evaluate() const = 0;
    65 
    66             void addSubExpression(Expression* expr)
    67             {
    68                 m_subExpressions.append(expr);
    69                 m_isContextNodeSensitive |= expr->m_isContextNodeSensitive;
    70                 m_isContextPositionSensitive |= expr->m_isContextPositionSensitive;
    71                 m_isContextSizeSensitive |= expr->m_isContextSizeSensitive;
    72             }
    73 
    74             bool isContextNodeSensitive() const { return m_isContextNodeSensitive; }
    75             bool isContextPositionSensitive() const { return m_isContextPositionSensitive; }
    76             bool isContextSizeSensitive() const { return m_isContextSizeSensitive; }
    77             void setIsContextNodeSensitive(bool value) { m_isContextNodeSensitive = value; }
    78             void setIsContextPositionSensitive(bool value) { m_isContextPositionSensitive = value; }
    79             void setIsContextSizeSensitive(bool value) { m_isContextSizeSensitive = value; }
    80 
    81             virtual Value::Type resultType() const = 0;
    82 
    83         protected:
    84             unsigned subExprCount() const { return m_subExpressions.size(); }
    85             Expression* subExpr(unsigned i) { return m_subExpressions[i]; }
    86             const Expression* subExpr(unsigned i) const { return m_subExpressions[i]; }
    87 
    88         private:
    89             Vector<Expression*> m_subExpressions;
    90 
    91             // Evaluation details that can be used for optimization.
    92             bool m_isContextNodeSensitive;
    93             bool m_isContextPositionSensitive;
    94             bool m_isContextSizeSensitive;
    95         };
    96 
     62    void addSubExpression(Expression* expr)
     63    {
     64        m_subExpressions.append(expr);
     65        m_isContextNodeSensitive |= expr->m_isContextNodeSensitive;
     66        m_isContextPositionSensitive |= expr->m_isContextPositionSensitive;
     67        m_isContextSizeSensitive |= expr->m_isContextSizeSensitive;
    9768    }
    9869
    99 }
     70    bool isContextNodeSensitive() const { return m_isContextNodeSensitive; }
     71    void setIsContextNodeSensitive(bool value) { m_isContextNodeSensitive = value; }
    10072
    101 #endif // EXPRESSION_H
     73    bool isContextPositionSensitive() const { return m_isContextPositionSensitive; }
     74    void setIsContextPositionSensitive(bool value) { m_isContextPositionSensitive = value; }
     75
     76    bool isContextSizeSensitive() const { return m_isContextSizeSensitive; }
     77    void setIsContextSizeSensitive(bool value) { m_isContextSizeSensitive = value; }
     78
     79    virtual Value evaluate() const = 0;
     80    virtual Value::Type resultType() const = 0;
     81
     82protected:
     83    Expression();
     84
     85    unsigned subExpressionCount() const { return m_subExpressions.size(); }
     86    const Expression& subExpression(unsigned i) const { return *m_subExpressions[i]; }
     87
     88private:
     89    Vector<Expression*> m_subExpressions;
     90
     91    // Evaluation details that can be used for optimization.
     92    bool m_isContextNodeSensitive;
     93    bool m_isContextPositionSensitive;
     94    bool m_isContextSizeSensitive;
     95};
     96
     97} // namespace XPath
     98} // namespace WebCore
     99
     100#endif // XPathExpressionNode_h
  • trunk/Source/WebCore/xml/XPathFunctions.cpp

    r153826 r154745  
    293293void Function::setArguments(const Vector<Expression*>& args)
    294294{
    295     ASSERT(!subExprCount());
     295    ASSERT(!subExpressionCount());
    296296
    297297    // Some functions use context node as implicit argument, so when explicit arguments are added, they may no longer be context node sensitive.
     
    316316Value FunId::evaluate() const
    317317{
    318     Value a = arg(0)->evaluate();
     318    Value a = argument(0).evaluate();
    319319    StringBuilder idList; // A whitespace-separated list of IDs
    320320
     
    379379Value FunLocalName::evaluate() const
    380380{
    381     if (argCount() > 0) {
    382         Value a = arg(0)->evaluate();
     381    if (argumentCount() > 0) {
     382        Value a = argument(0).evaluate();
    383383        if (!a.isNodeSet())
    384384            return "";
     
    393393Value FunNamespaceURI::evaluate() const
    394394{
    395     if (argCount() > 0) {
    396         Value a = arg(0)->evaluate();
     395    if (argumentCount() > 0) {
     396        Value a = argument(0).evaluate();
    397397        if (!a.isNodeSet())
    398398            return "";
     
    407407Value FunName::evaluate() const
    408408{
    409     if (argCount() > 0) {
    410         Value a = arg(0)->evaluate();
     409    if (argumentCount() > 0) {
     410        Value a = argument(0).evaluate();
    411411        if (!a.isNodeSet())
    412412            return "";
     
    421421Value FunCount::evaluate() const
    422422{
    423     Value a = arg(0)->evaluate();
     423    Value a = argument(0).evaluate();
    424424   
    425425    return double(a.toNodeSet().size());
     
    428428Value FunString::evaluate() const
    429429{
    430     if (!argCount())
     430    if (!argumentCount())
    431431        return Value(Expression::evaluationContext().node.get()).toString();
    432     return arg(0)->evaluate().toString();
     432    return argument(0).evaluate().toString();
    433433}
    434434
     
    438438    result.reserveCapacity(1024);
    439439
    440     unsigned count = argCount();
    441     for (unsigned i = 0; i < count; ++i) {
    442         String str(arg(i)->evaluate().toString());
     440    for (unsigned i = 0, count = argumentCount(); i < count; ++i) {
     441        String str(argument(i).evaluate().toString());
    443442        result.append(str);
    444443    }
     
    449448Value FunStartsWith::evaluate() const
    450449{
    451     String s1 = arg(0)->evaluate().toString();
    452     String s2 = arg(1)->evaluate().toString();
     450    String s1 = argument(0).evaluate().toString();
     451    String s2 = argument(1).evaluate().toString();
    453452
    454453    if (s2.isEmpty())
     
    460459Value FunContains::evaluate() const
    461460{
    462     String s1 = arg(0)->evaluate().toString();
    463     String s2 = arg(1)->evaluate().toString();
     461    String s1 = argument(0).evaluate().toString();
     462    String s2 = argument(1).evaluate().toString();
    464463
    465464    if (s2.isEmpty())
     
    471470Value FunSubstringBefore::evaluate() const
    472471{
    473     String s1 = arg(0)->evaluate().toString();
    474     String s2 = arg(1)->evaluate().toString();
     472    String s1 = argument(0).evaluate().toString();
     473    String s2 = argument(1).evaluate().toString();
    475474
    476475    if (s2.isEmpty())
     
    487486Value FunSubstringAfter::evaluate() const
    488487{
    489     String s1 = arg(0)->evaluate().toString();
    490     String s2 = arg(1)->evaluate().toString();
     488    String s1 = argument(0).evaluate().toString();
     489    String s2 = argument(1).evaluate().toString();
    491490
    492491    size_t i = s1.find(s2);
     
    499498Value FunSubstring::evaluate() const
    500499{
    501     String s = arg(0)->evaluate().toString();
    502     double doublePos = arg(1)->evaluate().toNumber();
     500    String s = argument(0).evaluate().toString();
     501    double doublePos = argument(1).evaluate().toNumber();
    503502    if (std::isnan(doublePos))
    504503        return "";
    505504    long pos = static_cast<long>(FunRound::round(doublePos));
    506     bool haveLength = argCount() == 3;
     505    bool haveLength = argumentCount() == 3;
    507506    long len = -1;
    508507    if (haveLength) {
    509         double doubleLen = arg(2)->evaluate().toNumber();
     508        double doubleLen = argument(2).evaluate().toNumber();
    510509        if (std::isnan(doubleLen))
    511510            return "";
     
    530529Value FunStringLength::evaluate() const
    531530{
    532     if (!argCount())
     531    if (!argumentCount())
    533532        return Value(Expression::evaluationContext().node.get()).toString().length();
    534     return arg(0)->evaluate().toString().length();
     533    return argument(0).evaluate().toString().length();
    535534}
    536535
    537536Value FunNormalizeSpace::evaluate() const
    538537{
    539     if (!argCount()) {
     538    if (!argumentCount()) {
    540539        String s = Value(Expression::evaluationContext().node.get()).toString();
    541540        return s.simplifyWhiteSpace();
    542541    }
    543542
    544     String s = arg(0)->evaluate().toString();
     543    String s = argument(0).evaluate().toString();
    545544    return s.simplifyWhiteSpace();
    546545}
     
    548547Value FunTranslate::evaluate() const
    549548{
    550     String s1 = arg(0)->evaluate().toString();
    551     String s2 = arg(1)->evaluate().toString();
    552     String s3 = arg(2)->evaluate().toString();
     549    String s1 = argument(0).evaluate().toString();
     550    String s2 = argument(1).evaluate().toString();
     551    String s3 = argument(2).evaluate().toString();
    553552    StringBuilder result;
    554553
     
    568567Value FunBoolean::evaluate() const
    569568{
    570     return arg(0)->evaluate().toBoolean();
     569    return argument(0).evaluate().toBoolean();
    571570}
    572571
    573572Value FunNot::evaluate() const
    574573{
    575     return !arg(0)->evaluate().toBoolean();
     574    return !argument(0).evaluate().toBoolean();
    576575}
    577576
     
    583582Value FunLang::evaluate() const
    584583{
    585     String lang = arg(0)->evaluate().toString();
     584    String lang = argument(0).evaluate().toString();
    586585
    587586    const Attribute* languageAttribute = 0;
     
    623622Value FunNumber::evaluate() const
    624623{
    625     if (!argCount())
     624    if (!argumentCount())
    626625        return Value(Expression::evaluationContext().node.get()).toNumber();
    627     return arg(0)->evaluate().toNumber();
     626    return argument(0).evaluate().toNumber();
    628627}
    629628
    630629Value FunSum::evaluate() const
    631630{
    632     Value a = arg(0)->evaluate();
     631    Value a = argument(0).evaluate();
    633632    if (!a.isNodeSet())
    634633        return 0.0;
     
    647646Value FunFloor::evaluate() const
    648647{
    649     return floor(arg(0)->evaluate().toNumber());
     648    return floor(argument(0).evaluate().toNumber());
    650649}
    651650
    652651Value FunCeiling::evaluate() const
    653652{
    654     return ceil(arg(0)->evaluate().toNumber());
     653    return ceil(argument(0).evaluate().toNumber());
    655654}
    656655
     
    668667Value FunRound::evaluate() const
    669668{
    670     return round(arg(0)->evaluate().toNumber());
     669    return round(argument(0).evaluate().toNumber());
    671670}
    672671
  • trunk/Source/WebCore/xml/XPathFunctions.h

    r97771 r154745  
    3131
    3232namespace WebCore {
     33namespace XPath {
    3334
    34     namespace XPath {
     35class Function : public Expression {
     36public:
     37    void setArguments(const Vector<Expression*>&);
     38    void setName(const String& name) { m_name = name; }
    3539
    36         class Function : public Expression {
    37         public:
    38             void setArguments(const Vector<Expression*>&);
    39             void setName(const String& name) { m_name = name; }
    40         protected:
    41             Expression* arg(int pos) { return subExpr(pos); }
    42             const Expression* arg(int pos) const { return subExpr(pos); }
    43             unsigned int argCount() const { return subExprCount(); }
    44             String name() const { return m_name; }
     40protected:
     41    unsigned argumentCount() const { return subExpressionCount(); }
     42    const Expression& argument(int pos) const { return subExpression(pos); }
    4543
    46         private:
    47             String m_name;
    48         };
     44    String name() const { return m_name; }
    4945
    50         Function* createFunction(const String& name, const Vector<Expression*>& args = Vector<Expression*>());
     46private:
     47    String m_name;
     48};
    5149
    52     } // namespace XPath
     50Function* createFunction(const String& name, const Vector<Expression*>& args = Vector<Expression*>());
    5351
     52} // namespace XPath
    5453} // namespace WebCore
    5554
  • trunk/Source/WebCore/xml/XPathPredicate.cpp

    r112555 r154745  
    6161Value Negative::evaluate() const
    6262{
    63     Value p(subExpr(0)->evaluate());
     63    Value p(subExpression(0).evaluate());
    6464    return -p.toNumber();
    6565}
     
    7474Value NumericOp::evaluate() const
    7575{
    76     Value lhs(subExpr(0)->evaluate());
    77     Value rhs(subExpr(1)->evaluate());
     76    Value lhs(subExpression(0).evaluate());
     77    Value rhs(subExpression(1).evaluate());
    7878   
    7979    double leftVal = lhs.toNumber();
     
    193193Value EqTestOp::evaluate() const
    194194{
    195     Value lhs(subExpr(0)->evaluate());
    196     Value rhs(subExpr(1)->evaluate());
     195    Value lhs(subExpression(0).evaluate());
     196    Value rhs(subExpression(1).evaluate());
    197197
    198198    return compare(lhs, rhs);
     
    216216Value LogicalOp::evaluate() const
    217217{
    218     Value lhs(subExpr(0)->evaluate());
     218    Value lhs(subExpression(0).evaluate());
    219219
    220220    // This is not only an optimization, http://www.w3.org/TR/xpath
     
    224224        return lhsBool;
    225225
    226     return subExpr(1)->evaluate().toBoolean();
     226    return subExpression(1).evaluate().toBoolean();
    227227}
    228228
    229229Value Union::evaluate() const
    230230{
    231     Value lhsResult = subExpr(0)->evaluate();
    232     Value rhs = subExpr(1)->evaluate();
     231    Value lhsResult = subExpression(0).evaluate();
     232    Value rhs = subExpression(1).evaluate();
    233233   
    234234    NodeSet& resultSet = lhsResult.modifiableNodeSet();
Note: See TracChangeset for help on using the changeset viewer.