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
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.
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"
51 #include "nsNodeUtils.h"
53 namespace css
= mozilla::css
;
54 namespace dom
= mozilla::dom
;
56 nsDOMCSSAttributeDeclaration::nsDOMCSSAttributeDeclaration(dom::Element
* aElement
58 , PRBool aIsSMILOverride
63 , mIsSMILOverride(aIsSMILOverride
)
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
)
87 nsDOMCSSAttributeDeclaration::SetCSSDeclaration(css::Declaration
* aDecl
)
89 NS_ASSERTION(mElement
, "Must have Element to set the declaration!");
90 nsICSSStyleRule
* oldRule
=
92 mIsSMILOverride
? mElement
->GetSMILOverrideStyleRule() :
94 mElement
->GetInlineStyleRule();
95 NS_ASSERTION(oldRule
, "Element must have rule");
97 nsCOMPtr
<nsICSSStyleRule
> newRule
=
98 oldRule
->DeclarationChanged(aDecl
, PR_FALSE
);
100 return NS_ERROR_OUT_OF_MEMORY
;
105 mIsSMILOverride
? mElement
->SetSMILOverrideStyleRule(newRule
, PR_TRUE
) :
107 mElement
->SetInlineStyleRule(newRule
, PR_TRUE
);
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.
118 if (!mIsSMILOverride
)
121 nsNodeUtils::AttributeWillChange(mElement
, kNameSpaceID_None
,
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();
132 nsDOMCSSAttributeDeclaration::GetCSSDeclaration(PRBool aAllocate
)
137 nsICSSStyleRule
* cssRule
;
140 cssRule
= mElement
->GetSMILOverrideStyleRule();
143 cssRule
= mElement
->GetInlineStyleRule();
146 return cssRule
->GetDeclaration();
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).
161 rv
= mElement
->SetSMILOverrideStyleRule(newRule
, PR_FALSE
);
164 rv
= mElement
->SetInlineStyleRule(newRule
, PR_FALSE
);
167 return nsnull
; // the decl will be destroyed along with the style rule
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
179 nsDOMCSSAttributeDeclaration::GetCSSParsingEnvironment(nsIURI
** aSheetURI
,
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
188 *aSheetPrincipal
= nsnull
;
189 *aCSSLoader
= nsnull
;
191 nsIDocument
* doc
= mElement
->GetOwnerDoc();
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());
210 nsDOMCSSAttributeDeclaration::GetParentRule(nsIDOMCSSRule
**aParent
)
212 NS_ENSURE_ARG_POINTER(aParent
);
218 /* virtual */ nsINode
*
219 nsDOMCSSAttributeDeclaration::GetParentObject()