1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 // vim:cindent:ts=8:et:sw=4:
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2002
21 * the Initial Developer. All Rights Reserved.
24 * L. David Baron <dbaron@dbaron.org> (original author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* XPCOM interface to provide some internal information to DOM inspector */
42 #include "nsInspectorCSSUtils.h"
43 #include "nsIStyleRule.h"
44 #include "nsRuleNode.h"
46 #include "nsGkAtoms.h"
47 #include "nsIDocument.h"
48 #include "nsIPresShell.h"
49 #include "nsAutoPtr.h"
51 #include "nsStyleSet.h"
52 #include "nsXBLBinding.h"
53 #include "nsXBLPrototypeBinding.h"
54 #include "nsIDOMElement.h"
55 #include "nsIMutableArray.h"
56 #include "nsBindingManager.h"
58 nsInspectorCSSUtils::nsInspectorCSSUtils()
60 nsCSSProps::AddRefTable();
63 nsInspectorCSSUtils::~nsInspectorCSSUtils()
65 nsCSSProps::ReleaseTable();
68 NS_IMPL_ISUPPORTS1(nsInspectorCSSUtils
, nsIInspectorCSSUtils
)
71 nsInspectorCSSUtils::LookupCSSProperty(const nsAString
& aName
, nsCSSProperty
*aProp
)
73 *aProp
= nsCSSProps::LookupProperty(aName
);
78 nsInspectorCSSUtils::GetRuleNodeParent(nsRuleNode
*aNode
, nsRuleNode
**aParent
)
80 *aParent
= aNode
->GetParent();
85 nsInspectorCSSUtils::GetRuleNodeRule(nsRuleNode
*aNode
, nsIStyleRule
**aRule
)
87 *aRule
= aNode
->GetRule();
93 nsInspectorCSSUtils::IsRuleNodeRoot(nsRuleNode
*aNode
, PRBool
*aIsRoot
)
95 *aIsRoot
= aNode
->IsRoot();
101 nsInspectorCSSUtils::GetStyleContextForFrame(nsIFrame
* aFrame
)
103 nsStyleContext
* styleContext
= aFrame
->GetStyleContext();
107 /* For tables the primary frame is the "outer frame" but the style
108 * rules are applied to the "inner frame". Luckily, the "outer
109 * frame" actually inherits style from the "inner frame" so we can
110 * just move one level up in the style context hierarchy....
112 if (aFrame
->GetType() == nsGkAtoms::tableOuterFrame
)
113 return styleContext
->GetParent();
119 already_AddRefed
<nsStyleContext
>
120 nsInspectorCSSUtils::GetStyleContextForContent(nsIContent
* aContent
,
122 nsIPresShell
* aPresShell
)
125 aPresShell
->FlushPendingNotifications(Flush_Style
);
126 nsIFrame
* frame
= aPresShell
->GetPrimaryFrameFor(aContent
);
128 nsStyleContext
* result
= GetStyleContextForFrame(frame
);
129 // this function returns an addrefed style context
136 // No frame has been created or we have a pseudo, so resolve the
138 nsRefPtr
<nsStyleContext
> parentContext
;
139 nsIContent
* parent
= aPseudo
? aContent
: aContent
->GetParent();
141 parentContext
= GetStyleContextForContent(parent
, nsnull
, aPresShell
);
143 nsPresContext
*presContext
= aPresShell
->GetPresContext();
147 nsStyleSet
*styleSet
= aPresShell
->StyleSet();
149 if (!aContent
->IsNodeOfType(nsINode::eELEMENT
)) {
150 NS_ASSERTION(!aPseudo
, "Shouldn't have a pseudo for a non-element!");
151 return styleSet
->ResolveStyleForNonElement(parentContext
);
155 return styleSet
->ResolvePseudoStyleFor(aContent
, aPseudo
, parentContext
);
158 return styleSet
->ResolveStyleFor(aContent
, parentContext
);
162 nsInspectorCSSUtils::GetRuleNodeForContent(nsIContent
* aContent
,
163 nsRuleNode
** aRuleNode
)
167 nsIDocument
* doc
= aContent
->GetDocument();
168 NS_ENSURE_TRUE(doc
, NS_ERROR_UNEXPECTED
);
170 nsIPresShell
*presShell
= doc
->GetPrimaryShell();
171 NS_ENSURE_TRUE(presShell
, NS_ERROR_UNEXPECTED
);
173 nsRefPtr
<nsStyleContext
> sContext
=
174 GetStyleContextForContent(aContent
, nsnull
, presShell
);
175 *aRuleNode
= sContext
->GetRuleNode();
180 nsInspectorCSSUtils::GetBindingURLs(nsIDOMElement
*aElement
,
185 nsCOMPtr
<nsIMutableArray
> urls
= do_CreateInstance(NS_ARRAY_CONTRACTID
);
187 return NS_ERROR_FAILURE
;
189 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(aElement
);
190 NS_ASSERTION(content
, "elements must implement nsIContent");
192 nsIDocument
*ownerDoc
= content
->GetOwnerDoc();
194 nsXBLBinding
*binding
=
195 ownerDoc
->BindingManager()->GetBinding(content
);
198 urls
->AppendElement(binding
->PrototypeBinding()->BindingURI(),
200 binding
= binding
->GetBaseBinding();
204 NS_ADDREF(*aResult
= urls
);