Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / style / nsHTMLCSSStyleSheet.cpp
blob8739e465731b98d7e57951a9e10ce96235f28d6e
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
13 * License.
15 * The Original Code is mozilla.org 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.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 * style sheet and style rule processor representing style attributes
43 #include "nsIHTMLCSSStyleSheet.h"
44 #include "nsCRT.h"
45 #include "nsIAtom.h"
46 #include "nsIURL.h"
47 #include "nsCSSPseudoElements.h"
48 #include "nsIStyleRule.h"
49 #include "nsIFrame.h"
50 #include "nsICSSStyleRule.h"
51 #include "nsIStyleRuleProcessor.h"
52 #include "nsPresContext.h"
53 #include "nsIDocument.h"
54 #include "nsCOMPtr.h"
55 #include "nsRuleWalker.h"
56 #include "nsRuleData.h"
58 // -----------------------------------------------------------
60 class HTMLCSSStyleSheetImpl : public nsIHTMLCSSStyleSheet,
61 public nsIStyleRuleProcessor {
62 public:
63 HTMLCSSStyleSheetImpl();
65 NS_DECL_ISUPPORTS
67 // basic style sheet data
68 NS_IMETHOD Init(nsIURI* aURL, nsIDocument* aDocument);
69 NS_IMETHOD Reset(nsIURI* aURL);
70 NS_IMETHOD GetSheetURI(nsIURI** aSheetURL) const;
71 NS_IMETHOD GetBaseURI(nsIURI** aBaseURL) const;
72 NS_IMETHOD GetTitle(nsString& aTitle) const;
73 NS_IMETHOD GetType(nsString& aType) const;
74 NS_IMETHOD_(PRBool) HasRules() const;
76 NS_IMETHOD GetApplicable(PRBool& aApplicable) const;
78 NS_IMETHOD SetEnabled(PRBool aEnabled);
80 NS_IMETHOD GetComplete(PRBool& aComplete) const;
81 NS_IMETHOD SetComplete();
83 // style sheet owner info
84 NS_IMETHOD GetParentSheet(nsIStyleSheet*& aParent) const; // will be null
85 NS_IMETHOD GetOwningDocument(nsIDocument*& aDocument) const;
86 NS_IMETHOD SetOwningDocument(nsIDocument* aDocument);
88 // nsIStyleRuleProcessor api
89 NS_IMETHOD RulesMatching(ElementRuleProcessorData* aData);
91 NS_IMETHOD RulesMatching(PseudoRuleProcessorData* aData);
93 NS_IMETHOD HasStateDependentStyle(StateRuleProcessorData* aData,
94 nsReStyleHint* aResult);
96 NS_IMETHOD HasAttributeDependentStyle(AttributeRuleProcessorData* aData,
97 nsReStyleHint* aResult);
98 NS_IMETHOD MediumFeaturesChanged(nsPresContext* aPresContext,
99 PRBool* aResult);
101 #ifdef DEBUG
102 virtual void List(FILE* out = stdout, PRInt32 aIndent = 0) const;
103 #endif
105 private:
106 // These are not supported and are not implemented!
107 HTMLCSSStyleSheetImpl(const HTMLCSSStyleSheetImpl& aCopy);
108 HTMLCSSStyleSheetImpl& operator=(const HTMLCSSStyleSheetImpl& aCopy);
110 protected:
111 virtual ~HTMLCSSStyleSheetImpl();
113 protected:
114 nsIURI* mURL;
115 nsIDocument* mDocument;
119 HTMLCSSStyleSheetImpl::HTMLCSSStyleSheetImpl()
120 : nsIHTMLCSSStyleSheet(),
121 mRefCnt(0),
122 mURL(nsnull),
123 mDocument(nsnull)
127 HTMLCSSStyleSheetImpl::~HTMLCSSStyleSheetImpl()
129 NS_RELEASE(mURL);
132 NS_IMPL_ISUPPORTS3(HTMLCSSStyleSheetImpl,
133 nsIHTMLCSSStyleSheet,
134 nsIStyleSheet,
135 nsIStyleRuleProcessor)
137 NS_IMETHODIMP
138 HTMLCSSStyleSheetImpl::RulesMatching(ElementRuleProcessorData* aData)
140 nsIContent* content = aData->mContent;
142 if (content) {
143 // just get the one and only style rule from the content's STYLE attribute
144 nsICSSStyleRule* rule = content->GetInlineStyleRule();
145 if (rule)
146 aData->mRuleWalker->Forward(rule);
149 return NS_OK;
152 NS_IMETHODIMP
153 HTMLCSSStyleSheetImpl::RulesMatching(PseudoRuleProcessorData* aData)
155 return NS_OK;
158 NS_IMETHODIMP
159 HTMLCSSStyleSheetImpl::Init(nsIURI* aURL, nsIDocument* aDocument)
161 NS_PRECONDITION(aURL && aDocument, "null ptr");
162 if (! aURL || ! aDocument)
163 return NS_ERROR_NULL_POINTER;
165 if (mURL || mDocument)
166 return NS_ERROR_ALREADY_INITIALIZED;
168 mDocument = aDocument; // not refcounted!
169 mURL = aURL;
170 NS_ADDREF(mURL);
171 return NS_OK;
174 // Test if style is dependent on content state
175 NS_IMETHODIMP
176 HTMLCSSStyleSheetImpl::HasStateDependentStyle(StateRuleProcessorData* aData,
177 nsReStyleHint* aResult)
179 *aResult = nsReStyleHint(0);
180 return NS_OK;
183 // Test if style is dependent on attribute
184 NS_IMETHODIMP
185 HTMLCSSStyleSheetImpl::HasAttributeDependentStyle(AttributeRuleProcessorData* aData,
186 nsReStyleHint* aResult)
188 *aResult = nsReStyleHint(0);
189 return NS_OK;
192 NS_IMETHODIMP
193 HTMLCSSStyleSheetImpl::MediumFeaturesChanged(nsPresContext* aPresContext,
194 PRBool* aRulesChanged)
196 *aRulesChanged = PR_FALSE;
197 return NS_OK;
201 NS_IMETHODIMP
202 HTMLCSSStyleSheetImpl::Reset(nsIURI* aURL)
204 NS_IF_RELEASE(mURL);
205 mURL = aURL;
206 NS_ADDREF(mURL);
208 return NS_OK;
211 NS_IMETHODIMP
212 HTMLCSSStyleSheetImpl::GetSheetURI(nsIURI** aSheetURL) const
214 NS_IF_ADDREF(mURL);
215 *aSheetURL = mURL;
216 return NS_OK;
219 NS_IMETHODIMP
220 HTMLCSSStyleSheetImpl::GetBaseURI(nsIURI** aBaseURL) const
222 NS_IF_ADDREF(mURL);
223 *aBaseURL = mURL;
224 return NS_OK;
227 NS_IMETHODIMP
228 HTMLCSSStyleSheetImpl::GetTitle(nsString& aTitle) const
230 aTitle.AssignLiteral("Internal HTML/CSS Style Sheet");
231 return NS_OK;
234 NS_IMETHODIMP
235 HTMLCSSStyleSheetImpl::GetType(nsString& aType) const
237 aType.AssignLiteral("text/html");
238 return NS_OK;
241 NS_IMETHODIMP_(PRBool)
242 HTMLCSSStyleSheetImpl::HasRules() const
244 // Say we always have rules, since we don't know.
245 return PR_TRUE;
248 NS_IMETHODIMP
249 HTMLCSSStyleSheetImpl::GetApplicable(PRBool& aApplicable) const
251 aApplicable = PR_TRUE;
252 return NS_OK;
255 NS_IMETHODIMP
256 HTMLCSSStyleSheetImpl::SetEnabled(PRBool aEnabled)
257 { // these can't be disabled
258 return NS_OK;
261 NS_IMETHODIMP
262 HTMLCSSStyleSheetImpl::GetComplete(PRBool& aComplete) const
264 aComplete = PR_TRUE;
265 return NS_OK;
268 NS_IMETHODIMP
269 HTMLCSSStyleSheetImpl::SetComplete()
271 return NS_OK;
274 // style sheet owner info
275 NS_IMETHODIMP
276 HTMLCSSStyleSheetImpl::GetParentSheet(nsIStyleSheet*& aParent) const
278 aParent = nsnull;
279 return NS_OK;
282 NS_IMETHODIMP
283 HTMLCSSStyleSheetImpl::GetOwningDocument(nsIDocument*& aDocument) const
285 NS_IF_ADDREF(mDocument);
286 aDocument = mDocument;
287 return NS_OK;
290 NS_IMETHODIMP
291 HTMLCSSStyleSheetImpl::SetOwningDocument(nsIDocument* aDocument)
293 mDocument = aDocument;
294 return NS_OK;
297 #ifdef DEBUG
298 void HTMLCSSStyleSheetImpl::List(FILE* out, PRInt32 aIndent) const
300 // Indent
301 for (PRInt32 index = aIndent; --index >= 0; ) fputs(" ", out);
303 fputs("HTML CSS Style Sheet: ", out);
304 nsCAutoString urlSpec;
305 mURL->GetSpec(urlSpec);
306 if (!urlSpec.IsEmpty()) {
307 fputs(urlSpec.get(), out);
309 fputs("\n", out);
311 #endif
313 // XXX For backwards compatibility and convenience
314 nsresult
315 NS_NewHTMLCSSStyleSheet(nsIHTMLCSSStyleSheet** aInstancePtrResult,
316 nsIURI* aURL, nsIDocument* aDocument)
318 nsresult rv;
319 nsIHTMLCSSStyleSheet* sheet;
320 if (NS_FAILED(rv = NS_NewHTMLCSSStyleSheet(&sheet)))
321 return rv;
323 if (NS_FAILED(rv = sheet->Init(aURL, aDocument))) {
324 NS_RELEASE(sheet);
325 return rv;
328 *aInstancePtrResult = sheet;
329 return NS_OK;
332 nsresult
333 NS_NewHTMLCSSStyleSheet(nsIHTMLCSSStyleSheet** aInstancePtrResult)
335 if (aInstancePtrResult == nsnull) {
336 return NS_ERROR_NULL_POINTER;
339 HTMLCSSStyleSheetImpl* it = new HTMLCSSStyleSheetImpl();
341 if (nsnull == it) {
342 return NS_ERROR_OUT_OF_MEMORY;
345 NS_ADDREF(it);
346 *aInstancePtrResult = it;
347 return NS_OK;