Bug 1337305 Part 1: Refactor GetRuleNodeForElement to just return the StyleContext, so caller can either get a RuleNode or a raw servo rule.
MozReview-Commit-ID: DBkMfSvOA2k
--- a/layout/inspector/inDOMUtils.cpp
+++ b/layout/inspector/inDOMUtils.cpp
@@ -227,27 +227,28 @@ inDOMUtils::GetCSSStyleRules(nsIDOMEleme
*_retval = nullptr;
nsCOMPtr<nsIAtom> pseudoElt;
if (!aPseudo.IsEmpty()) {
pseudoElt = NS_Atomize(aPseudo);
}
- nsRuleNode* ruleNode = nullptr;
nsCOMPtr<Element> element = do_QueryInterface(aElement);
NS_ENSURE_STATE(element);
- RefPtr<nsStyleContext> styleContext;
- GetRuleNodeForElement(element, pseudoElt, getter_AddRefs(styleContext), &ruleNode);
- if (!ruleNode) {
+ RefPtr<nsStyleContext> styleContext =
+ GetCleanStyleContextForElement(element, pseudoElt);
+ if (!styleContext) {
// This can fail for elements that are not in the document or
// if the document they're in doesn't have a presshell. Bail out.
return NS_OK;
}
+ nsRuleNode* ruleNode = styleContext->RuleNode();
+
AutoTArray<nsRuleNode*, 16> ruleNodes;
while (!ruleNode->IsRoot()) {
ruleNodes.AppendElement(ruleNode);
ruleNode = ruleNode->GetParent();
}
nsCOMPtr<nsIMutableArray> rules = nsArray::Create();
for (nsRuleNode* ruleNode : Reversed(ruleNodes)) {
@@ -1141,45 +1142,42 @@ inDOMUtils::GetContentState(nsIDOMElemen
NS_ENSURE_ARG_POINTER(content);
// NOTE: if this method is removed,
// please remove GetInternalValue from EventStates
*aState = content->AsElement()->State().GetInternalValue();
return NS_OK;
}
-/* static */ nsresult
-inDOMUtils::GetRuleNodeForElement(dom::Element* aElement,
- nsIAtom* aPseudo,
- nsStyleContext** aStyleContext,
- nsRuleNode** aRuleNode)
+/* static */ already_AddRefed<nsStyleContext>
+inDOMUtils::GetCleanStyleContextForElement(dom::Element* aElement,
+ nsIAtom* aPseudo)
{
MOZ_ASSERT(aElement);
- *aRuleNode = nullptr;
- *aStyleContext = nullptr;
-
nsIDocument* doc = aElement->GetComposedDoc();
- NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
+ if (!doc) {
+ return nullptr;
+ }
nsIPresShell *presShell = doc->GetShell();
- NS_ENSURE_TRUE(presShell, NS_ERROR_UNEXPECTED);
+ if (!presShell) {
+ return nullptr;
+ }
nsPresContext *presContext = presShell->GetPresContext();
- NS_ENSURE_TRUE(presContext, NS_ERROR_UNEXPECTED);
+ if (!presContext) {
+ return nullptr;
+ }
presContext->EnsureSafeToHandOutCSSRules();
- RefPtr<nsStyleContext> sContext =
+ RefPtr<nsStyleContext> styleContext =
nsComputedDOMStyle::GetStyleContextForElement(aElement, aPseudo, presShell);
- if (sContext) {
- *aRuleNode = sContext->RuleNode();
- sContext.forget(aStyleContext);
- }
- return NS_OK;
+ return styleContext.forget();
}
NS_IMETHODIMP
inDOMUtils::GetUsedFontFaces(nsIDOMRange* aRange,
nsIDOMFontFaceList** aFontFaceList)
{
return static_cast<nsRange*>(aRange)->GetUsedFontFaces(aFontFaceList);
}
--- a/layout/inspector/inDOMUtils.h
+++ b/layout/inspector/inDOMUtils.h
@@ -23,20 +23,18 @@ public:
NS_DECL_ISUPPORTS
NS_DECL_INIDOMUTILS
inDOMUtils();
private:
virtual ~inDOMUtils();
- // aStyleContext must be released by the caller once he's done with aRuleNode.
- static nsresult GetRuleNodeForElement(mozilla::dom::Element* aElement,
- nsIAtom* aPseudo,
- nsStyleContext** aStyleContext,
- nsRuleNode** aRuleNode);
+ static already_AddRefed<nsStyleContext>
+ GetCleanStyleContextForElement(mozilla::dom::Element* aElement,
+ nsIAtom* aPseudo);
};
// {0a499822-a287-4089-ad3f-9ffcd4f40263}
#define IN_DOMUTILS_CID \
{0x0a499822, 0xa287, 0x4089, {0xad, 0x3f, 0x9f, 0xfc, 0xd4, 0xf4, 0x02, 0x63}}
#endif // __inDOMUtils_h__