1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:tabstop=2:expandtab:shiftwidth=2:
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) 1998
21 * the Initial Developer. All Rights Reserved.
24 * L. David Baron <dbaron@dbaron.org>
25 * Pierre Phaneuf <pp@ludusdesign.com>
26 * Daniel Glazman <glazman@netscape.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either of the GNU General Public License Version 2 or later (the "GPL"),
30 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 /* representation of a CSS style sheet */
44 #ifndef nsCSSStyleSheet_h_
45 #define nsCSSStyleSheet_h_
49 #include "nsAutoPtr.h"
50 #include "nsIStyleSheet.h"
51 #include "nsIDOMCSSStyleSheet.h"
52 #include "nsICSSLoaderObserver.h"
53 #include "nsCOMArray.h"
56 class nsXMLNameSpaceMap
;
57 class nsCSSRuleProcessor
;
59 class nsICSSGroupRule
;
60 class nsICSSImportRule
;
64 class nsMediaQueryResultCacheKey
;
65 class nsCSSStyleSheet
;
67 template<class E
, class A
> class nsTArray
;
69 // -------------------------------
70 // CSS Style Sheet Inner Data Container
73 class nsCSSStyleSheetInner
{
75 friend class nsCSSStyleSheet
;
76 friend class nsCSSRuleProcessor
;
77 friend nsresult
NS_NewCSSStyleSheet(nsCSSStyleSheet
** aInstancePtrResult
);
79 nsCSSStyleSheetInner(nsCSSStyleSheet
* aPrimarySheet
);
80 nsCSSStyleSheetInner(nsCSSStyleSheetInner
& aCopy
,
81 nsCSSStyleSheet
* aPrimarySheet
);
82 ~nsCSSStyleSheetInner();
84 nsCSSStyleSheetInner
* CloneFor(nsCSSStyleSheet
* aPrimarySheet
);
85 void AddSheet(nsCSSStyleSheet
* aSheet
);
86 void RemoveSheet(nsCSSStyleSheet
* aSheet
);
88 void RebuildNameSpaces();
90 // Create a new namespace map
91 nsresult
CreateNamespaceMap();
93 nsAutoTArray
<nsCSSStyleSheet
*, 8> mSheets
;
94 nsCOMPtr
<nsIURI
> mSheetURI
; // for error reports, etc.
95 nsCOMPtr
<nsIURI
> mOriginalSheetURI
; // for GetHref. Can be null.
96 nsCOMPtr
<nsIURI
> mBaseURI
; // for resolving relative URIs
97 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
98 nsCOMArray
<nsICSSRule
> mOrderedRules
;
99 nsAutoPtr
<nsXMLNameSpaceMap
> mNameSpaceMap
;
100 // Linked list of child sheets. This is al fundamentally broken, because
101 // each of the child sheets has a unique parent... We can only hope (and
102 // currently this is the case) that any time page JS can get ts hands on a
103 // child sheet that means we've already ensured unique inners throughout its
104 // parent chain and things are good.
105 nsRefPtr
<nsCSSStyleSheet
> mFirstChild
;
109 PRBool mPrincipalSet
;
114 // -------------------------------
118 class CSSRuleListImpl
;
119 struct ChildSheetListBuilder
;
121 // CID for the nsCSSStyleSheet class
122 // ca926f30-2a7e-477e-8467-803fb32af20a
123 #define NS_CSS_STYLE_SHEET_IMPL_CID \
124 { 0xca926f30, 0x2a7e, 0x477e, \
125 { 0x84, 0x67, 0x80, 0x3f, 0xb3, 0x2a, 0xf2, 0x0a } }
128 class NS_FINAL_CLASS nsCSSStyleSheet
: public nsIStyleSheet
,
129 public nsIDOMCSSStyleSheet
,
130 public nsICSSLoaderObserver
137 NS_DECLARE_STATIC_IID_ACCESSOR(NS_CSS_STYLE_SHEET_IMPL_CID
)
139 // nsIStyleSheet interface
140 virtual nsIURI
* GetSheetURI() const;
141 virtual nsIURI
* GetBaseURI() const;
142 virtual void GetTitle(nsString
& aTitle
) const;
143 virtual void GetType(nsString
& aType
) const;
144 virtual PRBool
HasRules() const;
145 virtual PRBool
IsApplicable() const;
146 virtual void SetEnabled(PRBool aEnabled
);
147 virtual PRBool
IsComplete() const;
148 virtual void SetComplete();
149 virtual nsIStyleSheet
* GetParentSheet() const; // may be null
150 virtual nsIDocument
* GetOwningDocument() const; // may be null
151 virtual void SetOwningDocument(nsIDocument
* aDocument
);
153 // Find the ID of the owner outer window.
154 virtual PRUint64
FindOwningWindowID() const;
156 virtual void List(FILE* out
= stdout
, PRInt32 aIndent
= 0) const;
159 void AppendStyleSheet(nsCSSStyleSheet
* aSheet
);
160 void InsertStyleSheetAt(nsCSSStyleSheet
* aSheet
, PRInt32 aIndex
);
162 // XXX do these belong here or are they generic?
163 void PrependStyleRule(nsICSSRule
* aRule
);
164 void AppendStyleRule(nsICSSRule
* aRule
);
165 void ReplaceStyleRule(nsICSSRule
* aOld
, nsICSSRule
* aNew
);
167 PRInt32
StyleRuleCount() const;
168 nsresult
GetStyleRuleAt(PRInt32 aIndex
, nsICSSRule
*& aRule
) const;
170 nsresult
DeleteRuleFromGroup(nsICSSGroupRule
* aGroup
, PRUint32 aIndex
);
171 nsresult
InsertRuleIntoGroup(const nsAString
& aRule
, nsICSSGroupRule
* aGroup
, PRUint32 aIndex
, PRUint32
* _retval
);
172 nsresult
ReplaceRuleInGroup(nsICSSGroupRule
* aGroup
, nsICSSRule
* aOld
, nsICSSRule
* aNew
);
174 PRInt32
StyleSheetCount() const;
177 * SetURIs must be called on all sheets before parsing into them.
178 * SetURIs may only be called while the sheet is 1) incomplete and 2)
181 void SetURIs(nsIURI
* aSheetURI
, nsIURI
* aOriginalSheetURI
, nsIURI
* aBaseURI
);
184 * SetPrincipal should be called on all sheets before parsing into them.
185 * This can only be called once with a non-null principal. Calling this with
186 * a null pointer is allowed and is treated as a no-op.
188 void SetPrincipal(nsIPrincipal
* aPrincipal
);
190 // Principal() never returns a null pointer.
191 nsIPrincipal
* Principal() const { return mInner
->mPrincipal
; }
193 void SetTitle(const nsAString
& aTitle
) { mTitle
= aTitle
; }
194 void SetMedia(nsMediaList
* aMedia
);
195 void SetOwningNode(nsIDOMNode
* aOwningNode
) { mOwningNode
= aOwningNode
; /* Not ref counted */ }
197 void SetOwnerRule(nsICSSImportRule
* aOwnerRule
) { mOwnerRule
= aOwnerRule
; /* Not ref counted */ }
198 nsICSSImportRule
* GetOwnerRule() const { return mOwnerRule
; }
200 nsXMLNameSpaceMap
* GetNameSpaceMap() const { return mInner
->mNameSpaceMap
; }
202 already_AddRefed
<nsCSSStyleSheet
> Clone(nsCSSStyleSheet
* aCloneParent
,
203 nsICSSImportRule
* aCloneOwnerRule
,
204 nsIDocument
* aCloneDocument
,
205 nsIDOMNode
* aCloneOwningNode
) const;
207 PRBool
IsModified() const { return mDirty
; }
208 void SetModified(PRBool aModified
) { mDirty
= aModified
; }
210 nsresult
AddRuleProcessor(nsCSSRuleProcessor
* aProcessor
);
211 nsresult
DropRuleProcessor(nsCSSRuleProcessor
* aProcessor
);
214 * Like the DOM insertRule() method, but doesn't do any security checks
216 nsresult
InsertRuleInternal(const nsAString
& aRule
,
217 PRUint32 aIndex
, PRUint32
* aReturn
);
219 /* Get the URI this sheet was originally loaded from, if any. Can
221 virtual nsIURI
* GetOriginalURI() const;
223 // nsICSSLoaderObserver interface
224 NS_IMETHOD
StyleSheetLoaded(nsCSSStyleSheet
* aSheet
, PRBool aWasAlternate
,
227 enum EnsureUniqueInnerResult
{
228 // No work was needed to ensure a unique inner.
229 eUniqueInner_AlreadyUnique
,
230 // A clone was done to ensure a unique inner (which means the style
231 // rules in this sheet have changed).
232 eUniqueInner_ClonedInner
,
233 // A clone was attempted, but it failed.
234 eUniqueInner_CloneFailed
236 EnsureUniqueInnerResult
EnsureUniqueInner();
238 // Append all of this sheet's child sheets to aArray. Return PR_TRUE
239 // on success and PR_FALSE on allocation failure.
240 PRBool
AppendAllChildSheets(nsTArray
<nsCSSStyleSheet
*>& aArray
);
242 PRBool
UseForPresentation(nsPresContext
* aPresContext
,
243 nsMediaQueryResultCacheKey
& aKey
) const;
245 // nsIDOMStyleSheet interface
246 NS_DECL_NSIDOMSTYLESHEET
248 // nsIDOMCSSStyleSheet interface
249 NS_DECL_NSIDOMCSSSTYLESHEET
251 // Function used as a callback to rebuild our inner's child sheet
252 // list after we clone a unique inner for ourselves.
253 static PRBool
RebuildChildList(nsICSSRule
* aRule
, void* aBuilder
);
256 nsCSSStyleSheet(const nsCSSStyleSheet
& aCopy
,
257 nsCSSStyleSheet
* aParentToUse
,
258 nsICSSImportRule
* aOwnerRuleToUse
,
259 nsIDocument
* aDocumentToUse
,
260 nsIDOMNode
* aOwningNodeToUse
);
262 // These are not supported and are not implemented!
263 nsCSSStyleSheet(const nsCSSStyleSheet
& aCopy
);
264 nsCSSStyleSheet
& operator=(const nsCSSStyleSheet
& aCopy
);
267 virtual ~nsCSSStyleSheet();
269 void ClearRuleCascades();
271 nsresult
WillDirty();
274 // Return success if the subject principal subsumes the principal of our
275 // inner, error otherwise. This will also succeed if the subject has
276 // UniversalBrowserWrite.
277 nsresult
SubjectSubsumesInnerPrincipal() const;
279 // Add the namespace mapping from this @namespace rule to our namespace map
280 nsresult
RegisterNamespaceRule(nsICSSRule
* aRule
);
284 nsRefPtr
<nsMediaList
> mMedia
;
285 nsRefPtr
<nsCSSStyleSheet
> mNext
;
286 nsCSSStyleSheet
* mParent
; // weak ref
287 nsICSSImportRule
* mOwnerRule
; // weak ref
289 CSSRuleListImpl
* mRuleCollection
;
290 nsIDocument
* mDocument
; // weak ref; parents maintain this for their children
291 nsIDOMNode
* mOwningNode
; // weak ref
292 PRPackedBool mDisabled
;
293 PRPackedBool mDirty
; // has been modified
295 nsCSSStyleSheetInner
* mInner
;
297 nsAutoTArray
<nsCSSRuleProcessor
*, 8>* mRuleProcessors
;
299 friend class nsMediaList
;
300 friend class nsCSSRuleProcessor
;
301 friend nsresult
NS_NewCSSStyleSheet(nsCSSStyleSheet
** aInstancePtrResult
);
302 friend struct ChildSheetListBuilder
;
305 NS_DEFINE_STATIC_IID_ACCESSOR(nsCSSStyleSheet
, NS_CSS_STYLE_SHEET_IMPL_CID
)
307 nsresult
NS_NewCSSStyleSheet(nsCSSStyleSheet
** aInstancePtrResult
);
309 #endif /* !defined(nsCSSStyleSheet_h_) */