Bug 458861. Validate TrueType headers before activating downloaded font. r=roc, sr...
[wine-gecko.git] / layout / style / nsCSSStyleRule.cpp
blobf1632c6ddfa38b11a9d990cb2041dc06672deeca
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 * 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
44 * declarations
47 #include "nsCOMPtr.h"
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"
55 #include "nsIURL.h"
56 #include "nsPresContext.h"
57 #include "nsIDocument.h"
58 #include "nsIDeviceContext.h"
59 #include "nsIAtom.h"
60 #include "nsCRT.h"
61 #include "nsString.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_) \
84 PR_BEGIN_MACRO \
85 if (member_) { \
86 result->member_ = member_->Clone(); \
87 if (!result->member_) { \
88 delete result; \
89 return nsnull; \
90 } \
91 } \
92 PR_END_MACRO
94 #define NS_IF_DELETE(ptr) \
95 PR_BEGIN_MACRO \
96 delete ptr; \
97 ptr = nsnull; \
98 PR_END_MACRO
100 /* ************************************************************************** */
102 nsAtomList::nsAtomList(nsIAtom* aAtom)
103 : mAtom(aAtom),
104 mNext(nsnull)
106 MOZ_COUNT_CTOR(nsAtomList);
109 nsAtomList::nsAtomList(const nsString& aAtomValue)
110 : mAtom(nsnull),
111 mNext(nsnull)
113 MOZ_COUNT_CTOR(nsAtomList);
114 mAtom = do_GetAtom(aAtomValue);
117 nsAtomList*
118 nsAtomList::Clone(PRBool aDeep) const
120 nsAtomList *result = new nsAtomList(mAtom);
121 if (!result)
122 return nsnull;
124 if (aDeep)
125 NS_CSS_CLONE_LIST_MEMBER(nsAtomList, this, mNext, result, (PR_FALSE));
126 return result;
129 nsAtomList::~nsAtomList(void)
131 MOZ_COUNT_DTOR(nsAtomList);
132 NS_CSS_DELETE_LIST_MEMBER(nsAtomList, this, mNext);
135 nsPseudoClassList::nsPseudoClassList(nsIAtom* aAtom)
136 : mAtom(aAtom),
137 mNext(nsnull)
139 NS_ASSERTION(!nsCSSPseudoClasses::HasStringArg(aAtom) &&
140 !nsCSSPseudoClasses::HasNthPairArg(aAtom),
141 "unexpected pseudo-class");
142 MOZ_COUNT_CTOR(nsPseudoClassList);
143 u.mMemory = nsnull;
146 nsPseudoClassList::nsPseudoClassList(nsIAtom* aAtom, const PRUnichar* aString)
147 : mAtom(aAtom),
148 mNext(nsnull)
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)
158 : mAtom(aAtom),
159 mNext(nsnull)
161 NS_ASSERTION(nsCSSPseudoClasses::HasNthPairArg(aAtom),
162 "unexpected pseudo-class");
163 NS_ASSERTION(aIntPair, "integer pair expected");
164 MOZ_COUNT_CTOR(nsPseudoClassList);
165 u.mNumbers =
166 static_cast<PRInt32*>(nsMemory::Clone(aIntPair, sizeof(PRInt32) * 2));
169 nsPseudoClassList*
170 nsPseudoClassList::Clone(PRBool aDeep) const
172 nsPseudoClassList *result;
173 if (!u.mMemory) {
174 result = new nsPseudoClassList(mAtom);
175 } else if (nsCSSPseudoClasses::HasStringArg(mAtom)) {
176 result = new nsPseudoClassList(mAtom, u.mString);
177 } else {
178 NS_ASSERTION(nsCSSPseudoClasses::HasNthPairArg(mAtom),
179 "unexpected pseudo-class");
180 result = new nsPseudoClassList(mAtom, u.mNumbers);
183 if (aDeep)
184 NS_CSS_CLONE_LIST_MEMBER(nsPseudoClassList, this, mNext, result,
185 (PR_FALSE));
187 return result;
190 nsPseudoClassList::~nsPseudoClassList(void)
192 MOZ_COUNT_DTOR(nsPseudoClassList);
193 if (u.mMemory)
194 NS_Free(u.mMemory);
195 NS_CSS_DELETE_LIST_MEMBER(nsPseudoClassList, this, mNext);
198 nsAttrSelector::nsAttrSelector(PRInt32 aNameSpace, const nsString& aAttr)
199 : mNameSpace(aNameSpace),
200 mAttr(nsnull),
201 mFunction(NS_ATTR_FUNC_SET),
202 mCaseSensitive(1),
203 mValue(),
204 mNext(nsnull)
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),
214 mAttr(nsnull),
215 mFunction(aFunction),
216 mCaseSensitive(aCaseSensitive),
217 mValue(aValue),
218 mNext(nsnull)
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),
229 mAttr(aAttr),
230 mFunction(aFunction),
231 mCaseSensitive(aCaseSensitive),
232 mValue(aValue),
233 mNext(nsnull)
235 MOZ_COUNT_CTOR(nsAttrSelector);
238 nsAttrSelector*
239 nsAttrSelector::Clone(PRBool aDeep) const
241 nsAttrSelector *result =
242 new nsAttrSelector(mNameSpace, mAttr, mFunction, mValue, mCaseSensitive);
244 if (aDeep)
245 NS_CSS_CLONE_LIST_MEMBER(nsAttrSelector, this, mNext, result, (PR_FALSE));
247 return result;
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),
261 mIDList(nsnull),
262 mClassList(nsnull),
263 mPseudoClassList(nsnull),
264 mAttrList(nsnull),
265 mOperator(0),
266 mNegations(nsnull),
267 mNext(nsnull)
269 MOZ_COUNT_CTOR(nsCSSSelector);
272 nsCSSSelector*
273 nsCSSSelector::Clone(PRBool aDeepNext, PRBool aDeepNegations) const
275 nsCSSSelector *result = new nsCSSSelector();
276 if (!result)
277 return nsnull;
279 result->mNameSpace = mNameSpace;
280 result->mTag = mTag;
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));
296 if (aDeepNext) {
297 NS_CSS_CLONE_LIST_MEMBER(nsCSSSelector, this, mNext, result,
298 (PR_FALSE, PR_TRUE));
301 return result;
304 nsCSSSelector::~nsCSSSelector(void)
306 MOZ_COUNT_DTOR(nsCSSSelector);
307 Reset();
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;
316 mTag = nsnull;
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)
336 if (aTag.IsEmpty())
337 mTag = nsnull;
338 else
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
420 PRInt32 weight = 0;
422 if (nsnull != mTag) {
423 weight += 0x000001;
425 nsAtomList* list = mIDList;
426 while (nsnull != list) {
427 weight += 0x010000;
428 list = list->mNext;
430 list = mClassList;
431 while (nsnull != list) {
432 weight += 0x000100;
433 list = list->mNext;
435 nsPseudoClassList *plist = mPseudoClassList;
436 while (nsnull != plist) {
437 weight += 0x000100;
438 plist = plist->mNext;
440 nsAttrSelector* attr = mAttrList;
441 while (nsnull != attr) {
442 weight += 0x000100;
443 attr = attr->mNext;
445 if (nsnull != mNegations) {
446 weight += mNegations->CalcWeight();
448 return weight;
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)
455 if (aAtom) {
456 const char* str;
457 aAtom->GetUTF8String(&str);
458 return str && (*str == ':');
461 return PR_FALSE;
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
473 void
474 nsCSSSelector::ToString(nsAString& aString, nsICSSStyleSheet* aSheet,
475 PRBool aAppend) const
477 if (!aAppend)
478 aString.Truncate();
480 ToStringInternal(aString, aSheet, IsPseudoElement(mTag), PR_FALSE);
483 void nsCSSSelector::ToStringInternal(nsAString& aString,
484 nsICSSStyleSheet* aSheet,
485 PRBool aIsPseudoElem,
486 PRBool aIsNegated) const
488 nsAutoString temp;
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
493 if (mNext) {
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
510 // before it.
511 aString.Append(PRUnichar('|'));
512 wroteNamespace = PR_TRUE;
513 } else {
514 if (aSheet) {
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
521 // sheetNS is null.
522 if (sheetNS) {
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?");
528 nsAutoString 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
535 } else {
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;
552 if (!mTag) {
553 // Universal selector: avoid writing the universal selector when we
554 // can avoid it, especially since we're required to avoid it for the
555 // inside of :not()
556 if (wroteNamespace ||
557 (!mIDList && !mClassList && !mPseudoClassList && !mAttrList &&
558 (aIsNegated || !mNegations))) {
559 aString.Append(PRUnichar('*'));
561 } else {
562 // Append the tag name
563 if (isPseudoElement) {
564 if (!mNext) {
565 // Lone pseudo-element selector -- toss in a wildcard type selector
566 // XXXldb Why?
567 aString.Append(PRUnichar('*'));
569 if (!nsCSSPseudoElements::IsCSS2PseudoElement(mTag)) {
570 aString.Append(PRUnichar(':'));
573 nsAutoString prefix;
574 mTag->ToString(prefix);
575 aString.Append(prefix);
578 // Append the id, if there is one
579 if (mIDList) {
580 nsAtomList* list = mIDList;
581 while (list != nsnull) {
582 list->mAtom->ToString(temp);
583 aString.Append(PRUnichar('#'));
584 aString.Append(temp);
585 list = list->mNext;
589 // Append each class in the linked list
590 if (mClassList) {
591 nsAtomList* list = mClassList;
592 while (list != nsnull) {
593 list->mAtom->ToString(temp);
594 aString.Append(PRUnichar('.'));
595 aString.Append(temp);
596 list = list->mNext;
600 // Append each attribute selector in the linked list
601 if (mAttrList) {
602 nsAttrSelector* list = mAttrList;
603 while (list != nsnull) {
604 aString.Append(PRUnichar('['));
605 // Append the namespace prefix
606 if (list->mNameSpace > 0) {
607 if (aSheet) {
608 nsXMLNameSpaceMap *sheetNS = aSheet->GetNameSpaceMap();
609 // will return null if namespace was the default
610 nsIAtom *prefixAtom = sheetNS->FindPrefix(list->mNameSpace);
611 if (prefixAtom) {
612 nsAutoString prefix;
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('='));
638 // Append the value
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(']'));
649 list = list->mNext;
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);
663 } else {
664 NS_ASSERTION(nsCSSPseudoClasses::HasNthPairArg(list->mAtom),
665 "unexpected pseudo-class");
666 PRInt32 a = list->u.mNumbers[0],
667 b = list->u.mNumbers[1];
668 temp.Truncate();
669 if (a != 0) {
670 if (a == -1) {
671 temp.Append(PRUnichar('-'));
672 } else if (a != 1) {
673 temp.AppendInt(a);
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('+'));
680 temp.AppendInt(b);
682 aString.Append(temp);
684 aString.Append(PRUnichar(')'));
686 list = list->mNext;
690 if (!aIsNegated) {
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
700 // a pseudo-element
701 if (!aIsNegated && mOperator && !aIsPseudoElem) {
702 aString.Append(PRUnichar(' '));
703 aString.Append(mOperator);
707 // -- nsCSSSelectorList -------------------------------
709 nsCSSSelectorList::nsCSSSelectorList(void)
710 : mSelectors(nsnull),
711 mWeight(0),
712 mNext(nsnull)
714 MOZ_COUNT_CTOR(nsCSSSelectorList);
717 nsCSSSelectorList::~nsCSSSelectorList()
719 MOZ_COUNT_DTOR(nsCSSSelectorList);
720 delete mSelectors;
721 NS_CSS_DELETE_LIST_MEMBER(nsCSSSelectorList, this, mNext);
724 void nsCSSSelectorList::AddSelector(nsAutoPtr<nsCSSSelector>& aSelector)
725 { // prepend to list
726 nsCSSSelector* newSel = aSelector.forget();
727 if (newSel) {
728 newSel->mNext = mSelectors;
729 mSelectors = newSel;
733 void
734 nsCSSSelectorList::ToString(nsAString& aResult, nsICSSStyleSheet* aSheet)
736 aResult.Truncate();
737 nsCSSSelectorList *p = this;
738 for (;;) {
739 p->mSelectors->ToString(aResult, aSheet, PR_TRUE);
740 p = p->mNext;
741 if (!p)
742 break;
743 aResult.AppendLiteral(", ");
747 nsCSSSelectorList*
748 nsCSSSelectorList::Clone(PRBool aDeep) const
750 nsCSSSelectorList *result = new nsCSSSelectorList();
751 result->mWeight = mWeight;
752 NS_IF_CLONE(mSelectors);
754 if (aDeep) {
755 NS_CSS_CLONE_LIST_MEMBER(nsCSSSelectorList, this, mNext, result,
756 (PR_FALSE));
758 return result;
761 // -- CSSImportantRule -------------------------------
763 class CSSStyleRuleImpl;
765 class CSSImportantRule : public nsIStyleRule {
766 public:
767 CSSImportantRule(nsCSSDeclaration* aDeclaration);
769 NS_DECL_ISUPPORTS
771 // nsIStyleRule interface
772 NS_IMETHOD MapRuleInfoInto(nsRuleData* aRuleData);
773 #ifdef DEBUG
774 NS_IMETHOD List(FILE* out = stdout, PRInt32 aIndent = 0) const;
775 #endif
777 protected:
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)
797 NS_IMETHODIMP
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);
806 #ifdef DEBUG
807 NS_IMETHODIMP
808 CSSImportantRule::List(FILE* out, PRInt32 aIndent) const
810 // Indent
811 for (PRInt32 index = aIndent; --index >= 0; ) fputs(" ", out);
813 fputs("! Important rule ", out);
814 if (nsnull != mDeclaration) {
815 mDeclaration->List(out);
817 else {
818 fputs("{ null declaration }", out);
820 fputs("\n", out);
822 return NS_OK;
824 #endif
826 // --------------------------------------------------------
828 class DOMCSSStyleRuleImpl;
830 class DOMCSSDeclarationImpl : public nsDOMCSSDeclaration
832 public:
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,
839 PRBool aAllocate);
840 virtual nsresult GetCSSParsingEnvironment(nsIURI** aSheetURI,
841 nsIURI** aBaseURI,
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;
854 protected:
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();
861 private:
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
870 public:
871 DOMCSSStyleRuleImpl(nsICSSStyleRule *aRule);
872 virtual ~DOMCSSStyleRuleImpl();
874 NS_DECL_ISUPPORTS
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;
885 protected:
886 DOMCSSDeclarationImpl mDOMDeclaration;
888 nsICSSStyleRule* Rule() {
889 return mDOMDeclaration.mRule;
893 DOMCSSDeclarationImpl::DOMCSSDeclarationImpl(nsICSSStyleRule *aRule)
894 : mRule(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())
916 void
917 DOMCSSDeclarationImpl::DropReference(void)
919 mRule = nsnull;
922 nsresult
923 DOMCSSDeclarationImpl::GetCSSDeclaration(nsCSSDeclaration **aDecl,
924 PRBool aAllocate)
926 if (mRule) {
927 *aDecl = mRule->GetDeclaration();
929 else {
930 *aDecl = nsnull;
933 return NS_OK;
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
939 * being initialized.
941 nsresult
942 DOMCSSDeclarationImpl::GetCSSParsingEnvironment(nsIURI** aSheetURI,
943 nsIURI** aBaseURI,
944 nsIPrincipal** aSheetPrincipal,
945 nsICSSLoader** aCSSLoader,
946 nsICSSParser** aCSSParser)
948 // null out the out params since some of them may not get initialized below
949 *aSheetURI = nsnull;
950 *aBaseURI = nsnull;
951 *aSheetPrincipal = nsnull;
952 *aCSSLoader = nsnull;
953 *aCSSParser = nsnull;
954 nsresult result;
955 nsCOMPtr<nsIStyleSheet> sheet;
956 if (mRule) {
957 mRule->GetStyleSheet(*getter_AddRefs(sheet));
958 if (sheet) {
959 sheet->GetSheetURI(aSheetURI);
960 sheet->GetBaseURI(aBaseURI);
962 nsCOMPtr<nsICSSStyleSheet> cssSheet(do_QueryInterface(sheet));
963 if (cssSheet) {
964 NS_ADDREF(*aSheetPrincipal = cssSheet->Principal());
967 nsCOMPtr<nsIDocument> document;
968 sheet->GetOwningDocument(*getter_AddRefs(document));
969 if (document) {
970 NS_ADDREF(*aCSSLoader = document->CSSLoader());
974 // XXXldb Why bother if |mRule| is null?
975 if (*aCSSLoader) {
976 result = (*aCSSLoader)->GetParserFor(nsnull, aCSSParser);
977 } else {
978 result = NS_NewCSSParser(aCSSParser);
981 if (NS_SUCCEEDED(result) && !*aSheetPrincipal) {
982 result = CallCreateInstance("@mozilla.org/nullprincipal;1",
983 aSheetPrincipal);
986 return result;
989 NS_IMETHODIMP
990 DOMCSSDeclarationImpl::GetParentRule(nsIDOMCSSRule **aParent)
992 NS_ENSURE_ARG_POINTER(aParent);
994 if (!mRule) {
995 *aParent = nsnull;
996 return NS_OK;
999 return mRule->GetDOMRule(aParent);
1002 nsresult
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));
1011 if (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();
1019 if (!mRule)
1020 return NS_ERROR_OUT_OF_MEMORY;
1021 nsrefcnt cnt = mRule->Release();
1022 if (cnt == 0) {
1023 NS_NOTREACHED("container didn't take ownership");
1024 mRule = nsnull;
1025 return NS_ERROR_UNEXPECTED;
1028 if (owningDoc) {
1029 owningDoc->StyleRuleChanged(sheet, oldRule, mRule);
1031 return NS_OK;
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)
1054 NS_IMETHODIMP
1055 DOMCSSStyleRuleImpl::GetType(PRUint16* aType)
1057 *aType = nsIDOMCSSRule::STYLE_RULE;
1059 return NS_OK;
1062 NS_IMETHODIMP
1063 DOMCSSStyleRuleImpl::GetCssText(nsAString& aCssText)
1065 if (!Rule()) {
1066 aCssText.Truncate();
1067 return NS_OK;
1069 return Rule()->GetCssText(aCssText);
1072 NS_IMETHODIMP
1073 DOMCSSStyleRuleImpl::SetCssText(const nsAString& aCssText)
1075 if (!Rule()) {
1076 return NS_OK;
1078 return Rule()->SetCssText(aCssText);
1081 NS_IMETHODIMP
1082 DOMCSSStyleRuleImpl::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
1084 if (!Rule()) {
1085 *aSheet = nsnull;
1086 return NS_OK;
1088 nsCOMPtr<nsICSSStyleSheet> sheet;
1089 Rule()->GetParentStyleSheet(getter_AddRefs(sheet));
1090 if (!sheet) {
1091 *aSheet = nsnull;
1092 return NS_OK;
1094 return CallQueryInterface(sheet, aSheet);
1097 NS_IMETHODIMP
1098 DOMCSSStyleRuleImpl::GetParentRule(nsIDOMCSSRule** aParentRule)
1100 if (!Rule()) {
1101 *aParentRule = nsnull;
1102 return NS_OK;
1104 nsCOMPtr<nsICSSGroupRule> rule;
1105 Rule()->GetParentRule(getter_AddRefs(rule));
1106 if (!rule) {
1107 *aParentRule = nsnull;
1108 return NS_OK;
1110 return rule->GetDOMRule(aParentRule);
1113 NS_IMETHODIMP
1114 DOMCSSStyleRuleImpl::GetSelectorText(nsAString& aSelectorText)
1116 if (!Rule()) {
1117 aSelectorText.Truncate();
1118 return NS_OK;
1120 return Rule()->GetSelectorText(aSelectorText);
1123 NS_IMETHODIMP
1124 DOMCSSStyleRuleImpl::SetSelectorText(const nsAString& aSelectorText)
1126 if (!Rule()) {
1127 return NS_OK;
1129 return Rule()->SetSelectorText(aSelectorText);
1132 NS_IMETHODIMP
1133 DOMCSSStyleRuleImpl::GetStyle(nsIDOMCSSStyleDeclaration** aStyle)
1135 *aStyle = &mDOMDeclaration;
1136 NS_ADDREF(*aStyle);
1137 return NS_OK;
1140 NS_IMETHODIMP
1141 DOMCSSStyleRuleImpl::GetCSSStyleRule(nsICSSStyleRule **aResult)
1143 *aResult = Rule();
1144 NS_IF_ADDREF(*aResult);
1145 return NS_OK;
1148 // -- nsCSSStyleRule -------------------------------
1150 class CSSStyleRuleImpl : public nsCSSRule,
1151 public nsICSSStyleRule
1153 public:
1154 CSSStyleRuleImpl(nsCSSSelectorList* aSelector,
1155 nsCSSDeclaration *aDeclaration);
1156 private:
1157 // for |Clone|
1158 CSSStyleRuleImpl(const CSSStyleRuleImpl& aCopy);
1159 // for |DeclarationChanged|
1160 CSSStyleRuleImpl(CSSStyleRuleImpl& aCopy,
1161 nsCSSDeclaration *aDeclaration);
1162 public:
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);
1198 #ifdef DEBUG
1199 NS_IMETHOD List(FILE* out = stdout, PRInt32 aIndent = 0) const;
1200 #endif
1202 private:
1203 // These are not supported and are not implemented!
1204 CSSStyleRuleImpl& operator=(const CSSStyleRuleImpl& aCopy);
1206 protected:
1207 virtual ~CSSStyleRuleImpl(void);
1209 protected:
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)
1219 : nsCSSRule(),
1220 mSelector(aSelector),
1221 mDeclaration(aDeclaration),
1222 mImportantRule(nsnull),
1223 mDOMRule(nsnull),
1224 mLineNumber(0)
1226 mDeclaration->AddRef();
1229 // for |Clone|
1230 CSSStyleRuleImpl::CSSStyleRuleImpl(const CSSStyleRuleImpl& aCopy)
1231 : nsCSSRule(aCopy),
1232 mSelector(aCopy.mSelector ? aCopy.mSelector->Clone() : nsnull),
1233 mDeclaration(aCopy.mDeclaration->Clone()),
1234 mImportantRule(nsnull),
1235 mDOMRule(nsnull),
1236 mLineNumber(aCopy.mLineNumber)
1238 if (mDeclaration)
1239 mDeclaration->AddRef();
1240 // rest is constructed lazily on existing data
1243 // for |DeclarationChanged|
1244 CSSStyleRuleImpl::CSSStyleRuleImpl(CSSStyleRuleImpl& aCopy,
1245 nsCSSDeclaration* aDeclaration)
1246 : nsCSSRule(aCopy),
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;
1260 #if 0
1261 aCopy.mDeclaration = nsnull;
1262 #else
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
1267 // rule.
1268 mDeclaration->AddRef();
1269 #endif
1273 CSSStyleRuleImpl::~CSSStyleRuleImpl(void)
1275 if (mSelector) {
1276 delete mSelector;
1277 mSelector = nsnull;
1279 if (nsnull != mDeclaration) {
1280 mDeclaration->Release();
1281 mDeclaration = nsnull;
1283 if (nsnull != mImportantRule) {
1284 NS_RELEASE(mImportantRule);
1285 mImportantRule = nsnull;
1287 if (mDOMRule) {
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)
1306 return mSelector;
1309 PRUint32 CSSStyleRuleImpl::GetLineNumber(void) const
1311 return mLineNumber;
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");
1328 return nsnull;
1331 if (!mImportantRule) {
1332 mImportantRule = new CSSImportantRule(mDeclaration);
1333 if (!mImportantRule)
1334 return nsnull;
1335 NS_ADDREF(mImportantRule);
1337 NS_ADDREF(mImportantRule);
1338 return mImportantRule;
1341 NS_IMETHODIMP
1342 CSSStyleRuleImpl::GetStyleSheet(nsIStyleSheet*& aSheet) const
1344 // XXX What about inner, etc.
1345 return nsCSSRule::GetStyleSheet(aSheet);
1348 NS_IMETHODIMP
1349 CSSStyleRuleImpl::SetStyleSheet(nsICSSStyleSheet* aSheet)
1351 return nsCSSRule::SetStyleSheet(aSheet);
1354 NS_IMETHODIMP
1355 CSSStyleRuleImpl::SetParentRule(nsICSSGroupRule* aRule)
1357 return nsCSSRule::SetParentRule(aRule);
1360 NS_IMETHODIMP
1361 CSSStyleRuleImpl::GetType(PRInt32& aType) const
1363 aType = nsICSSRule::STYLE_RULE;
1364 return NS_OK;
1367 NS_IMETHODIMP
1368 CSSStyleRuleImpl::Clone(nsICSSRule*& aClone) const
1370 CSSStyleRuleImpl* clone = new CSSStyleRuleImpl(*this);
1371 if (!clone || !clone->mDeclaration || (!clone->mSelector != !mSelector)) {
1372 delete clone;
1373 aClone = nsnull;
1374 return NS_ERROR_OUT_OF_MEMORY;
1376 return CallQueryInterface(clone, &aClone);
1379 nsIDOMCSSRule*
1380 CSSStyleRuleImpl::GetDOMRuleWeak(nsresult *aResult)
1382 *aResult = NS_OK;
1383 if (!mSheet) {
1384 // inline style rules aren't supposed to have a DOM rule object, only
1385 // a declaration.
1386 return nsnull;
1388 if (!mDOMRule) {
1389 mDOMRule = new DOMCSSStyleRuleImpl(this);
1390 if (!mDOMRule) {
1391 *aResult = NS_ERROR_OUT_OF_MEMORY;
1392 return nsnull;
1394 NS_ADDREF(mDOMRule);
1396 return mDOMRule;
1399 /* virtual */ already_AddRefed<nsICSSStyleRule>
1400 CSSStyleRuleImpl::DeclarationChanged(PRBool aHandleContainer)
1402 CSSStyleRuleImpl* clone = new CSSStyleRuleImpl(*this, mDeclaration);
1403 if (!clone) {
1404 return nsnull;
1407 NS_ADDREF(clone); // for return
1409 if (aHandleContainer) {
1410 NS_ASSERTION(mSheet, "rule must be in a sheet");
1411 if (mParentRule) {
1412 mSheet->ReplaceRuleInGroup(mParentRule, this, clone);
1413 } else {
1414 mSheet->ReplaceStyleRule(this, clone);
1418 return clone;
1421 NS_IMETHODIMP
1422 CSSStyleRuleImpl::MapRuleInfoInto(nsRuleData* aRuleData)
1424 return mDeclaration->MapRuleInfoInto(aRuleData);
1427 #ifdef DEBUG
1428 NS_IMETHODIMP
1429 CSSStyleRuleImpl::List(FILE* out, PRInt32 aIndent) const
1431 // Indent
1432 for (PRInt32 index = aIndent; --index >= 0; ) fputs(" ", out);
1434 nsAutoString buffer;
1435 if (mSelector)
1436 mSelector->ToString(buffer, mSheet);
1438 buffer.AppendLiteral(" ");
1439 fputs(NS_LossyConvertUTF16toASCII(buffer).get(), out);
1440 if (nsnull != mDeclaration) {
1441 mDeclaration->List(out);
1443 else {
1444 fputs("{ null declaration }", out);
1446 fputs("\n", out);
1448 return NS_OK;
1450 #endif
1452 /* virtual */ nsresult
1453 CSSStyleRuleImpl::GetCssText(nsAString& aCssText)
1455 if (mSelector) {
1456 mSelector->ToString(aCssText, mSheet);
1457 aCssText.Append(PRUnichar(' '));
1459 aCssText.Append(PRUnichar('{'));
1460 aCssText.Append(PRUnichar(' '));
1461 if (mDeclaration)
1463 nsAutoString tempString;
1464 mDeclaration->ToString( tempString );
1465 aCssText.Append( tempString );
1467 aCssText.Append(PRUnichar(' '));
1468 aCssText.Append(PRUnichar('}'));
1469 return NS_OK;
1472 /* virtual */ nsresult
1473 CSSStyleRuleImpl::SetCssText(const nsAString& aCssText)
1475 // XXX TBI - need to re-parse rule & declaration
1476 return NS_OK;
1479 /* virtual */ nsresult
1480 CSSStyleRuleImpl::GetParentStyleSheet(nsICSSStyleSheet** aSheet)
1482 *aSheet = mSheet;
1483 NS_IF_ADDREF(*aSheet);
1484 return NS_OK;
1487 /* virtual */ nsresult
1488 CSSStyleRuleImpl::GetParentRule(nsICSSGroupRule** aParentRule)
1490 *aParentRule = mParentRule;
1491 NS_IF_ADDREF(*aParentRule);
1492 return NS_OK;
1495 /* virtual */ nsresult
1496 CSSStyleRuleImpl::GetSelectorText(nsAString& aSelectorText)
1498 if (mSelector)
1499 mSelector->ToString(aSelectorText, mSheet);
1500 else
1501 aSelectorText.Truncate();
1502 return NS_OK;
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
1511 return NS_OK;
1514 nsresult
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);
1521 if (!it) {
1522 return NS_ERROR_OUT_OF_MEMORY;
1525 return CallQueryInterface(it, aInstancePtrResult);