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