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.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* DOM object for element.style */
40 #include "nsDOMCSSAttrDeclaration.h"
41 #include "nsCSSDeclaration.h"
42 #include "nsIDocument.h"
43 #include "nsIDOMMutationEvent.h"
44 #include "nsICSSStyleRule.h"
45 #include "nsICSSLoader.h"
46 #include "nsICSSParser.h"
48 #include "nsINameSpaceManager.h"
49 #include "nsStyleConsts.h"
50 #include "nsContentUtils.h"
51 #include "nsIContent.h"
52 #include "nsIPrincipal.h"
54 nsDOMCSSAttributeDeclaration::nsDOMCSSAttributeDeclaration(nsIContent
*aContent
)
56 MOZ_COUNT_CTOR(nsDOMCSSAttributeDeclaration
);
58 // This reference is not reference-counted. The content
59 // object tells us when its about to go away.
60 NS_ASSERTION(aContent
&& aContent
->IsNodeOfType(nsINode::eELEMENT
),
61 "Inline style for non-element content?");
65 nsDOMCSSAttributeDeclaration::~nsDOMCSSAttributeDeclaration()
67 MOZ_COUNT_DTOR(nsDOMCSSAttributeDeclaration
);
70 NS_IMPL_ADDREF(nsDOMCSSAttributeDeclaration
)
71 NS_IMPL_RELEASE(nsDOMCSSAttributeDeclaration
)
74 nsDOMCSSAttributeDeclaration::DropReference()
80 nsDOMCSSAttributeDeclaration::DeclarationChanged()
82 NS_ASSERTION(mContent
, "Must have content node to set the decl!");
83 nsICSSStyleRule
* oldRule
= mContent
->GetInlineStyleRule();
84 NS_ASSERTION(oldRule
, "content must have rule");
86 nsCOMPtr
<nsICSSStyleRule
> newRule
= oldRule
->DeclarationChanged(PR_FALSE
);
88 return NS_ERROR_OUT_OF_MEMORY
;
91 return mContent
->SetInlineStyleRule(newRule
, PR_TRUE
);
95 nsDOMCSSAttributeDeclaration::GetCSSDeclaration(nsCSSDeclaration
**aDecl
,
98 nsresult result
= NS_OK
;
102 nsICSSStyleRule
* cssRule
= mContent
->GetInlineStyleRule();
104 *aDecl
= cssRule
->GetDeclaration();
106 else if (aAllocate
) {
107 nsCSSDeclaration
*decl
= new nsCSSDeclaration();
109 return NS_ERROR_OUT_OF_MEMORY
;
110 if (!decl
->InitializeEmpty()) {
112 return NS_ERROR_OUT_OF_MEMORY
;
115 nsCOMPtr
<nsICSSStyleRule
> newRule
;
116 result
= NS_NewCSSStyleRule(getter_AddRefs(newRule
), nsnull
, decl
);
117 if (NS_FAILED(result
)) {
122 result
= mContent
->SetInlineStyleRule(newRule
, PR_FALSE
);
123 if (NS_SUCCEEDED(result
)) {
133 * This is a utility function. It will only fail if it can't get a
134 * parser. This means it can return NS_OK without aURI or aCSSLoader
138 nsDOMCSSAttributeDeclaration::GetCSSParsingEnvironment(nsIURI
** aSheetURI
,
140 nsIPrincipal
** aSheetPrincipal
,
141 nsICSSLoader
** aCSSLoader
,
142 nsICSSParser
** aCSSParser
)
144 NS_ASSERTION(mContent
, "Something is severely broken -- there should be an nsIContent here!");
145 // null out the out params since some of them may not get initialized below
148 *aSheetPrincipal
= nsnull
;
149 *aCSSLoader
= nsnull
;
150 *aCSSParser
= nsnull
;
152 nsIDocument
* doc
= mContent
->GetOwnerDoc();
154 // document has been destroyed
155 return NS_ERROR_NOT_AVAILABLE
;
158 nsCOMPtr
<nsIURI
> baseURI
= mContent
->GetBaseURI();
159 nsCOMPtr
<nsIURI
> sheetURI
= doc
->GetDocumentURI();
161 NS_ADDREF(*aCSSLoader
= doc
->CSSLoader());
165 // Note: parsers coming from a CSSLoader for a document already have
166 // the right case-sensitivity, quirkiness, etc.
167 rv
= (*aCSSLoader
)->GetParserFor(nsnull
, aCSSParser
);
172 baseURI
.swap(*aBaseURI
);
173 sheetURI
.swap(*aSheetURI
);
174 NS_ADDREF(*aSheetPrincipal
= mContent
->NodePrincipal());
180 nsDOMCSSAttributeDeclaration::GetParentRule(nsIDOMCSSRule
**aParent
)
182 NS_ENSURE_ARG_POINTER(aParent
);