gecko/layout/style/nsHTMLCSSStyleSheet.cpp

373 lines
9.1 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* style sheet and style rule processor representing style attributes
*/
#include "nsIHTMLCSSStyleSheet.h"
#include "nsCRT.h"
#include "nsIAtom.h"
#include "nsIURL.h"
#include "nsCSSPseudoElements.h"
#include "nsIStyleRule.h"
#include "nsIFrame.h"
#include "nsICSSStyleRule.h"
#include "nsIStyleRuleProcessor.h"
#include "nsPresContext.h"
#include "nsIDocument.h"
#include "nsCOMPtr.h"
#include "nsRuleWalker.h"
#include "nsRuleData.h"
#include "nsRuleProcessorData.h"
// -----------------------------------------------------------
class HTMLCSSStyleSheetImpl : public nsIHTMLCSSStyleSheet,
public nsIStyleRuleProcessor {
public:
HTMLCSSStyleSheetImpl();
NS_DECL_ISUPPORTS
// basic style sheet data
NS_IMETHOD Init(nsIURI* aURL, nsIDocument* aDocument);
NS_IMETHOD Reset(nsIURI* aURL);
NS_IMETHOD GetSheetURI(nsIURI** aSheetURL) const;
NS_IMETHOD GetBaseURI(nsIURI** aBaseURL) const;
NS_IMETHOD GetTitle(nsString& aTitle) const;
NS_IMETHOD GetType(nsString& aType) const;
NS_IMETHOD_(PRBool) HasRules() const;
NS_IMETHOD GetApplicable(PRBool& aApplicable) const;
NS_IMETHOD SetEnabled(PRBool aEnabled);
NS_IMETHOD GetComplete(PRBool& aComplete) const;
NS_IMETHOD SetComplete();
// style sheet owner info
NS_IMETHOD GetParentSheet(nsIStyleSheet*& aParent) const; // will be null
NS_IMETHOD GetOwningDocument(nsIDocument*& aDocument) const;
NS_IMETHOD SetOwningDocument(nsIDocument* aDocument);
// nsIStyleRuleProcessor api
NS_IMETHOD RulesMatching(ElementRuleProcessorData* aData);
NS_IMETHOD RulesMatching(PseudoElementRuleProcessorData* aData);
NS_IMETHOD RulesMatching(AnonBoxRuleProcessorData* aData);
#ifdef MOZ_XUL
NS_IMETHOD RulesMatching(XULTreeRuleProcessorData* aData);
#endif
virtual nsReStyleHint HasStateDependentStyle(StateRuleProcessorData* aData);
virtual nsReStyleHint
HasAttributeDependentStyle(AttributeRuleProcessorData* aData);
NS_IMETHOD MediumFeaturesChanged(nsPresContext* aPresContext,
PRBool* aResult);
#ifdef DEBUG
virtual void List(FILE* out = stdout, PRInt32 aIndent = 0) const;
#endif
private:
// These are not supported and are not implemented!
HTMLCSSStyleSheetImpl(const HTMLCSSStyleSheetImpl& aCopy);
HTMLCSSStyleSheetImpl& operator=(const HTMLCSSStyleSheetImpl& aCopy);
protected:
virtual ~HTMLCSSStyleSheetImpl();
protected:
nsIURI* mURL;
nsIDocument* mDocument;
};
HTMLCSSStyleSheetImpl::HTMLCSSStyleSheetImpl()
: nsIHTMLCSSStyleSheet(),
mRefCnt(0),
mURL(nsnull),
mDocument(nsnull)
{
}
HTMLCSSStyleSheetImpl::~HTMLCSSStyleSheetImpl()
{
NS_RELEASE(mURL);
}
NS_IMPL_ISUPPORTS3(HTMLCSSStyleSheetImpl,
nsIHTMLCSSStyleSheet,
nsIStyleSheet,
nsIStyleRuleProcessor)
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::RulesMatching(ElementRuleProcessorData* aData)
{
nsIContent* content = aData->mContent;
// just get the one and only style rule from the content's STYLE attribute
nsICSSStyleRule* rule = content->GetInlineStyleRule();
if (rule) {
rule->RuleMatched();
aData->mRuleWalker->Forward(rule);
}
#ifdef MOZ_SMIL
rule = content->GetSMILOverrideStyleRule();
if (rule) {
rule->RuleMatched();
aData->mRuleWalker->Forward(rule);
}
#endif // MOZ_SMIL
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::RulesMatching(PseudoElementRuleProcessorData* aData)
{
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::RulesMatching(AnonBoxRuleProcessorData* aData)
{
return NS_OK;
}
#ifdef MOZ_XUL
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::RulesMatching(XULTreeRuleProcessorData* aData)
{
return NS_OK;
}
#endif
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::Init(nsIURI* aURL, nsIDocument* aDocument)
{
NS_PRECONDITION(aURL && aDocument, "null ptr");
if (! aURL || ! aDocument)
return NS_ERROR_NULL_POINTER;
if (mURL || mDocument)
return NS_ERROR_ALREADY_INITIALIZED;
mDocument = aDocument; // not refcounted!
mURL = aURL;
NS_ADDREF(mURL);
return NS_OK;
}
// Test if style is dependent on content state
nsReStyleHint
HTMLCSSStyleSheetImpl::HasStateDependentStyle(StateRuleProcessorData* aData)
{
return nsReStyleHint(0);
}
// Test if style is dependent on attribute
nsReStyleHint
HTMLCSSStyleSheetImpl::HasAttributeDependentStyle(AttributeRuleProcessorData* aData)
{
return nsReStyleHint(0);
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::MediumFeaturesChanged(nsPresContext* aPresContext,
PRBool* aRulesChanged)
{
*aRulesChanged = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::Reset(nsIURI* aURL)
{
NS_IF_RELEASE(mURL);
mURL = aURL;
NS_ADDREF(mURL);
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetSheetURI(nsIURI** aSheetURL) const
{
NS_IF_ADDREF(mURL);
*aSheetURL = mURL;
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetBaseURI(nsIURI** aBaseURL) const
{
NS_IF_ADDREF(mURL);
*aBaseURL = mURL;
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetTitle(nsString& aTitle) const
{
aTitle.AssignLiteral("Internal HTML/CSS Style Sheet");
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetType(nsString& aType) const
{
aType.AssignLiteral("text/html");
return NS_OK;
}
NS_IMETHODIMP_(PRBool)
HTMLCSSStyleSheetImpl::HasRules() const
{
// Say we always have rules, since we don't know.
return PR_TRUE;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetApplicable(PRBool& aApplicable) const
{
aApplicable = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::SetEnabled(PRBool aEnabled)
{ // these can't be disabled
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetComplete(PRBool& aComplete) const
{
aComplete = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::SetComplete()
{
return NS_OK;
}
// style sheet owner info
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetParentSheet(nsIStyleSheet*& aParent) const
{
aParent = nsnull;
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::GetOwningDocument(nsIDocument*& aDocument) const
{
NS_IF_ADDREF(mDocument);
aDocument = mDocument;
return NS_OK;
}
NS_IMETHODIMP
HTMLCSSStyleSheetImpl::SetOwningDocument(nsIDocument* aDocument)
{
mDocument = aDocument;
return NS_OK;
}
#ifdef DEBUG
void HTMLCSSStyleSheetImpl::List(FILE* out, PRInt32 aIndent) const
{
// Indent
for (PRInt32 index = aIndent; --index >= 0; ) fputs(" ", out);
fputs("HTML CSS Style Sheet: ", out);
nsCAutoString urlSpec;
mURL->GetSpec(urlSpec);
if (!urlSpec.IsEmpty()) {
fputs(urlSpec.get(), out);
}
fputs("\n", out);
}
#endif
// XXX For backwards compatibility and convenience
nsresult
NS_NewHTMLCSSStyleSheet(nsIHTMLCSSStyleSheet** aInstancePtrResult,
nsIURI* aURL, nsIDocument* aDocument)
{
nsresult rv;
nsIHTMLCSSStyleSheet* sheet;
if (NS_FAILED(rv = NS_NewHTMLCSSStyleSheet(&sheet)))
return rv;
if (NS_FAILED(rv = sheet->Init(aURL, aDocument))) {
NS_RELEASE(sheet);
return rv;
}
*aInstancePtrResult = sheet;
return NS_OK;
}
nsresult
NS_NewHTMLCSSStyleSheet(nsIHTMLCSSStyleSheet** aInstancePtrResult)
{
if (aInstancePtrResult == nsnull) {
return NS_ERROR_NULL_POINTER;
}
HTMLCSSStyleSheetImpl* it = new HTMLCSSStyleSheetImpl();
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(it);
*aInstancePtrResult = it;
return NS_OK;
}