1/*
2 * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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#pragma once
22
23#include "CSSPrimitiveValue.h"
24#include <wtf/RefPtr.h>
25#include <wtf/text/StringBuilder.h>
26
27namespace WebCore {
28
29class RectBase {
30public:
31 CSSPrimitiveValue* top() const { return m_top.get(); }
32 CSSPrimitiveValue* right() const { return m_right.get(); }
33 CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
34 CSSPrimitiveValue* left() const { return m_left.get(); }
35
36 void setTop(RefPtr<CSSPrimitiveValue>&& top) { m_top = WTFMove(top); }
37 void setRight(RefPtr<CSSPrimitiveValue>&& right) { m_right = WTFMove(right); }
38 void setBottom(RefPtr<CSSPrimitiveValue>&& bottom) { m_bottom = WTFMove(bottom); }
39 void setLeft(RefPtr<CSSPrimitiveValue>&& left) { m_left = WTFMove(left); }
40
41 bool equals(const RectBase& other) const
42 {
43 return compareCSSValuePtr(m_top, other.m_top)
44 && compareCSSValuePtr(m_right, other.m_right)
45 && compareCSSValuePtr(m_left, other.m_left)
46 && compareCSSValuePtr(m_bottom, other.m_bottom);
47 }
48
49protected:
50 RectBase() = default;
51 ~RectBase() = default;
52
53private:
54 RefPtr<CSSPrimitiveValue> m_top;
55 RefPtr<CSSPrimitiveValue> m_right;
56 RefPtr<CSSPrimitiveValue> m_bottom;
57 RefPtr<CSSPrimitiveValue> m_left;
58};
59
60class Rect final : public RectBase, public RefCounted<Rect> {
61public:
62 static Ref<Rect> create() { return adoptRef(*new Rect); }
63
64 String cssText() const
65 {
66 return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
67 }
68
69private:
70 Rect() = default;
71 static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
72 {
73 return "rect(" + top + ", " + right + ", " + bottom + ", " + left + ')';
74 }
75};
76
77class Quad final : public RectBase, public RefCounted<Quad> {
78public:
79 static Ref<Quad> create() { return adoptRef(*new Quad); }
80
81 String cssText() const
82 {
83 return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
84 }
85
86private:
87 Quad() = default;
88 static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
89 {
90 StringBuilder result;
91 // reserve space for the four strings, plus three space separator characters.
92 result.reserveCapacity(top.length() + right.length() + bottom.length() + left.length() + 3);
93 result.append(top);
94 if (right != top || bottom != top || left != top) {
95 result.append(' ');
96 result.append(right);
97 if (bottom != top || right != left) {
98 result.append(' ');
99 result.append(bottom);
100 if (left != right) {
101 result.append(' ');
102 result.append(left);
103 }
104 }
105 }
106 return result.toString();
107 }
108};
109
110} // namespace WebCore
111