blob: e36d8d42405b7f81b7cdad88ccb9e84e39112311 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 Murdoche69819b2013-07-17 14:56:49 +010025#include "wtf/PassRefPtr.h"
26#include "wtf/RefCounted.h"
27#include "wtf/text/StringBuilder.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010028
29namespace 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)09380292014-02-21 12:17:33 +000035class Pair FINAL : public RefCountedWillBeGarbageCollected<Pair> {
36 DECLARE_GC_INFO;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010037public:
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010038 enum IdenticalValuesPolicy { DropIdenticalValues, KeepIdenticalValues };
39
Torne (Richard Coles)09380292014-02-21 12:17:33 +000040 static PassRefPtrWillBeRawPtr<Pair> create(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> first, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> second,
41 IdenticalValuesPolicy identicalValuesPolicy)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010042 {
Torne (Richard Coles)09380292014-02-21 12:17:33 +000043 return adoptRefWillBeNoop(new Pair(first, second, identicalValuesPolicy));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010044 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010045
46 CSSPrimitiveValue* first() const { return m_first.get(); }
47 CSSPrimitiveValue* second() const { return m_second.get(); }
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010048 IdenticalValuesPolicy identicalValuesPolicy() const { return m_identicalValuesPolicy; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010049
Torne (Richard Coles)09380292014-02-21 12:17:33 +000050 void setFirst(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> first) { m_first = first; }
51 void setSecond(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> second) { m_second = second; }
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010052 void setIdenticalValuesPolicy(IdenticalValuesPolicy identicalValuesPolicy) { m_identicalValuesPolicy = identicalValuesPolicy; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010053
54 String cssText() const
55 {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010056 return generateCSSString(first()->cssText(), second()->cssText(), m_identicalValuesPolicy);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010057 }
58
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010059 bool equals(const Pair& other) const
60 {
61 return compareCSSValuePtr(m_first, other.m_first)
62 && compareCSSValuePtr(m_second, other.m_second)
63 && m_identicalValuesPolicy == other.m_identicalValuesPolicy;
64 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010065
Torne (Richard Coles)09380292014-02-21 12:17:33 +000066 void trace(Visitor*);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010067
68private:
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010069 Pair()
70 : m_first(0)
71 , m_second(0)
72 , m_identicalValuesPolicy(DropIdenticalValues) { }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010073
Torne (Richard Coles)09380292014-02-21 12:17:33 +000074 Pair(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> first, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> second, IdenticalValuesPolicy identicalValuesPolicy)
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010075 : m_first(first)
76 , m_second(second)
77 , m_identicalValuesPolicy(identicalValuesPolicy) { }
78
79 static String generateCSSString(const String& first, const String& second, IdenticalValuesPolicy identicalValuesPolicy)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010080 {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010081 if (identicalValuesPolicy == DropIdenticalValues && first == second)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010082 return first;
83 return first + ' ' + second;
84 }
85
Torne (Richard Coles)09380292014-02-21 12:17:33 +000086 RefPtrWillBeMember<CSSPrimitiveValue> m_first;
87 RefPtrWillBeMember<CSSPrimitiveValue> m_second;
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010088 IdenticalValuesPolicy m_identicalValuesPolicy;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010089};
90
91} // namespace
92
93#endif