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
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.
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"
47 #include "nsCSSPseudoElements.h"
48 #include "nsIStyleRule.h"
50 #include "nsICSSStyleRule.h"
51 #include "nsIStyleRuleProcessor.h"
52 #include "nsPresContext.h"
53 #include "nsIDocument.h"
55 #include "nsRuleWalker.h"
56 #include "nsRuleData.h"
58 // -----------------------------------------------------------
60 class HTMLCSSStyleSheetImpl
: public nsIHTMLCSSStyleSheet
,
61 public nsIStyleRuleProcessor
{
63 HTMLCSSStyleSheetImpl();
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
,
102 virtual void List(FILE* out
= stdout
, PRInt32 aIndent
= 0) const;
106 // These are not supported and are not implemented!
107 HTMLCSSStyleSheetImpl(const HTMLCSSStyleSheetImpl
& aCopy
);
108 HTMLCSSStyleSheetImpl
& operator=(const HTMLCSSStyleSheetImpl
& aCopy
);
111 virtual ~HTMLCSSStyleSheetImpl();
115 nsIDocument
* mDocument
;
119 HTMLCSSStyleSheetImpl::HTMLCSSStyleSheetImpl()
120 : nsIHTMLCSSStyleSheet(),
127 HTMLCSSStyleSheetImpl::~HTMLCSSStyleSheetImpl()
132 NS_IMPL_ISUPPORTS3(HTMLCSSStyleSheetImpl
,
133 nsIHTMLCSSStyleSheet
,
135 nsIStyleRuleProcessor
)
138 HTMLCSSStyleSheetImpl::RulesMatching(ElementRuleProcessorData
* aData
)
140 nsIContent
* content
= aData
->mContent
;
143 // just get the one and only style rule from the content's STYLE attribute
144 nsICSSStyleRule
* rule
= content
->GetInlineStyleRule();
146 aData
->mRuleWalker
->Forward(rule
);
153 HTMLCSSStyleSheetImpl::RulesMatching(PseudoRuleProcessorData
* aData
)
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!
174 // Test if style is dependent on content state
176 HTMLCSSStyleSheetImpl::HasStateDependentStyle(StateRuleProcessorData
* aData
,
177 nsReStyleHint
* aResult
)
179 *aResult
= nsReStyleHint(0);
183 // Test if style is dependent on attribute
185 HTMLCSSStyleSheetImpl::HasAttributeDependentStyle(AttributeRuleProcessorData
* aData
,
186 nsReStyleHint
* aResult
)
188 *aResult
= nsReStyleHint(0);
193 HTMLCSSStyleSheetImpl::MediumFeaturesChanged(nsPresContext
* aPresContext
,
194 PRBool
* aRulesChanged
)
196 *aRulesChanged
= PR_FALSE
;
202 HTMLCSSStyleSheetImpl::Reset(nsIURI
* aURL
)
212 HTMLCSSStyleSheetImpl::GetSheetURI(nsIURI
** aSheetURL
) const
220 HTMLCSSStyleSheetImpl::GetBaseURI(nsIURI
** aBaseURL
) const
228 HTMLCSSStyleSheetImpl::GetTitle(nsString
& aTitle
) const
230 aTitle
.AssignLiteral("Internal HTML/CSS Style Sheet");
235 HTMLCSSStyleSheetImpl::GetType(nsString
& aType
) const
237 aType
.AssignLiteral("text/html");
241 NS_IMETHODIMP_(PRBool
)
242 HTMLCSSStyleSheetImpl::HasRules() const
244 // Say we always have rules, since we don't know.
249 HTMLCSSStyleSheetImpl::GetApplicable(PRBool
& aApplicable
) const
251 aApplicable
= PR_TRUE
;
256 HTMLCSSStyleSheetImpl::SetEnabled(PRBool aEnabled
)
257 { // these can't be disabled
262 HTMLCSSStyleSheetImpl::GetComplete(PRBool
& aComplete
) const
269 HTMLCSSStyleSheetImpl::SetComplete()
274 // style sheet owner info
276 HTMLCSSStyleSheetImpl::GetParentSheet(nsIStyleSheet
*& aParent
) const
283 HTMLCSSStyleSheetImpl::GetOwningDocument(nsIDocument
*& aDocument
) const
285 NS_IF_ADDREF(mDocument
);
286 aDocument
= mDocument
;
291 HTMLCSSStyleSheetImpl::SetOwningDocument(nsIDocument
* aDocument
)
293 mDocument
= aDocument
;
298 void HTMLCSSStyleSheetImpl::List(FILE* out
, PRInt32 aIndent
) const
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
);
313 // XXX For backwards compatibility and convenience
315 NS_NewHTMLCSSStyleSheet(nsIHTMLCSSStyleSheet
** aInstancePtrResult
,
316 nsIURI
* aURL
, nsIDocument
* aDocument
)
319 nsIHTMLCSSStyleSheet
* sheet
;
320 if (NS_FAILED(rv
= NS_NewHTMLCSSStyleSheet(&sheet
)))
323 if (NS_FAILED(rv
= sheet
->Init(aURL
, aDocument
))) {
328 *aInstancePtrResult
= sheet
;
333 NS_NewHTMLCSSStyleSheet(nsIHTMLCSSStyleSheet
** aInstancePtrResult
)
335 if (aInstancePtrResult
== nsnull
) {
336 return NS_ERROR_NULL_POINTER
;
339 HTMLCSSStyleSheetImpl
* it
= new HTMLCSSStyleSheetImpl();
342 return NS_ERROR_OUT_OF_MEMORY
;
346 *aInstancePtrResult
= it
;