1998-04-14 00:24:54 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
#include "nsICSSStyleSheet.h"
|
|
|
|
#include "nsIArena.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsICSSStyleRule.h"
|
|
|
|
#include "nsIHTMLContent.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsString.h"
|
1998-04-22 10:41:10 +04:00
|
|
|
#include "nsIPtr.h"
|
|
|
|
|
|
|
|
//#define DEBUG_REFS
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kICSSStyleSheetIID, NS_ICSS_STYLE_SHEET_IID);
|
|
|
|
static NS_DEFINE_IID(kIStyleSheetIID, NS_ISTYLE_SHEET_IID);
|
|
|
|
static NS_DEFINE_IID(kIStyleRuleIID, NS_ISTYLE_RULE_IID);
|
|
|
|
static NS_DEFINE_IID(kIHTMLContentIID, NS_IHTMLCONTENT_IID);
|
|
|
|
|
1998-04-22 10:41:10 +04:00
|
|
|
NS_DEF_PTR(nsIHTMLContent);
|
|
|
|
NS_DEF_PTR(nsIContent);
|
|
|
|
NS_DEF_PTR(nsIStyleRule);
|
|
|
|
NS_DEF_PTR(nsICSSStyleRule);
|
|
|
|
NS_DEF_PTR(nsIURL);
|
|
|
|
NS_DEF_PTR(nsISupportsArray);
|
|
|
|
NS_DEF_PTR(nsICSSStyleSheet);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
class CSSStyleSheetImpl : public nsICSSStyleSheet {
|
|
|
|
public:
|
|
|
|
void* operator new(size_t size);
|
|
|
|
void* operator new(size_t size, nsIArena* aArena);
|
|
|
|
void operator delete(void* ptr);
|
|
|
|
|
|
|
|
CSSStyleSheetImpl(nsIURL* aURL);
|
|
|
|
|
|
|
|
NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr);
|
|
|
|
NS_IMETHOD_(nsrefcnt) AddRef();
|
|
|
|
NS_IMETHOD_(nsrefcnt) Release();
|
|
|
|
|
|
|
|
virtual PRBool SelectorMatches(nsCSSSelector* aSelector,
|
|
|
|
nsIContent* aContent);
|
|
|
|
virtual PRInt32 RulesMatching(nsIPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIFrame* aParentFrame,
|
|
|
|
nsISupportsArray* aResults);
|
|
|
|
|
|
|
|
virtual nsIURL* GetURL(void);
|
|
|
|
|
|
|
|
virtual PRBool ContainsStyleSheet(nsIURL* aURL);
|
|
|
|
|
|
|
|
virtual void AppendStyleSheet(nsICSSStyleSheet* aSheet);
|
|
|
|
|
|
|
|
// XXX do these belong here or are they generic?
|
|
|
|
virtual void PrependStyleRule(nsICSSStyleRule* aRule);
|
|
|
|
virtual void AppendStyleRule(nsICSSStyleRule* aRule);
|
|
|
|
|
|
|
|
// XXX style rule enumerations
|
|
|
|
|
|
|
|
virtual void List(FILE* out = stdout, PRInt32 aIndent = 0) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// These are not supported and are not implemented!
|
|
|
|
CSSStyleSheetImpl(const CSSStyleSheetImpl& aCopy);
|
|
|
|
CSSStyleSheetImpl& operator=(const CSSStyleSheetImpl& aCopy);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~CSSStyleSheetImpl();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PRUint32 mInHeap : 1;
|
|
|
|
PRUint32 mRefCnt : 31;
|
|
|
|
|
1998-04-22 10:41:10 +04:00
|
|
|
nsIURLPtr mURL;
|
|
|
|
nsICSSStyleSheetPtr mFirstChild;
|
|
|
|
nsISupportsArrayPtr mRules;
|
|
|
|
nsICSSStyleSheetPtr mNext;
|
1998-04-14 00:24:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void* CSSStyleSheetImpl::operator new(size_t size)
|
|
|
|
{
|
|
|
|
CSSStyleSheetImpl* rv = (CSSStyleSheetImpl*) ::operator new(size);
|
|
|
|
#ifdef NS_DEBUG
|
|
|
|
if (nsnull != rv) {
|
|
|
|
nsCRT::memset(rv, 0xEE, size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
rv->mInHeap = 1;
|
|
|
|
return (void*) rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* CSSStyleSheetImpl::operator new(size_t size, nsIArena* aArena)
|
|
|
|
{
|
|
|
|
CSSStyleSheetImpl* rv = (CSSStyleSheetImpl*) aArena->Alloc(PRInt32(size));
|
|
|
|
#ifdef NS_DEBUG
|
|
|
|
if (nsnull != rv) {
|
|
|
|
nsCRT::memset(rv, 0xEE, size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
rv->mInHeap = 0;
|
|
|
|
return (void*) rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSStyleSheetImpl::operator delete(void* ptr)
|
|
|
|
{
|
|
|
|
CSSStyleSheetImpl* sheet = (CSSStyleSheetImpl*) ptr;
|
|
|
|
if (nsnull != sheet) {
|
|
|
|
if (sheet->mInHeap) {
|
|
|
|
::delete ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-04-22 10:41:10 +04:00
|
|
|
#ifdef DEBUG_REFS
|
|
|
|
static PRInt32 gInstanceCount;
|
|
|
|
#endif
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
CSSStyleSheetImpl::CSSStyleSheetImpl(nsIURL* aURL)
|
|
|
|
: nsICSSStyleSheet(),
|
1998-04-22 10:41:10 +04:00
|
|
|
mURL(nsnull), mFirstChild(nsnull), mRules(nsnull), mNext(nsnull)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
1998-04-22 10:41:10 +04:00
|
|
|
mURL.SetAddRef(aURL);
|
|
|
|
#ifdef DEBUG_REFS
|
|
|
|
++gInstanceCount;
|
|
|
|
fprintf(stdout, "%d + CSSStyleSheet size: %d\n", gInstanceCount, sizeof(*this));
|
|
|
|
#endif
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CSSStyleSheetImpl::~CSSStyleSheetImpl()
|
|
|
|
{
|
1998-04-22 10:41:10 +04:00
|
|
|
#ifdef DEBUG_REFS
|
|
|
|
--gInstanceCount;
|
|
|
|
fprintf(stdout, "%d - CSSStyleSheet\n", gInstanceCount);
|
|
|
|
#endif
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(CSSStyleSheetImpl)
|
|
|
|
NS_IMPL_RELEASE(CSSStyleSheetImpl)
|
|
|
|
|
|
|
|
nsresult CSSStyleSheetImpl::QueryInterface(const nsIID& aIID,
|
|
|
|
void** aInstancePtrResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull != aInstancePtrResult, "null pointer");
|
|
|
|
if (nsnull == aInstancePtrResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
if (aIID.Equals(kICSSStyleSheetIID)) {
|
|
|
|
*aInstancePtrResult = (void*) ((nsICSSStyleSheet*)this);
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kIStyleSheetIID)) {
|
|
|
|
*aInstancePtrResult = (void*) ((nsIStyleSheet*)this);
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kISupportsIID)) {
|
|
|
|
*aInstancePtrResult = (void*) ((nsISupports*)this);
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool CSSStyleSheetImpl::SelectorMatches(nsCSSSelector* aSelector, nsIContent* aContent)
|
|
|
|
{
|
|
|
|
PRBool result = PR_FALSE;
|
|
|
|
|
|
|
|
if ((nsnull == aSelector->mTag) || (aSelector->mTag == aContent->GetTag())) {
|
1998-04-22 19:43:46 +04:00
|
|
|
if ((nsnull != aSelector->mClass) || (nsnull != aSelector->mID)) {
|
1998-04-22 10:41:10 +04:00
|
|
|
nsIHTMLContentPtr htmlContent;
|
|
|
|
if (NS_OK == aContent->QueryInterface(kIHTMLContentIID, htmlContent.Query())) {
|
1998-04-14 00:24:54 +04:00
|
|
|
if ((nsnull == aSelector->mClass) || (aSelector->mClass == htmlContent->GetClass())) {
|
|
|
|
if ((nsnull == aSelector->mID) || (aSelector->mID == htmlContent->GetID())) {
|
|
|
|
result = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 CSSStyleSheetImpl::RulesMatching(nsIPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIFrame* aParentFrame,
|
|
|
|
nsISupportsArray* aResults)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull != aPresContext, "null arg");
|
|
|
|
NS_PRECONDITION(nsnull != aContent, "null arg");
|
|
|
|
// NS_PRECONDITION(nsnull != aParentFrame, "null arg");
|
|
|
|
NS_PRECONDITION(nsnull != aResults, "null arg");
|
|
|
|
|
|
|
|
PRInt32 matchCount = 0;
|
1998-04-22 10:41:10 +04:00
|
|
|
nsICSSStyleSheet* child = mFirstChild;
|
1998-04-14 00:24:54 +04:00
|
|
|
while (nsnull != child) {
|
|
|
|
matchCount += child->RulesMatching(aPresContext, aContent, aParentFrame, aResults);
|
1998-04-22 10:41:10 +04:00
|
|
|
child = ((CSSStyleSheetImpl*)child)->mNext;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-04-26 06:59:29 +04:00
|
|
|
PRInt32 count = (mRules.IsNotNull() ? mRules->Count() : 0);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
for (PRInt32 index = 0; index < count; index++) {
|
1998-04-22 10:41:10 +04:00
|
|
|
nsICSSStyleRulePtr rule = (nsICSSStyleRule*)mRules->ElementAt(index);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
nsCSSSelector* selector = rule->FirstSelector();
|
|
|
|
if (SelectorMatches(selector, aContent)) {
|
|
|
|
selector = selector->mNext;
|
|
|
|
nsIFrame* frame = aParentFrame;
|
|
|
|
while ((nsnull != selector) && (nsnull != frame)) { // check compound selectors
|
1998-04-22 10:41:10 +04:00
|
|
|
nsIContentPtr content;
|
|
|
|
frame->GetContent(content.AssignRef());
|
1998-04-14 00:24:54 +04:00
|
|
|
if (SelectorMatches(selector, content)) {
|
|
|
|
selector = selector->mNext;
|
|
|
|
}
|
1998-04-17 05:41:24 +04:00
|
|
|
frame->GetGeometricParent(frame);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
if (nsnull == selector) { // ran out, it matched
|
1998-04-22 10:41:10 +04:00
|
|
|
nsIStyleRulePtr iRule;
|
|
|
|
if (NS_OK == rule->QueryInterface(kIStyleRuleIID, iRule.Query())) {
|
1998-04-14 00:24:54 +04:00
|
|
|
aResults->AppendElement(iRule);
|
|
|
|
matchCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matchCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIURL* CSSStyleSheetImpl::GetURL(void)
|
|
|
|
{
|
1998-04-22 10:41:10 +04:00
|
|
|
return mURL.AddRef();
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool CSSStyleSheetImpl::ContainsStyleSheet(nsIURL* aURL)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull != aURL, "null arg");
|
|
|
|
|
|
|
|
PRBool result = (*mURL == *aURL);
|
|
|
|
|
1998-04-22 10:41:10 +04:00
|
|
|
nsICSSStyleSheet* child = mFirstChild;
|
1998-04-14 00:24:54 +04:00
|
|
|
while ((PR_FALSE == result) && (nsnull != child)) {
|
|
|
|
result = child->ContainsStyleSheet(aURL);
|
1998-04-22 10:41:10 +04:00
|
|
|
child = ((CSSStyleSheetImpl*)child)->mNext;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSStyleSheetImpl::AppendStyleSheet(nsICSSStyleSheet* aSheet)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull != aSheet, "null arg");
|
|
|
|
|
1998-04-26 06:59:29 +04:00
|
|
|
if (mFirstChild.IsNull()) {
|
1998-04-22 10:41:10 +04:00
|
|
|
mFirstChild.SetAddRef(aSheet);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
else {
|
1998-04-22 10:41:10 +04:00
|
|
|
nsICSSStyleSheet* child = mFirstChild;
|
1998-04-26 06:59:29 +04:00
|
|
|
while (((CSSStyleSheetImpl*)child)->mNext.IsNotNull()) {
|
1998-04-22 10:41:10 +04:00
|
|
|
child = ((CSSStyleSheetImpl*)child)->mNext;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1998-04-22 10:41:10 +04:00
|
|
|
((CSSStyleSheetImpl*)child)->mNext.SetAddRef(aSheet);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSStyleSheetImpl::PrependStyleRule(nsICSSStyleRule* aRule)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull != aRule, "null arg");
|
|
|
|
//XXX replace this with a binary search?
|
|
|
|
PRInt32 weight = aRule->GetWeight();
|
1998-04-26 06:59:29 +04:00
|
|
|
if (mRules.IsNull()) {
|
1998-04-22 10:41:10 +04:00
|
|
|
if (NS_OK != NS_NewISupportsArray(mRules.AssignPtr()))
|
1998-04-14 00:24:54 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
PRInt32 index = mRules->Count();
|
|
|
|
while (0 <= --index) {
|
1998-04-22 10:41:10 +04:00
|
|
|
nsICSSStyleRulePtr rule = (nsICSSStyleRule*)mRules->ElementAt(index);
|
1998-04-14 00:24:54 +04:00
|
|
|
if (rule->GetWeight() > weight) { // insert before rules with equal or lesser weight
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mRules->InsertElementAt(aRule, index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSStyleSheetImpl::AppendStyleRule(nsICSSStyleRule* aRule)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull != aRule, "null arg");
|
|
|
|
|
|
|
|
//XXX replace this with a binary search?
|
|
|
|
PRInt32 weight = aRule->GetWeight();
|
1998-04-26 06:59:29 +04:00
|
|
|
if (mRules.IsNull()) {
|
1998-04-22 10:41:10 +04:00
|
|
|
if (NS_OK != NS_NewISupportsArray(mRules.AssignPtr()))
|
1998-04-14 00:24:54 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
PRInt32 count = mRules->Count();
|
|
|
|
PRInt32 index = -1;
|
|
|
|
while (++index < count) {
|
1998-04-22 10:41:10 +04:00
|
|
|
nsICSSStyleRulePtr rule = (nsICSSStyleRule*)mRules->ElementAt(index);
|
1998-04-14 00:24:54 +04:00
|
|
|
if (rule->GetWeight() < weight) { // insert after rules with equal or greater weight (before lower weight)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mRules->InsertElementAt(aRule, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSStyleSheetImpl::List(FILE* out, PRInt32 aIndent) const
|
|
|
|
{
|
|
|
|
nsAutoString buffer;
|
|
|
|
PRInt32 index;
|
|
|
|
|
|
|
|
// Indent
|
|
|
|
for (index = aIndent; --index >= 0; ) fputs(" ", out);
|
|
|
|
|
|
|
|
fputs("CSS Style Sheet: ", out);
|
|
|
|
mURL->ToString(buffer);
|
|
|
|
fputs(buffer, out);
|
|
|
|
fputs("\n", out);
|
|
|
|
|
1998-04-22 10:41:10 +04:00
|
|
|
const nsICSSStyleSheet* child = mFirstChild;
|
1998-04-14 00:24:54 +04:00
|
|
|
while (nsnull != child) {
|
|
|
|
child->List(out, aIndent + 1);
|
1998-04-22 10:41:10 +04:00
|
|
|
child = ((CSSStyleSheetImpl*)child)->mNext;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-04-26 06:59:29 +04:00
|
|
|
PRInt32 count = (mRules.IsNotNull() ? mRules->Count() : 0);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
for (index = 0; index < count; index++) {
|
1998-04-22 10:41:10 +04:00
|
|
|
nsICSSStyleRulePtr rule = (nsICSSStyleRule*)mRules->ElementAt(index);
|
1998-04-14 00:24:54 +04:00
|
|
|
rule->List(out, aIndent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_HTML nsresult
|
|
|
|
NS_NewCSSStyleSheet(nsICSSStyleSheet** aInstancePtrResult, nsIURL* aURL)
|
|
|
|
{
|
|
|
|
if (aInstancePtrResult == nsnull) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSStyleSheetImpl *it = new CSSStyleSheetImpl(aURL);
|
|
|
|
|
|
|
|
if (nsnull == it) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it->QueryInterface(kICSSStyleSheetIID, (void **) aInstancePtrResult);
|
|
|
|
}
|