Bug 768756 part 2 - Make nsComputedDOMStyle Init infallible and merge into constructor; r=bz

This commit is contained in:
Aryeh Gregor 2012-07-06 15:28:33 +03:00
Родитель 4ea3421a9a
Коммит b82f9e8e6c
4 изменённых файлов: 59 добавлений и 86 удалений

Просмотреть файл

@ -17,7 +17,7 @@ using namespace mozilla::dom;
// Helper function
static bool
GetCSSComputedValue(nsIContent* aElem,
GetCSSComputedValue(Element* aElem,
nsCSSProperty aPropID,
nsAString& aResult)
{
@ -40,16 +40,11 @@ GetCSSComputedValue(nsIContent* aElem,
return false;
}
nsRefPtr<nsComputedDOMStyle> computedStyle;
nsCOMPtr<nsIDOMElement> domElement(do_QueryInterface(aElem));
nsresult rv = NS_NewComputedDOMStyle(domElement, EmptyString(), shell,
getter_AddRefs(computedStyle));
nsRefPtr<nsComputedDOMStyle> computedStyle =
NS_NewComputedDOMStyle(aElem, EmptyString(), shell);
if (NS_SUCCEEDED(rv)) {
computedStyle->GetPropertyValue(aPropID, aResult);
return true;
}
return false;
computedStyle->GetPropertyValue(aPropID, aResult);
return true;
}
// Class Methods

Просмотреть файл

@ -8176,10 +8176,10 @@ nsGlobalWindow::GetComputedStyle(nsIDOMElement* aElt,
return NS_OK;
}
nsRefPtr<nsComputedDOMStyle> compStyle;
nsresult rv = NS_NewComputedDOMStyle(aElt, aPseudoElt, presShell,
getter_AddRefs(compStyle));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<dom::Element> element = do_QueryInterface(aElt);
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
nsRefPtr<nsComputedDOMStyle> compStyle =
NS_NewComputedDOMStyle(element, aPseudoElt, presShell);
*aReturn = compStyle.forget().get();

Просмотреть файл

@ -60,10 +60,9 @@ using namespace mozilla::dom;
static nsComputedDOMStyle *sCachedComputedDOMStyle;
nsresult
NS_NewComputedDOMStyle(nsIDOMElement *aElement, const nsAString &aPseudoElt,
nsIPresShell *aPresShell,
nsComputedDOMStyle **aComputedStyle)
already_AddRefed<nsComputedDOMStyle>
NS_NewComputedDOMStyle(dom::Element* aElement, const nsAString& aPseudoElt,
nsIPresShell* aPresShell)
{
nsRefPtr<nsComputedDOMStyle> computedStyle;
if (sCachedComputedDOMStyle) {
@ -71,23 +70,17 @@ NS_NewComputedDOMStyle(nsIDOMElement *aElement, const nsAString &aPseudoElt,
// But before we use it, re-initialize the object.
// Oh yeah baby, placement new!
computedStyle = new (sCachedComputedDOMStyle) nsComputedDOMStyle();
computedStyle = new (sCachedComputedDOMStyle)
nsComputedDOMStyle(aElement, aPseudoElt, aPresShell);
sCachedComputedDOMStyle = nsnull;
} else {
// No nsComputedDOMStyle cached, create a new one.
computedStyle = new nsComputedDOMStyle();
NS_ENSURE_TRUE(computedStyle, NS_ERROR_OUT_OF_MEMORY);
computedStyle = new nsComputedDOMStyle(aElement, aPseudoElt, aPresShell);
}
nsresult rv = computedStyle->Init(aElement, aPseudoElt, aPresShell);
NS_ENSURE_SUCCESS(rv, rv);
*aComputedStyle = nsnull;
computedStyle.swap(*aComputedStyle);
return NS_OK;
return computedStyle.forget();
}
static nsIFrame*
@ -98,11 +91,46 @@ GetContainingBlockFor(nsIFrame* aFrame) {
return aFrame->GetContainingBlock();
}
nsComputedDOMStyle::nsComputedDOMStyle()
nsComputedDOMStyle::nsComputedDOMStyle(dom::Element* aElement,
const nsAString& aPseudoElt,
nsIPresShell* aPresShell)
: mDocumentWeak(nsnull), mOuterFrame(nsnull),
mInnerFrame(nsnull), mPresShell(nsnull),
mExposeVisitedStyle(false)
{
MOZ_ASSERT(aElement && aPresShell);
mDocumentWeak = do_GetWeakReference(aPresShell->GetDocument());
mContent = aElement;
if (!DOMStringIsNull(aPseudoElt) && !aPseudoElt.IsEmpty() &&
aPseudoElt.First() == PRUnichar(':')) {
// deal with two-colon forms of aPseudoElt
nsAString::const_iterator start, end;
aPseudoElt.BeginReading(start);
aPseudoElt.EndReading(end);
NS_ASSERTION(start != end, "aPseudoElt is not empty!");
++start;
bool haveTwoColons = true;
if (start == end || *start != PRUnichar(':')) {
--start;
haveTwoColons = false;
}
mPseudo = do_GetAtom(Substring(start, end));
MOZ_ASSERT(mPseudo);
// There aren't any non-CSS2 pseudo-elements with a single ':'
if (!haveTwoColons &&
!nsCSSPseudoElements::IsCSS2PseudoElement(mPseudo)) {
// XXXbz I'd really rather we threw an exception or something, but
// the DOM spec sucks.
mPseudo = nsnull;
}
}
nsPresContext *presCtx = aPresShell->GetPresContext();
MOZ_ASSERT(presCtx);
}
@ -168,54 +196,6 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(nsComputedDOMStyle,
doDestroyComputedDOMStyle(this))
NS_IMETHODIMP
nsComputedDOMStyle::Init(nsIDOMElement *aElement,
const nsAString& aPseudoElt,
nsIPresShell *aPresShell)
{
NS_ENSURE_ARG_POINTER(aElement);
NS_ENSURE_ARG_POINTER(aPresShell);
mDocumentWeak = do_GetWeakReference(aPresShell->GetDocument());
mContent = do_QueryInterface(aElement);
if (!mContent) {
// This should not happen, all our elements support nsIContent!
return NS_ERROR_FAILURE;
}
if (!DOMStringIsNull(aPseudoElt) && !aPseudoElt.IsEmpty() &&
aPseudoElt.First() == PRUnichar(':')) {
// deal with two-colon forms of aPseudoElt
nsAString::const_iterator start, end;
aPseudoElt.BeginReading(start);
aPseudoElt.EndReading(end);
NS_ASSERTION(start != end, "aPseudoElt is not empty!");
++start;
bool haveTwoColons = true;
if (start == end || *start != PRUnichar(':')) {
--start;
haveTwoColons = false;
}
mPseudo = do_GetAtom(Substring(start, end));
NS_ENSURE_TRUE(mPseudo, NS_ERROR_OUT_OF_MEMORY);
// There aren't any non-CSS2 pseudo-elements with a single ':'
if (!haveTwoColons &&
!nsCSSPseudoElements::IsCSS2PseudoElement(mPseudo)) {
// XXXbz I'd really rather we threw an exception or something, but
// the DOM spec sucks.
mPseudo = nsnull;
}
}
nsPresContext *presCtx = aPresShell->GetPresContext();
NS_ENSURE_TRUE(presCtx, NS_ERROR_FAILURE);
return NS_OK;
}
NS_IMETHODIMP
nsComputedDOMStyle::GetPropertyValue(const nsCSSProperty aPropID,
nsAString& aValue)

Просмотреть файл

@ -32,15 +32,13 @@ public:
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_CLASS_AMBIGUOUS(nsComputedDOMStyle,
nsICSSDeclaration)
NS_IMETHOD Init(nsIDOMElement *aElement,
const nsAString& aPseudoElt,
nsIPresShell *aPresShell);
NS_DECL_NSICSSDECLARATION
NS_DECL_NSIDOMCSSSTYLEDECLARATION
nsComputedDOMStyle();
nsComputedDOMStyle(mozilla::dom::Element* aElement,
const nsAString& aPseudoElt,
nsIPresShell* aPresShell);
virtual ~nsComputedDOMStyle();
static void Shutdown();
@ -511,10 +509,10 @@ private:
#endif
};
nsresult
NS_NewComputedDOMStyle(nsIDOMElement *aElement, const nsAString &aPseudoElt,
nsIPresShell *aPresShell,
nsComputedDOMStyle **aComputedStyle);
already_AddRefed<nsComputedDOMStyle>
NS_NewComputedDOMStyle(mozilla::dom::Element* aElement,
const nsAString& aPseudoElt,
nsIPresShell* aPresShell);
#endif /* nsComputedDOMStyle_h__ */