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 * David Hyatt <hyatt@netscape.com>
24 * Daniel Glazman <glazman@netscape.com>
25 * L. David Baron <dbaron@dbaron.org>, Mozilla Corporation
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * representation of CSS style rules (selectors+declaration), CSS
43 * selectors, and DOM objects for style rules, selectors, and
48 #include "nsCSSRule.h"
49 #include "nsICSSStyleRule.h"
50 #include "nsICSSGroupRule.h"
51 #include "nsCSSDeclaration.h"
52 #include "nsICSSStyleSheet.h"
53 #include "nsICSSParser.h"
54 #include "nsICSSLoader.h"
56 #include "nsPresContext.h"
57 #include "nsIDocument.h"
58 #include "nsIDeviceContext.h"
62 #include "nsStyleConsts.h"
63 #include "nsStyleUtil.h"
64 #include "nsIFontMetrics.h"
65 #include "nsIDOMCSSStyleSheet.h"
66 #include "nsICSSStyleRuleDOMWrapper.h"
67 #include "nsIDOMCSSStyleDeclaration.h"
68 #include "nsDOMCSSDeclaration.h"
69 #include "nsINameSpaceManager.h"
70 #include "nsXMLNameSpaceMap.h"
71 #include "nsILookAndFeel.h"
72 #include "nsRuleNode.h"
73 #include "nsUnicharUtils.h"
74 #include "nsCSSPseudoElements.h"
75 #include "nsIPrincipal.h"
76 #include "nsComponentManagerUtils.h"
77 #include "nsCSSPseudoClasses.h"
79 #include "nsContentUtils.h"
80 #include "nsContentErrors.h"
81 #include "mozAutoDocUpdate.h"
83 #define NS_IF_CLONE(member_) \
86 result->member_ = member_->Clone(); \
87 if (!result->member_) { \
94 #define NS_IF_DELETE(ptr) \
100 /* ************************************************************************** */
102 nsAtomList::nsAtomList(nsIAtom
* aAtom
)
106 MOZ_COUNT_CTOR(nsAtomList
);
109 nsAtomList::nsAtomList(const nsString
& aAtomValue
)
113 MOZ_COUNT_CTOR(nsAtomList
);
114 mAtom
= do_GetAtom(aAtomValue
);
118 nsAtomList::Clone(PRBool aDeep
) const
120 nsAtomList
*result
= new nsAtomList(mAtom
);
125 NS_CSS_CLONE_LIST_MEMBER(nsAtomList
, this, mNext
, result
, (PR_FALSE
));
129 nsAtomList::~nsAtomList(void)
131 MOZ_COUNT_DTOR(nsAtomList
);
132 NS_CSS_DELETE_LIST_MEMBER(nsAtomList
, this, mNext
);
135 nsPseudoClassList::nsPseudoClassList(nsIAtom
* aAtom
)
139 NS_ASSERTION(!nsCSSPseudoClasses::HasStringArg(aAtom
) &&
140 !nsCSSPseudoClasses::HasNthPairArg(aAtom
),
141 "unexpected pseudo-class");
142 MOZ_COUNT_CTOR(nsPseudoClassList
);
146 nsPseudoClassList::nsPseudoClassList(nsIAtom
* aAtom
, const PRUnichar
* aString
)
150 NS_ASSERTION(nsCSSPseudoClasses::HasStringArg(aAtom
),
151 "unexpected pseudo-class");
152 NS_ASSERTION(aString
, "string expected");
153 MOZ_COUNT_CTOR(nsPseudoClassList
);
154 u
.mString
= NS_strdup(aString
);
157 nsPseudoClassList::nsPseudoClassList(nsIAtom
* aAtom
, const PRInt32
* aIntPair
)
161 NS_ASSERTION(nsCSSPseudoClasses::HasNthPairArg(aAtom
),
162 "unexpected pseudo-class");
163 NS_ASSERTION(aIntPair
, "integer pair expected");
164 MOZ_COUNT_CTOR(nsPseudoClassList
);
166 static_cast<PRInt32
*>(nsMemory::Clone(aIntPair
, sizeof(PRInt32
) * 2));
170 nsPseudoClassList::Clone(PRBool aDeep
) const
172 nsPseudoClassList
*result
;
174 result
= new nsPseudoClassList(mAtom
);
175 } else if (nsCSSPseudoClasses::HasStringArg(mAtom
)) {
176 result
= new nsPseudoClassList(mAtom
, u
.mString
);
178 NS_ASSERTION(nsCSSPseudoClasses::HasNthPairArg(mAtom
),
179 "unexpected pseudo-class");
180 result
= new nsPseudoClassList(mAtom
, u
.mNumbers
);
184 NS_CSS_CLONE_LIST_MEMBER(nsPseudoClassList
, this, mNext
, result
,
190 nsPseudoClassList::~nsPseudoClassList(void)
192 MOZ_COUNT_DTOR(nsPseudoClassList
);
195 NS_CSS_DELETE_LIST_MEMBER(nsPseudoClassList
, this, mNext
);
198 nsAttrSelector::nsAttrSelector(PRInt32 aNameSpace
, const nsString
& aAttr
)
199 : mNameSpace(aNameSpace
),
201 mFunction(NS_ATTR_FUNC_SET
),
206 MOZ_COUNT_CTOR(nsAttrSelector
);
208 mAttr
= do_GetAtom(aAttr
);
211 nsAttrSelector::nsAttrSelector(PRInt32 aNameSpace
, const nsString
& aAttr
, PRUint8 aFunction
,
212 const nsString
& aValue
, PRBool aCaseSensitive
)
213 : mNameSpace(aNameSpace
),
215 mFunction(aFunction
),
216 mCaseSensitive(aCaseSensitive
),
220 MOZ_COUNT_CTOR(nsAttrSelector
);
222 mAttr
= do_GetAtom(aAttr
);
225 nsAttrSelector::nsAttrSelector(PRInt32 aNameSpace
, nsIAtom
* aAttr
,
226 PRUint8 aFunction
, const nsString
& aValue
,
227 PRBool aCaseSensitive
)
228 : mNameSpace(aNameSpace
),
230 mFunction(aFunction
),
231 mCaseSensitive(aCaseSensitive
),
235 MOZ_COUNT_CTOR(nsAttrSelector
);
239 nsAttrSelector::Clone(PRBool aDeep
) const
241 nsAttrSelector
*result
=
242 new nsAttrSelector(mNameSpace
, mAttr
, mFunction
, mValue
, mCaseSensitive
);
245 NS_CSS_CLONE_LIST_MEMBER(nsAttrSelector
, this, mNext
, result
, (PR_FALSE
));
250 nsAttrSelector::~nsAttrSelector(void)
252 MOZ_COUNT_DTOR(nsAttrSelector
);
254 NS_CSS_DELETE_LIST_MEMBER(nsAttrSelector
, this, mNext
);
257 // -- nsCSSSelector -------------------------------
259 nsCSSSelector::nsCSSSelector(void)
260 : mNameSpace(kNameSpaceID_Unknown
), mTag(nsnull
),
263 mPseudoClassList(nsnull
),
269 MOZ_COUNT_CTOR(nsCSSSelector
);
273 nsCSSSelector::Clone(PRBool aDeepNext
, PRBool aDeepNegations
) const
275 nsCSSSelector
*result
= new nsCSSSelector();
279 result
->mNameSpace
= mNameSpace
;
282 NS_IF_CLONE(mIDList
);
283 NS_IF_CLONE(mClassList
);
284 NS_IF_CLONE(mPseudoClassList
);
285 NS_IF_CLONE(mAttrList
);
287 // No need to worry about multiple levels of recursion since an
288 // mNegations can't have an mNext.
289 NS_ASSERTION(!mNegations
|| !mNegations
->mNext
,
290 "mNegations can't have non-null mNext");
291 if (aDeepNegations
) {
292 NS_CSS_CLONE_LIST_MEMBER(nsCSSSelector
, this, mNegations
, result
,
293 (PR_TRUE
, PR_FALSE
));
297 NS_CSS_CLONE_LIST_MEMBER(nsCSSSelector
, this, mNext
, result
,
298 (PR_FALSE
, PR_TRUE
));
304 nsCSSSelector::~nsCSSSelector(void)
306 MOZ_COUNT_DTOR(nsCSSSelector
);
308 // No need to worry about multiple levels of recursion since an
309 // mNegations can't have an mNext.
310 NS_CSS_DELETE_LIST_MEMBER(nsCSSSelector
, this, mNext
);
313 void nsCSSSelector::Reset(void)
315 mNameSpace
= kNameSpaceID_Unknown
;
317 NS_IF_DELETE(mIDList
);
318 NS_IF_DELETE(mClassList
);
319 NS_IF_DELETE(mPseudoClassList
);
320 NS_IF_DELETE(mAttrList
);
321 // No need to worry about multiple levels of recursion since an
322 // mNegations can't have an mNext.
323 NS_ASSERTION(!mNegations
|| !mNegations
->mNext
,
324 "mNegations can't have non-null mNext");
325 NS_CSS_DELETE_LIST_MEMBER(nsCSSSelector
, this, mNegations
);
326 mOperator
= PRUnichar(0);
329 void nsCSSSelector::SetNameSpace(PRInt32 aNameSpace
)
331 mNameSpace
= aNameSpace
;
334 void nsCSSSelector::SetTag(const nsString
& aTag
)
339 mTag
= do_GetAtom(aTag
);
342 void nsCSSSelector::AddID(const nsString
& aID
)
344 if (!aID
.IsEmpty()) {
345 nsAtomList
** list
= &mIDList
;
346 while (nsnull
!= *list
) {
347 list
= &((*list
)->mNext
);
349 *list
= new nsAtomList(aID
);
353 void nsCSSSelector::AddClass(const nsString
& aClass
)
355 if (!aClass
.IsEmpty()) {
356 nsAtomList
** list
= &mClassList
;
357 while (nsnull
!= *list
) {
358 list
= &((*list
)->mNext
);
360 *list
= new nsAtomList(aClass
);
364 void nsCSSSelector::AddPseudoClass(nsIAtom
* aPseudoClass
)
366 AddPseudoClassInternal(new nsPseudoClassList(aPseudoClass
));
369 void nsCSSSelector::AddPseudoClass(nsIAtom
* aPseudoClass
,
370 const PRUnichar
* aString
)
372 AddPseudoClassInternal(new nsPseudoClassList(aPseudoClass
, aString
));
375 void nsCSSSelector::AddPseudoClass(nsIAtom
* aPseudoClass
,
376 const PRInt32
* aIntPair
)
378 AddPseudoClassInternal(new nsPseudoClassList(aPseudoClass
, aIntPair
));
381 void nsCSSSelector::AddPseudoClassInternal(nsPseudoClassList
*aPseudoClass
)
383 nsPseudoClassList
** list
= &mPseudoClassList
;
384 while (nsnull
!= *list
) {
385 list
= &((*list
)->mNext
);
387 *list
= aPseudoClass
;
390 void nsCSSSelector::AddAttribute(PRInt32 aNameSpace
, const nsString
& aAttr
)
392 if (!aAttr
.IsEmpty()) {
393 nsAttrSelector
** list
= &mAttrList
;
394 while (nsnull
!= *list
) {
395 list
= &((*list
)->mNext
);
397 *list
= new nsAttrSelector(aNameSpace
, aAttr
);
401 void nsCSSSelector::AddAttribute(PRInt32 aNameSpace
, const nsString
& aAttr
, PRUint8 aFunc
,
402 const nsString
& aValue
, PRBool aCaseSensitive
)
404 if (!aAttr
.IsEmpty()) {
405 nsAttrSelector
** list
= &mAttrList
;
406 while (nsnull
!= *list
) {
407 list
= &((*list
)->mNext
);
409 *list
= new nsAttrSelector(aNameSpace
, aAttr
, aFunc
, aValue
, aCaseSensitive
);
413 void nsCSSSelector::SetOperator(PRUnichar aOperator
)
415 mOperator
= aOperator
;
418 PRInt32
nsCSSSelector::CalcWeight(void) const
422 if (nsnull
!= mTag
) {
425 nsAtomList
* list
= mIDList
;
426 while (nsnull
!= list
) {
431 while (nsnull
!= list
) {
435 nsPseudoClassList
*plist
= mPseudoClassList
;
436 while (nsnull
!= plist
) {
438 plist
= plist
->mNext
;
440 nsAttrSelector
* attr
= mAttrList
;
441 while (nsnull
!= attr
) {
445 if (nsnull
!= mNegations
) {
446 weight
+= mNegations
->CalcWeight();
451 // pseudo-elements are stored in the selectors' chain using fictional elements;
452 // these fictional elements have mTag starting with a colon
453 static PRBool
IsPseudoElement(nsIAtom
* aAtom
)
457 aAtom
->GetUTF8String(&str
);
458 return str
&& (*str
== ':');
464 void nsCSSSelector::AppendNegationToString(nsAString
& aString
)
466 aString
.AppendLiteral(":not(");
470 // Builds the textual representation of a selector. Called by DOM 2 CSS
471 // StyleRule:selectorText
474 nsCSSSelector::ToString(nsAString
& aString
, nsICSSStyleSheet
* aSheet
,
475 PRBool aAppend
) const
480 ToStringInternal(aString
, aSheet
, IsPseudoElement(mTag
), PR_FALSE
);
483 void nsCSSSelector::ToStringInternal(nsAString
& aString
,
484 nsICSSStyleSheet
* aSheet
,
485 PRBool aIsPseudoElem
,
486 PRBool aIsNegated
) const
489 PRBool isPseudoElement
= IsPseudoElement(mTag
);
491 // selectors are linked from right-to-left, so the next selector in the linked list
492 // actually precedes this one in the resulting string
494 mNext
->ToStringInternal(aString
, aSheet
, IsPseudoElement(mTag
), 0);
495 if (!aIsNegated
&& !isPseudoElement
) {
496 // don't add a leading whitespace if we have a pseudo-element
497 // or a negated simple selector
498 aString
.Append(PRUnichar(' '));
502 // For non-pseudo-element selectors or for lone pseudo-elements, deal with
503 // namespace prefixes.
504 PRBool wroteNamespace
= PR_FALSE
;
505 if (!isPseudoElement
|| !mNext
) {
506 // append the namespace prefix if needed
507 if (mNameSpace
== kNameSpaceID_None
) {
508 // The only way to do this in CSS is to have an explicit namespace
509 // of "none" specified in the sheet by having a '|' with nothing
511 aString
.Append(PRUnichar('|'));
512 wroteNamespace
= PR_TRUE
;
515 nsXMLNameSpaceMap
*sheetNS
= aSheet
->GetNameSpaceMap();
517 // sheetNS is non-null if and only if we had an @namespace rule. If it's
518 // null, that means that the only namespaces we could have are the
519 // wildcard namespace (which can be implicit in this case) and the "none"
520 // namespace, which we handled above. So no need to output anything when
523 if (mNameSpace
!= kNameSpaceID_Unknown
) {
524 if (sheetNS
->FindNameSpaceID(nsnull
) != mNameSpace
) {
525 nsIAtom
*prefixAtom
= sheetNS
->FindPrefix(mNameSpace
);
526 NS_ASSERTION(prefixAtom
, "how'd we get a non-default namespace "
527 "without a prefix?");
529 prefixAtom
->ToString(prefix
);
530 aString
.Append(prefix
);
531 aString
.Append(PRUnichar('|'));
532 wroteNamespace
= PR_TRUE
;
534 // otherwise it must be the default namespace
536 // A selector for an element in any namespace.
537 if (// Use explicit "*|" only when it's not implied
538 sheetNS
->FindNameSpaceID(nsnull
) != kNameSpaceID_None
&&
539 // :not() is special in that the default namespace is
540 // not implied for non-type selectors
541 (!aIsNegated
|| (!mIDList
&& !mClassList
&&
542 !mPseudoClassList
&& !mAttrList
))) {
543 aString
.AppendLiteral("*|");
544 wroteNamespace
= PR_TRUE
;
553 // Universal selector: avoid writing the universal selector when we
554 // can avoid it, especially since we're required to avoid it for the
556 if (wroteNamespace
||
557 (!mIDList
&& !mClassList
&& !mPseudoClassList
&& !mAttrList
&&
558 (aIsNegated
|| !mNegations
))) {
559 aString
.Append(PRUnichar('*'));
562 // Append the tag name
563 if (isPseudoElement
) {
565 // Lone pseudo-element selector -- toss in a wildcard type selector
567 aString
.Append(PRUnichar('*'));
569 if (!nsCSSPseudoElements::IsCSS2PseudoElement(mTag
)) {
570 aString
.Append(PRUnichar(':'));
574 mTag
->ToString(prefix
);
575 aString
.Append(prefix
);
578 // Append the id, if there is one
580 nsAtomList
* list
= mIDList
;
581 while (list
!= nsnull
) {
582 list
->mAtom
->ToString(temp
);
583 aString
.Append(PRUnichar('#'));
584 aString
.Append(temp
);
589 // Append each class in the linked list
591 nsAtomList
* list
= mClassList
;
592 while (list
!= nsnull
) {
593 list
->mAtom
->ToString(temp
);
594 aString
.Append(PRUnichar('.'));
595 aString
.Append(temp
);
600 // Append each attribute selector in the linked list
602 nsAttrSelector
* list
= mAttrList
;
603 while (list
!= nsnull
) {
604 aString
.Append(PRUnichar('['));
605 // Append the namespace prefix
606 if (list
->mNameSpace
> 0) {
608 nsXMLNameSpaceMap
*sheetNS
= aSheet
->GetNameSpaceMap();
609 // will return null if namespace was the default
610 nsIAtom
*prefixAtom
= sheetNS
->FindPrefix(list
->mNameSpace
);
613 prefixAtom
->ToString(prefix
);
614 aString
.Append(prefix
);
615 aString
.Append(PRUnichar('|'));
619 // Append the attribute name
620 list
->mAttr
->ToString(temp
);
621 aString
.Append(temp
);
623 if (list
->mFunction
!= NS_ATTR_FUNC_SET
) {
624 // Append the function
625 if (list
->mFunction
== NS_ATTR_FUNC_INCLUDES
)
626 aString
.Append(PRUnichar('~'));
627 else if (list
->mFunction
== NS_ATTR_FUNC_DASHMATCH
)
628 aString
.Append(PRUnichar('|'));
629 else if (list
->mFunction
== NS_ATTR_FUNC_BEGINSMATCH
)
630 aString
.Append(PRUnichar('^'));
631 else if (list
->mFunction
== NS_ATTR_FUNC_ENDSMATCH
)
632 aString
.Append(PRUnichar('$'));
633 else if (list
->mFunction
== NS_ATTR_FUNC_CONTAINSMATCH
)
634 aString
.Append(PRUnichar('*'));
636 aString
.Append(PRUnichar('='));
639 nsAutoString escaped
;
640 nsStyleUtil::EscapeCSSString(list
->mValue
, escaped
);
642 aString
.Append(PRUnichar('\"'));
643 aString
.Append(escaped
);
644 aString
.Append(PRUnichar('\"'));
647 aString
.Append(PRUnichar(']'));
653 // Append each pseudo-class in the linked list
654 if (mPseudoClassList
) {
655 nsPseudoClassList
* list
= mPseudoClassList
;
656 while (list
!= nsnull
) {
657 list
->mAtom
->ToString(temp
);
658 aString
.Append(temp
);
659 if (list
->u
.mMemory
) {
660 aString
.Append(PRUnichar('('));
661 if (nsCSSPseudoClasses::HasStringArg(list
->mAtom
)) {
662 aString
.Append(list
->u
.mString
);
664 NS_ASSERTION(nsCSSPseudoClasses::HasNthPairArg(list
->mAtom
),
665 "unexpected pseudo-class");
666 PRInt32 a
= list
->u
.mNumbers
[0],
667 b
= list
->u
.mNumbers
[1];
671 temp
.Append(PRUnichar('-'));
675 temp
.Append(PRUnichar('n'));
677 if (b
!= 0 || a
== 0) {
678 if (b
>= 0 && a
!= 0) // check a != 0 for whether we printed above
679 temp
.Append(PRUnichar('+'));
682 aString
.Append(temp
);
684 aString
.Append(PRUnichar(')'));
691 for (nsCSSSelector
* negation
= mNegations
; negation
;
692 negation
= negation
->mNegations
) {
693 aString
.AppendLiteral(":not(");
694 negation
->ToStringInternal(aString
, aSheet
, PR_FALSE
, PR_TRUE
);
695 aString
.Append(PRUnichar(')'));
699 // Append the operator only if the selector is not negated and is not
701 if (!aIsNegated
&& mOperator
&& !aIsPseudoElem
) {
702 aString
.Append(PRUnichar(' '));
703 aString
.Append(mOperator
);
707 // -- nsCSSSelectorList -------------------------------
709 nsCSSSelectorList::nsCSSSelectorList(void)
710 : mSelectors(nsnull
),
714 MOZ_COUNT_CTOR(nsCSSSelectorList
);
717 nsCSSSelectorList::~nsCSSSelectorList()
719 MOZ_COUNT_DTOR(nsCSSSelectorList
);
721 NS_CSS_DELETE_LIST_MEMBER(nsCSSSelectorList
, this, mNext
);
724 void nsCSSSelectorList::AddSelector(nsAutoPtr
<nsCSSSelector
>& aSelector
)
726 nsCSSSelector
* newSel
= aSelector
.forget();
728 newSel
->mNext
= mSelectors
;
734 nsCSSSelectorList::ToString(nsAString
& aResult
, nsICSSStyleSheet
* aSheet
)
737 nsCSSSelectorList
*p
= this;
739 p
->mSelectors
->ToString(aResult
, aSheet
, PR_TRUE
);
743 aResult
.AppendLiteral(", ");
748 nsCSSSelectorList::Clone(PRBool aDeep
) const
750 nsCSSSelectorList
*result
= new nsCSSSelectorList();
751 result
->mWeight
= mWeight
;
752 NS_IF_CLONE(mSelectors
);
755 NS_CSS_CLONE_LIST_MEMBER(nsCSSSelectorList
, this, mNext
, result
,
761 // -- CSSImportantRule -------------------------------
763 class CSSStyleRuleImpl
;
765 class CSSImportantRule
: public nsIStyleRule
{
767 CSSImportantRule(nsCSSDeclaration
* aDeclaration
);
771 // nsIStyleRule interface
772 NS_IMETHOD
MapRuleInfoInto(nsRuleData
* aRuleData
);
774 NS_IMETHOD
List(FILE* out
= stdout
, PRInt32 aIndent
= 0) const;
778 virtual ~CSSImportantRule(void);
780 nsCSSDeclaration
* mDeclaration
;
782 friend class CSSStyleRuleImpl
;
785 CSSImportantRule::CSSImportantRule(nsCSSDeclaration
* aDeclaration
)
786 : mDeclaration(aDeclaration
)
790 CSSImportantRule::~CSSImportantRule(void)
792 mDeclaration
= nsnull
;
795 NS_IMPL_ISUPPORTS1(CSSImportantRule
, nsIStyleRule
)
798 CSSImportantRule::MapRuleInfoInto(nsRuleData
* aRuleData
)
800 // Check this at runtime because it might be hit in some out-of-memory cases.
801 NS_ENSURE_TRUE(mDeclaration
->HasImportantData(), NS_ERROR_UNEXPECTED
);
803 return mDeclaration
->MapImportantRuleInfoInto(aRuleData
);
808 CSSImportantRule::List(FILE* out
, PRInt32 aIndent
) const
811 for (PRInt32 index
= aIndent
; --index
>= 0; ) fputs(" ", out
);
813 fputs("! Important rule ", out
);
814 if (nsnull
!= mDeclaration
) {
815 mDeclaration
->List(out
);
818 fputs("{ null declaration }", out
);
826 // --------------------------------------------------------
828 class DOMCSSStyleRuleImpl
;
830 class DOMCSSDeclarationImpl
: public nsDOMCSSDeclaration
833 DOMCSSDeclarationImpl(nsICSSStyleRule
*aRule
);
834 virtual ~DOMCSSDeclarationImpl(void);
836 NS_IMETHOD
GetParentRule(nsIDOMCSSRule
**aParent
);
837 virtual void DropReference(void);
838 virtual nsresult
GetCSSDeclaration(nsCSSDeclaration
**aDecl
,
840 virtual nsresult
GetCSSParsingEnvironment(nsIURI
** aSheetURI
,
842 nsIPrincipal
** aSheetPrincipal
,
843 nsICSSLoader
** aCSSLoader
,
844 nsICSSParser
** aCSSParser
);
845 virtual nsresult
DeclarationChanged();
847 // Override |AddRef| and |Release| for being a member of
848 // |DOMCSSStyleRuleImpl|.
849 NS_IMETHOD_(nsrefcnt
) AddRef(void);
850 NS_IMETHOD_(nsrefcnt
) Release(void);
852 friend class DOMCSSStyleRuleImpl
;
855 // This reference is not reference-counted. The rule object tells us
856 // when it's about to go away.
857 nsICSSStyleRule
*mRule
;
859 inline DOMCSSStyleRuleImpl
* DomRule();
862 // NOT TO BE IMPLEMENTED
863 // This object cannot be allocated on its own. It must be a member of
864 // DOMCSSStyleRuleImpl.
865 void* operator new(size_t size
) CPP_THROW_NEW
;
868 class DOMCSSStyleRuleImpl
: public nsICSSStyleRuleDOMWrapper
871 DOMCSSStyleRuleImpl(nsICSSStyleRule
*aRule
);
872 virtual ~DOMCSSStyleRuleImpl();
875 NS_DECL_NSIDOMCSSRULE
876 NS_DECL_NSIDOMCSSSTYLERULE
878 // nsICSSStyleRuleDOMWrapper
879 NS_IMETHOD
GetCSSStyleRule(nsICSSStyleRule
**aResult
);
881 DOMCSSDeclarationImpl
* DOMDeclaration() { return &mDOMDeclaration
; }
883 friend class DOMCSSDeclarationImpl
;
886 DOMCSSDeclarationImpl mDOMDeclaration
;
888 nsICSSStyleRule
* Rule() {
889 return mDOMDeclaration
.mRule
;
893 DOMCSSDeclarationImpl::DOMCSSDeclarationImpl(nsICSSStyleRule
*aRule
)
896 MOZ_COUNT_CTOR(DOMCSSDeclarationImpl
);
899 DOMCSSDeclarationImpl::~DOMCSSDeclarationImpl(void)
901 NS_ASSERTION(!mRule
, "DropReference not called.");
903 MOZ_COUNT_DTOR(DOMCSSDeclarationImpl
);
906 inline DOMCSSStyleRuleImpl
* DOMCSSDeclarationImpl::DomRule()
908 return reinterpret_cast<DOMCSSStyleRuleImpl
*>
909 (reinterpret_cast<char*>(this) -
910 offsetof(DOMCSSStyleRuleImpl
, mDOMDeclaration
));
913 NS_IMPL_ADDREF_USING_AGGREGATOR(DOMCSSDeclarationImpl
, DomRule())
914 NS_IMPL_RELEASE_USING_AGGREGATOR(DOMCSSDeclarationImpl
, DomRule())
917 DOMCSSDeclarationImpl::DropReference(void)
923 DOMCSSDeclarationImpl::GetCSSDeclaration(nsCSSDeclaration
**aDecl
,
927 *aDecl
= mRule
->GetDeclaration();
937 * This is a utility function. It will only fail if it can't get a
938 * parser. This means it can return NS_OK without aURI or aCSSLoader
942 DOMCSSDeclarationImpl::GetCSSParsingEnvironment(nsIURI
** aSheetURI
,
944 nsIPrincipal
** aSheetPrincipal
,
945 nsICSSLoader
** aCSSLoader
,
946 nsICSSParser
** aCSSParser
)
948 // null out the out params since some of them may not get initialized below
951 *aSheetPrincipal
= nsnull
;
952 *aCSSLoader
= nsnull
;
953 *aCSSParser
= nsnull
;
955 nsCOMPtr
<nsIStyleSheet
> sheet
;
957 mRule
->GetStyleSheet(*getter_AddRefs(sheet
));
959 sheet
->GetSheetURI(aSheetURI
);
960 sheet
->GetBaseURI(aBaseURI
);
962 nsCOMPtr
<nsICSSStyleSheet
> cssSheet(do_QueryInterface(sheet
));
964 NS_ADDREF(*aSheetPrincipal
= cssSheet
->Principal());
967 nsCOMPtr
<nsIDocument
> document
;
968 sheet
->GetOwningDocument(*getter_AddRefs(document
));
970 NS_ADDREF(*aCSSLoader
= document
->CSSLoader());
974 // XXXldb Why bother if |mRule| is null?
976 result
= (*aCSSLoader
)->GetParserFor(nsnull
, aCSSParser
);
978 result
= NS_NewCSSParser(aCSSParser
);
981 if (NS_SUCCEEDED(result
) && !*aSheetPrincipal
) {
982 result
= CallCreateInstance("@mozilla.org/nullprincipal;1",
990 DOMCSSDeclarationImpl::GetParentRule(nsIDOMCSSRule
**aParent
)
992 NS_ENSURE_ARG_POINTER(aParent
);
999 return mRule
->GetDOMRule(aParent
);
1003 DOMCSSDeclarationImpl::DeclarationChanged()
1005 NS_PRECONDITION(mRule
,
1006 "can only be called when |GetCSSDeclaration| returned a declaration");
1008 nsCOMPtr
<nsIDocument
> owningDoc
;
1009 nsCOMPtr
<nsIStyleSheet
> sheet
;
1010 mRule
->GetStyleSheet(*getter_AddRefs(sheet
));
1012 sheet
->GetOwningDocument(*getter_AddRefs(owningDoc
));
1015 mozAutoDocUpdate
updateBatch(owningDoc
, UPDATE_STYLE
, PR_TRUE
);
1017 nsCOMPtr
<nsICSSStyleRule
> oldRule
= mRule
;
1018 mRule
= oldRule
->DeclarationChanged(PR_TRUE
).get();
1020 return NS_ERROR_OUT_OF_MEMORY
;
1021 nsrefcnt cnt
= mRule
->Release();
1023 NS_NOTREACHED("container didn't take ownership");
1025 return NS_ERROR_UNEXPECTED
;
1029 owningDoc
->StyleRuleChanged(sheet
, oldRule
, mRule
);
1034 DOMCSSStyleRuleImpl::DOMCSSStyleRuleImpl(nsICSSStyleRule
* aRule
)
1035 : mDOMDeclaration(aRule
)
1039 DOMCSSStyleRuleImpl::~DOMCSSStyleRuleImpl()
1043 NS_INTERFACE_MAP_BEGIN(DOMCSSStyleRuleImpl
)
1044 NS_INTERFACE_MAP_ENTRY(nsICSSStyleRuleDOMWrapper
)
1045 NS_INTERFACE_MAP_ENTRY(nsIDOMCSSStyleRule
)
1046 NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule
)
1047 NS_INTERFACE_MAP_ENTRY(nsISupports
)
1048 NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(CSSStyleRule
)
1049 NS_INTERFACE_MAP_END
1051 NS_IMPL_ADDREF(DOMCSSStyleRuleImpl
)
1052 NS_IMPL_RELEASE(DOMCSSStyleRuleImpl
)
1055 DOMCSSStyleRuleImpl::GetType(PRUint16
* aType
)
1057 *aType
= nsIDOMCSSRule::STYLE_RULE
;
1063 DOMCSSStyleRuleImpl::GetCssText(nsAString
& aCssText
)
1066 aCssText
.Truncate();
1069 return Rule()->GetCssText(aCssText
);
1073 DOMCSSStyleRuleImpl::SetCssText(const nsAString
& aCssText
)
1078 return Rule()->SetCssText(aCssText
);
1082 DOMCSSStyleRuleImpl::GetParentStyleSheet(nsIDOMCSSStyleSheet
** aSheet
)
1088 nsCOMPtr
<nsICSSStyleSheet
> sheet
;
1089 Rule()->GetParentStyleSheet(getter_AddRefs(sheet
));
1094 return CallQueryInterface(sheet
, aSheet
);
1098 DOMCSSStyleRuleImpl::GetParentRule(nsIDOMCSSRule
** aParentRule
)
1101 *aParentRule
= nsnull
;
1104 nsCOMPtr
<nsICSSGroupRule
> rule
;
1105 Rule()->GetParentRule(getter_AddRefs(rule
));
1107 *aParentRule
= nsnull
;
1110 return rule
->GetDOMRule(aParentRule
);
1114 DOMCSSStyleRuleImpl::GetSelectorText(nsAString
& aSelectorText
)
1117 aSelectorText
.Truncate();
1120 return Rule()->GetSelectorText(aSelectorText
);
1124 DOMCSSStyleRuleImpl::SetSelectorText(const nsAString
& aSelectorText
)
1129 return Rule()->SetSelectorText(aSelectorText
);
1133 DOMCSSStyleRuleImpl::GetStyle(nsIDOMCSSStyleDeclaration
** aStyle
)
1135 *aStyle
= &mDOMDeclaration
;
1141 DOMCSSStyleRuleImpl::GetCSSStyleRule(nsICSSStyleRule
**aResult
)
1144 NS_IF_ADDREF(*aResult
);
1148 // -- nsCSSStyleRule -------------------------------
1150 class CSSStyleRuleImpl
: public nsCSSRule
,
1151 public nsICSSStyleRule
1154 CSSStyleRuleImpl(nsCSSSelectorList
* aSelector
,
1155 nsCSSDeclaration
*aDeclaration
);
1158 CSSStyleRuleImpl(const CSSStyleRuleImpl
& aCopy
);
1159 // for |DeclarationChanged|
1160 CSSStyleRuleImpl(CSSStyleRuleImpl
& aCopy
,
1161 nsCSSDeclaration
*aDeclaration
);
1164 NS_DECL_ISUPPORTS_INHERITED
1166 virtual nsCSSSelectorList
* Selector(void);
1168 virtual PRUint32
GetLineNumber(void) const;
1169 virtual void SetLineNumber(PRUint32 aLineNumber
);
1171 virtual nsCSSDeclaration
* GetDeclaration(void) const;
1173 virtual already_AddRefed
<nsIStyleRule
> GetImportantRule(void);
1175 NS_IMETHOD
GetStyleSheet(nsIStyleSheet
*& aSheet
) const;
1176 NS_IMETHOD
SetStyleSheet(nsICSSStyleSheet
* aSheet
);
1178 NS_IMETHOD
SetParentRule(nsICSSGroupRule
* aRule
);
1180 virtual nsresult
GetCssText(nsAString
& aCssText
);
1181 virtual nsresult
SetCssText(const nsAString
& aCssText
);
1182 virtual nsresult
GetParentStyleSheet(nsICSSStyleSheet
** aSheet
);
1183 virtual nsresult
GetParentRule(nsICSSGroupRule
** aParentRule
);
1184 virtual nsresult
GetSelectorText(nsAString
& aSelectorText
);
1185 virtual nsresult
SetSelectorText(const nsAString
& aSelectorText
);
1187 NS_IMETHOD
GetType(PRInt32
& aType
) const;
1188 NS_IMETHOD
Clone(nsICSSRule
*& aClone
) const;
1190 nsIDOMCSSRule
* GetDOMRuleWeak(nsresult
* aResult
);
1192 virtual already_AddRefed
<nsICSSStyleRule
>
1193 DeclarationChanged(PRBool aHandleContainer
);
1195 // The new mapping function.
1196 NS_IMETHOD
MapRuleInfoInto(nsRuleData
* aRuleData
);
1199 NS_IMETHOD
List(FILE* out
= stdout
, PRInt32 aIndent
= 0) const;
1203 // These are not supported and are not implemented!
1204 CSSStyleRuleImpl
& operator=(const CSSStyleRuleImpl
& aCopy
);
1207 virtual ~CSSStyleRuleImpl(void);
1210 nsCSSSelectorList
* mSelector
; // null for style attribute
1211 nsCSSDeclaration
* mDeclaration
;
1212 CSSImportantRule
* mImportantRule
;
1213 DOMCSSStyleRuleImpl
* mDOMRule
;
1214 PRUint32 mLineNumber
;
1217 CSSStyleRuleImpl::CSSStyleRuleImpl(nsCSSSelectorList
* aSelector
,
1218 nsCSSDeclaration
* aDeclaration
)
1220 mSelector(aSelector
),
1221 mDeclaration(aDeclaration
),
1222 mImportantRule(nsnull
),
1226 mDeclaration
->AddRef();
1230 CSSStyleRuleImpl::CSSStyleRuleImpl(const CSSStyleRuleImpl
& aCopy
)
1232 mSelector(aCopy
.mSelector
? aCopy
.mSelector
->Clone() : nsnull
),
1233 mDeclaration(aCopy
.mDeclaration
->Clone()),
1234 mImportantRule(nsnull
),
1236 mLineNumber(aCopy
.mLineNumber
)
1239 mDeclaration
->AddRef();
1240 // rest is constructed lazily on existing data
1243 // for |DeclarationChanged|
1244 CSSStyleRuleImpl::CSSStyleRuleImpl(CSSStyleRuleImpl
& aCopy
,
1245 nsCSSDeclaration
* aDeclaration
)
1247 mSelector(aCopy
.mSelector
),
1248 mDeclaration(aDeclaration
),
1249 mImportantRule(nsnull
),
1250 mDOMRule(aCopy
.mDOMRule
),
1251 mLineNumber(aCopy
.mLineNumber
)
1253 // The DOM rule is replacing |aCopy| with |this|, so transfer
1254 // the reverse pointer as well (and transfer ownership).
1255 aCopy
.mDOMRule
= nsnull
;
1257 NS_ASSERTION(aDeclaration
== aCopy
.mDeclaration
, "declaration mismatch");
1258 // Transfer ownership of selector and declaration:
1259 aCopy
.mSelector
= nsnull
;
1261 aCopy
.mDeclaration
= nsnull
;
1263 // We ought to be able to transfer ownership of the selector and the
1264 // declaration since this rule should now be unused, but unfortunately
1265 // SetInlineStyleRule might use it before setting the new rule (see
1266 // stack in bug 209575). So leave the declaration pointer on the old
1268 mDeclaration
->AddRef();
1273 CSSStyleRuleImpl::~CSSStyleRuleImpl(void)
1279 if (nsnull
!= mDeclaration
) {
1280 mDeclaration
->Release();
1281 mDeclaration
= nsnull
;
1283 if (nsnull
!= mImportantRule
) {
1284 NS_RELEASE(mImportantRule
);
1285 mImportantRule
= nsnull
;
1288 mDOMRule
->DOMDeclaration()->DropReference();
1289 NS_RELEASE(mDOMRule
);
1293 // QueryInterface implementation for CSSStyleRuleImpl
1294 NS_INTERFACE_MAP_BEGIN(CSSStyleRuleImpl
)
1295 NS_INTERFACE_MAP_ENTRY(nsICSSStyleRule
)
1296 NS_INTERFACE_MAP_ENTRY(nsICSSRule
)
1297 NS_INTERFACE_MAP_ENTRY(nsIStyleRule
)
1298 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsICSSStyleRule
)
1299 NS_INTERFACE_MAP_END
1301 NS_IMPL_ADDREF_INHERITED(CSSStyleRuleImpl
, nsCSSRule
)
1302 NS_IMPL_RELEASE_INHERITED(CSSStyleRuleImpl
, nsCSSRule
)
1304 nsCSSSelectorList
* CSSStyleRuleImpl::Selector(void)
1309 PRUint32
CSSStyleRuleImpl::GetLineNumber(void) const
1314 void CSSStyleRuleImpl::SetLineNumber(PRUint32 aLineNumber
)
1316 mLineNumber
= aLineNumber
;
1319 nsCSSDeclaration
* CSSStyleRuleImpl::GetDeclaration(void) const
1321 return mDeclaration
;
1324 already_AddRefed
<nsIStyleRule
> CSSStyleRuleImpl::GetImportantRule(void)
1326 if (!mDeclaration
->HasImportantData()) {
1327 NS_ASSERTION(!mImportantRule
, "immutable, so should be no important rule");
1331 if (!mImportantRule
) {
1332 mImportantRule
= new CSSImportantRule(mDeclaration
);
1333 if (!mImportantRule
)
1335 NS_ADDREF(mImportantRule
);
1337 NS_ADDREF(mImportantRule
);
1338 return mImportantRule
;
1342 CSSStyleRuleImpl::GetStyleSheet(nsIStyleSheet
*& aSheet
) const
1344 // XXX What about inner, etc.
1345 return nsCSSRule::GetStyleSheet(aSheet
);
1349 CSSStyleRuleImpl::SetStyleSheet(nsICSSStyleSheet
* aSheet
)
1351 return nsCSSRule::SetStyleSheet(aSheet
);
1355 CSSStyleRuleImpl::SetParentRule(nsICSSGroupRule
* aRule
)
1357 return nsCSSRule::SetParentRule(aRule
);
1361 CSSStyleRuleImpl::GetType(PRInt32
& aType
) const
1363 aType
= nsICSSRule::STYLE_RULE
;
1368 CSSStyleRuleImpl::Clone(nsICSSRule
*& aClone
) const
1370 CSSStyleRuleImpl
* clone
= new CSSStyleRuleImpl(*this);
1371 if (!clone
|| !clone
->mDeclaration
|| (!clone
->mSelector
!= !mSelector
)) {
1374 return NS_ERROR_OUT_OF_MEMORY
;
1376 return CallQueryInterface(clone
, &aClone
);
1380 CSSStyleRuleImpl::GetDOMRuleWeak(nsresult
*aResult
)
1384 // inline style rules aren't supposed to have a DOM rule object, only
1389 mDOMRule
= new DOMCSSStyleRuleImpl(this);
1391 *aResult
= NS_ERROR_OUT_OF_MEMORY
;
1394 NS_ADDREF(mDOMRule
);
1399 /* virtual */ already_AddRefed
<nsICSSStyleRule
>
1400 CSSStyleRuleImpl::DeclarationChanged(PRBool aHandleContainer
)
1402 CSSStyleRuleImpl
* clone
= new CSSStyleRuleImpl(*this, mDeclaration
);
1407 NS_ADDREF(clone
); // for return
1409 if (aHandleContainer
) {
1410 NS_ASSERTION(mSheet
, "rule must be in a sheet");
1412 mSheet
->ReplaceRuleInGroup(mParentRule
, this, clone
);
1414 mSheet
->ReplaceStyleRule(this, clone
);
1422 CSSStyleRuleImpl::MapRuleInfoInto(nsRuleData
* aRuleData
)
1424 return mDeclaration
->MapRuleInfoInto(aRuleData
);
1429 CSSStyleRuleImpl::List(FILE* out
, PRInt32 aIndent
) const
1432 for (PRInt32 index
= aIndent
; --index
>= 0; ) fputs(" ", out
);
1434 nsAutoString buffer
;
1436 mSelector
->ToString(buffer
, mSheet
);
1438 buffer
.AppendLiteral(" ");
1439 fputs(NS_LossyConvertUTF16toASCII(buffer
).get(), out
);
1440 if (nsnull
!= mDeclaration
) {
1441 mDeclaration
->List(out
);
1444 fputs("{ null declaration }", out
);
1452 /* virtual */ nsresult
1453 CSSStyleRuleImpl::GetCssText(nsAString
& aCssText
)
1456 mSelector
->ToString(aCssText
, mSheet
);
1457 aCssText
.Append(PRUnichar(' '));
1459 aCssText
.Append(PRUnichar('{'));
1460 aCssText
.Append(PRUnichar(' '));
1463 nsAutoString tempString
;
1464 mDeclaration
->ToString( tempString
);
1465 aCssText
.Append( tempString
);
1467 aCssText
.Append(PRUnichar(' '));
1468 aCssText
.Append(PRUnichar('}'));
1472 /* virtual */ nsresult
1473 CSSStyleRuleImpl::SetCssText(const nsAString
& aCssText
)
1475 // XXX TBI - need to re-parse rule & declaration
1479 /* virtual */ nsresult
1480 CSSStyleRuleImpl::GetParentStyleSheet(nsICSSStyleSheet
** aSheet
)
1483 NS_IF_ADDREF(*aSheet
);
1487 /* virtual */ nsresult
1488 CSSStyleRuleImpl::GetParentRule(nsICSSGroupRule
** aParentRule
)
1490 *aParentRule
= mParentRule
;
1491 NS_IF_ADDREF(*aParentRule
);
1495 /* virtual */ nsresult
1496 CSSStyleRuleImpl::GetSelectorText(nsAString
& aSelectorText
)
1499 mSelector
->ToString(aSelectorText
, mSheet
);
1501 aSelectorText
.Truncate();
1505 /* virtual */ nsresult
1506 CSSStyleRuleImpl::SetSelectorText(const nsAString
& aSelectorText
)
1508 // XXX TBI - get a parser and re-parse the selectors,
1509 // XXX then need to re-compute the cascade
1510 // XXX and dirty sheet
1515 NS_NewCSSStyleRule(nsICSSStyleRule
** aInstancePtrResult
,
1516 nsCSSSelectorList
* aSelector
,
1517 nsCSSDeclaration
* aDeclaration
)
1519 NS_PRECONDITION(aDeclaration
, "must have a declaration");
1520 CSSStyleRuleImpl
*it
= new CSSStyleRuleImpl(aSelector
, aDeclaration
);
1522 return NS_ERROR_OUT_OF_MEMORY
;
1525 return CallQueryInterface(it
, aInstancePtrResult
);