1/*
2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3 * Copyright (C) 2018-2019 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 "SVGPropertyOwner.h"
24#include <wtf/Optional.h>
25#include <wtf/RefCounted.h>
26#include <wtf/text/WTFString.h>
27
28namespace WebCore {
29
30enum class SVGPropertyAccess : uint8_t { ReadWrite, ReadOnly };
31enum class SVGPropertyState : uint8_t { Clean, Dirty };
32
33class SVGProperty : public RefCounted<SVGProperty> {
34public:
35 virtual ~SVGProperty() = default;
36
37 // Managing the relationship with the owner.
38 bool isAttached() const { return m_owner; }
39 void attach(SVGPropertyOwner* owner, SVGPropertyAccess access)
40 {
41 ASSERT(!m_owner);
42 ASSERT(m_state == SVGPropertyState::Clean);
43 m_owner = owner;
44 m_access = access;
45 }
46
47 void detach()
48 {
49 m_owner = nullptr;
50 m_access = SVGPropertyAccess::ReadWrite;
51 m_state = SVGPropertyState::Clean;
52 }
53
54 const SVGElement* contextElement() const
55 {
56 if (!m_owner)
57 return nullptr;
58 return m_owner->attributeContextElement();
59 }
60
61 void commitChange()
62 {
63 if (!m_owner)
64 return;
65 m_owner->commitPropertyChange(this);
66 }
67
68 // DOM access.
69 SVGPropertyAccess access() const { return m_access; }
70 bool isReadOnly() const { return m_access == SVGPropertyAccess::ReadOnly; }
71
72 // Synchronizing the SVG attribute and its reflection here.
73 bool isDirty() const { return m_state == SVGPropertyState::Dirty; }
74 void setDirty() { m_state = SVGPropertyState::Dirty; }
75 Optional<String> synchronize()
76 {
77 if (m_state == SVGPropertyState::Clean)
78 return WTF::nullopt;
79 m_state = SVGPropertyState::Clean;
80 return valueAsString();
81 }
82
83 // This is used when calling setAttribute().
84 virtual String valueAsString() const { return emptyString(); }
85
86 // Visual Studio doesn't seem to see these private constructors from subclasses.
87 // FIXME: See what it takes to remove this hack.
88#if !COMPILER(MSVC)
89protected:
90#endif
91 SVGProperty(SVGPropertyOwner* owner = nullptr, SVGPropertyAccess access = SVGPropertyAccess::ReadWrite)
92 : m_owner(owner)
93 , m_access(access)
94 {
95 }
96
97 SVGPropertyOwner* m_owner { nullptr };
98 SVGPropertyAccess m_access { SVGPropertyAccess::ReadWrite };
99 SVGPropertyState m_state { SVGPropertyState::Clean };
100};
101
102} // namespace WebCore
103