Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 1999-2003 Lars Knoll (knoll@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" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 25 | #include "wtf/PassRefPtr.h" |
| 26 | #include "wtf/RefCounted.h" |
| 27 | #include "wtf/text/StringBuilder.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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). |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 35 | class Pair FINAL : public RefCountedWillBeGarbageCollected<Pair> { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 36 | public: |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 37 | enum IdenticalValuesPolicy { DropIdenticalValues, KeepIdenticalValues }; |
| 38 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 39 | static PassRefPtrWillBeRawPtr<Pair> create(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> first, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> second, |
| 40 | IdenticalValuesPolicy identicalValuesPolicy) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 41 | { |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 42 | return adoptRefWillBeNoop(new Pair(first, second, identicalValuesPolicy)); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 43 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 44 | |
| 45 | CSSPrimitiveValue* first() const { return m_first.get(); } |
| 46 | CSSPrimitiveValue* second() const { return m_second.get(); } |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 47 | IdenticalValuesPolicy identicalValuesPolicy() const { return m_identicalValuesPolicy; } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 48 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 49 | void setFirst(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> first) { m_first = first; } |
| 50 | void setSecond(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> second) { m_second = second; } |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 51 | void setIdenticalValuesPolicy(IdenticalValuesPolicy identicalValuesPolicy) { m_identicalValuesPolicy = identicalValuesPolicy; } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 52 | |
| 53 | String cssText() const |
| 54 | { |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 55 | return generateCSSString(first()->cssText(), second()->cssText(), m_identicalValuesPolicy); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 58 | bool equals(const Pair& other) const |
| 59 | { |
| 60 | return compareCSSValuePtr(m_first, other.m_first) |
| 61 | && compareCSSValuePtr(m_second, other.m_second) |
| 62 | && m_identicalValuesPolicy == other.m_identicalValuesPolicy; |
| 63 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 64 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 65 | void trace(Visitor*); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 66 | |
| 67 | private: |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 68 | Pair() |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 69 | : m_first(nullptr) |
| 70 | , m_second(nullptr) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 71 | , m_identicalValuesPolicy(DropIdenticalValues) { } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 72 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 73 | Pair(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> first, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> second, IdenticalValuesPolicy identicalValuesPolicy) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 74 | : m_first(first) |
| 75 | , m_second(second) |
| 76 | , m_identicalValuesPolicy(identicalValuesPolicy) { } |
| 77 | |
| 78 | static String generateCSSString(const String& first, const String& second, IdenticalValuesPolicy identicalValuesPolicy) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 79 | { |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 80 | if (identicalValuesPolicy == DropIdenticalValues && first == second) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 81 | return first; |
| 82 | return first + ' ' + second; |
| 83 | } |
| 84 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 85 | RefPtrWillBeMember<CSSPrimitiveValue> m_first; |
| 86 | RefPtrWillBeMember<CSSPrimitiveValue> m_second; |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 87 | IdenticalValuesPolicy m_identicalValuesPolicy; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | #endif |