Back to index

lightning-sunbird  0.9+nobinonly
nsInspectorCSSUtils.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 // vim:cindent:ts=8:et:sw=4:
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2002
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   L. David Baron <dbaron@dbaron.org> (original author)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsInspectorCSSUtils.h"
00041 #include "nsIStyleRule.h"
00042 #include "nsRuleNode.h"
00043 #include "nsString.h"
00044 #include "nsLayoutAtoms.h"
00045 #include "nsIDocument.h"
00046 #include "nsIPresShell.h"
00047 #include "nsAutoPtr.h"
00048 #include "nsIFrame.h"
00049 #include "nsStyleSet.h"
00050 #include "nsXBLBinding.h"
00051 #include "nsXBLPrototypeBinding.h"
00052 #include "nsIDOMElement.h"
00053 #include "nsArray.h"
00054 
00055 nsInspectorCSSUtils::nsInspectorCSSUtils()
00056 {
00057     nsCSSProps::AddRefTable();
00058 }
00059 
00060 nsInspectorCSSUtils::~nsInspectorCSSUtils()
00061 {
00062     nsCSSProps::ReleaseTable();
00063 }
00064 
00065 NS_IMPL_ISUPPORTS1(nsInspectorCSSUtils, nsIInspectorCSSUtils)
00066 
00067 NS_IMETHODIMP
00068 nsInspectorCSSUtils::LookupCSSProperty(const nsAString& aName, nsCSSProperty *aProp)
00069 {
00070     *aProp = nsCSSProps::LookupProperty(aName);
00071     return NS_OK;
00072 }
00073 
00074 NS_IMETHODIMP
00075 nsInspectorCSSUtils::GetRuleNodeParent(nsRuleNode *aNode, nsRuleNode **aParent)
00076 {
00077     *aParent = aNode->GetParent();
00078     return NS_OK;
00079 }
00080 
00081 NS_IMETHODIMP
00082 nsInspectorCSSUtils::GetRuleNodeRule(nsRuleNode *aNode, nsIStyleRule **aRule)
00083 {
00084     *aRule = aNode->GetRule();
00085     NS_IF_ADDREF(*aRule);
00086     return NS_OK;
00087 }
00088 
00089 NS_IMETHODIMP
00090 nsInspectorCSSUtils::IsRuleNodeRoot(nsRuleNode *aNode, PRBool *aIsRoot)
00091 {
00092     *aIsRoot = aNode->IsRoot();
00093     return NS_OK;
00094 }
00095 
00096 NS_IMETHODIMP
00097 nsInspectorCSSUtils::AdjustRectForMargins(nsIFrame* aFrame, nsRect& aRect)
00098 {
00099     const nsStyleMargin* margins = aFrame->GetStyleMargin();
00100 
00101     // adjust coordinates for margins
00102     nsStyleCoord coord;
00103     if (margins->mMargin.GetTopUnit() == eStyleUnit_Coord) {
00104         margins->mMargin.GetTop(coord);
00105         aRect.y -= coord.GetCoordValue();
00106         aRect.height += coord.GetCoordValue();
00107     }
00108     if (margins->mMargin.GetLeftUnit() == eStyleUnit_Coord) {
00109         margins->mMargin.GetLeft(coord);
00110         aRect.x -= coord.GetCoordValue();
00111         aRect.width += coord.GetCoordValue();
00112     }
00113     if (margins->mMargin.GetRightUnit() == eStyleUnit_Coord) {
00114         margins->mMargin.GetRight(coord);
00115         aRect.width += coord.GetCoordValue();
00116     }
00117     if (margins->mMargin.GetBottomUnit() == eStyleUnit_Coord) {
00118         margins->mMargin.GetBottom(coord);
00119         aRect.height += coord.GetCoordValue();
00120     }
00121 
00122     return NS_OK;
00123 }
00124 
00125 /* static */
00126 nsStyleContext*
00127 nsInspectorCSSUtils::GetStyleContextForFrame(nsIFrame* aFrame)
00128 {
00129     nsStyleContext* styleContext = aFrame->GetStyleContext();
00130     if (!styleContext)
00131         return nsnull;
00132 
00133     /* For tables the primary frame is the "outer frame" but the style
00134      * rules are applied to the "inner frame".  Luckily, the "outer
00135      * frame" actually inherits style from the "inner frame" so we can
00136      * just move one level up in the style context hierarchy....
00137      */
00138     if (aFrame->GetType() == nsLayoutAtoms::tableOuterFrame)
00139         return styleContext->GetParent();
00140 
00141     return styleContext;
00142 }    
00143 
00144 /* static */
00145 already_AddRefed<nsStyleContext>
00146 nsInspectorCSSUtils::GetStyleContextForContent(nsIContent* aContent,
00147                                                nsIAtom* aPseudo,
00148                                                nsIPresShell* aPresShell)
00149 {
00150     if (!aPseudo) {
00151         nsIFrame* frame = nsnull;
00152         aPresShell->FlushPendingNotifications(Flush_StyleReresolves);
00153         aPresShell->GetPrimaryFrameFor(aContent, &frame);
00154         if (frame) {
00155             nsStyleContext* result = GetStyleContextForFrame(frame);
00156             // this function returns an addrefed style context
00157             if (result)
00158                 result->AddRef();
00159             return result;
00160         }
00161     }
00162 
00163     // No frame has been created or we have a pseudo, so resolve the
00164     // style ourselves
00165     nsRefPtr<nsStyleContext> parentContext;
00166     nsIContent* parent = aPseudo ? aContent : aContent->GetParent();
00167     if (parent)
00168         parentContext = GetStyleContextForContent(parent, nsnull, aPresShell);
00169 
00170     nsPresContext *presContext = aPresShell->GetPresContext();
00171     if (!presContext)
00172         return nsnull;
00173 
00174     nsStyleSet *styleSet = aPresShell->StyleSet();
00175 
00176     if (!aContent->IsContentOfType(nsIContent::eELEMENT)) {
00177         NS_ASSERTION(!aPseudo, "Shouldn't have a pseudo for a non-element!");
00178         return styleSet->ResolveStyleForNonElement(parentContext);
00179     }
00180 
00181     if (aPseudo) {
00182         return styleSet->ResolvePseudoStyleFor(aContent, aPseudo, parentContext);
00183     }
00184     
00185     return styleSet->ResolveStyleFor(aContent, parentContext);
00186 }
00187 
00188 NS_IMETHODIMP
00189 nsInspectorCSSUtils::GetRuleNodeForContent(nsIContent* aContent,
00190                                            nsRuleNode** aRuleNode)
00191 {
00192     *aRuleNode = nsnull;
00193 
00194     nsIDocument* doc = aContent->GetDocument();
00195     NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
00196 
00197     nsIPresShell *presShell = doc->GetShellAt(0);
00198     NS_ENSURE_TRUE(presShell, NS_ERROR_UNEXPECTED);
00199 
00200     nsRefPtr<nsStyleContext> sContext =
00201         GetStyleContextForContent(aContent, nsnull, presShell);
00202     *aRuleNode = sContext->GetRuleNode();
00203     return NS_OK;
00204 }
00205 
00206 NS_IMETHODIMP
00207 nsInspectorCSSUtils::GetBindingURLs(nsIDOMElement *aElement,
00208                                     nsIArray **aResult)
00209 {
00210     *aResult = nsnull;
00211 
00212     nsCOMArray<nsIURI> urls;
00213 
00214     nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
00215     NS_ASSERTION(content, "elements must implement nsIContent");
00216 
00217     nsIDocument *ownerDoc = content->GetOwnerDoc();
00218     if (ownerDoc) {
00219         nsXBLBinding *binding =
00220             ownerDoc->BindingManager()->GetBinding(content);
00221 
00222         while (binding) {
00223             urls.AppendObject(binding->PrototypeBinding()->BindingURI());
00224             binding = binding->GetBaseBinding();
00225         }
00226     }
00227 
00228     nsIMutableArray *mutableResult = nsnull;
00229     nsresult rv = NS_NewArray(&mutableResult, urls);
00230     *aResult = mutableResult;
00231     return rv;
00232 }