1/*
2 * Copyright (C) 2004 Zack Rusin <zack@kde.org>
3 * Copyright (C) 2004-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 Lesser 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 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 */
20
21#pragma once
22
23#include "CSSStyleDeclaration.h"
24#include "RenderStyleConstants.h"
25#include "SVGRenderStyleDefs.h"
26#include "TextFlags.h"
27#include <wtf/IsoMalloc.h>
28#include <wtf/RefPtr.h>
29#include <wtf/text/WTFString.h>
30
31namespace WebCore {
32
33class CSSFontStyleValue;
34class CSSPrimitiveValue;
35class CSSValueList;
36class Color;
37class Element;
38class FilterOperations;
39class FontSelectionValue;
40class MutableStyleProperties;
41class Node;
42class RenderElement;
43class RenderStyle;
44class SVGPaint;
45class ShadowData;
46class StyleProperties;
47class StylePropertyShorthand;
48
49enum EUpdateLayout { DoNotUpdateLayout = false, UpdateLayout = true };
50
51enum AdjustPixelValuesForComputedStyle { AdjustPixelValues, DoNotAdjustPixelValues };
52
53class ComputedStyleExtractor {
54 WTF_MAKE_FAST_ALLOCATED;
55public:
56 ComputedStyleExtractor(Node*, bool allowVisitedStyle = false, PseudoId = PseudoId::None);
57 ComputedStyleExtractor(Element*, bool allowVisitedStyle = false, PseudoId = PseudoId::None);
58
59 RefPtr<CSSValue> propertyValue(CSSPropertyID, EUpdateLayout = UpdateLayout);
60 RefPtr<CSSValue> valueForPropertyInStyle(const RenderStyle&, CSSPropertyID, RenderElement* = nullptr);
61 String customPropertyText(const String& propertyName);
62 RefPtr<CSSValue> customPropertyValue(const String& propertyName);
63
64 // Helper methods for HTML editing.
65 Ref<MutableStyleProperties> copyPropertiesInSet(const CSSPropertyID* set, unsigned length);
66 Ref<MutableStyleProperties> copyProperties();
67 RefPtr<CSSPrimitiveValue> getFontSizeCSSValuePreferringKeyword();
68 bool useFixedFontDefaultSize();
69 bool propertyMatches(CSSPropertyID, const CSSValue*);
70
71 static Ref<CSSValue> valueForFilter(const RenderStyle&, const FilterOperations&, AdjustPixelValuesForComputedStyle = AdjustPixelValues);
72
73 static Ref<CSSPrimitiveValue> fontNonKeywordWeightFromStyleValue(FontSelectionValue);
74 static Ref<CSSPrimitiveValue> fontWeightFromStyleValue(FontSelectionValue);
75 static Ref<CSSPrimitiveValue> fontNonKeywordStretchFromStyleValue(FontSelectionValue);
76 static Ref<CSSPrimitiveValue> fontStretchFromStyleValue(FontSelectionValue);
77 static Ref<CSSFontStyleValue> fontNonKeywordStyleFromStyleValue(FontSelectionValue);
78 static Ref<CSSFontStyleValue> fontStyleFromStyleValue(Optional<FontSelectionValue>, FontStyleAxis);
79
80private:
81 // The styled element is either the element passed into
82 // computedPropertyValue, or the PseudoElement for :before and :after if
83 // they exist.
84 Element* styledElement() const;
85
86 // The renderer we should use for resolving layout-dependent properties.
87 // Note that it differs from styledElement()->renderer() in the case we have
88 // no pseudo-element.
89 RenderElement* styledRenderer() const;
90
91 RefPtr<CSSValue> svgPropertyValue(CSSPropertyID);
92 Ref<CSSValue> adjustSVGPaintForCurrentColor(SVGPaintType, const String& url, const Color&, const Color& currentColor) const;
93 static Ref<CSSValue> valueForShadow(const ShadowData*, CSSPropertyID, const RenderStyle&, AdjustPixelValuesForComputedStyle = AdjustPixelValues);
94 Ref<CSSPrimitiveValue> currentColorOrValidColor(const RenderStyle*, const Color&) const;
95
96 Ref<CSSValueList> getCSSPropertyValuesForShorthandProperties(const StylePropertyShorthand&);
97 RefPtr<CSSValueList> getCSSPropertyValuesFor2SidesShorthand(const StylePropertyShorthand&);
98 RefPtr<CSSValueList> getCSSPropertyValuesFor4SidesShorthand(const StylePropertyShorthand&);
99 Ref<CSSValueList> getBackgroundShorthandValue();
100 Ref<CSSValueList> getCSSPropertyValuesForGridShorthand(const StylePropertyShorthand&);
101
102 RefPtr<Element> m_element;
103 PseudoId m_pseudoElementSpecifier;
104 bool m_allowVisitedStyle;
105};
106
107class CSSComputedStyleDeclaration final : public CSSStyleDeclaration {
108 WTF_MAKE_ISO_ALLOCATED_EXPORT(CSSComputedStyleDeclaration, WEBCORE_EXPORT);
109public:
110 static Ref<CSSComputedStyleDeclaration> create(Element& element, bool allowVisitedStyle = false, const String& pseudoElementName = String())
111 {
112 return adoptRef(*new CSSComputedStyleDeclaration(element, allowVisitedStyle, pseudoElementName));
113 }
114 virtual ~CSSComputedStyleDeclaration();
115
116 WEBCORE_EXPORT void ref() final;
117 WEBCORE_EXPORT void deref() final;
118
119 String getPropertyValue(CSSPropertyID) const;
120
121private:
122 WEBCORE_EXPORT CSSComputedStyleDeclaration(Element&, bool allowVisitedStyle, const String&);
123
124 // CSSOM functions. Don't make these public.
125 CSSRule* parentRule() const final;
126 unsigned length() const final;
127 String item(unsigned index) const final;
128 RefPtr<DeprecatedCSSOMValue> getPropertyCSSValue(const String& propertyName) final;
129 String getPropertyValue(const String& propertyName) final;
130 String getPropertyPriority(const String& propertyName) final;
131 String getPropertyShorthand(const String& propertyName) final;
132 bool isPropertyImplicit(const String& propertyName) final;
133 ExceptionOr<void> setProperty(const String& propertyName, const String& value, const String& priority) final;
134 ExceptionOr<String> removeProperty(const String& propertyName) final;
135 String cssText() const final;
136 ExceptionOr<void> setCssText(const String&) final;
137 RefPtr<CSSValue> getPropertyCSSValueInternal(CSSPropertyID) final;
138 String getPropertyValueInternal(CSSPropertyID) final;
139 ExceptionOr<bool> setPropertyInternal(CSSPropertyID, const String& value, bool important) final;
140 Ref<MutableStyleProperties> copyProperties() const final;
141
142 RefPtr<CSSValue> getPropertyCSSValue(CSSPropertyID, EUpdateLayout = UpdateLayout) const;
143
144 mutable Ref<Element> m_element;
145 PseudoId m_pseudoElementSpecifier;
146 bool m_allowVisitedStyle;
147 unsigned m_refCount;
148};
149
150} // namespace WebCore
151