Back to index

lightning-sunbird  0.9+nobinonly
nsDOMCSSAttrDeclaration.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsDOMCSSAttrDeclaration.h"
00039 #include "nsCSSDeclaration.h"
00040 #include "nsIDocument.h"
00041 #include "nsHTMLAtoms.h"
00042 #include "nsIStyledContent.h"
00043 #include "nsIDOMMutationEvent.h"
00044 #include "nsICSSStyleRule.h"
00045 #include "nsICSSLoader.h"
00046 #include "nsICSSParser.h"
00047 #include "nsIURI.h"
00048 #include "nsINameSpaceManager.h"
00049 #include "nsStyleConsts.h"
00050 #include "nsContentUtils.h"
00051 
00052 MOZ_DECL_CTOR_COUNTER(nsDOMCSSAttributeDeclaration)
00053 
00054 nsDOMCSSAttributeDeclaration::nsDOMCSSAttributeDeclaration(nsIStyledContent *aContent)
00055 {
00056   MOZ_COUNT_CTOR(nsDOMCSSAttributeDeclaration);
00057 
00058   // This reference is not reference-counted. The content
00059   // object tells us when its about to go away.
00060   mContent = aContent;
00061 }
00062 
00063 nsDOMCSSAttributeDeclaration::~nsDOMCSSAttributeDeclaration()
00064 {
00065   MOZ_COUNT_DTOR(nsDOMCSSAttributeDeclaration);
00066 }
00067 
00068 NS_IMPL_ADDREF(nsDOMCSSAttributeDeclaration)
00069 NS_IMPL_RELEASE(nsDOMCSSAttributeDeclaration)
00070 
00071 void
00072 nsDOMCSSAttributeDeclaration::DropReference()
00073 {
00074   mContent = nsnull;
00075 }
00076 
00077 nsresult
00078 nsDOMCSSAttributeDeclaration::DeclarationChanged()
00079 {
00080   NS_ASSERTION(mContent, "Must have content node to set the decl!");
00081   nsICSSStyleRule* oldRule = mContent->GetInlineStyleRule();
00082   NS_ASSERTION(oldRule, "content must have rule");
00083 
00084   nsCOMPtr<nsICSSStyleRule> newRule = oldRule->DeclarationChanged(PR_FALSE);
00085   if (!newRule) {
00086     return NS_ERROR_OUT_OF_MEMORY;
00087   }
00088     
00089   return mContent->SetInlineStyleRule(newRule, PR_TRUE);
00090 }
00091 
00092 nsresult
00093 nsDOMCSSAttributeDeclaration::GetCSSDeclaration(nsCSSDeclaration **aDecl,
00094                                                 PRBool aAllocate)
00095 {
00096   nsresult result = NS_OK;
00097 
00098   *aDecl = nsnull;
00099   if (mContent) {
00100     nsICSSStyleRule* cssRule = mContent->GetInlineStyleRule();
00101     if (cssRule) {
00102       *aDecl = cssRule->GetDeclaration();
00103     }
00104     else if (aAllocate) {
00105       nsCSSDeclaration *decl = new nsCSSDeclaration();
00106       if (!decl)
00107         return NS_ERROR_OUT_OF_MEMORY;
00108       if (!decl->InitializeEmpty()) {
00109         decl->RuleAbort();
00110         return NS_ERROR_OUT_OF_MEMORY;
00111       }
00112       
00113       nsCOMPtr<nsICSSStyleRule> newRule;
00114       result = NS_NewCSSStyleRule(getter_AddRefs(newRule), nsnull, decl);
00115       if (NS_FAILED(result)) {
00116         decl->RuleAbort();
00117         return result;
00118       }
00119         
00120       result = mContent->SetInlineStyleRule(newRule, PR_FALSE);
00121       if (NS_SUCCEEDED(result)) {
00122         *aDecl = decl;
00123       }
00124     }
00125   }
00126 
00127   return result;
00128 }
00129 
00130 /*
00131  * This is a utility function.  It will only fail if it can't get a
00132  * parser.  This means it can return NS_OK without aURI or aCSSLoader
00133  * being initialized.
00134  */
00135 nsresult
00136 nsDOMCSSAttributeDeclaration::GetCSSParsingEnvironment(nsIURI** aSheetURI,
00137                                                        nsIURI** aBaseURI,
00138                                                        nsICSSLoader** aCSSLoader,
00139                                                        nsICSSParser** aCSSParser)
00140 {
00141   NS_ASSERTION(mContent, "Something is severely broken -- there should be an nsIContent here!");
00142   // null out the out params since some of them may not get initialized below
00143   *aSheetURI = nsnull;
00144   *aBaseURI = nsnull;
00145   *aCSSLoader = nsnull;
00146   *aCSSParser = nsnull;
00147 
00148   nsIDocument* doc = mContent->GetOwnerDoc();
00149   if (!doc) {
00150     // document has been destroyed
00151     return NS_ERROR_NOT_AVAILABLE;
00152   }
00153 
00154   nsCOMPtr<nsIURI> baseURI = mContent->GetBaseURI();
00155   nsCOMPtr<nsIURI> sheetURI = doc->GetDocumentURI();
00156 
00157   NS_ADDREF(*aCSSLoader = doc->CSSLoader());
00158   
00159   nsresult rv = NS_OK;
00160 
00161   // Note: parsers coming from a CSSLoader for a document already have
00162   // the right case-sensitivity, quirkiness, etc.
00163   rv = (*aCSSLoader)->GetParserFor(nsnull, aCSSParser);
00164   if (NS_FAILED(rv)) {
00165     return rv;
00166   }
00167   
00168   baseURI.swap(*aBaseURI);
00169   sheetURI.swap(*aSheetURI);
00170 
00171   return NS_OK;
00172 }
00173 
00174 NS_IMETHODIMP
00175 nsDOMCSSAttributeDeclaration::GetParentRule(nsIDOMCSSRule **aParent)
00176 {
00177   NS_ENSURE_ARG_POINTER(aParent);
00178 
00179   *aParent = nsnull;
00180   return NS_OK;
00181 }
00182