181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)/*
281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) *
581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * This library is free software; you can redistribute it and/or
681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * modify it under the terms of the GNU Library General Public
781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * License as published by the Free Software Foundation; either
881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * version 2 of the License, or (at your option) any later version.
981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) *
1081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * This library is distributed in the hope that it will be useful,
1181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * but WITHOUT ANY WARRANTY; without even the implied warranty of
1281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * Library General Public License for more details.
1481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) *
1581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * You should have received a copy of the GNU Library General Public License
1681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * along with this library; see the file COPYING.LIB.  If not, write to
1781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
1881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * Boston, MA 02110-1301, USA.
1981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) *
2081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) */
2181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
2281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#ifndef StyleResolverState_h
2381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#define StyleResolverState_h
2481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
255d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)#include "core/CSSPropertyNames.h"
2681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "core/css/CSSSVGDocumentValue.h"
28a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "core/css/CSSToLengthConversionData.h"
29a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "core/css/resolver/CSSToStyleMap.h"
30fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch#include "core/css/resolver/ElementResolveContext.h"
31591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "core/css/resolver/ElementStyleResources.h"
320019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch#include "core/css/resolver/FontBuilder.h"
33197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "core/dom/Document.h"
3481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#include "core/dom/Element.h"
35e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#include "core/rendering/style/CachedUAStyle.h"
3681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#include "core/rendering/style/RenderStyle.h"
3781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#include "core/rendering/style/StyleInheritedData.h"
3881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
39c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
4081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)class CSSAnimationUpdate;
4281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)class FontDescription;
4381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
4481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)class StyleResolverState {
4507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    STACK_ALLOCATED();
4607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    WTF_MAKE_NONCOPYABLE(StyleResolverState);
4781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)public:
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    StyleResolverState(Document&, Element*, RenderStyle* parentStyle = 0);
4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ~StyleResolverState();
5081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // In FontFaceSet and CanvasRenderingContext2D, we don't have an element to grab the document from.
52f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    // This is why we have to store the document separately.
53c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    Document& document() const { return *m_document; }
54e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // These are all just pass-through methods to ElementResolveContext.
55e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    Element* element() const { return m_elementContext.element(); }
56e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    const ContainerNode* parentNode() const { return m_elementContext.parentNode(); }
5702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    const RenderStyle* rootElementStyle() const { return m_elementContext.rootElementStyle(); }
58e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    EInsideLink elementLinkState() const { return m_elementContext.elementLinkState(); }
59e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    bool distributedToInsertionPoint() const { return m_elementContext.distributedToInsertionPoint(); }
6081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
61e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    const ElementResolveContext& elementContext() const { return m_elementContext; }
6281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    void setStyle(PassRefPtr<RenderStyle> style) { m_style = style; m_cssToLengthConversionData.setStyle(m_style.get()); }
6402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    const RenderStyle* style() const { return m_style.get(); }
6502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    RenderStyle* style() { return m_style.get(); }
6681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    PassRefPtr<RenderStyle> takeStyle() { return m_style.release(); }
6781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
68a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    const CSSToLengthConversionData& cssToLengthConversionData() const { return m_cssToLengthConversionData; }
69a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
7007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    void setAnimationUpdate(PassOwnPtrWillBeRawPtr<CSSAnimationUpdate>);
71c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles)    const CSSAnimationUpdate* animationUpdate() { return m_animationUpdate.get(); }
7207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> takeAnimationUpdate();
73c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles)
7481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    void setParentStyle(PassRefPtr<RenderStyle> parentStyle) { m_parentStyle = parentStyle; }
7502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    const RenderStyle* parentStyle() const { return m_parentStyle.get(); }
7602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    RenderStyle* parentStyle() { return m_parentStyle.get(); }
7781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
78e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // FIXME: These are effectively side-channel "out parameters" for the various
79e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // map functions. When we map from CSS to style objects we use this state object
80e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // to track various meta-data about that mapping (e.g. if it's cache-able).
81e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // We need to move this data off of StyleResolverState and closer to the
82e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // objects it applies to. Possibly separating (immutable) inputs from (mutable) outputs.
8381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    void setApplyPropertyToRegularStyle(bool isApply) { m_applyPropertyToRegularStyle = isApply; }
8481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    void setApplyPropertyToVisitedLinkStyle(bool isApply) { m_applyPropertyToVisitedLinkStyle = isApply; }
8581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    bool applyPropertyToRegularStyle() const { return m_applyPropertyToRegularStyle; }
8681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    bool applyPropertyToVisitedLinkStyle() const { return m_applyPropertyToVisitedLinkStyle; }
8781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
88f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    // Holds all attribute names found while applying "content" properties that contain an "attr()" value.
89f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    Vector<AtomicString>& contentAttrValues() { return m_contentAttrValues; }
90f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
9181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    void setLineHeightValue(CSSValue* value) { m_lineHeightValue = value; }
9281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    CSSValue* lineHeightValue() { return m_lineHeightValue; }
9381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
9443e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)    void cacheUserAgentBorderAndBackground()
9543e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)    {
9643e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)        // RenderTheme only needs the cached style if it has an appearance,
9743e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)        // and constructing it is expensive so we avoid it if possible.
9843e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)        if (!style()->hasAppearance())
9943e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)            return;
1005d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
1015d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        m_cachedUAStyle = CachedUAStyle::create(style());
1025d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    }
1035d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
1045d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    const CachedUAStyle* cachedUAStyle() const
1055d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    {
1065d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        return m_cachedUAStyle.get();
10743e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)    }
10881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
109e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    ElementStyleResources& elementStyleResources() { return m_elementStyleResources; }
110e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    const CSSToStyleMap& styleMap() const { return m_styleMap; }
111e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    CSSToStyleMap& styleMap() { return m_styleMap; }
112e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
113e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // FIXME: Once styleImage can be made to not take a StyleResolverState
114e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // this convenience function should be removed. As-is, without this, call
115e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // sites are extremely verbose.
116e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    PassRefPtr<StyleImage> styleImage(CSSPropertyID propertyId, CSSValue* value)
117e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    {
118f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        return m_elementStyleResources.styleImage(document(), document().textLinkColors(), style()->color(), propertyId, value);
119e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    }
120e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
1210019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch    FontBuilder& fontBuilder() { return m_fontBuilder; }
122e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // FIXME: These exist as a primitive way to track mutations to font-related properties
123e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // on a RenderStyle. As designed, these are very error-prone, as some callers
124e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // set these directly on the RenderStyle w/o telling us. Presumably we'll
125e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // want to design a better wrapper around RenderStyle for tracking these mutations
126e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // and separate it from StyleResolverState.
12781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    const FontDescription& parentFontDescription() { return m_parentStyle->fontDescription(); }
1280019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch    void setZoom(float f) { m_fontBuilder.didChangeFontParameters(m_style->setZoom(f)); }
1290019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch    void setEffectiveZoom(float f) { m_fontBuilder.didChangeFontParameters(m_style->setEffectiveZoom(f)); }
1300019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch    void setWritingMode(WritingMode writingMode) { m_fontBuilder.didChangeFontParameters(m_style->setWritingMode(writingMode)); }
1310019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch    void setTextOrientation(TextOrientation textOrientation) { m_fontBuilder.didChangeFontParameters(m_style->setTextOrientation(textOrientation)); }
13281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
13381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)private:
134e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    ElementResolveContext m_elementContext;
135c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    RawPtrWillBeMember<Document> m_document;
136e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
137e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // m_style is the primary output for each element's style resolve.
13881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    RefPtr<RenderStyle> m_style;
139e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
140a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    CSSToLengthConversionData m_cssToLengthConversionData;
141a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
142e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // m_parentStyle is not always just element->parentNode()->style()
143e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // so we keep it separate from m_elementContext.
14481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    RefPtr<RenderStyle> m_parentStyle;
14581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
14607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    OwnPtrWillBeMember<CSSAnimationUpdate> m_animationUpdate;
147c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles)
14881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    bool m_applyPropertyToRegularStyle;
14981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    bool m_applyPropertyToVisitedLinkStyle;
15081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
15107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    RawPtrWillBeMember<CSSValue> m_lineHeightValue;
152e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
1530019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch    FontBuilder m_fontBuilder;
15481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
1555d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    OwnPtr<CachedUAStyle> m_cachedUAStyle;
156e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
157591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch    ElementStyleResources m_elementStyleResources;
158e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // CSSToStyleMap is a pure-logic class and only contains
159e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    // a back-pointer to this object.
160e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    CSSToStyleMap m_styleMap;
161f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    Vector<AtomicString> m_contentAttrValues;
16281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)};
16381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
164c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
16581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
16681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#endif // StyleResolverState_h
167