Back to index

lightning-sunbird  0.9+nobinonly
inDOMUtils.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Joe Hewitt <hewitt@netscape.com> (original author)
00023  *   Christopher A. Aillon <christopher@aillon.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "inDOMUtils.h"
00040 #include "inLayoutUtils.h"
00041 
00042 #include "nsIServiceManager.h"
00043 #include "nsString.h"
00044 #include "nsIDOMElement.h"
00045 #include "nsIDocument.h"
00046 #include "nsIDOMDocument.h"
00047 #include "nsIDOMCharacterData.h"
00048 #include "nsITextContent.h"
00049 #include "nsRuleNode.h"
00050 #include "nsIStyleRule.h"
00051 #include "nsICSSStyleRule.h"
00052 #include "nsICSSStyleRuleDOMWrapper.h"
00053 #include "nsIDOMWindowInternal.h"
00054 
00055 static NS_DEFINE_CID(kInspectorCSSUtilsCID, NS_INSPECTORCSSUTILS_CID);
00056 
00058 
00059 inDOMUtils::inDOMUtils()
00060 {
00061   mCSSUtils = do_GetService(kInspectorCSSUtilsCID);
00062 }
00063 
00064 inDOMUtils::~inDOMUtils()
00065 {
00066 }
00067 
00068 NS_IMPL_ISUPPORTS1(inDOMUtils, inIDOMUtils)
00069 
00070 
00071 // inIDOMUtils
00072 
00073 NS_IMETHODIMP
00074 inDOMUtils::IsIgnorableWhitespace(nsIDOMCharacterData *aDataNode,
00075                                   PRBool *aReturn)
00076 {
00077   NS_PRECONDITION(aDataNode, "Must have a character data node");
00078   NS_PRECONDITION(aReturn, "Must have an out parameter");
00079 
00080   *aReturn = PR_FALSE;
00081 
00082   nsCOMPtr<nsITextContent> textContent = do_QueryInterface(aDataNode);
00083   NS_ASSERTION(textContent, "Does not implement nsITextContent!");
00084 
00085   if (!textContent->IsOnlyWhitespace()) {
00086     return NS_OK;
00087   }
00088 
00089   // Okay.  We have only white space.  Let's check the white-space
00090   // property now and make sure that this isn't preformatted text...
00091 
00092   nsCOMPtr<nsIDOMWindowInternal> win = inLayoutUtils::GetWindowFor(aDataNode);
00093   if (!win) {
00094     // Hmm.  Things are screwy if we have no window...
00095     NS_ERROR("No window!");
00096     return NS_OK;
00097   }
00098 
00099   nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(win);
00100   if (!presShell) {
00101     // Display:none iframe or something... Bail out
00102     return NS_OK;
00103   }
00104 
00105 
00106   nsIFrame* frame;
00107   nsCOMPtr<nsIContent> content = do_QueryInterface(aDataNode);
00108   presShell->GetPrimaryFrameFor(content, &frame);
00109   if (frame) {
00110     const nsStyleText* text = frame->GetStyleText();
00111     *aReturn = text->mWhiteSpace != NS_STYLE_WHITESPACE_PRE &&
00112                text->mWhiteSpace != NS_STYLE_WHITESPACE_MOZ_PRE_WRAP;
00113   }
00114   else {
00115     // empty inter-tag text node without frame, e.g., in between <table>\n<tr>
00116     *aReturn = PR_TRUE;
00117   }
00118 
00119   return NS_OK;
00120 }
00121 
00122 NS_IMETHODIMP
00123 inDOMUtils::GetParentForNode(nsIDOMNode* aNode,
00124                              PRBool aShowingAnonymousContent,
00125                              nsIDOMNode** aParent)
00126 {
00127     // First do the special cases -- document nodes and anonymous content
00128   nsCOMPtr<nsIDOMDocument> doc(do_QueryInterface(aNode));
00129   nsCOMPtr<nsIDOMNode> parent;
00130 
00131   if (doc) {
00132     parent = inLayoutUtils::GetContainerFor(doc);
00133   } else if (aShowingAnonymousContent) {
00134     nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
00135     if (content) {
00136       nsCOMPtr<nsIContent> bparent;
00137       nsCOMPtr<nsIBindingManager> bindingManager = inLayoutUtils::GetBindingManagerFor(aNode);
00138       if (bindingManager) {
00139         bindingManager->GetInsertionParent(content, getter_AddRefs(bparent));
00140       }
00141     
00142       parent = do_QueryInterface(bparent);
00143     }
00144   }
00145   
00146   if (!parent) {
00147     // Ok, just get the normal DOM parent node
00148     aNode->GetParentNode(getter_AddRefs(parent));
00149   }
00150 
00151   NS_IF_ADDREF(*aParent = parent);
00152   return NS_OK;
00153 }
00154 
00155 NS_IMETHODIMP
00156 inDOMUtils::GetCSSStyleRules(nsIDOMElement *aElement,
00157                              nsISupportsArray **_retval)
00158 {
00159   if (!aElement) return NS_ERROR_NULL_POINTER;
00160 
00161   *_retval = nsnull;
00162 
00163   nsRuleNode* ruleNode = nsnull;
00164   nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
00165   mCSSUtils->GetRuleNodeForContent(content, &ruleNode);
00166   if (!ruleNode) {
00167     // This can fail for content nodes that are not in the document or
00168     // if the document they're in doesn't have a presshell.  Bail out.
00169     return NS_OK;
00170   }
00171 
00172   nsCOMPtr<nsISupportsArray> rules;
00173   NS_NewISupportsArray(getter_AddRefs(rules));
00174   if (!rules) return NS_ERROR_OUT_OF_MEMORY;
00175 
00176   nsCOMPtr<nsIStyleRule> srule;
00177   nsCOMPtr<nsICSSStyleRule> cssRule;
00178   nsCOMPtr<nsIDOMCSSRule> domRule;
00179   for (PRBool isRoot;
00180        mCSSUtils->IsRuleNodeRoot(ruleNode, &isRoot), !isRoot;
00181        mCSSUtils->GetRuleNodeParent(ruleNode, &ruleNode))
00182   {
00183     mCSSUtils->GetRuleNodeRule(ruleNode, getter_AddRefs(srule));
00184     cssRule = do_QueryInterface(srule);
00185     if (cssRule) {
00186       cssRule->GetDOMRule(getter_AddRefs(domRule));
00187       if (domRule)
00188         rules->InsertElementAt(domRule, 0);
00189     }
00190   }
00191 
00192   *_retval = rules;
00193   NS_ADDREF(*_retval);
00194 
00195   return NS_OK;
00196 }
00197 
00198 NS_IMETHODIMP
00199 inDOMUtils::GetRuleLine(nsIDOMCSSStyleRule *aRule, PRUint32 *_retval)
00200 {
00201   *_retval = 0;
00202   if (!aRule)
00203     return NS_OK;
00204   nsCOMPtr<nsICSSStyleRuleDOMWrapper> rule = do_QueryInterface(aRule);
00205   nsCOMPtr<nsICSSStyleRule> cssrule;
00206   rule->GetCSSStyleRule(getter_AddRefs(cssrule));
00207   if (cssrule)
00208     *_retval = cssrule->GetLineNumber();
00209   return NS_OK;
00210 }
00211 
00212 NS_IMETHODIMP 
00213 inDOMUtils::GetBindingURLs(nsIDOMElement *aElement, nsIArray **_retval)
00214 {
00215   return mCSSUtils->GetBindingURLs(aElement, _retval);
00216 }
00217 
00218 NS_IMETHODIMP
00219 inDOMUtils::SetContentState(nsIDOMElement *aElement, PRInt32 aState)
00220 {
00221   if (!aElement)
00222     return NS_ERROR_NULL_POINTER;
00223   
00224   nsCOMPtr<nsIEventStateManager> esm = inLayoutUtils::GetEventStateManagerFor(aElement);
00225   if (esm) {
00226     nsCOMPtr<nsIContent> content;
00227     content = do_QueryInterface(aElement);
00228   
00229     return esm->SetContentState(content, aState);
00230   }
00231   
00232   return NS_ERROR_FAILURE;
00233 }
00234 
00235 NS_IMETHODIMP
00236 inDOMUtils::GetContentState(nsIDOMElement *aElement, PRInt32* aState)
00237 {
00238   *aState = 0;
00239 
00240   if (!aElement)
00241     return NS_ERROR_NULL_POINTER;
00242 
00243   nsCOMPtr<nsIEventStateManager> esm = inLayoutUtils::GetEventStateManagerFor(aElement);
00244   if (esm) {
00245     nsCOMPtr<nsIContent> content;
00246     content = do_QueryInterface(aElement);
00247   
00248     return esm->GetContentState(content, *aState);
00249   }
00250 
00251   return NS_ERROR_FAILURE;
00252 }
00253