Home | History | Annotate | Download | only in css
      1 /*
      2  * (C) 1999-2003 Lars Knoll (knoll (at) kde.org)
      3  * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #ifndef Pair_h
     22 #define Pair_h
     23 
     24 #include "core/css/CSSPrimitiveValue.h"
     25 #include "wtf/PassRefPtr.h"
     26 #include "wtf/RefCounted.h"
     27 #include "wtf/text/StringBuilder.h"
     28 
     29 namespace WebCore {
     30 
     31 // A primitive value representing a pair.  This is useful for properties like border-radius, background-size/position,
     32 // and border-spacing (all of which are space-separated sets of two values).  At the moment we are only using it for
     33 // border-radius and background-size, but (FIXME) border-spacing and background-position could be converted over to use
     34 // it (eliminating some extra -webkit- internal properties).
     35 class Pair FINAL : public RefCounted<Pair> {
     36 public:
     37     enum IdenticalValuesPolicy { DropIdenticalValues, KeepIdenticalValues };
     38 
     39     static PassRefPtr<Pair> create()
     40     {
     41         return adoptRef(new Pair);
     42     }
     43     static PassRefPtr<Pair> create(PassRefPtr<CSSPrimitiveValue> first, PassRefPtr<CSSPrimitiveValue> second, IdenticalValuesPolicy identicalValuesPolicy)
     44     {
     45         return adoptRef(new Pair(first, second, identicalValuesPolicy));
     46     }
     47 
     48     CSSPrimitiveValue* first() const { return m_first.get(); }
     49     CSSPrimitiveValue* second() const { return m_second.get(); }
     50     IdenticalValuesPolicy identicalValuesPolicy() const { return m_identicalValuesPolicy; }
     51 
     52     void setFirst(PassRefPtr<CSSPrimitiveValue> first) { m_first = first; }
     53     void setSecond(PassRefPtr<CSSPrimitiveValue> second) { m_second = second; }
     54     void setIdenticalValuesPolicy(IdenticalValuesPolicy identicalValuesPolicy) { m_identicalValuesPolicy = identicalValuesPolicy; }
     55 
     56     String cssText() const
     57     {
     58         return generateCSSString(first()->cssText(), second()->cssText(), m_identicalValuesPolicy);
     59     }
     60 
     61     bool equals(const Pair& other) const
     62     {
     63         return compareCSSValuePtr(m_first, other.m_first)
     64             && compareCSSValuePtr(m_second, other.m_second)
     65             && m_identicalValuesPolicy == other.m_identicalValuesPolicy;
     66     }
     67 
     68     String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
     69     {
     70         return generateCSSString(
     71             first()->customSerializeResolvingVariables(variables),
     72             second()->customSerializeResolvingVariables(variables),
     73             m_identicalValuesPolicy);
     74     }
     75 
     76     bool hasVariableReference() const { return first()->hasVariableReference() || second()->hasVariableReference(); }
     77 
     78 private:
     79     Pair()
     80         : m_first(0)
     81         , m_second(0)
     82         , m_identicalValuesPolicy(DropIdenticalValues) { }
     83 
     84     Pair(PassRefPtr<CSSPrimitiveValue> first, PassRefPtr<CSSPrimitiveValue> second, IdenticalValuesPolicy identicalValuesPolicy)
     85         : m_first(first)
     86         , m_second(second)
     87         , m_identicalValuesPolicy(identicalValuesPolicy) { }
     88 
     89     static String generateCSSString(const String& first, const String& second, IdenticalValuesPolicy identicalValuesPolicy)
     90     {
     91         if (identicalValuesPolicy == DropIdenticalValues && first == second)
     92             return first;
     93         return first + ' ' + second;
     94     }
     95 
     96     RefPtr<CSSPrimitiveValue> m_first;
     97     RefPtr<CSSPrimitiveValue> m_second;
     98     IdenticalValuesPolicy m_identicalValuesPolicy;
     99 };
    100 
    101 } // namespace
    102 
    103 #endif
    104