Backed out changeset b88172246b66 due to Win32 debug failures.
[mozilla-central.git] / layout / style / nsDOMCSSAttrDeclaration.cpp
blob5d26b1d96da153fb1704ed9d231be3acbf5e90f0
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Ms2ger <ms2ger@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 /* DOM object for element.style */
41 #include "nsDOMCSSAttrDeclaration.h"
43 #include "mozilla/css/Declaration.h"
44 #include "mozilla/css/Loader.h"
45 #include "mozilla/dom/Element.h"
46 #include "nsICSSStyleRule.h"
47 #include "nsIDocument.h"
48 #include "nsIDOMMutationEvent.h"
49 #include "nsIPrincipal.h"
50 #include "nsIURI.h"
51 #include "nsNodeUtils.h"
53 namespace css = mozilla::css;
54 namespace dom = mozilla::dom;
56 nsDOMCSSAttributeDeclaration::nsDOMCSSAttributeDeclaration(dom::Element* aElement
57 #ifdef MOZ_SMIL
58 , PRBool aIsSMILOverride
59 #endif // MOZ_SMIL
61 : mElement(aElement)
62 #ifdef MOZ_SMIL
63 , mIsSMILOverride(aIsSMILOverride)
64 #endif // MOZ_SMIL
66 MOZ_COUNT_CTOR(nsDOMCSSAttributeDeclaration);
68 NS_ASSERTION(aElement, "Inline style for a NULL element?");
71 nsDOMCSSAttributeDeclaration::~nsDOMCSSAttributeDeclaration()
73 MOZ_COUNT_DTOR(nsDOMCSSAttributeDeclaration);
76 NS_IMPL_CYCLE_COLLECTION_1(nsDOMCSSAttributeDeclaration, mElement)
78 NS_INTERFACE_MAP_BEGIN(nsDOMCSSAttributeDeclaration)
79 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
80 NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsDOMCSSAttributeDeclaration)
81 NS_IMPL_QUERY_TAIL_INHERITING(nsDOMCSSDeclaration)
83 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCSSAttributeDeclaration)
84 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCSSAttributeDeclaration)
86 nsresult
87 nsDOMCSSAttributeDeclaration::SetCSSDeclaration(css::Declaration* aDecl)
89 NS_ASSERTION(mElement, "Must have Element to set the declaration!");
90 nsICSSStyleRule* oldRule =
91 #ifdef MOZ_SMIL
92 mIsSMILOverride ? mElement->GetSMILOverrideStyleRule() :
93 #endif // MOZ_SMIL
94 mElement->GetInlineStyleRule();
95 NS_ASSERTION(oldRule, "Element must have rule");
97 nsCOMPtr<nsICSSStyleRule> newRule =
98 oldRule->DeclarationChanged(aDecl, PR_FALSE);
99 if (!newRule) {
100 return NS_ERROR_OUT_OF_MEMORY;
103 return
104 #ifdef MOZ_SMIL
105 mIsSMILOverride ? mElement->SetSMILOverrideStyleRule(newRule, PR_TRUE) :
106 #endif // MOZ_SMIL
107 mElement->SetInlineStyleRule(newRule, PR_TRUE);
110 nsIDocument*
111 nsDOMCSSAttributeDeclaration::DocToUpdate()
113 // XXXbz this is a bit of a hack, especially doing it before the
114 // BeginUpdate(), but this is a good chokepoint where we know we
115 // plan to modify the CSSDeclaration, so need to notify
116 // AttributeWillChange if this is inline style.
117 #ifdef MOZ_SMIL
118 if (!mIsSMILOverride)
119 #endif
121 nsNodeUtils::AttributeWillChange(mElement, kNameSpaceID_None,
122 nsGkAtoms::style,
123 nsIDOMMutationEvent::MODIFICATION);
126 // We need GetOwnerDoc() rather than GetCurrentDoc() because it might
127 // be the BeginUpdate call that inserts mElement into the document.
128 return mElement->GetOwnerDoc();
131 css::Declaration*
132 nsDOMCSSAttributeDeclaration::GetCSSDeclaration(PRBool aAllocate)
134 if (!mElement)
135 return nsnull;
137 nsICSSStyleRule* cssRule;
138 #ifdef MOZ_SMIL
139 if (mIsSMILOverride)
140 cssRule = mElement->GetSMILOverrideStyleRule();
141 else
142 #endif // MOZ_SMIL
143 cssRule = mElement->GetInlineStyleRule();
145 if (cssRule) {
146 return cssRule->GetDeclaration();
148 if (!aAllocate) {
149 return nsnull;
152 // cannot fail
153 css::Declaration *decl = new css::Declaration();
154 decl->InitializeEmpty();
155 nsCOMPtr<nsICSSStyleRule> newRule = NS_NewCSSStyleRule(nsnull, decl);
157 // this *can* fail (inside SetAttrAndNotify, at least).
158 nsresult rv;
159 #ifdef MOZ_SMIL
160 if (mIsSMILOverride)
161 rv = mElement->SetSMILOverrideStyleRule(newRule, PR_FALSE);
162 else
163 #endif // MOZ_SMIL
164 rv = mElement->SetInlineStyleRule(newRule, PR_FALSE);
166 if (NS_FAILED(rv)) {
167 return nsnull; // the decl will be destroyed along with the style rule
170 return decl;
174 * This is a utility function. It will only fail if it can't get a
175 * parser. This means it can return NS_OK without aURI or aCSSLoader
176 * being initialized.
178 nsresult
179 nsDOMCSSAttributeDeclaration::GetCSSParsingEnvironment(nsIURI** aSheetURI,
180 nsIURI** aBaseURI,
181 nsIPrincipal** aSheetPrincipal,
182 mozilla::css::Loader** aCSSLoader)
184 NS_ASSERTION(mElement, "Something is severely broken -- there should be an Element here!");
185 // null out the out params since some of them may not get initialized below
186 *aSheetURI = nsnull;
187 *aBaseURI = nsnull;
188 *aSheetPrincipal = nsnull;
189 *aCSSLoader = nsnull;
191 nsIDocument* doc = mElement->GetOwnerDoc();
192 if (!doc) {
193 // document has been destroyed
194 return NS_ERROR_NOT_AVAILABLE;
197 nsCOMPtr<nsIURI> baseURI = mElement->GetBaseURI();
198 nsCOMPtr<nsIURI> sheetURI = doc->GetDocumentURI();
200 NS_ADDREF(*aCSSLoader = doc->CSSLoader());
202 baseURI.swap(*aBaseURI);
203 sheetURI.swap(*aSheetURI);
204 NS_ADDREF(*aSheetPrincipal = mElement->NodePrincipal());
206 return NS_OK;
209 NS_IMETHODIMP
210 nsDOMCSSAttributeDeclaration::GetParentRule(nsIDOMCSSRule **aParent)
212 NS_ENSURE_ARG_POINTER(aParent);
214 *aParent = nsnull;
215 return NS_OK;
218 /* virtual */ nsINode*
219 nsDOMCSSAttributeDeclaration::GetParentObject()
221 return mElement;