Title: [112765] trunk/Source/WebCore
Revision
112765
Author
[email protected]
Date
2012-03-30 19:46:08 -0700 (Fri, 30 Mar 2012)

Log Message

styleForElement() should use enums instead of bools so we can all understand what it's doing
https://bugs.webkit.org/show_bug.cgi?id=82807

Reviewed by Adam Barth.

No change in behavior, thus no tests.

Mostly this is just replacing true/false with the correct new enum value
or removing true/false from the callsites when they would have been default anyway.
I think this makes the code *way* more clear.

The old code was extra confusing because the defaults were "true, false". :)
The new defaults are AllowStyleSharing, MatchAllRules.
It's very uncommon for callers to want to override either of these behaviors.
I think most callers which specify DisallowStyleSharing likely don't actually need to
(our style-sharing code should be smart enough to only share when safe anyway).

* css/CSSStyleSelector.cpp:
(WebCore::CSSStyleSelector::CSSStyleSelector):
 - Use enums and remove bogus comment (m_rootDefaultStyle is a RefPtr!)
(WebCore::CSSStyleSelector::collectMatchingRulesForList):
* css/CSSStyleSelector.h:
(CSSStyleSelector):
* css/MediaQueryMatcher.cpp:
(WebCore::MediaQueryMatcher::prepareEvaluator):
* css/StyleMedia.cpp:
(WebCore::StyleMedia::matchMedium):
* dom/Element.cpp:
(WebCore::Element::styleForRenderer):
* rendering/RenderObject.cpp:
(WebCore::RenderObject::getUncachedPseudoStyle):
 - Updated to use enums
 - Also fixed this to use toElement and modern early-return styles.
* rendering/RenderRegion.cpp:
(WebCore::RenderRegion::computeStyleInRegion):
* svg/SVGElement.cpp:
(WebCore::SVGElement::customStyleForRenderer):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (112764 => 112765)


--- trunk/Source/WebCore/ChangeLog	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/ChangeLog	2012-03-31 02:46:08 UTC (rev 112765)
@@ -1,3 +1,42 @@
+2012-03-30  Eric Seidel  <[email protected]>
+        styleForElement() should use enums instead of bools so we can all understand what it's doing
+        https://bugs.webkit.org/show_bug.cgi?id=82807
+
+        Reviewed by Adam Barth.
+
+        No change in behavior, thus no tests.
+
+        Mostly this is just replacing true/false with the correct new enum value
+        or removing true/false from the callsites when they would have been default anyway.
+        I think this makes the code *way* more clear.
+
+        The old code was extra confusing because the defaults were "true, false". :)
+        The new defaults are AllowStyleSharing, MatchAllRules.
+        It's very uncommon for callers to want to override either of these behaviors.
+        I think most callers which specify DisallowStyleSharing likely don't actually need to
+        (our style-sharing code should be smart enough to only share when safe anyway).
+
+        * css/CSSStyleSelector.cpp:
+        (WebCore::CSSStyleSelector::CSSStyleSelector):
+         - Use enums and remove bogus comment (m_rootDefaultStyle is a RefPtr!)
+        (WebCore::CSSStyleSelector::collectMatchingRulesForList):
+        * css/CSSStyleSelector.h:
+        (CSSStyleSelector):
+        * css/MediaQueryMatcher.cpp:
+        (WebCore::MediaQueryMatcher::prepareEvaluator):
+        * css/StyleMedia.cpp:
+        (WebCore::StyleMedia::matchMedium):
+        * dom/Element.cpp:
+        (WebCore::Element::styleForRenderer):
+        * rendering/RenderObject.cpp:
+        (WebCore::RenderObject::getUncachedPseudoStyle):
+         - Updated to use enums
+         - Also fixed this to use toElement and modern early-return styles.
+        * rendering/RenderRegion.cpp:
+        (WebCore::RenderRegion::computeStyleInRegion):
+        * svg/SVGElement.cpp:
+        (WebCore::SVGElement::customStyleForRenderer):
+
 2012-03-30  Jason Liu  <[email protected]>
 
         [BlackBerry] Headers with no content shouldn't be dropped by platform's request.

Modified: trunk/Source/WebCore/css/CSSStyleSelector.cpp (112764 => 112765)


--- trunk/Source/WebCore/css/CSSStyleSelector.cpp	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/css/CSSStyleSelector.cpp	2012-03-31 02:46:08 UTC (rev 112765)
@@ -375,7 +375,7 @@
         m_medium = adoptPtr(new MediaQueryEvaluator("all"));
 
     if (root)
-        m_rootDefaultStyle = styleForElement(root, 0, false, true); // don't ref, because the RenderStyle is allocated from global heap
+        m_rootDefaultStyle = styleForElement(root, 0, DisallowStyleSharing, MatchOnlyUserAgentRules);
 
     if (m_rootDefaultStyle && view)
         m_medium = adoptPtr(new MediaQueryEvaluator(view->mediaType(), view->frame(), m_rootDefaultStyle.get()));
@@ -1567,14 +1567,12 @@
     return parentNode && parentNode->isShadowRoot();
 }
 
-// If resolveForRootDefault is true, style based on user agent style sheet only. This is used in media queries, where
-// relative units are interpreted according to document root element style, styled only with UA stylesheet
-
-PassRefPtr<RenderStyle> CSSStyleSelector::styleForElement(Element* element, RenderStyle* defaultParent, bool allowSharing, bool resolveForRootDefault, RenderRegion* regionForStyling)
+PassRefPtr<RenderStyle> CSSStyleSelector::styleForElement(Element* element, RenderStyle* defaultParent,
+    StyleSharingBehavior sharingBehavior, RuleMatchingBehavior matchingBehavior, RenderRegion* regionForStyling)
 {
     // Once an element has a renderer, we don't try to destroy it, since otherwise the renderer
     // will vanish if a style recalc happens during loading.
-    if (allowSharing && !element->document()->haveStylesheetsLoaded() && !element->renderer()) {
+    if (sharingBehavior == AllowStyleSharing && !element->document()->haveStylesheetsLoaded() && !element->renderer()) {
         if (!s_styleNotYetAvailable) {
             s_styleNotYetAvailable = RenderStyle::create().leakRef();
             s_styleNotYetAvailable->setDisplay(NONE);
@@ -1587,7 +1585,7 @@
     initElement(element);
     initForStyleResolve(element, defaultParent);
     m_regionForStyling = regionForStyling;
-    if (allowSharing) {
+    if (sharingBehavior == AllowStyleSharing) {
         RenderStyle* sharedStyle = locateSharedStyle();
         if (sharedStyle)
             return sharedStyle;
@@ -1615,7 +1613,7 @@
     ensureDefaultStyleSheetsForElement(element);
 
     MatchResult matchResult;
-    if (resolveForRootDefault)
+    if (matchingBehavior == MatchOnlyUserAgentRules)
         matchUARules(matchResult);
     else
         matchAllRules(matchResult);

Modified: trunk/Source/WebCore/css/CSSStyleSelector.h (112764 => 112765)


--- trunk/Source/WebCore/css/CSSStyleSelector.h	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/css/CSSStyleSelector.h	2012-03-31 02:46:08 UTC (rev 112765)
@@ -100,6 +100,20 @@
     bool m_result;
 };
 
+enum StyleSharingBehavior {
+    AllowStyleSharing,
+    DisallowStyleSharing,
+};
+
+// MatchOnlyUserAgentRules is used in media queries, where relative units
+// are interpreted according to the document root element style, and styled only
+// from the User Agent Stylesheet rules.
+
+enum RuleMatchingBehavior {
+    MatchAllRules,
+    MatchOnlyUserAgentRules,
+};
+
 // This class selects a RenderStyle for a given element based on a collection of stylesheets.
 class CSSStyleSelector {
     WTF_MAKE_NONCOPYABLE(CSSStyleSelector); WTF_MAKE_FAST_ALLOCATED;
@@ -113,7 +127,8 @@
     void pushParentShadowRoot(const ShadowRoot*);
     void popParentShadowRoot(const ShadowRoot*);
 
-    PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, bool allowSharing = true, bool resolveForRootDefault = false, RenderRegion* regionForStyling = 0);
+    PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, StyleSharingBehavior = AllowStyleSharing,
+        RuleMatchingBehavior = MatchAllRules, RenderRegion* regionForStyling = 0);
 
     void keyframeStylesForAnimation(Element*, const RenderStyle*, KeyframeList&);
 

Modified: trunk/Source/WebCore/css/MediaQueryMatcher.cpp (112764 => 112765)


--- trunk/Source/WebCore/css/MediaQueryMatcher.cpp	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/css/MediaQueryMatcher.cpp	2012-03-31 02:46:08 UTC (rev 112765)
@@ -88,7 +88,7 @@
     if (!styleSelector)
         return nullptr;
 
-    RefPtr<RenderStyle> rootStyle = styleSelector->styleForElement(documentElement, 0 /*defaultParent*/, false /*allowSharing*/, true /*resolveForRootDefault*/);
+    RefPtr<RenderStyle> rootStyle = styleSelector->styleForElement(documentElement, 0 /*defaultParent*/, DisallowStyleSharing, MatchOnlyUserAgentRules);
 
     return adoptPtr(new MediaQueryEvaluator(mediaType(), m_document->frame(), rootStyle.get()));
 }

Modified: trunk/Source/WebCore/css/StyleMedia.cpp (112764 => 112765)


--- trunk/Source/WebCore/css/StyleMedia.cpp	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/css/StyleMedia.cpp	2012-03-31 02:46:08 UTC (rev 112765)
@@ -64,7 +64,7 @@
     if (!styleSelector)
         return false;
 
-    RefPtr<RenderStyle> rootStyle = styleSelector->styleForElement(documentElement, 0 /*defaultParent*/, false /*allowSharing*/, true /*resolveForRootDefault*/);
+    RefPtr<RenderStyle> rootStyle = styleSelector->styleForElement(documentElement, 0 /*defaultParent*/, DisallowStyleSharing, MatchOnlyUserAgentRules);
 
     RefPtr<MediaQuerySet> media = MediaQuerySet::create();
     if (!media->parse(query))

Modified: trunk/Source/WebCore/dom/Element.cpp (112764 => 112765)


--- trunk/Source/WebCore/dom/Element.cpp	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/dom/Element.cpp	2012-03-31 02:46:08 UTC (rev 112765)
@@ -1038,7 +1038,7 @@
 {
     if (hasCustomStyleForRenderer())
         return customStyleForRenderer();
-    return document()->styleSelector()->styleForElement(static_cast<Element*>(this), 0, true/*allowSharing*/);
+    return document()->styleSelector()->styleForElement(static_cast<Element*>(this));
 }
 
 void Element::recalcStyle(StyleChange change)

Modified: trunk/Source/WebCore/rendering/RenderObject.cpp (112764 => 112765)


--- trunk/Source/WebCore/rendering/RenderObject.cpp	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/rendering/RenderObject.cpp	2012-03-31 02:46:08 UTC (rev 112765)
@@ -2560,19 +2560,20 @@
         parentStyle = style();
     }
 
+    // FIXME: This "find nearest element parent" should be a helper function.
     Node* n = node();
     while (n && !n->isElementNode())
         n = n->parentNode();
     if (!n)
         return 0;
+    Element* element = toElement(n);
 
-    RefPtr<RenderStyle> result;
     if (pseudo == FIRST_LINE_INHERITED) {
-        result = document()->styleSelector()->styleForElement(static_cast<Element*>(n), parentStyle, false);
+        RefPtr<RenderStyle> result = document()->styleSelector()->styleForElement(element, parentStyle, DisallowStyleSharing);
         result->setStyleType(FIRST_LINE_INHERITED);
-    } else
-        result = document()->styleSelector()->pseudoStyleForElement(pseudo, static_cast<Element*>(n), parentStyle);
-    return result.release();
+        return result.release();
+    }
+    return document()->styleSelector()->pseudoStyleForElement(pseudo, element, parentStyle);
 }
 
 static Color decorationColor(RenderObject* renderer)

Modified: trunk/Source/WebCore/rendering/RenderRegion.cpp (112764 => 112765)


--- trunk/Source/WebCore/rendering/RenderRegion.cpp	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/rendering/RenderRegion.cpp	2012-03-31 02:46:08 UTC (rev 112765)
@@ -300,7 +300,7 @@
     ASSERT(box->node() && box->node()->isElementNode());
 
     Element* element = toElement(box->node());
-    RefPtr<RenderStyle> renderBoxRegionStyle = box->view()->document()->styleSelector()->styleForElement(element, 0, false, false, this);
+    RefPtr<RenderStyle> renderBoxRegionStyle = box->view()->document()->styleSelector()->styleForElement(element, 0, DisallowStyleSharing, MatchAllRules, this);
     m_renderBoxRegionStyle.add(box, renderBoxRegionStyle);
 
     if (!box->hasBoxDecorations()) {

Modified: trunk/Source/WebCore/svg/SVGElement.cpp (112764 => 112765)


--- trunk/Source/WebCore/svg/SVGElement.cpp	2012-03-31 02:36:35 UTC (rev 112764)
+++ trunk/Source/WebCore/svg/SVGElement.cpp	2012-03-31 02:46:08 UTC (rev 112765)
@@ -478,7 +478,7 @@
 PassRefPtr<RenderStyle> SVGElement::customStyleForRenderer()
 {
     if (!correspondingElement())
-        return document()->styleSelector()->styleForElement(static_cast<Element*>(this), 0, true);
+        return document()->styleSelector()->styleForElement(this);
 
     RenderStyle* style = 0;
     if (Element* parent = parentOrHostElement()) {
@@ -486,7 +486,7 @@
             style = renderer->style();
     }
 
-    return document()->styleSelector()->styleForElement(correspondingElement(), style, false /*allowSharing*/);
+    return document()->styleSelector()->styleForElement(correspondingElement(), style, DisallowStyleSharing);
 }
 
 StylePropertySet* SVGElement::animatedSMILStyleProperties() const
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to