1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Peter Kelly (pmk@post.com)
5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2003-2018 Apple Inc. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#pragma once
26
27#include "CSSPrimitiveValue.h"
28#include "CSSPropertyNames.h"
29#include "CSSValueKeywords.h"
30#include "Element.h"
31
32namespace WebCore {
33
34class Attribute;
35class MutableStyleProperties;
36class PropertySetCSSStyleDeclaration;
37class StyleProperties;
38class StylePropertyMap;
39
40class StyledElement : public Element {
41 WTF_MAKE_ISO_ALLOCATED(StyledElement);
42public:
43 virtual ~StyledElement();
44
45 virtual const StyleProperties* additionalPresentationAttributeStyle() const { return nullptr; }
46 void invalidateStyleAttribute();
47
48 const StyleProperties* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : nullptr; }
49
50 bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
51 bool setInlineStyleProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
52 WEBCORE_EXPORT bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitType, bool important = false);
53 WEBCORE_EXPORT bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
54 bool removeInlineStyleProperty(CSSPropertyID);
55 void removeAllInlineStyleProperties();
56
57 static void synchronizeStyleAttributeInternal(StyledElement*);
58 void synchronizeStyleAttributeInternal() const { StyledElement::synchronizeStyleAttributeInternal(const_cast<StyledElement*>(this)); }
59
60 WEBCORE_EXPORT CSSStyleDeclaration& cssomStyle();
61#if ENABLE(CSS_TYPED_OM)
62 StylePropertyMap& ensureAttributeStyleMap();
63#endif
64
65 const StyleProperties* presentationAttributeStyle() const;
66 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomString&, MutableStyleProperties&) { }
67
68protected:
69 StyledElement(const QualifiedName& name, Document& document, ConstructionType type)
70 : Element(name, document, type)
71 {
72 }
73
74 void attributeChanged(const QualifiedName&, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason = ModifiedDirectly) override;
75
76 virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
77
78 void addPropertyToPresentationAttributeStyle(MutableStyleProperties&, CSSPropertyID, CSSValueID identifier);
79 void addPropertyToPresentationAttributeStyle(MutableStyleProperties&, CSSPropertyID, double value, CSSPrimitiveValue::UnitType);
80 void addPropertyToPresentationAttributeStyle(MutableStyleProperties&, CSSPropertyID, const String& value);
81
82 void addSubresourceAttributeURLs(ListHashSet<URL>&) const override;
83
84private:
85 void styleAttributeChanged(const AtomString& newStyleString, AttributeModificationReason);
86
87 void inlineStyleChanged();
88 PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
89 void setInlineStyleFromString(const AtomString&);
90 MutableStyleProperties& ensureMutableInlineStyle();
91
92 void rebuildPresentationAttributeStyle();
93};
94
95inline const StyleProperties* StyledElement::presentationAttributeStyle() const
96{
97 if (!elementData())
98 return nullptr;
99 if (elementData()->presentationAttributeStyleIsDirty())
100 const_cast<StyledElement&>(*this).rebuildPresentationAttributeStyle();
101 return elementData()->presentationAttributeStyle();
102}
103
104} // namespace WebCore
105
106SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyledElement)
107 static bool isType(const WebCore::Node& node) { return node.isStyledElement(); }
108SPECIALIZE_TYPE_TRAITS_END()
109