Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / resolver / StyleResolverState.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 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
22 #ifndef StyleResolverState_h
23 #define StyleResolverState_h
24
25 #include "CSSPropertyNames.h"
26
27 #include "core/css/CSSSVGDocumentValue.h"
28 #include "core/css/CSSToLengthConversionData.h"
29 #include "core/css/resolver/CSSToStyleMap.h"
30 #include "core/css/resolver/ElementResolveContext.h"
31 #include "core/css/resolver/ElementStyleResources.h"
32 #include "core/css/resolver/FontBuilder.h"
33 #include "core/dom/Element.h"
34 #include "core/rendering/style/CachedUAStyle.h"
35 #include "core/rendering/style/RenderStyle.h"
36 #include "core/rendering/style/StyleInheritedData.h"
37
38 namespace WebCore {
39
40 class CSSAnimationUpdate;
41 class FontDescription;
42 class StyleRule;
43
44 class StyleResolverState {
45     STACK_ALLOCATED();
46     WTF_MAKE_NONCOPYABLE(StyleResolverState);
47 public:
48     StyleResolverState(Document&, Element*, RenderStyle* parentStyle = 0);
49     ~StyleResolverState();
50
51     // In FontFaceSet and CanvasRenderingContext2D, we don't have an element to grab the document from.
52     // This is why we have to store the document separately.
53     Document& document() const { return m_document; }
54     // These are all just pass-through methods to ElementResolveContext.
55     Element* element() const { return m_elementContext.element(); }
56     const ContainerNode* parentNode() const { return m_elementContext.parentNode(); }
57     const RenderStyle* rootElementStyle() const { return m_elementContext.rootElementStyle(); }
58     EInsideLink elementLinkState() const { return m_elementContext.elementLinkState(); }
59     bool distributedToInsertionPoint() const { return m_elementContext.distributedToInsertionPoint(); }
60
61     const ElementResolveContext& elementContext() const { return m_elementContext; }
62
63     void setStyle(PassRefPtr<RenderStyle> style) { m_style = style; m_cssToLengthConversionData.setStyle(m_style.get()); }
64     const RenderStyle* style() const { return m_style.get(); }
65     RenderStyle* style() { return m_style.get(); }
66     PassRefPtr<RenderStyle> takeStyle() { return m_style.release(); }
67
68     const CSSToLengthConversionData& cssToLengthConversionData() const { return m_cssToLengthConversionData; }
69
70     void setAnimationUpdate(PassOwnPtrWillBeRawPtr<CSSAnimationUpdate>);
71     const CSSAnimationUpdate* animationUpdate() { return m_animationUpdate.get(); }
72     PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> takeAnimationUpdate();
73
74     void setParentStyle(PassRefPtr<RenderStyle> parentStyle) { m_parentStyle = parentStyle; }
75     const RenderStyle* parentStyle() const { return m_parentStyle.get(); }
76     RenderStyle* parentStyle() { return m_parentStyle.get(); }
77
78     void setCurrentRule(StyleRule* currentRule) { m_currentRule = currentRule; }
79     const StyleRule* currentRule() const { return m_currentRule; }
80
81     // FIXME: These are effectively side-channel "out parameters" for the various
82     // map functions. When we map from CSS to style objects we use this state object
83     // to track various meta-data about that mapping (e.g. if it's cache-able).
84     // We need to move this data off of StyleResolverState and closer to the
85     // objects it applies to. Possibly separating (immutable) inputs from (mutable) outputs.
86     void setApplyPropertyToRegularStyle(bool isApply) { m_applyPropertyToRegularStyle = isApply; }
87     void setApplyPropertyToVisitedLinkStyle(bool isApply) { m_applyPropertyToVisitedLinkStyle = isApply; }
88     bool applyPropertyToRegularStyle() const { return m_applyPropertyToRegularStyle; }
89     bool applyPropertyToVisitedLinkStyle() const { return m_applyPropertyToVisitedLinkStyle; }
90
91     // Holds all attribute names found while applying "content" properties that contain an "attr()" value.
92     Vector<AtomicString>& contentAttrValues() { return m_contentAttrValues; }
93
94     void setLineHeightValue(CSSValue* value) { m_lineHeightValue = value; }
95     CSSValue* lineHeightValue() { return m_lineHeightValue; }
96
97     void cacheUserAgentBorderAndBackground()
98     {
99         // RenderTheme only needs the cached style if it has an appearance,
100         // and constructing it is expensive so we avoid it if possible.
101         if (!style()->hasAppearance())
102             return;
103         m_cachedUAStyle = CachedUAStyle(style());
104     }
105     const CachedUAStyle& cachedUAStyle() const { return m_cachedUAStyle; }
106
107     ElementStyleResources& elementStyleResources() { return m_elementStyleResources; }
108     const CSSToStyleMap& styleMap() const { return m_styleMap; }
109     CSSToStyleMap& styleMap() { return m_styleMap; }
110
111     // FIXME: Once styleImage can be made to not take a StyleResolverState
112     // this convenience function should be removed. As-is, without this, call
113     // sites are extremely verbose.
114     PassRefPtr<StyleImage> styleImage(CSSPropertyID propertyId, CSSValue* value)
115     {
116         return m_elementStyleResources.styleImage(document(), document().textLinkColors(), style()->color(), propertyId, value);
117     }
118
119     FontBuilder& fontBuilder() { return m_fontBuilder; }
120     // FIXME: These exist as a primitive way to track mutations to font-related properties
121     // on a RenderStyle. As designed, these are very error-prone, as some callers
122     // set these directly on the RenderStyle w/o telling us. Presumably we'll
123     // want to design a better wrapper around RenderStyle for tracking these mutations
124     // and separate it from StyleResolverState.
125     const FontDescription& parentFontDescription() { return m_parentStyle->fontDescription(); }
126     void setZoom(float f) { m_fontBuilder.didChangeFontParameters(m_style->setZoom(f)); }
127     void setEffectiveZoom(float f) { m_fontBuilder.didChangeFontParameters(m_style->setEffectiveZoom(f)); }
128     void setWritingMode(WritingMode writingMode) { m_fontBuilder.didChangeFontParameters(m_style->setWritingMode(writingMode)); }
129     void setTextOrientation(TextOrientation textOrientation) { m_fontBuilder.didChangeFontParameters(m_style->setTextOrientation(textOrientation)); }
130
131     // SVG handles zooming in a different way compared to CSS. The whole document is scaled instead
132     // of each individual length value in the render style / tree. CSSPrimitiveValue::computeLength*()
133     // multiplies each resolved length with the zoom multiplier - so for SVG we need to disable that.
134     // Though all CSS values that can be applied to outermost <svg> elements (width/height/border/padding...)
135     // need to respect the scaling. RenderBox (the parent class of RenderSVGRoot) grabs values like
136     // width/height/border/padding/... from the RenderStyle -> for SVG these values would never scale,
137     // if we'd pass a 1.0 zoom factor everyhwere. So we only pass a zoom factor of 1.0 for specific
138     // properties that are NOT allowed to scale within a zoomed SVG document (letter/word-spacing/font-size).
139     bool useSVGZoomRules() const { return element() && element()->isSVGElement(); }
140
141 private:
142     ElementResolveContext m_elementContext;
143     Document& m_document;
144
145     // m_style is the primary output for each element's style resolve.
146     RefPtr<RenderStyle> m_style;
147
148     CSSToLengthConversionData m_cssToLengthConversionData;
149
150     // m_parentStyle is not always just element->parentNode()->style()
151     // so we keep it separate from m_elementContext.
152     RefPtr<RenderStyle> m_parentStyle;
153
154     OwnPtrWillBeMember<CSSAnimationUpdate> m_animationUpdate;
155
156     bool m_applyPropertyToRegularStyle;
157     bool m_applyPropertyToVisitedLinkStyle;
158
159     RawPtrWillBeMember<CSSValue> m_lineHeightValue;
160
161     FontBuilder m_fontBuilder;
162
163     CachedUAStyle m_cachedUAStyle;
164
165     ElementStyleResources m_elementStyleResources;
166     // CSSToStyleMap is a pure-logic class and only contains
167     // a back-pointer to this object.
168     CSSToStyleMap m_styleMap;
169     Vector<AtomicString> m_contentAttrValues;
170
171     RawPtrWillBeMember<StyleRule> m_currentRule;
172 };
173
174 } // namespace WebCore
175
176 #endif // StyleResolverState_h