blob: 2c1940f0b5267cd1b4aeda31a481b5c1360edd85 [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).
35class Pair : public RefCounted<Pair> {
36public:
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010037 enum IdenticalValuesPolicy { DropIdenticalValues, KeepIdenticalValues };
38
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010039 static PassRefPtr<Pair> create()
40 {
41 return adoptRef(new Pair);
42 }
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010043 static PassRefPtr<Pair> create(PassRefPtr<CSSPrimitiveValue> first, PassRefPtr<CSSPrimitiveValue> second, IdenticalValuesPolicy identicalValuesPolicy)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010044 {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010045 return adoptRef(new Pair(first, second, identicalValuesPolicy));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010046 }
47 virtual ~Pair() { }
48
49 CSSPrimitiveValue* first() const { return m_first.get(); }
50 CSSPrimitiveValue* second() const { return m_second.get(); }
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010051 IdenticalValuesPolicy identicalValuesPolicy() const { return m_identicalValuesPolicy; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010052
53 void setFirst(PassRefPtr<CSSPrimitiveValue> first) { m_first = first; }
54 void setSecond(PassRefPtr<CSSPrimitiveValue> second) { m_second = second; }
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010055 void setIdenticalValuesPolicy(IdenticalValuesPolicy identicalValuesPolicy) { m_identicalValuesPolicy = identicalValuesPolicy; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010056
57 String cssText() const
58 {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010059 return generateCSSString(first()->cssText(), second()->cssText(), m_identicalValuesPolicy);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010060 }
61
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010062 bool equals(const Pair& other) const
63 {
64 return compareCSSValuePtr(m_first, other.m_first)
65 && compareCSSValuePtr(m_second, other.m_second)
66 && m_identicalValuesPolicy == other.m_identicalValuesPolicy;
67 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010068
69 String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
70 {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010071 return generateCSSString(
72 first()->customSerializeResolvingVariables(variables),
73 second()->customSerializeResolvingVariables(variables),
74 m_identicalValuesPolicy);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010075 }
Ben Murdoch02772c62013-07-26 10:21:05 +010076
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010077 bool hasVariableReference() const { return first()->hasVariableReference() || second()->hasVariableReference(); }
78
79private:
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010080 Pair()
81 : m_first(0)
82 , m_second(0)
83 , m_identicalValuesPolicy(DropIdenticalValues) { }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010084
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010085 Pair(PassRefPtr<CSSPrimitiveValue> first, PassRefPtr<CSSPrimitiveValue> second, IdenticalValuesPolicy identicalValuesPolicy)
86 : m_first(first)
87 , m_second(second)
88 , m_identicalValuesPolicy(identicalValuesPolicy) { }
89
90 static String generateCSSString(const String& first, const String& second, IdenticalValuesPolicy identicalValuesPolicy)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010091 {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010092 if (identicalValuesPolicy == DropIdenticalValues && first == second)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010093 return first;
94 return first + ' ' + second;
95 }
96
97 RefPtr<CSSPrimitiveValue> m_first;
98 RefPtr<CSSPrimitiveValue> m_second;
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010099 IdenticalValuesPolicy m_identicalValuesPolicy;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100100};
101
102} // namespace
103
104#endif