2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:cindent:tabstop=2:expandtab:shiftwidth=2:
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* representation of a CSS style sheet */
|
|
|
|
|
|
|
|
#include "nsCSSStyleSheet.h"
|
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsCSSRuleProcessor.h"
|
2013-01-08 15:25:47 -08:00
|
|
|
#include "mozilla/dom/Element.h"
|
2011-03-17 10:41:52 -07:00
|
|
|
#include "mozilla/css/NameSpaceRule.h"
|
2010-08-07 22:28:33 -07:00
|
|
|
#include "mozilla/css/GroupRule.h"
|
2011-03-17 10:41:52 -07:00
|
|
|
#include "mozilla/css/ImportRule.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIMediaList.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsString.h"
|
2009-02-03 06:42:18 -08:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMCSSStyleSheet.h"
|
2008-10-22 07:31:14 -07:00
|
|
|
#include "nsICSSRuleList.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMMediaList.h"
|
|
|
|
#include "nsIDOMNode.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2010-03-02 12:59:32 -08:00
|
|
|
#include "nsCSSParser.h"
|
2010-06-28 15:49:35 -07:00
|
|
|
#include "mozilla/css/Loader.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsICSSLoaderObserver.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsXMLNameSpaceMap.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2008-04-11 10:29:06 -07:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2008-07-26 09:14:48 -07:00
|
|
|
#include "nsRuleNode.h"
|
2010-05-11 13:41:47 -07:00
|
|
|
#include "nsMediaFeatures.h"
|
2012-07-20 09:42:08 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-10-26 06:32:10 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2013-03-17 07:45:01 -07:00
|
|
|
#include "mozilla/dom/CSSStyleSheetBinding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-28 10:10:08 -07:00
|
|
|
using namespace mozilla;
|
2013-01-08 15:25:47 -08:00
|
|
|
using namespace mozilla::dom;
|
2012-08-28 10:10:08 -07:00
|
|
|
|
2010-03-02 13:00:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// -------------------------------
|
|
|
|
// Style Rule List for the DOM
|
|
|
|
//
|
2008-10-22 07:31:14 -07:00
|
|
|
class CSSRuleListImpl : public nsICSSRuleList
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CSSRuleListImpl(nsCSSStyleSheet *aStyleSheet);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIDOMCSSRuleList interface
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD GetLength(uint32_t* aLength);
|
|
|
|
NS_IMETHOD Item(uint32_t aIndex, nsIDOMCSSRule** aReturn);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual nsIDOMCSSRule* GetItemAt(uint32_t aIndex, nsresult* aResult);
|
2008-10-22 07:31:14 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
void DropReference() { mStyleSheet = nullptr; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~CSSRuleListImpl();
|
|
|
|
|
|
|
|
nsCSSStyleSheet* mStyleSheet;
|
|
|
|
};
|
|
|
|
|
|
|
|
CSSRuleListImpl::CSSRuleListImpl(nsCSSStyleSheet *aStyleSheet)
|
|
|
|
{
|
|
|
|
// Not reference counted to avoid circular references.
|
|
|
|
// The style sheet will tell us when its going away.
|
|
|
|
mStyleSheet = aStyleSheet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSRuleListImpl::~CSSRuleListImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(CSSRuleList, CSSRuleListImpl)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// QueryInterface implementation for CSSRuleList
|
|
|
|
NS_INTERFACE_MAP_BEGIN(CSSRuleListImpl)
|
2008-10-22 07:31:14 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICSSRuleList)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRuleList)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSRuleList)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(CSSRuleListImpl)
|
|
|
|
NS_IMPL_RELEASE(CSSRuleListImpl)
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
CSSRuleListImpl::GetLength(uint32_t* aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (nullptr != mStyleSheet) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = mStyleSheet->StyleRuleCount();
|
|
|
|
*aLength = (uint32_t)count;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-22 07:31:14 -07:00
|
|
|
nsIDOMCSSRule*
|
2012-08-22 08:56:38 -07:00
|
|
|
CSSRuleListImpl::GetItemAt(uint32_t aIndex, nsresult* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
if (mStyleSheet) {
|
2009-12-31 07:56:33 -08:00
|
|
|
// ensure rules have correct parent
|
|
|
|
if (mStyleSheet->EnsureUniqueInner() !=
|
|
|
|
nsCSSStyleSheet::eUniqueInner_CloneFailed) {
|
2011-04-07 18:23:46 -07:00
|
|
|
nsRefPtr<css::Rule> rule;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = mStyleSheet->GetStyleRuleAt(aIndex, *getter_AddRefs(rule));
|
|
|
|
if (rule) {
|
2011-04-07 23:36:09 -07:00
|
|
|
*aResult = NS_OK;
|
|
|
|
return rule->GetDOMRule();
|
2008-10-22 07:31:14 -07:00
|
|
|
}
|
|
|
|
if (result == NS_ERROR_ILLEGAL_VALUE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
result = NS_OK; // per spec: "Return Value ... null if ... not a valid index."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-22 07:31:14 -07:00
|
|
|
|
|
|
|
*aResult = result;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-10-22 07:31:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
CSSRuleListImpl::Item(uint32_t aIndex, nsIDOMCSSRule** aReturn)
|
2008-10-22 07:31:14 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIDOMCSSRule* rule = GetItemAt(aIndex, &rv);
|
|
|
|
if (!rule) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aReturn = nullptr;
|
2008-10-22 07:31:14 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-07-18 13:23:12 -07:00
|
|
|
NS_ADDREF(*aReturn = rule);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
template <class Numeric>
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t DoCompare(Numeric a, Numeric b)
|
2008-07-26 09:14:48 -07:00
|
|
|
{
|
|
|
|
if (a == b)
|
|
|
|
return 0;
|
|
|
|
if (a < b)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-07-26 09:14:48 -07:00
|
|
|
nsMediaExpression::Matches(nsPresContext *aPresContext,
|
|
|
|
const nsCSSValue& aActualValue) const
|
|
|
|
{
|
|
|
|
const nsCSSValue& actual = aActualValue;
|
|
|
|
const nsCSSValue& required = mValue;
|
|
|
|
|
|
|
|
// If we don't have the feature, the match fails.
|
|
|
|
if (actual.GetUnit() == eCSSUnit_Null) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the expression had no value to match, the match succeeds,
|
2008-09-06 07:04:28 -07:00
|
|
|
// unless the value is an integer 0 or a zero length.
|
2008-07-26 09:14:48 -07:00
|
|
|
if (required.GetUnit() == eCSSUnit_Null) {
|
2008-09-06 07:04:28 -07:00
|
|
|
if (actual.GetUnit() == eCSSUnit_Integer)
|
|
|
|
return actual.GetIntValue() != 0;
|
|
|
|
if (actual.IsLengthUnit())
|
|
|
|
return actual.GetFloatValue() != 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mFeature->mRangeType == nsMediaFeature::eMinMaxAllowed ||
|
|
|
|
mRange == nsMediaExpression::eEqual, "yikes");
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t cmp; // -1 (actual < required)
|
2008-07-26 09:14:48 -07:00
|
|
|
// 0 (actual == required)
|
|
|
|
// 1 (actual > required)
|
|
|
|
switch (mFeature->mValueType) {
|
|
|
|
case nsMediaFeature::eLength:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(actual.IsLengthUnit(), "bad actual value");
|
|
|
|
NS_ASSERTION(required.IsLengthUnit(), "bad required value");
|
|
|
|
nscoord actualCoord = nsRuleNode::CalcLengthWithInitialFont(
|
|
|
|
aPresContext, actual);
|
|
|
|
nscoord requiredCoord = nsRuleNode::CalcLengthWithInitialFont(
|
|
|
|
aPresContext, required);
|
|
|
|
cmp = DoCompare(actualCoord, requiredCoord);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsMediaFeature::eInteger:
|
2008-09-06 07:04:28 -07:00
|
|
|
case nsMediaFeature::eBoolInteger:
|
2008-07-26 09:14:48 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(actual.GetUnit() == eCSSUnit_Integer,
|
|
|
|
"bad actual value");
|
|
|
|
NS_ASSERTION(required.GetUnit() == eCSSUnit_Integer,
|
|
|
|
"bad required value");
|
2008-09-06 07:04:28 -07:00
|
|
|
NS_ASSERTION(mFeature->mValueType != nsMediaFeature::eBoolInteger ||
|
|
|
|
actual.GetIntValue() == 0 || actual.GetIntValue() == 1,
|
|
|
|
"bad actual bool integer value");
|
|
|
|
NS_ASSERTION(mFeature->mValueType != nsMediaFeature::eBoolInteger ||
|
|
|
|
required.GetIntValue() == 0 || required.GetIntValue() == 1,
|
|
|
|
"bad required bool integer value");
|
2008-07-26 09:14:48 -07:00
|
|
|
cmp = DoCompare(actual.GetIntValue(), required.GetIntValue());
|
|
|
|
}
|
|
|
|
break;
|
2010-09-10 13:27:52 -07:00
|
|
|
case nsMediaFeature::eFloat:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(actual.GetUnit() == eCSSUnit_Number,
|
|
|
|
"bad actual value");
|
|
|
|
NS_ASSERTION(required.GetUnit() == eCSSUnit_Number,
|
|
|
|
"bad required value");
|
|
|
|
cmp = DoCompare(actual.GetFloatValue(), required.GetFloatValue());
|
|
|
|
}
|
|
|
|
break;
|
2008-07-26 09:14:48 -07:00
|
|
|
case nsMediaFeature::eIntRatio:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(actual.GetUnit() == eCSSUnit_Array &&
|
|
|
|
actual.GetArrayValue()->Count() == 2 &&
|
|
|
|
actual.GetArrayValue()->Item(0).GetUnit() ==
|
|
|
|
eCSSUnit_Integer &&
|
|
|
|
actual.GetArrayValue()->Item(1).GetUnit() ==
|
|
|
|
eCSSUnit_Integer,
|
|
|
|
"bad actual value");
|
|
|
|
NS_ASSERTION(required.GetUnit() == eCSSUnit_Array &&
|
|
|
|
required.GetArrayValue()->Count() == 2 &&
|
|
|
|
required.GetArrayValue()->Item(0).GetUnit() ==
|
|
|
|
eCSSUnit_Integer &&
|
|
|
|
required.GetArrayValue()->Item(1).GetUnit() ==
|
|
|
|
eCSSUnit_Integer,
|
|
|
|
"bad required value");
|
2012-08-22 08:56:38 -07:00
|
|
|
// Convert to int64_t so we can multiply without worry. Note
|
2008-07-26 09:14:48 -07:00
|
|
|
// that while the spec requires that both halves of |required|
|
|
|
|
// be positive, the numerator or denominator of |actual| might
|
|
|
|
// be zero (e.g., when testing 'aspect-ratio' on a 0-width or
|
|
|
|
// 0-height iframe).
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t actualNum = actual.GetArrayValue()->Item(0).GetIntValue(),
|
2008-07-26 09:14:48 -07:00
|
|
|
actualDen = actual.GetArrayValue()->Item(1).GetIntValue(),
|
|
|
|
requiredNum = required.GetArrayValue()->Item(0).GetIntValue(),
|
|
|
|
requiredDen = required.GetArrayValue()->Item(1).GetIntValue();
|
|
|
|
cmp = DoCompare(actualNum * requiredDen, requiredNum * actualDen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsMediaFeature::eResolution:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(actual.GetUnit() == eCSSUnit_Inch ||
|
2012-07-07 21:41:27 -07:00
|
|
|
actual.GetUnit() == eCSSUnit_Pixel ||
|
2008-07-26 09:14:48 -07:00
|
|
|
actual.GetUnit() == eCSSUnit_Centimeter,
|
|
|
|
"bad actual value");
|
|
|
|
NS_ASSERTION(required.GetUnit() == eCSSUnit_Inch ||
|
2012-07-07 21:41:27 -07:00
|
|
|
required.GetUnit() == eCSSUnit_Pixel ||
|
2008-07-26 09:14:48 -07:00
|
|
|
required.GetUnit() == eCSSUnit_Centimeter,
|
|
|
|
"bad required value");
|
|
|
|
float actualDPI = actual.GetFloatValue();
|
2012-07-07 21:41:27 -07:00
|
|
|
if (actual.GetUnit() == eCSSUnit_Centimeter) {
|
2008-07-26 09:14:48 -07:00
|
|
|
actualDPI = actualDPI * 2.54f;
|
2012-07-07 21:41:27 -07:00
|
|
|
} else if (actual.GetUnit() == eCSSUnit_Pixel) {
|
|
|
|
actualDPI = actualDPI * 96.0f;
|
|
|
|
}
|
2008-07-26 09:14:48 -07:00
|
|
|
float requiredDPI = required.GetFloatValue();
|
2012-07-07 21:41:27 -07:00
|
|
|
if (required.GetUnit() == eCSSUnit_Centimeter) {
|
2008-07-26 09:14:48 -07:00
|
|
|
requiredDPI = requiredDPI * 2.54f;
|
2012-07-07 21:41:27 -07:00
|
|
|
} else if (required.GetUnit() == eCSSUnit_Pixel) {
|
|
|
|
requiredDPI = requiredDPI * 96.0f;
|
|
|
|
}
|
2008-07-26 09:14:48 -07:00
|
|
|
cmp = DoCompare(actualDPI, requiredDPI);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsMediaFeature::eEnumerated:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(actual.GetUnit() == eCSSUnit_Enumerated,
|
|
|
|
"bad actual value");
|
|
|
|
NS_ASSERTION(required.GetUnit() == eCSSUnit_Enumerated,
|
|
|
|
"bad required value");
|
|
|
|
NS_ASSERTION(mFeature->mRangeType == nsMediaFeature::eMinMaxNotAllowed,
|
|
|
|
"bad range"); // we asserted above about mRange
|
|
|
|
// We don't really need DoCompare, but it doesn't hurt (and
|
|
|
|
// maybe the compiler will condense this case with eInteger).
|
|
|
|
cmp = DoCompare(actual.GetIntValue(), required.GetIntValue());
|
|
|
|
}
|
|
|
|
break;
|
2010-10-09 13:53:45 -07:00
|
|
|
case nsMediaFeature::eIdent:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(actual.GetUnit() == eCSSUnit_Ident,
|
|
|
|
"bad actual value");
|
|
|
|
NS_ASSERTION(required.GetUnit() == eCSSUnit_Ident,
|
|
|
|
"bad required value");
|
|
|
|
NS_ASSERTION(mFeature->mRangeType == nsMediaFeature::eMinMaxNotAllowed,
|
|
|
|
"bad range");
|
|
|
|
cmp = !(actual == required); // string comparison
|
|
|
|
}
|
|
|
|
break;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
switch (mRange) {
|
|
|
|
case nsMediaExpression::eMin:
|
|
|
|
return cmp != -1;
|
|
|
|
case nsMediaExpression::eMax:
|
|
|
|
return cmp != 1;
|
|
|
|
case nsMediaExpression::eEqual:
|
|
|
|
return cmp == 0;
|
|
|
|
}
|
|
|
|
NS_NOTREACHED("unexpected mRange");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
void
|
|
|
|
nsMediaQueryResultCacheKey::AddExpression(const nsMediaExpression* aExpression,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aExpressionMatches)
|
2008-07-26 09:14:48 -07:00
|
|
|
{
|
|
|
|
const nsMediaFeature *feature = aExpression->mFeature;
|
2012-07-30 07:20:58 -07:00
|
|
|
FeatureEntry *entry = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mFeatureCache.Length(); ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
if (mFeatureCache[i].mFeature == feature) {
|
|
|
|
entry = &mFeatureCache[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!entry) {
|
|
|
|
entry = mFeatureCache.AppendElement();
|
|
|
|
if (!entry) {
|
|
|
|
return; /* out of memory */
|
|
|
|
}
|
|
|
|
entry->mFeature = feature;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionEntry eentry = { *aExpression, aExpressionMatches };
|
|
|
|
entry->mExpressions.AppendElement(eentry);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-07-26 09:14:48 -07:00
|
|
|
nsMediaQueryResultCacheKey::Matches(nsPresContext* aPresContext) const
|
|
|
|
{
|
|
|
|
if (aPresContext->Medium() != mMedium) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mFeatureCache.Length(); ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
const FeatureEntry *entry = &mFeatureCache[i];
|
|
|
|
nsCSSValue actual;
|
2009-10-15 17:23:18 -07:00
|
|
|
nsresult rv =
|
|
|
|
(entry->mFeature->mGetter)(aPresContext, entry->mFeature, actual);
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false); // any better ideas?
|
2008-07-26 09:14:48 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < entry->mExpressions.Length(); ++j) {
|
2008-07-26 09:14:48 -07:00
|
|
|
const ExpressionEntry &eentry = entry->mExpressions[j];
|
|
|
|
if (eentry.mExpression.Matches(aPresContext, actual) !=
|
|
|
|
eentry.mExpressionMatches) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
void
|
|
|
|
nsMediaQuery::AppendToString(nsAString& aString) const
|
|
|
|
{
|
|
|
|
if (mHadUnknownExpression) {
|
|
|
|
aString.AppendLiteral("not all");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mNegated || !mHasOnly, "can't have not and only");
|
|
|
|
NS_ASSERTION(!mTypeOmitted || (!mNegated && !mHasOnly),
|
|
|
|
"can't have not or only when type is omitted");
|
|
|
|
if (!mTypeOmitted) {
|
|
|
|
if (mNegated) {
|
|
|
|
aString.AppendLiteral("not ");
|
|
|
|
} else if (mHasOnly) {
|
|
|
|
aString.AppendLiteral("only ");
|
|
|
|
}
|
2010-03-08 07:45:00 -08:00
|
|
|
aString.Append(nsDependentAtomString(mMediaType));
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0, i_end = mExpressions.Length(); i < i_end; ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
if (i > 0 || !mTypeOmitted)
|
|
|
|
aString.AppendLiteral(" and ");
|
|
|
|
aString.AppendLiteral("(");
|
|
|
|
|
|
|
|
const nsMediaExpression &expr = mExpressions[i];
|
|
|
|
if (expr.mRange == nsMediaExpression::eMin) {
|
|
|
|
aString.AppendLiteral("min-");
|
|
|
|
} else if (expr.mRange == nsMediaExpression::eMax) {
|
|
|
|
aString.AppendLiteral("max-");
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsMediaFeature *feature = expr.mFeature;
|
2010-03-08 07:45:00 -08:00
|
|
|
aString.Append(nsDependentAtomString(*feature->mName));
|
2008-07-26 09:14:48 -07:00
|
|
|
|
|
|
|
if (expr.mValue.GetUnit() != eCSSUnit_Null) {
|
|
|
|
aString.AppendLiteral(": ");
|
|
|
|
switch (feature->mValueType) {
|
|
|
|
case nsMediaFeature::eLength:
|
|
|
|
NS_ASSERTION(expr.mValue.IsLengthUnit(), "bad unit");
|
|
|
|
// Use 'width' as a property that takes length values
|
|
|
|
// written in the normal way.
|
2010-07-23 11:00:27 -07:00
|
|
|
expr.mValue.AppendToString(eCSSProperty_width, aString);
|
2008-07-26 09:14:48 -07:00
|
|
|
break;
|
|
|
|
case nsMediaFeature::eInteger:
|
2008-09-06 07:04:28 -07:00
|
|
|
case nsMediaFeature::eBoolInteger:
|
2008-07-26 09:14:48 -07:00
|
|
|
NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Integer,
|
|
|
|
"bad unit");
|
|
|
|
// Use 'z-index' as a property that takes integer values
|
|
|
|
// written without anything extra.
|
2010-07-23 11:00:27 -07:00
|
|
|
expr.mValue.AppendToString(eCSSProperty_z_index, aString);
|
2008-07-26 09:14:48 -07:00
|
|
|
break;
|
2010-09-10 13:27:52 -07:00
|
|
|
case nsMediaFeature::eFloat:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Number,
|
|
|
|
"bad unit");
|
|
|
|
// Use 'line-height' as a property that takes float values
|
|
|
|
// written in the normal way.
|
|
|
|
expr.mValue.AppendToString(eCSSProperty_line_height, aString);
|
|
|
|
}
|
|
|
|
break;
|
2008-07-26 09:14:48 -07:00
|
|
|
case nsMediaFeature::eIntRatio:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Array,
|
|
|
|
"bad unit");
|
|
|
|
nsCSSValue::Array *array = expr.mValue.GetArrayValue();
|
|
|
|
NS_ASSERTION(array->Count() == 2, "unexpected length");
|
|
|
|
NS_ASSERTION(array->Item(0).GetUnit() == eCSSUnit_Integer,
|
|
|
|
"bad unit");
|
|
|
|
NS_ASSERTION(array->Item(1).GetUnit() == eCSSUnit_Integer,
|
|
|
|
"bad unit");
|
2010-07-23 11:00:27 -07:00
|
|
|
array->Item(0).AppendToString(eCSSProperty_z_index, aString);
|
2008-07-26 09:14:48 -07:00
|
|
|
aString.AppendLiteral("/");
|
2010-07-23 11:00:27 -07:00
|
|
|
array->Item(1).AppendToString(eCSSProperty_z_index, aString);
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsMediaFeature::eResolution:
|
2010-03-08 07:45:00 -08:00
|
|
|
{
|
2010-11-30 10:18:15 -08:00
|
|
|
aString.AppendFloat(expr.mValue.GetFloatValue());
|
2010-03-08 07:45:00 -08:00
|
|
|
if (expr.mValue.GetUnit() == eCSSUnit_Inch) {
|
|
|
|
aString.AppendLiteral("dpi");
|
2012-07-07 21:41:27 -07:00
|
|
|
} else if (expr.mValue.GetUnit() == eCSSUnit_Pixel) {
|
|
|
|
aString.AppendLiteral("dppx");
|
2010-03-08 07:45:00 -08:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Centimeter,
|
|
|
|
"bad unit");
|
|
|
|
aString.AppendLiteral("dpcm");
|
|
|
|
}
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsMediaFeature::eEnumerated:
|
|
|
|
NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Enumerated,
|
|
|
|
"bad unit");
|
|
|
|
AppendASCIItoUTF16(
|
|
|
|
nsCSSProps::ValueToKeyword(expr.mValue.GetIntValue(),
|
2009-10-15 17:23:18 -07:00
|
|
|
feature->mData.mKeywordTable),
|
2008-07-26 09:14:48 -07:00
|
|
|
aString);
|
|
|
|
break;
|
2010-10-09 13:53:45 -07:00
|
|
|
case nsMediaFeature::eIdent:
|
|
|
|
NS_ASSERTION(expr.mValue.GetUnit() == eCSSUnit_Ident,
|
|
|
|
"bad unit");
|
|
|
|
aString.Append(expr.mValue.GetStringBufferValue());
|
|
|
|
break;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aString.AppendLiteral(")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMediaQuery*
|
|
|
|
nsMediaQuery::Clone() const
|
|
|
|
{
|
|
|
|
nsAutoPtr<nsMediaQuery> result(new nsMediaQuery(*this));
|
|
|
|
NS_ENSURE_TRUE(result &&
|
|
|
|
result->mExpressions.Length() == mExpressions.Length(),
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr);
|
2008-07-26 09:14:48 -07:00
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-07-26 09:14:48 -07:00
|
|
|
nsMediaQuery::Matches(nsPresContext* aPresContext,
|
2011-04-21 20:17:31 -07:00
|
|
|
nsMediaQueryResultCacheKey* aKey) const
|
2008-07-26 09:14:48 -07:00
|
|
|
{
|
|
|
|
if (mHadUnknownExpression)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-07-26 09:14:48 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool match =
|
2008-07-26 09:14:48 -07:00
|
|
|
mMediaType == aPresContext->Medium() || mMediaType == nsGkAtoms::all;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0, i_end = mExpressions.Length(); match && i < i_end; ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
const nsMediaExpression &expr = mExpressions[i];
|
|
|
|
nsCSSValue actual;
|
2009-10-15 17:23:18 -07:00
|
|
|
nsresult rv =
|
|
|
|
(expr.mFeature->mGetter)(aPresContext, expr.mFeature, actual);
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false); // any better ideas?
|
2008-07-26 09:14:48 -07:00
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
match = expr.Matches(aPresContext, actual);
|
2011-04-21 20:17:31 -07:00
|
|
|
if (aKey) {
|
|
|
|
aKey->AddExpression(&expr, match);
|
|
|
|
}
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return match == !mNegated;
|
|
|
|
}
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(MediaList, nsMediaList)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsMediaList)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMediaList)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MediaList)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsMediaList)
|
|
|
|
NS_IMPL_RELEASE(nsMediaList)
|
|
|
|
|
|
|
|
|
|
|
|
nsMediaList::nsMediaList()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mStyleSheet(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMediaList::~nsMediaList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaList::GetText(nsAString& aMediaText)
|
|
|
|
{
|
|
|
|
aMediaText.Truncate();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0, i_end = mArray.Length(); i < i_end; ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
nsMediaQuery* query = mArray[i];
|
|
|
|
NS_ENSURE_TRUE(query, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
query->AppendToString(aMediaText);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (i + 1 < i_end) {
|
|
|
|
aMediaText.AppendLiteral(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXXbz this is so ill-defined in the spec, it's not clear quite what
|
|
|
|
// it should be doing....
|
|
|
|
nsresult
|
|
|
|
nsMediaList::SetText(const nsAString& aMediaText)
|
|
|
|
{
|
2010-03-02 12:59:32 -08:00
|
|
|
nsCSSParser parser;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-17 07:42:59 -07:00
|
|
|
bool htmlMode = mStyleSheet && mStyleSheet->GetOwnerNode();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return parser.ParseMediaList(aMediaText, nullptr, 0,
|
2010-03-02 12:59:32 -08:00
|
|
|
this, htmlMode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-07-26 09:14:48 -07:00
|
|
|
nsMediaList::Matches(nsPresContext* aPresContext,
|
2011-04-21 20:17:31 -07:00
|
|
|
nsMediaQueryResultCacheKey* aKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0, i_end = mArray.Length(); i < i_end; ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
if (mArray[i]->Matches(aPresContext, aKey)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-07-26 09:14:48 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 13:49:22 -07:00
|
|
|
return mArray.IsEmpty();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-05-11 13:41:47 -07:00
|
|
|
nsMediaList::SetStyleSheet(nsCSSStyleSheet *aSheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aSheet == mStyleSheet || !aSheet || !mStyleSheet,
|
|
|
|
"multiple style sheets competing for one media list");
|
2010-05-11 13:41:47 -07:00
|
|
|
mStyleSheet = aSheet;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaList::Clone(nsMediaList** aResult)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsMediaList> result = new nsMediaList();
|
2008-07-26 09:14:48 -07:00
|
|
|
if (!result || !result->mArray.AppendElements(mArray.Length()))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0, i_end = mArray.Length(); i < i_end; ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
if (!(result->mArray[i] = mArray[i]->Clone())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(*aResult = result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMediaList::GetMediaText(nsAString& aMediaText)
|
|
|
|
{
|
|
|
|
return GetText(aMediaText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// "sheet" should be an nsCSSStyleSheet and "doc" should be an
|
|
|
|
// nsCOMPtr<nsIDocument>
|
|
|
|
#define BEGIN_MEDIA_CHANGE(sheet, doc) \
|
|
|
|
if (sheet) { \
|
2010-05-12 13:18:47 -07:00
|
|
|
doc = sheet->GetOwningDocument(); \
|
2007-03-22 10:30:00 -07:00
|
|
|
} \
|
2011-10-17 07:59:28 -07:00
|
|
|
mozAutoDocUpdate updateBatch(doc, UPDATE_STYLE, true); \
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sheet) { \
|
|
|
|
rv = sheet->WillDirty(); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define END_MEDIA_CHANGE(sheet, doc) \
|
|
|
|
if (sheet) { \
|
|
|
|
sheet->DidDirty(); \
|
|
|
|
} \
|
|
|
|
/* XXXldb Pass something meaningful? */ \
|
|
|
|
if (doc) { \
|
2012-07-30 07:20:58 -07:00
|
|
|
doc->StyleRuleChanged(sheet, nullptr, nullptr); \
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMediaList::SetMediaText(const nsAString& aMediaText)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
|
|
|
|
BEGIN_MEDIA_CHANGE(mStyleSheet, doc)
|
|
|
|
|
|
|
|
rv = SetText(aMediaText);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
END_MEDIA_CHANGE(mStyleSheet, doc)
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMediaList::GetLength(uint32_t* aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aLength);
|
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
*aLength = mArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMediaList::Item(uint32_t aIndex, nsAString& aReturn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = aIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (0 <= index && index < Count()) {
|
2008-07-26 09:14:48 -07:00
|
|
|
nsMediaQuery* query = mArray[index];
|
|
|
|
NS_ENSURE_TRUE(query, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
aReturn.Truncate();
|
|
|
|
query->AppendToString(aReturn);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMediaList::DeleteMedium(const nsAString& aOldMedium)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
|
|
|
|
BEGIN_MEDIA_CHANGE(mStyleSheet, doc)
|
|
|
|
|
|
|
|
rv = Delete(aOldMedium);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
END_MEDIA_CHANGE(mStyleSheet, doc)
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMediaList::AppendMedium(const nsAString& aNewMedium)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
|
|
|
|
BEGIN_MEDIA_CHANGE(mStyleSheet, doc)
|
|
|
|
|
|
|
|
rv = Append(aNewMedium);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
END_MEDIA_CHANGE(mStyleSheet, doc)
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaList::Delete(const nsAString& aOldMedium)
|
|
|
|
{
|
|
|
|
if (aOldMedium.IsEmpty())
|
|
|
|
return NS_ERROR_DOM_NOT_FOUND_ERR;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0, i_end = mArray.Length(); i < i_end; ++i) {
|
2008-07-26 09:14:48 -07:00
|
|
|
nsMediaQuery* query = mArray[i];
|
|
|
|
NS_ENSURE_TRUE(query, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
nsAutoString buf;
|
|
|
|
query->AppendToString(buf);
|
|
|
|
if (buf == aOldMedium) {
|
|
|
|
mArray.RemoveElementAt(i);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
return NS_ERROR_DOM_NOT_FOUND_ERR;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMediaList::Append(const nsAString& aNewMedium)
|
|
|
|
{
|
|
|
|
if (aNewMedium.IsEmpty())
|
|
|
|
return NS_ERROR_DOM_NOT_FOUND_ERR;
|
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
Delete(aNewMedium);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-26 09:14:48 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsTArray<nsAutoPtr<nsMediaQuery> > buf;
|
2013-04-01 10:46:26 -07:00
|
|
|
mArray.SwapElements(buf);
|
2008-07-26 09:14:48 -07:00
|
|
|
SetText(aNewMedium);
|
|
|
|
if (mArray.Length() == 1) {
|
|
|
|
nsMediaQuery *query = mArray[0].forget();
|
|
|
|
if (!buf.AppendElement(query)) {
|
|
|
|
delete query;
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-04-01 10:46:26 -07:00
|
|
|
|
|
|
|
mArray.SwapElements(buf);
|
2008-07-26 09:14:48 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------
|
|
|
|
// CSS Style Sheet Inner Data Container
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2012-08-28 10:10:08 -07:00
|
|
|
nsCSSStyleSheetInner::nsCSSStyleSheetInner(nsCSSStyleSheet* aPrimarySheet,
|
|
|
|
CORSMode aCORSMode)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mSheets(),
|
2012-08-28 10:10:08 -07:00
|
|
|
mCORSMode(aCORSMode),
|
2011-10-17 07:59:28 -07:00
|
|
|
mComplete(false)
|
2007-05-01 12:26:15 -07:00
|
|
|
#ifdef DEBUG
|
2011-10-17 07:59:28 -07:00
|
|
|
, mPrincipalSet(false)
|
2007-05-01 12:26:15 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsCSSStyleSheetInner);
|
2008-09-09 11:08:11 -07:00
|
|
|
mSheets.AppendElement(aPrimarySheet);
|
2007-05-01 12:26:15 -07:00
|
|
|
|
|
|
|
mPrincipal = do_CreateInstance("@mozilla.org/nullprincipal;1");
|
2012-08-04 00:44:01 -07:00
|
|
|
if (!mPrincipal) {
|
|
|
|
NS_RUNTIMEABORT("OOM");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool SetStyleSheetReference(css::Rule* aRule, void* aSheet)
|
2008-09-09 11:08:11 -07:00
|
|
|
{
|
|
|
|
if (aRule) {
|
2010-05-11 13:41:47 -07:00
|
|
|
aRule->SetStyleSheet(static_cast<nsCSSStyleSheet*>(aSheet));
|
2008-09-09 11:08:11 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-09-09 11:08:11 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2011-04-07 18:23:46 -07:00
|
|
|
CloneRuleInto(css::Rule* aRule, void* aArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-07 18:23:46 -07:00
|
|
|
nsRefPtr<css::Rule> clone = aRule->Clone();
|
|
|
|
static_cast<nsCOMArray<css::Rule>*>(aArray)->AppendObject(clone);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-09-09 11:08:11 -07:00
|
|
|
struct ChildSheetListBuilder {
|
|
|
|
nsRefPtr<nsCSSStyleSheet>* sheetSlot;
|
|
|
|
nsCSSStyleSheet* parent;
|
|
|
|
|
|
|
|
void SetParentLinks(nsCSSStyleSheet* aSheet) {
|
|
|
|
aSheet->mParent = parent;
|
|
|
|
aSheet->SetOwningDocument(parent->mDocument);
|
|
|
|
}
|
2011-01-14 19:57:53 -08:00
|
|
|
|
|
|
|
static void ReparentChildList(nsCSSStyleSheet* aPrimarySheet,
|
|
|
|
nsCSSStyleSheet* aFirstChild)
|
|
|
|
{
|
|
|
|
for (nsCSSStyleSheet *child = aFirstChild; child; child = child->mNext) {
|
|
|
|
child->mParent = aPrimarySheet;
|
|
|
|
child->SetOwningDocument(aPrimarySheet->mDocument);
|
|
|
|
}
|
|
|
|
}
|
2008-09-09 11:08:11 -07:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-04-07 18:23:46 -07:00
|
|
|
nsCSSStyleSheet::RebuildChildList(css::Rule* aRule, void* aBuilder)
|
2008-09-09 11:08:11 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type = aRule->GetType();
|
2011-04-07 18:23:46 -07:00
|
|
|
if (type < css::Rule::IMPORT_RULE) {
|
2009-05-28 15:16:31 -07:00
|
|
|
// Keep going till we get to the import rules.
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-09-09 11:08:11 -07:00
|
|
|
}
|
|
|
|
|
2011-04-07 18:23:46 -07:00
|
|
|
if (type != css::Rule::IMPORT_RULE) {
|
2009-05-28 15:16:31 -07:00
|
|
|
// We're past all the import rules; stop the enumeration.
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-09-09 11:08:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ChildSheetListBuilder* builder =
|
|
|
|
static_cast<ChildSheetListBuilder*>(aBuilder);
|
|
|
|
|
|
|
|
// XXXbz We really need to decomtaminate all this stuff. Is there a reason
|
2011-03-17 10:41:52 -07:00
|
|
|
// that I can't just QI to ImportRule and get an nsCSSStyleSheet
|
2008-09-09 11:08:11 -07:00
|
|
|
// directly from it?
|
|
|
|
nsCOMPtr<nsIDOMCSSImportRule> importRule(do_QueryInterface(aRule));
|
|
|
|
NS_ASSERTION(importRule, "GetType lied");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleSheet> childSheet;
|
|
|
|
importRule->GetStyleSheet(getter_AddRefs(childSheet));
|
|
|
|
|
|
|
|
// Have to do this QI to be safe, since XPConnect can fake
|
|
|
|
// nsIDOMCSSStyleSheets
|
2010-05-11 13:41:47 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet> cssSheet = do_QueryObject(childSheet);
|
2008-09-09 11:08:11 -07:00
|
|
|
if (!cssSheet) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-09-09 11:08:11 -07:00
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
(*builder->sheetSlot) = cssSheet;
|
2008-09-09 11:08:11 -07:00
|
|
|
builder->SetParentLinks(*builder->sheetSlot);
|
2009-05-26 12:40:49 -07:00
|
|
|
builder->sheetSlot = &(*builder->sheetSlot)->mNext;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-09-09 11:08:11 -07:00
|
|
|
}
|
|
|
|
|
2012-01-02 18:19:14 -08:00
|
|
|
size_t
|
|
|
|
nsCSSStyleSheet::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
const nsCSSStyleSheet* s = this;
|
|
|
|
while (s) {
|
|
|
|
n += aMallocSizeOf(s);
|
|
|
|
|
2012-10-09 20:03:18 -07:00
|
|
|
// Each inner can be shared by multiple sheets. So we only count the inner
|
|
|
|
// if this sheet is the first one in the list of those sharing it. As a
|
|
|
|
// result, the first such sheet takes all the blame for the memory
|
|
|
|
// consumption of the inner, which isn't ideal but it's better than
|
|
|
|
// double-counting the inner.
|
|
|
|
if (s->mInner->mSheets[0] == s) {
|
|
|
|
n += s->mInner->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it
|
|
|
|
// is worthwhile:
|
2012-01-02 18:19:14 -08:00
|
|
|
// - s->mTitle
|
|
|
|
// - s->mMedia
|
|
|
|
// - s->mRuleCollection
|
|
|
|
// - s->mRuleProcessors
|
|
|
|
//
|
|
|
|
// The following members are not measured:
|
|
|
|
// - s->mOwnerRule, because it's non-owning
|
|
|
|
|
|
|
|
s = s->mNext;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheetInner::nsCSSStyleSheetInner(nsCSSStyleSheetInner& aCopy,
|
2008-09-09 11:08:11 -07:00
|
|
|
nsCSSStyleSheet* aPrimarySheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mSheets(),
|
|
|
|
mSheetURI(aCopy.mSheetURI),
|
2008-01-21 20:26:01 -08:00
|
|
|
mOriginalSheetURI(aCopy.mOriginalSheetURI),
|
2007-03-22 10:30:00 -07:00
|
|
|
mBaseURI(aCopy.mBaseURI),
|
2007-05-01 12:26:15 -07:00
|
|
|
mPrincipal(aCopy.mPrincipal),
|
2012-08-28 10:10:08 -07:00
|
|
|
mCORSMode(aCopy.mCORSMode),
|
2007-03-22 10:30:00 -07:00
|
|
|
mComplete(aCopy.mComplete)
|
2007-05-01 12:26:15 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mPrincipalSet(aCopy.mPrincipalSet)
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsCSSStyleSheetInner);
|
2009-02-05 01:38:51 -08:00
|
|
|
AddSheet(aPrimarySheet);
|
2007-03-22 10:30:00 -07:00
|
|
|
aCopy.mOrderedRules.EnumerateForwards(CloneRuleInto, &mOrderedRules);
|
2008-09-09 11:08:11 -07:00
|
|
|
mOrderedRules.EnumerateForwards(SetStyleSheetReference, aPrimarySheet);
|
|
|
|
|
|
|
|
ChildSheetListBuilder builder = { &mFirstChild, aPrimarySheet };
|
2009-05-26 12:40:49 -07:00
|
|
|
mOrderedRules.EnumerateForwards(nsCSSStyleSheet::RebuildChildList, &builder);
|
2008-09-09 11:08:11 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
RebuildNameSpaces();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSStyleSheetInner::~nsCSSStyleSheetInner()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsCSSStyleSheetInner);
|
2012-07-30 07:20:58 -07:00
|
|
|
mOrderedRules.EnumerateForwards(SetStyleSheetReference, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSStyleSheetInner*
|
2008-09-09 11:08:11 -07:00
|
|
|
nsCSSStyleSheetInner::CloneFor(nsCSSStyleSheet* aPrimarySheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-09-09 11:08:11 -07:00
|
|
|
return new nsCSSStyleSheetInner(*this, aPrimarySheet);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheetInner::AddSheet(nsCSSStyleSheet* aSheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-09-09 11:08:11 -07:00
|
|
|
mSheets.AppendElement(aSheet);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheetInner::RemoveSheet(nsCSSStyleSheet* aSheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-02-03 06:42:18 -08:00
|
|
|
if (1 == mSheets.Length()) {
|
|
|
|
NS_ASSERTION(aSheet == mSheets.ElementAt(0), "bad parent");
|
2007-03-22 10:30:00 -07:00
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
2009-02-03 06:42:18 -08:00
|
|
|
if (aSheet == mSheets.ElementAt(0)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mSheets.RemoveElementAt(0);
|
2009-02-03 06:42:18 -08:00
|
|
|
NS_ASSERTION(mSheets.Length(), "no parents");
|
2007-03-22 10:30:00 -07:00
|
|
|
mOrderedRules.EnumerateForwards(SetStyleSheetReference,
|
2009-02-03 06:42:18 -08:00
|
|
|
mSheets.ElementAt(0));
|
2011-01-14 19:57:53 -08:00
|
|
|
|
|
|
|
ChildSheetListBuilder::ReparentChildList(mSheets[0], mFirstChild);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2008-09-09 11:08:11 -07:00
|
|
|
mSheets.RemoveElement(aSheet);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-15 13:23:21 -07:00
|
|
|
static void
|
2011-04-07 18:23:46 -07:00
|
|
|
AddNamespaceRuleToMap(css::Rule* aRule, nsXMLNameSpaceMap* aMap)
|
2008-10-15 13:23:21 -07:00
|
|
|
{
|
2011-04-07 18:23:46 -07:00
|
|
|
NS_ASSERTION(aRule->GetType() == css::Rule::NAMESPACE_RULE, "Bogus rule type");
|
2008-10-15 13:23:21 -07:00
|
|
|
|
2011-03-17 10:41:52 -07:00
|
|
|
nsRefPtr<css::NameSpaceRule> nameSpaceRule = do_QueryObject(aRule);
|
|
|
|
|
2008-10-15 13:23:21 -07:00
|
|
|
nsAutoString urlSpec;
|
|
|
|
nameSpaceRule->GetURLSpec(urlSpec);
|
|
|
|
|
2011-03-17 10:41:52 -07:00
|
|
|
aMap->AddPrefix(nameSpaceRule->GetPrefix(), urlSpec);
|
2008-10-15 13:23:21 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2011-04-07 18:23:46 -07:00
|
|
|
CreateNameSpace(css::Rule* aRule, void* aNameSpacePtr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type = aRule->GetType();
|
2011-04-07 18:23:46 -07:00
|
|
|
if (css::Rule::NAMESPACE_RULE == type) {
|
2008-10-15 13:23:21 -07:00
|
|
|
AddNamespaceRuleToMap(aRule,
|
|
|
|
static_cast<nsXMLNameSpaceMap*>(aNameSpacePtr));
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-10-15 13:23:21 -07:00
|
|
|
// stop if not namespace, import or charset because namespace can't follow
|
|
|
|
// anything else
|
2011-04-07 18:23:46 -07:00
|
|
|
return (css::Rule::CHARSET_RULE == type || css::Rule::IMPORT_RULE == type);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSStyleSheetInner::RebuildNameSpaces()
|
|
|
|
{
|
2008-10-15 13:23:21 -07:00
|
|
|
// Just nuke our existing namespace map, if any
|
|
|
|
if (NS_SUCCEEDED(CreateNamespaceMap())) {
|
|
|
|
mOrderedRules.EnumerateForwards(CreateNameSpace, mNameSpaceMap);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-15 13:23:21 -07:00
|
|
|
nsresult
|
|
|
|
nsCSSStyleSheetInner::CreateNamespaceMap()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mNameSpaceMap = nsXMLNameSpaceMap::Create(false);
|
2008-10-15 13:23:21 -07:00
|
|
|
NS_ENSURE_TRUE(mNameSpaceMap, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
// Override the default namespace map behavior for the null prefix to
|
|
|
|
// return the wildcard namespace instead of the null namespace.
|
2012-07-30 07:20:58 -07:00
|
|
|
mNameSpaceMap->AddPrefix(nullptr, kNameSpaceID_Unknown);
|
2008-10-15 13:23:21 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-02 18:19:14 -08:00
|
|
|
size_t
|
|
|
|
nsCSSStyleSheetInner::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += mOrderedRules.SizeOfExcludingThis(css::Rule::SizeOfCOMArrayElementIncludingThis,
|
|
|
|
aMallocSizeOf);
|
|
|
|
n += mFirstChild ? mFirstChild->SizeOfIncludingThis(aMallocSizeOf) : 0;
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it is
|
|
|
|
// worthwhile:
|
|
|
|
// - mSheetURI
|
|
|
|
// - mOriginalSheetURI
|
|
|
|
// - mBaseURI
|
|
|
|
// - mPrincipal
|
|
|
|
// - mNameSpaceMap
|
|
|
|
//
|
|
|
|
// The following members are not measured:
|
|
|
|
// - mSheets, because it's non-owning
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// -------------------------------
|
|
|
|
// CSS Style Sheet
|
|
|
|
//
|
|
|
|
|
2012-08-28 10:10:08 -07:00
|
|
|
nsCSSStyleSheet::nsCSSStyleSheet(CORSMode aCORSMode)
|
2010-05-17 21:00:39 -07:00
|
|
|
: mTitle(),
|
2012-07-30 07:20:58 -07:00
|
|
|
mParent(nullptr),
|
|
|
|
mOwnerRule(nullptr),
|
|
|
|
mDocument(nullptr),
|
|
|
|
mOwningNode(nullptr),
|
2011-10-17 07:59:28 -07:00
|
|
|
mDisabled(false),
|
|
|
|
mDirty(false),
|
2013-01-08 15:25:47 -08:00
|
|
|
mScopeElement(nullptr),
|
2012-07-30 07:20:58 -07:00
|
|
|
mRuleProcessors(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-28 10:10:08 -07:00
|
|
|
mInner = new nsCSSStyleSheetInner(this, aCORSMode);
|
2013-03-17 07:45:01 -07:00
|
|
|
|
|
|
|
SetIsDOMBinding();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSStyleSheet::nsCSSStyleSheet(const nsCSSStyleSheet& aCopy,
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet* aParentToUse,
|
2011-03-17 10:41:52 -07:00
|
|
|
css::ImportRule* aOwnerRuleToUse,
|
2007-05-01 12:26:15 -07:00
|
|
|
nsIDocument* aDocumentToUse,
|
2013-03-17 07:42:59 -07:00
|
|
|
nsINode* aOwningNodeToUse)
|
2010-05-17 21:00:39 -07:00
|
|
|
: mTitle(aCopy.mTitle),
|
2007-03-22 10:30:00 -07:00
|
|
|
mParent(aParentToUse),
|
|
|
|
mOwnerRule(aOwnerRuleToUse),
|
|
|
|
mDocument(aDocumentToUse),
|
|
|
|
mOwningNode(aOwningNodeToUse),
|
|
|
|
mDisabled(aCopy.mDisabled),
|
2011-04-11 23:18:43 -07:00
|
|
|
mDirty(aCopy.mDirty),
|
2013-01-08 15:25:47 -08:00
|
|
|
mScopeElement(nullptr),
|
2007-03-22 10:30:00 -07:00
|
|
|
mInner(aCopy.mInner),
|
2012-07-30 07:20:58 -07:00
|
|
|
mRuleProcessors(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
mInner->AddSheet(this);
|
|
|
|
|
2011-04-11 23:18:43 -07:00
|
|
|
if (mDirty) { // CSSOM's been there, force full copy now
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(mInner->mComplete, "Why have rules been accessed on an incomplete sheet?");
|
2009-12-31 07:56:33 -08:00
|
|
|
// FIXME: handle failure?
|
2007-03-22 10:30:00 -07:00
|
|
|
EnsureUniqueInner();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCopy.mMedia) {
|
2008-09-09 11:08:11 -07:00
|
|
|
// XXX This is wrong; we should be keeping @import rules and
|
|
|
|
// sheets in sync!
|
2007-03-22 10:30:00 -07:00
|
|
|
aCopy.mMedia->Clone(getter_AddRefs(mMedia));
|
|
|
|
}
|
2013-03-17 07:45:01 -07:00
|
|
|
|
|
|
|
SetIsDOMBinding();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSStyleSheet::~nsCSSStyleSheet()
|
|
|
|
{
|
2008-09-09 11:08:11 -07:00
|
|
|
for (nsCSSStyleSheet* child = mInner->mFirstChild;
|
|
|
|
child;
|
|
|
|
child = child->mNext) {
|
|
|
|
// XXXbz this is a little bogus; see the XXX comment where we
|
|
|
|
// declare mFirstChild.
|
|
|
|
if (child->mParent == this) {
|
2012-07-30 07:20:58 -07:00
|
|
|
child->mParent = nullptr;
|
|
|
|
child->mDocument = nullptr;
|
2008-09-09 11:08:11 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-10-07 19:39:08 -07:00
|
|
|
DropRuleCollection();
|
|
|
|
DropMedia();
|
2007-03-22 10:30:00 -07:00
|
|
|
mInner->RemoveSheet(this);
|
|
|
|
// XXX The document reference is not reference counted and should
|
|
|
|
// not be released. The document will let us know when it is going
|
|
|
|
// away.
|
|
|
|
if (mRuleProcessors) {
|
2009-02-03 06:42:18 -08:00
|
|
|
NS_ASSERTION(mRuleProcessors->Length() == 0, "destructing sheet with rule processor reference");
|
2007-03-22 10:30:00 -07:00
|
|
|
delete mRuleProcessors; // weak refs, should be empty here anyway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 19:39:08 -07:00
|
|
|
void
|
|
|
|
nsCSSStyleSheet::DropRuleCollection()
|
|
|
|
{
|
|
|
|
if (mRuleCollection) {
|
|
|
|
mRuleCollection->DropReference();
|
2012-11-12 11:19:01 -08:00
|
|
|
mRuleCollection = nullptr;
|
2012-10-07 19:39:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSStyleSheet::DropMedia()
|
|
|
|
{
|
|
|
|
if (mMedia) {
|
|
|
|
mMedia->SetStyleSheet(nullptr);
|
|
|
|
mMedia = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSStyleSheet::UnlinkInner()
|
|
|
|
{
|
|
|
|
// We can only have a cycle through our inner if we have a unique inner,
|
|
|
|
// because otherwise there are no JS wrappers for anything in the inner.
|
|
|
|
if (mInner->mSheets.Length() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInner->mOrderedRules.EnumerateForwards(SetStyleSheetReference, nullptr);
|
|
|
|
mInner->mOrderedRules.Clear();
|
|
|
|
|
|
|
|
// Have to be a bit careful with child sheets, because we want to
|
|
|
|
// drop their mNext pointers and null out their mParent and
|
|
|
|
// mDocument, but don't want to work with deleted objects. And we
|
|
|
|
// don't want to do any addrefing in the process, just to make sure
|
|
|
|
// we don't confuse the cycle collector (though on the face of it,
|
|
|
|
// addref/release pairs during unlink should probably be ok).
|
|
|
|
nsRefPtr<nsCSSStyleSheet> child;
|
|
|
|
child.swap(mInner->mFirstChild);
|
|
|
|
while (child) {
|
|
|
|
MOZ_ASSERT(child->mParent == this, "We have a unique inner!");
|
|
|
|
child->mParent = nullptr;
|
|
|
|
child->mDocument = nullptr;
|
|
|
|
nsRefPtr<nsCSSStyleSheet> next;
|
|
|
|
// Null out child->mNext, but don't let it die yet
|
|
|
|
next.swap(child->mNext);
|
|
|
|
// Switch to looking at the old value of child->mNext next iteration
|
|
|
|
child.swap(next);
|
|
|
|
// "next" is now our previous value of child; it'll get released
|
|
|
|
// as we loop around.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSStyleSheet::TraverseInner(nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
|
|
|
// We can only have a cycle through our inner if we have a unique inner,
|
|
|
|
// because otherwise there are no JS wrappers for anything in the inner.
|
|
|
|
if (mInner->mSheets.Length() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsCSSStyleSheet>* childSheetSlot = &mInner->mFirstChild;
|
|
|
|
while (*childSheetSlot) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "child sheet");
|
|
|
|
cb.NoteXPCOMChild(NS_ISUPPORTS_CAST(nsIStyleSheet*, childSheetSlot->get()));
|
|
|
|
childSheetSlot = &(*childSheetSlot)->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCOMArray<css::Rule>& rules = mInner->mOrderedRules;
|
|
|
|
for (int32_t i = 0, count = rules.Count(); i < count; ++i) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mOrderedRules[i]");
|
|
|
|
cb.NoteXPCOMChild(rules[i]->GetExistingDOMRule());
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(CSSStyleSheet, nsCSSStyleSheet)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// QueryInterface implementation for nsCSSStyleSheet
|
2012-10-07 19:39:08 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCSSStyleSheet)
|
2013-03-17 07:42:09 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStyleSheet)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMStyleSheet)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMCSSStyleSheet)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)
|
2010-05-11 13:41:47 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStyleSheet)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSStyleSheet)
|
2010-05-11 13:41:47 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsCSSStyleSheet)))
|
|
|
|
foundInterface = reinterpret_cast<nsISupports*>(this);
|
|
|
|
else
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
2012-10-07 19:39:08 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCSSStyleSheet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCSSStyleSheet)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCSSStyleSheet)
|
|
|
|
tmp->DropMedia();
|
|
|
|
// We do not unlink mNext; our parent will handle that. If we
|
|
|
|
// unlinked it here, our parent would not be able to walk its list
|
|
|
|
// of child sheets and null out the back-references to it, if we got
|
|
|
|
// unlinked before it does.
|
|
|
|
tmp->DropRuleCollection();
|
|
|
|
tmp->UnlinkInner();
|
2013-01-08 15:25:47 -08:00
|
|
|
tmp->mScopeElement = nullptr;
|
2013-03-17 07:42:09 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2012-10-07 19:39:08 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsCSSStyleSheet)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMedia)
|
2012-10-07 19:39:08 -07:00
|
|
|
// We do not traverse mNext; our parent will handle that. See
|
|
|
|
// comments in Unlink for why.
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRuleCollection)
|
2013-01-08 15:25:47 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mScopeElement)
|
2012-10-07 19:39:08 -07:00
|
|
|
tmp->TraverseInner(cb);
|
2013-03-17 07:42:09 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-10-07 19:39:08 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-03-17 07:42:09 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsCSSStyleSheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::AddRuleProcessor(nsCSSRuleProcessor* aProcessor)
|
|
|
|
{
|
|
|
|
if (! mRuleProcessors) {
|
2009-02-03 06:42:18 -08:00
|
|
|
mRuleProcessors = new nsAutoTArray<nsCSSRuleProcessor*, 8>();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mRuleProcessors)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2009-05-18 15:13:12 -07:00
|
|
|
NS_ASSERTION(mRuleProcessors->NoIndex == mRuleProcessors->IndexOf(aProcessor),
|
2007-03-22 10:30:00 -07:00
|
|
|
"processor already registered");
|
|
|
|
mRuleProcessors->AppendElement(aProcessor); // weak ref
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::DropRuleProcessor(nsCSSRuleProcessor* aProcessor)
|
|
|
|
{
|
|
|
|
if (!mRuleProcessors)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return mRuleProcessors->RemoveElement(aProcessor)
|
|
|
|
? NS_OK
|
|
|
|
: NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
void
|
2007-10-23 14:56:41 -07:00
|
|
|
nsCSSStyleSheet::SetURIs(nsIURI* aSheetURI, nsIURI* aOriginalSheetURI,
|
|
|
|
nsIURI* aBaseURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aSheetURI && aBaseURI, "null ptr");
|
|
|
|
|
|
|
|
NS_ASSERTION(mInner->mOrderedRules.Count() == 0 && !mInner->mComplete,
|
|
|
|
"Can't call SetURL on sheets that are complete or have rules");
|
|
|
|
|
|
|
|
mInner->mSheetURI = aSheetURI;
|
2007-10-23 14:56:41 -07:00
|
|
|
mInner->mOriginalSheetURI = aOriginalSheetURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
mInner->mBaseURI = aBaseURI;
|
|
|
|
}
|
|
|
|
|
2007-05-01 12:26:15 -07:00
|
|
|
void
|
|
|
|
nsCSSStyleSheet::SetPrincipal(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!mInner->mPrincipalSet,
|
|
|
|
"Should have an inner whose principal has not yet been set");
|
|
|
|
if (aPrincipal) {
|
|
|
|
mInner->mPrincipal = aPrincipal;
|
|
|
|
#ifdef DEBUG
|
2011-10-17 07:59:28 -07:00
|
|
|
mInner->mPrincipalSet = true;
|
2007-05-01 12:26:15 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-17 21:00:39 -07:00
|
|
|
/* virtual */ nsIURI*
|
2010-05-12 13:18:47 -07:00
|
|
|
nsCSSStyleSheet::GetSheetURI() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-17 21:00:39 -07:00
|
|
|
return mInner->mSheetURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-17 21:00:39 -07:00
|
|
|
/* virtual */ nsIURI*
|
2010-05-12 13:18:47 -07:00
|
|
|
nsCSSStyleSheet::GetBaseURI() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-17 21:00:39 -07:00
|
|
|
return mInner->mBaseURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-14 12:05:14 -07:00
|
|
|
/* virtual */ void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::GetType(nsString& aType) const
|
|
|
|
{
|
|
|
|
aType.AssignLiteral("text/css");
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-07-26 09:14:48 -07:00
|
|
|
nsCSSStyleSheet::UseForPresentation(nsPresContext* aPresContext,
|
|
|
|
nsMediaQueryResultCacheKey& aKey) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mMedia) {
|
2011-04-21 20:17:31 -07:00
|
|
|
return mMedia->Matches(aPresContext, &aKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::SetMedia(nsMediaList* aMedia)
|
|
|
|
{
|
|
|
|
mMedia = aMedia;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* virtual */ bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::HasRules() const
|
|
|
|
{
|
2010-05-11 13:41:47 -07:00
|
|
|
return StyleRuleCount() != 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* virtual */ bool
|
2010-05-14 12:05:14 -07:00
|
|
|
nsCSSStyleSheet::IsApplicable() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-12 13:18:47 -07:00
|
|
|
return !mDisabled && mInner->mComplete;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-14 12:05:14 -07:00
|
|
|
/* virtual */ void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsCSSStyleSheet::SetEnabled(bool aEnabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Internal method, so callers must handle BeginUpdate/EndUpdate
|
2011-09-28 23:19:26 -07:00
|
|
|
bool oldDisabled = mDisabled;
|
2007-03-22 10:30:00 -07:00
|
|
|
mDisabled = !aEnabled;
|
|
|
|
|
2008-11-25 13:27:54 -08:00
|
|
|
if (mInner->mComplete && oldDisabled != mDisabled) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ClearRuleCascades();
|
|
|
|
|
2008-11-25 13:27:54 -08:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->SetStyleSheetApplicableState(this, !mDisabled);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* virtual */ bool
|
2010-05-14 12:05:14 -07:00
|
|
|
nsCSSStyleSheet::IsComplete() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-12 13:18:47 -07:00
|
|
|
return mInner->mComplete;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-14 12:05:14 -07:00
|
|
|
/* virtual */ void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::SetComplete()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mDirty, "Can't set a dirty sheet complete!");
|
2011-10-17 07:59:28 -07:00
|
|
|
mInner->mComplete = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mDocument && !mDisabled) {
|
|
|
|
// Let the document know
|
|
|
|
mDocument->BeginUpdate(UPDATE_STYLE);
|
2011-10-17 07:59:28 -07:00
|
|
|
mDocument->SetStyleSheetApplicableState(this, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
mDocument->EndUpdate(UPDATE_STYLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-17 21:00:39 -07:00
|
|
|
/* virtual */ nsIStyleSheet*
|
2010-05-12 13:18:47 -07:00
|
|
|
nsCSSStyleSheet::GetParentSheet() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-12 13:18:47 -07:00
|
|
|
return mParent;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-17 21:00:39 -07:00
|
|
|
/* virtual */ nsIDocument*
|
2010-05-12 13:18:47 -07:00
|
|
|
nsCSSStyleSheet::GetOwningDocument() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-12 13:18:47 -07:00
|
|
|
return mDocument;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-14 12:05:14 -07:00
|
|
|
/* virtual */ void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::SetOwningDocument(nsIDocument* aDocument)
|
|
|
|
{ // not ref counted
|
|
|
|
mDocument = aDocument;
|
|
|
|
// Now set the same document on all our child sheets....
|
2008-09-09 11:08:11 -07:00
|
|
|
// XXXbz this is a little bogus; see the XXX comment where we
|
|
|
|
// declare mFirstChild.
|
|
|
|
for (nsCSSStyleSheet* child = mInner->mFirstChild;
|
|
|
|
child; child = child->mNext) {
|
|
|
|
if (child->mParent == this) {
|
|
|
|
child->SetOwningDocument(aDocument);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t
|
2011-08-24 13:44:35 -07:00
|
|
|
nsCSSStyleSheet::FindOwningWindowInnerID() const
|
2010-12-20 08:21:59 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t windowID = 0;
|
2010-12-20 08:21:59 -08:00
|
|
|
if (mDocument) {
|
2011-08-24 13:44:35 -07:00
|
|
|
windowID = mDocument->InnerWindowID();
|
2010-12-20 08:21:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (windowID == 0 && mOwningNode) {
|
2013-03-17 07:42:59 -07:00
|
|
|
windowID = mOwningNode->OwnerDoc()->InnerWindowID();
|
2010-12-20 08:21:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (windowID == 0 && mOwnerRule) {
|
2011-04-06 00:11:42 -07:00
|
|
|
nsCOMPtr<nsIStyleSheet> sheet = static_cast<css::Rule*>(mOwnerRule)->GetStyleSheet();
|
2010-12-20 08:21:59 -08:00
|
|
|
if (sheet) {
|
|
|
|
nsRefPtr<nsCSSStyleSheet> cssSheet = do_QueryObject(sheet);
|
|
|
|
if (cssSheet) {
|
2011-08-24 13:44:35 -07:00
|
|
|
windowID = cssSheet->FindOwningWindowInnerID();
|
2010-12-20 08:21:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (windowID == 0 && mParent) {
|
2011-08-24 13:44:35 -07:00
|
|
|
windowID = mParent->FindOwningWindowInnerID();
|
2010-12-20 08:21:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return windowID;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
void
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet::AppendStyleSheet(nsCSSStyleSheet* aSheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(nullptr != aSheet, "null arg");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(WillDirty())) {
|
2008-09-09 11:08:11 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet>* tail = &mInner->mFirstChild;
|
|
|
|
while (*tail) {
|
|
|
|
tail = &(*tail)->mNext;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-14 12:05:14 -07:00
|
|
|
*tail = aSheet;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// This is not reference counted. Our parent tells us when
|
|
|
|
// it's going away.
|
2010-05-14 12:05:14 -07:00
|
|
|
aSheet->mParent = this;
|
|
|
|
aSheet->mDocument = mDocument;
|
2007-03-22 10:30:00 -07:00
|
|
|
DidDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCSSStyleSheet::InsertStyleSheetAt(nsCSSStyleSheet* aSheet, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(nullptr != aSheet, "null arg");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
if (NS_SUCCEEDED(WillDirty())) {
|
2008-09-09 11:08:11 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet>* tail = &mInner->mFirstChild;
|
|
|
|
while (*tail && aIndex) {
|
|
|
|
--aIndex;
|
|
|
|
tail = &(*tail)->mNext;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-14 12:05:14 -07:00
|
|
|
aSheet->mNext = *tail;
|
|
|
|
*tail = aSheet;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// This is not reference counted. Our parent tells us when
|
|
|
|
// it's going away.
|
2010-05-14 12:05:14 -07:00
|
|
|
aSheet->mParent = this;
|
|
|
|
aSheet->mDocument = mDocument;
|
2007-03-22 10:30:00 -07:00
|
|
|
DidDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
void
|
2011-04-07 18:23:46 -07:00
|
|
|
nsCSSStyleSheet::PrependStyleRule(css::Rule* aRule)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(nullptr != aRule, "null arg");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(WillDirty())) {
|
|
|
|
mInner->mOrderedRules.InsertObjectAt(aRule, 0);
|
|
|
|
aRule->SetStyleSheet(this);
|
|
|
|
DidDirty();
|
|
|
|
|
2011-04-07 18:23:46 -07:00
|
|
|
if (css::Rule::NAMESPACE_RULE == aRule->GetType()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// no api to prepend a namespace (ugh), release old ones and re-create them all
|
|
|
|
mInner->RebuildNameSpaces();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
void
|
2011-04-07 18:23:46 -07:00
|
|
|
nsCSSStyleSheet::AppendStyleRule(css::Rule* aRule)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(nullptr != aRule, "null arg");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(WillDirty())) {
|
|
|
|
mInner->mOrderedRules.AppendObject(aRule);
|
|
|
|
aRule->SetStyleSheet(this);
|
|
|
|
DidDirty();
|
|
|
|
|
2011-04-07 18:23:46 -07:00
|
|
|
if (css::Rule::NAMESPACE_RULE == aRule->GetType()) {
|
2010-07-11 05:47:45 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
RegisterNamespaceRule(aRule);
|
2010-05-11 13:41:47 -07:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
|
|
|
|
"RegisterNamespaceRule returned error");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
void
|
2011-04-07 18:23:46 -07:00
|
|
|
nsCSSStyleSheet::ReplaceStyleRule(css::Rule* aOld, css::Rule* aNew)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mInner->mOrderedRules.Count() != 0, "can't have old rule");
|
2007-05-01 12:26:15 -07:00
|
|
|
NS_PRECONDITION(mInner->mComplete, "No replacing in an incomplete sheet!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(WillDirty())) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = mInner->mOrderedRules.IndexOf(aOld);
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_UNLIKELY(index == -1)) {
|
2010-05-11 13:41:47 -07:00
|
|
|
NS_NOTREACHED("Couldn't find old rule");
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
mInner->mOrderedRules.ReplaceObjectAt(aNew, index);
|
|
|
|
|
|
|
|
aNew->SetStyleSheet(this);
|
2012-07-30 07:20:58 -07:00
|
|
|
aOld->SetStyleSheet(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
DidDirty();
|
2011-04-07 18:23:46 -07:00
|
|
|
NS_ASSERTION(css::Rule::NAMESPACE_RULE != aNew->GetType(), "not yet implemented");
|
|
|
|
NS_ASSERTION(css::Rule::NAMESPACE_RULE != aOld->GetType(), "not yet implemented");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet::StyleRuleCount() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-11 13:41:47 -07:00
|
|
|
return mInner->mOrderedRules.Count();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCSSStyleSheet::GetStyleRuleAt(int32_t aIndex, css::Rule*& aRule) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Important: If this function is ever made scriptable, we must add
|
2007-07-03 11:45:39 -07:00
|
|
|
// a security check here. See GetCssRules below for an example.
|
2007-05-01 12:26:15 -07:00
|
|
|
aRule = mInner->mOrderedRules.SafeObjectAt(aIndex);
|
|
|
|
if (aRule) {
|
|
|
|
NS_ADDREF(aRule);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-05-01 12:26:15 -07:00
|
|
|
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet::StyleSheetCount() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// XXX Far from an ideal way to do this, but the hope is that
|
|
|
|
// it won't be done too often. If it is, we might want to
|
|
|
|
// consider storing the children in an array.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-09 11:08:11 -07:00
|
|
|
const nsCSSStyleSheet* child = mInner->mFirstChild;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (child) {
|
2010-05-11 13:41:47 -07:00
|
|
|
count++;
|
2007-03-22 10:30:00 -07:00
|
|
|
child = child->mNext;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
return count;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-12-31 07:56:33 -08:00
|
|
|
nsCSSStyleSheet::EnsureUniqueInnerResult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::EnsureUniqueInner()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mDirty = true;
|
2011-04-11 23:18:43 -07:00
|
|
|
|
2009-12-31 07:56:33 -08:00
|
|
|
NS_ABORT_IF_FALSE(mInner->mSheets.Length() != 0,
|
|
|
|
"unexpected number of outers");
|
|
|
|
if (mInner->mSheets.Length() == 1) {
|
|
|
|
return eUniqueInner_AlreadyUnique;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-12-31 07:56:33 -08:00
|
|
|
nsCSSStyleSheetInner* clone = mInner->CloneFor(this);
|
|
|
|
if (!clone) {
|
|
|
|
return eUniqueInner_CloneFailed;
|
|
|
|
}
|
|
|
|
mInner->RemoveSheet(this);
|
|
|
|
mInner = clone;
|
2009-12-31 07:56:33 -08:00
|
|
|
|
|
|
|
// otherwise the rule processor has pointers to the old rules
|
|
|
|
ClearRuleCascades();
|
|
|
|
|
2009-12-31 07:56:33 -08:00
|
|
|
return eUniqueInner_ClonedInner;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-12-31 07:56:33 -08:00
|
|
|
nsCSSStyleSheet::AppendAllChildSheets(nsTArray<nsCSSStyleSheet*>& aArray)
|
|
|
|
{
|
|
|
|
for (nsCSSStyleSheet* child = mInner->mFirstChild; child;
|
|
|
|
child = child->mNext) {
|
|
|
|
if (!aArray.AppendElement(child)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-12-31 07:56:33 -08:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-12-31 07:56:33 -08:00
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
already_AddRefed<nsCSSStyleSheet>
|
|
|
|
nsCSSStyleSheet::Clone(nsCSSStyleSheet* aCloneParent,
|
2011-03-17 10:41:52 -07:00
|
|
|
css::ImportRule* aCloneOwnerRule,
|
2008-09-05 18:10:35 -07:00
|
|
|
nsIDocument* aCloneDocument,
|
2013-03-17 07:42:59 -07:00
|
|
|
nsINode* aCloneOwningNode) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-22 04:15:59 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet> clone = new nsCSSStyleSheet(*this,
|
|
|
|
aCloneParent,
|
|
|
|
aCloneOwnerRule,
|
|
|
|
aCloneDocument,
|
|
|
|
aCloneOwningNode);
|
|
|
|
return clone.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
static void
|
2012-08-22 08:56:38 -07:00
|
|
|
ListRules(const nsCOMArray<css::Rule>& aRules, FILE* aOut, int32_t aIndent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t index = aRules.Count() - 1; index >= 0; --index) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aRules.ObjectAt(index)->List(aOut, aIndent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ListEnumData {
|
2012-08-22 08:56:38 -07:00
|
|
|
ListEnumData(FILE* aOut, int32_t aIndent)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mOut(aOut),
|
|
|
|
mIndent(aIndent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
FILE* mOut;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mIndent;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-05-14 12:05:14 -07:00
|
|
|
/* virtual */ void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCSSStyleSheet::List(FILE* out, int32_t aIndent) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Indent
|
|
|
|
for (index = aIndent; --index >= 0; ) fputs(" ", out);
|
|
|
|
|
|
|
|
fputs("CSS Style Sheet: ", out);
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString urlSpec;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = mInner->mSheetURI->GetSpec(urlSpec);
|
|
|
|
if (NS_SUCCEEDED(rv) && !urlSpec.IsEmpty()) {
|
|
|
|
fputs(urlSpec.get(), out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMedia) {
|
|
|
|
fputs(" media: ", out);
|
|
|
|
nsAutoString buffer;
|
|
|
|
mMedia->GetText(buffer);
|
|
|
|
fputs(NS_ConvertUTF16toUTF8(buffer).get(), out);
|
|
|
|
}
|
|
|
|
fputs("\n", out);
|
|
|
|
|
2008-09-09 11:08:11 -07:00
|
|
|
for (const nsCSSStyleSheet* child = mInner->mFirstChild;
|
|
|
|
child;
|
|
|
|
child = child->mNext) {
|
2007-03-22 10:30:00 -07:00
|
|
|
child->List(out, aIndent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs("Rules in source order:\n", out);
|
|
|
|
ListRules(mInner->mOrderedRules, out, aIndent);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSStyleSheet::ClearRuleCascades()
|
|
|
|
{
|
|
|
|
if (mRuleProcessors) {
|
2009-02-03 06:42:18 -08:00
|
|
|
nsCSSRuleProcessor **iter = mRuleProcessors->Elements(),
|
|
|
|
**end = iter + mRuleProcessors->Length();
|
|
|
|
for(; iter != end; ++iter) {
|
|
|
|
(*iter)->ClearRuleCascades();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (mParent) {
|
|
|
|
nsCSSStyleSheet* parent = (nsCSSStyleSheet*)mParent;
|
|
|
|
parent->ClearRuleCascades();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCSSStyleSheet::WillDirty()
|
|
|
|
{
|
2007-05-01 12:26:15 -07:00
|
|
|
if (!mInner->mComplete) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Do nothing
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-12-31 07:56:33 -08:00
|
|
|
|
|
|
|
if (EnsureUniqueInner() == eUniqueInner_CloneFailed) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSStyleSheet::DidDirty()
|
|
|
|
{
|
2011-04-11 23:18:43 -07:00
|
|
|
NS_ABORT_IF_FALSE(!mInner->mComplete || mDirty,
|
|
|
|
"caller must have called WillDirty()");
|
2007-03-22 10:30:00 -07:00
|
|
|
ClearRuleCascades();
|
|
|
|
}
|
|
|
|
|
2007-07-03 11:45:39 -07:00
|
|
|
nsresult
|
2012-08-28 10:10:09 -07:00
|
|
|
nsCSSStyleSheet::SubjectSubsumesInnerPrincipal()
|
2007-07-03 11:45:39 -07:00
|
|
|
{
|
|
|
|
// Get the security manager and do the subsumes check
|
|
|
|
nsIScriptSecurityManager *securityManager =
|
|
|
|
nsContentUtils::GetSecurityManager();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> subjectPrincipal;
|
2011-06-21 09:52:22 -07:00
|
|
|
nsresult rv = securityManager->GetSubjectPrincipal(getter_AddRefs(subjectPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-03 11:45:39 -07:00
|
|
|
|
|
|
|
if (!subjectPrincipal) {
|
2008-01-17 21:23:44 -08:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2007-07-03 11:45:39 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool subsumes;
|
2011-06-21 09:52:22 -07:00
|
|
|
rv = subjectPrincipal->Subsumes(mInner->mPrincipal, &subsumes);
|
2007-07-03 11:45:39 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (subsumes) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-25 16:10:54 -07:00
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
2012-08-28 10:10:09 -07:00
|
|
|
// Allow access only if CORS mode is not NONE
|
|
|
|
if (GetCORSMode() == CORS_NONE) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we set the principal of our inner to the
|
|
|
|
// subjectPrincipal. That means we need a unique inner, of
|
|
|
|
// course. But we don't want to do that if we're not complete
|
|
|
|
// yet. Luckily, all the callers of this method throw anyway if
|
|
|
|
// not complete, so we can just do that here too.
|
|
|
|
if (!mInner->mComplete) {
|
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = WillDirty();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mInner->mPrincipal = subjectPrincipal;
|
|
|
|
|
|
|
|
DidDirty();
|
2007-07-03 11:45:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-15 13:23:21 -07:00
|
|
|
nsresult
|
2011-04-07 18:23:46 -07:00
|
|
|
nsCSSStyleSheet::RegisterNamespaceRule(css::Rule* aRule)
|
2008-10-15 13:23:21 -07:00
|
|
|
{
|
|
|
|
if (!mInner->mNameSpaceMap) {
|
|
|
|
nsresult rv = mInner->CreateNamespaceMap();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddNamespaceRuleToMap(aRule, mInner->mNameSpaceMap);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIDOMStyleSheet interface
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetType(nsAString& aType)
|
|
|
|
{
|
|
|
|
aType.AssignLiteral("text/css");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsCSSStyleSheet::GetDisabled(bool* aDisabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-03-17 07:42:59 -07:00
|
|
|
*aDisabled = Disabled();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsCSSStyleSheet::SetDisabled(bool aDisabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// DOM method, so handle BeginUpdate/EndUpdate
|
2011-10-17 07:59:28 -07:00
|
|
|
MOZ_AUTO_DOC_UPDATE(mDocument, UPDATE_STYLE, true);
|
2010-05-12 13:18:47 -07:00
|
|
|
nsCSSStyleSheet::SetEnabled(!aDisabled);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetOwnerNode(nsIDOMNode** aOwnerNode)
|
|
|
|
{
|
2013-03-17 07:42:59 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> ownerNode = do_QueryInterface(GetOwnerNode());
|
|
|
|
ownerNode.forget(aOwnerNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetParentStyleSheet(nsIDOMStyleSheet** aParentStyleSheet)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aParentStyleSheet);
|
|
|
|
|
2010-05-14 12:05:14 -07:00
|
|
|
NS_IF_ADDREF(*aParentStyleSheet = mParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-14 12:05:14 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetHref(nsAString& aHref)
|
|
|
|
{
|
2007-10-23 14:56:41 -07:00
|
|
|
if (mInner->mOriginalSheetURI) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString str;
|
2007-10-23 14:56:41 -07:00
|
|
|
mInner->mOriginalSheetURI->GetSpec(str);
|
|
|
|
CopyUTF8toUTF16(str, aHref);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aHref);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-17 21:00:39 -07:00
|
|
|
/* virtual */ void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::GetTitle(nsString& aTitle) const
|
|
|
|
{
|
|
|
|
aTitle = mTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetTitle(nsAString& aTitle)
|
|
|
|
{
|
|
|
|
aTitle.Assign(mTitle);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetMedia(nsIDOMMediaList** aMedia)
|
|
|
|
{
|
2013-03-17 07:42:59 -07:00
|
|
|
NS_ADDREF(*aMedia = Media());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-17 07:42:59 -07:00
|
|
|
nsIDOMMediaList*
|
|
|
|
nsCSSStyleSheet::Media()
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mMedia) {
|
|
|
|
mMedia = new nsMediaList();
|
|
|
|
mMedia->SetStyleSheet(this);
|
|
|
|
}
|
|
|
|
|
2013-03-17 07:42:59 -07:00
|
|
|
return mMedia;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetOwnerRule(nsIDOMCSSRule** aOwnerRule)
|
|
|
|
{
|
2013-03-17 07:42:59 -07:00
|
|
|
NS_IF_ADDREF(*aOwnerRule = GetOwnerRule());
|
2011-04-07 23:36:09 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-03-17 07:42:59 -07:00
|
|
|
nsIDOMCSSRule*
|
|
|
|
nsCSSStyleSheet::GetDOMOwnerRule() const
|
|
|
|
{
|
|
|
|
return mOwnerRule ? mOwnerRule->GetDOMRule() : nullptr;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::GetCssRules(nsIDOMCSSRuleList** aCssRules)
|
2013-03-17 07:42:59 -07:00
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsIDOMCSSRuleList> rules = GetCssRules(rv);
|
|
|
|
rules.forget(aCssRules);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMCSSRuleList*
|
|
|
|
nsCSSStyleSheet::GetCssRules(ErrorResult& aRv)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// No doing this on incomplete sheets!
|
2010-05-14 12:05:14 -07:00
|
|
|
if (!mInner->mComplete) {
|
2013-03-17 07:42:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-01 12:26:15 -07:00
|
|
|
//-- Security check: Only scripts whose principal subsumes that of the
|
|
|
|
// style sheet can access rule collections.
|
2007-07-03 11:45:39 -07:00
|
|
|
nsresult rv = SubjectSubsumesInnerPrincipal();
|
2013-03-17 07:42:59 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// OK, security check passed, so get the rule collection
|
2013-03-17 07:42:59 -07:00
|
|
|
if (!mRuleCollection) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mRuleCollection = new CSSRuleListImpl(this);
|
|
|
|
}
|
|
|
|
|
2013-03-17 07:42:59 -07:00
|
|
|
return mRuleCollection;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCSSStyleSheet::InsertRule(const nsAString& aRule,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aIndex,
|
|
|
|
uint32_t* aReturn)
|
2008-01-17 21:23:44 -08:00
|
|
|
{
|
|
|
|
//-- Security check: Only scripts whose principal subsumes that of the
|
|
|
|
// style sheet can modify rule collections.
|
|
|
|
nsresult rv = SubjectSubsumesInnerPrincipal();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return InsertRuleInternal(aRule, aIndex, aReturn);
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:54:38 -07:00
|
|
|
static bool
|
|
|
|
RuleHasPendingChildSheet(css::Rule *cssRule)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCSSImportRule> importRule(do_QueryInterface(cssRule));
|
|
|
|
NS_ASSERTION(importRule, "Rule which has type IMPORT_RULE and does not implement nsIDOMCSSImportRule!");
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleSheet> childSheet;
|
|
|
|
importRule->GetStyleSheet(getter_AddRefs(childSheet));
|
|
|
|
nsRefPtr<nsCSSStyleSheet> cssSheet = do_QueryObject(childSheet);
|
2012-07-30 07:20:58 -07:00
|
|
|
return cssSheet != nullptr && !cssSheet->IsComplete();
|
2012-06-03 09:54:38 -07:00
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsresult
|
2008-01-17 21:23:44 -08:00
|
|
|
nsCSSStyleSheet::InsertRuleInternal(const nsAString& aRule,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aIndex,
|
|
|
|
uint32_t* aReturn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// No doing this if the sheet is not complete!
|
2010-05-14 12:05:14 -07:00
|
|
|
if (!mInner->mComplete) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result;
|
|
|
|
result = WillDirty();
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (aIndex > uint32_t(mInner->mOrderedRules.Count()))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ASSERTION(uint32_t(mInner->mOrderedRules.Count()) <= INT32_MAX,
|
2007-03-22 10:30:00 -07:00
|
|
|
"Too many style rules!");
|
|
|
|
|
|
|
|
// Hold strong ref to the CSSLoader in case the document update
|
|
|
|
// kills the document
|
2010-03-02 13:00:53 -08:00
|
|
|
nsRefPtr<css::Loader> loader;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mDocument) {
|
|
|
|
loader = mDocument->CSSLoader();
|
|
|
|
NS_ASSERTION(loader, "Document with no CSS loader!");
|
|
|
|
}
|
2010-03-02 12:59:32 -08:00
|
|
|
|
|
|
|
nsCSSParser css(loader, this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
nsRefPtr<css::Rule> rule;
|
2010-03-02 12:59:32 -08:00
|
|
|
result = css.ParseRule(aRule, mInner->mSheetURI, mInner->mBaseURI,
|
2013-02-09 22:56:49 -08:00
|
|
|
mInner->mPrincipal, getter_AddRefs(rule));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
2010-03-02 12:59:32 -08:00
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
// Hierarchy checking.
|
|
|
|
int32_t newType = rule->GetType();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// check that we're not inserting before a charset rule
|
2011-04-07 18:23:46 -07:00
|
|
|
css::Rule* nextRule = mInner->mOrderedRules.SafeObjectAt(aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nextRule) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nextType = nextRule->GetType();
|
2011-04-07 18:23:46 -07:00
|
|
|
if (nextType == css::Rule::CHARSET_RULE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
|
|
|
}
|
|
|
|
|
2011-04-07 18:23:46 -07:00
|
|
|
if (nextType == css::Rule::IMPORT_RULE &&
|
2013-02-09 22:56:49 -08:00
|
|
|
newType != css::Rule::CHARSET_RULE &&
|
|
|
|
newType != css::Rule::IMPORT_RULE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
|
|
|
}
|
2013-02-09 22:56:49 -08:00
|
|
|
|
2011-04-07 18:23:46 -07:00
|
|
|
if (nextType == css::Rule::NAMESPACE_RULE &&
|
2013-02-09 22:56:49 -08:00
|
|
|
newType != css::Rule::CHARSET_RULE &&
|
|
|
|
newType != css::Rule::IMPORT_RULE &&
|
|
|
|
newType != css::Rule::NAMESPACE_RULE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
2013-02-09 22:56:49 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-02-09 22:56:49 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aIndex != 0) {
|
2013-02-09 22:56:49 -08:00
|
|
|
// no inserting charset at nonzero position
|
|
|
|
if (newType == css::Rule::CHARSET_RULE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
|
|
|
}
|
2013-02-09 22:56:49 -08:00
|
|
|
|
2011-04-07 18:23:46 -07:00
|
|
|
css::Rule* prevRule = mInner->mOrderedRules.SafeObjectAt(aIndex - 1);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t prevType = prevRule->GetType();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
if (newType == css::Rule::IMPORT_RULE &&
|
2011-04-07 18:23:46 -07:00
|
|
|
prevType != css::Rule::CHARSET_RULE &&
|
|
|
|
prevType != css::Rule::IMPORT_RULE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
|
|
|
}
|
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
if (newType == css::Rule::NAMESPACE_RULE &&
|
2011-04-07 18:23:46 -07:00
|
|
|
prevType != css::Rule::CHARSET_RULE &&
|
|
|
|
prevType != css::Rule::IMPORT_RULE &&
|
|
|
|
prevType != css::Rule::NAMESPACE_RULE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
|
|
|
}
|
|
|
|
}
|
2013-02-09 22:56:49 -08:00
|
|
|
|
|
|
|
bool insertResult = mInner->mOrderedRules.InsertObjectAt(rule, aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(insertResult, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
DidDirty();
|
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
rule->SetStyleSheet(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
int32_t type = rule->GetType();
|
|
|
|
if (type == css::Rule::NAMESPACE_RULE) {
|
|
|
|
// XXXbz does this screw up when inserting a namespace rule before
|
|
|
|
// another namespace rule that binds the same prefix to a different
|
|
|
|
// namespace?
|
|
|
|
result = RegisterNamespaceRule(rule);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't notify immediately for @import rules, but rather when
|
|
|
|
// the sheet the rule is importing is loaded (see StyleSheetLoaded)
|
|
|
|
if ((type != css::Rule::IMPORT_RULE || !RuleHasPendingChildSheet(rule)) &&
|
|
|
|
mDocument) {
|
|
|
|
mDocument->StyleRuleAdded(this, rule);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-03 09:54:38 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
*aReturn = aIndex;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCSSStyleSheet::DeleteRule(uint32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result = NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
// No doing this if the sheet is not complete!
|
2010-05-14 12:05:14 -07:00
|
|
|
if (!mInner->mComplete) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
|
2007-07-03 11:45:39 -07:00
|
|
|
//-- Security check: Only scripts whose principal subsumes that of the
|
|
|
|
// style sheet can modify rule collections.
|
|
|
|
nsresult rv = SubjectSubsumesInnerPrincipal();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX TBI: handle @rule types
|
2011-10-17 07:59:28 -07:00
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-01 12:26:15 -07:00
|
|
|
result = WillDirty();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-01 12:26:15 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
if (aIndex >= uint32_t(mInner->mOrderedRules.Count()))
|
2007-05-01 12:26:15 -07:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ASSERTION(uint32_t(mInner->mOrderedRules.Count()) <= INT32_MAX,
|
2007-05-01 12:26:15 -07:00
|
|
|
"Too many style rules!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-01 12:26:15 -07:00
|
|
|
// Hold a strong ref to the rule so it doesn't die when we RemoveObjectAt
|
2011-04-07 18:23:46 -07:00
|
|
|
nsRefPtr<css::Rule> rule = mInner->mOrderedRules.ObjectAt(aIndex);
|
2007-05-01 12:26:15 -07:00
|
|
|
if (rule) {
|
|
|
|
mInner->mOrderedRules.RemoveObjectAt(aIndex);
|
2013-05-16 00:13:36 -07:00
|
|
|
if (mDocument && mDocument->StyleSheetChangeEventsEnabled()) {
|
|
|
|
// Force creation of the DOM rule, so that it can be put on the
|
|
|
|
// StyleRuleRemoved event object.
|
|
|
|
rule->GetDOMRule();
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
rule->SetStyleSheet(nullptr);
|
2007-05-01 12:26:15 -07:00
|
|
|
DidDirty();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-01 12:26:15 -07:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->StyleRuleRemoved(this, rule);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCSSStyleSheet::DeleteRuleFromGroup(css::GroupRule* aGroup, uint32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aGroup);
|
2007-05-01 12:26:15 -07:00
|
|
|
NS_ASSERTION(mInner->mComplete, "No deleting from an incomplete sheet!");
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult result;
|
2011-04-07 18:23:46 -07:00
|
|
|
nsRefPtr<css::Rule> rule = aGroup->GetStyleRuleAt(aIndex);
|
2010-08-07 22:29:28 -07:00
|
|
|
NS_ENSURE_TRUE(rule, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// check that the rule actually belongs to this sheet!
|
2010-08-07 22:33:15 -07:00
|
|
|
if (this != rule->GetStyleSheet()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = WillDirty();
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
result = aGroup->DeleteStyleRuleAt(aIndex);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
rule->SetStyleSheet(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DidDirty();
|
|
|
|
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->StyleRuleRemoved(this, rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCSSStyleSheet::InsertRuleIntoGroup(const nsAString & aRule,
|
2010-08-07 22:28:33 -07:00
|
|
|
css::GroupRule* aGroup,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aIndex,
|
|
|
|
uint32_t* _retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
2007-05-01 12:26:15 -07:00
|
|
|
NS_ASSERTION(mInner->mComplete, "No inserting into an incomplete sheet!");
|
2007-03-22 10:30:00 -07:00
|
|
|
// check that the group actually belongs to this sheet!
|
2010-08-07 22:33:15 -07:00
|
|
|
if (this != aGroup->GetStyleSheet()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold strong ref to the CSSLoader in case the document update
|
|
|
|
// kills the document
|
2010-03-02 13:00:53 -08:00
|
|
|
nsRefPtr<css::Loader> loader;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mDocument) {
|
|
|
|
loader = mDocument->CSSLoader();
|
|
|
|
NS_ASSERTION(loader, "Document with no CSS loader!");
|
|
|
|
}
|
|
|
|
|
2010-03-02 12:59:32 -08:00
|
|
|
nsCSSParser css(loader, this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// parse and grab the rule
|
2011-10-17 07:59:28 -07:00
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = WillDirty();
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
nsRefPtr<css::Rule> rule;
|
2010-03-02 12:59:32 -08:00
|
|
|
result = css.ParseRule(aRule, mInner->mSheetURI, mInner->mBaseURI,
|
2013-02-09 22:56:49 -08:00
|
|
|
mInner->mPrincipal, getter_AddRefs(rule));
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-09 22:56:49 -08:00
|
|
|
switch (rule->GetType()) {
|
|
|
|
case css::Rule::STYLE_RULE:
|
|
|
|
case css::Rule::MEDIA_RULE:
|
|
|
|
case css::Rule::FONT_FACE_RULE:
|
|
|
|
case css::Rule::PAGE_RULE:
|
|
|
|
case css::Rule::KEYFRAMES_RULE:
|
|
|
|
case css::Rule::DOCUMENT_RULE:
|
|
|
|
case css::Rule::SUPPORTS_RULE:
|
|
|
|
// these types are OK to insert into a group
|
|
|
|
break;
|
|
|
|
case css::Rule::CHARSET_RULE:
|
|
|
|
case css::Rule::IMPORT_RULE:
|
|
|
|
case css::Rule::NAMESPACE_RULE:
|
|
|
|
// these aren't
|
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("unexpected rule type");
|
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-02-09 22:56:49 -08:00
|
|
|
|
|
|
|
result = aGroup->InsertStyleRuleAt(aIndex, rule);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
DidDirty();
|
2013-02-09 22:56:49 -08:00
|
|
|
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->StyleRuleAdded(this, rule);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = aIndex;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsresult
|
2010-08-07 22:28:33 -07:00
|
|
|
nsCSSStyleSheet::ReplaceRuleInGroup(css::GroupRule* aGroup,
|
2011-04-07 18:23:46 -07:00
|
|
|
css::Rule* aOld, css::Rule* aNew)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
2007-05-01 12:26:15 -07:00
|
|
|
NS_PRECONDITION(mInner->mComplete, "No replacing in an incomplete sheet!");
|
2010-08-07 22:33:15 -07:00
|
|
|
NS_ASSERTION(this == aGroup->GetStyleSheet(), "group doesn't belong to this sheet");
|
2007-03-22 10:30:00 -07:00
|
|
|
result = WillDirty();
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
result = aGroup->ReplaceStyleRule(aOld, aNew);
|
|
|
|
DidDirty();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsICSSLoaderObserver implementation
|
|
|
|
NS_IMETHODIMP
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet::StyleSheetLoaded(nsCSSStyleSheet* aSheet,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aWasAlternate,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (aSheet->GetParentSheet() == nullptr) {
|
2012-06-03 09:54:38 -07:00
|
|
|
return NS_OK; // ignore if sheet has been detached already (see parseSheet)
|
|
|
|
}
|
2010-05-17 21:00:40 -07:00
|
|
|
NS_ASSERTION(this == aSheet->GetParentSheet(),
|
|
|
|
"We are being notified of a sheet load for a sheet that is not our child!");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mDocument && NS_SUCCEEDED(aStatus)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXXldb @import rules shouldn't even implement nsIStyleRule (but
|
|
|
|
// they do)!
|
2011-03-17 10:41:52 -07:00
|
|
|
mDocument->StyleRuleAdded(this, aSheet->GetOwnerRule());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:54:38 -07:00
|
|
|
nsresult
|
|
|
|
nsCSSStyleSheet::ParseSheet(const nsAString& aInput)
|
|
|
|
{
|
|
|
|
// Not doing this if the sheet is not complete!
|
|
|
|
if (!mInner->mComplete) {
|
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold strong ref to the CSSLoader in case the document update
|
|
|
|
// kills the document
|
|
|
|
nsRefPtr<css::Loader> loader;
|
|
|
|
if (mDocument) {
|
|
|
|
loader = mDocument->CSSLoader();
|
|
|
|
NS_ASSERTION(loader, "Document with no CSS loader!");
|
|
|
|
} else {
|
|
|
|
loader = new css::Loader();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
|
|
|
|
|
|
|
nsresult rv = WillDirty();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// detach existing rules (including child sheets via import rules)
|
|
|
|
int ruleCount;
|
|
|
|
while ((ruleCount = mInner->mOrderedRules.Count()) != 0) {
|
|
|
|
nsRefPtr<css::Rule> rule = mInner->mOrderedRules.ObjectAt(ruleCount - 1);
|
|
|
|
mInner->mOrderedRules.RemoveObjectAt(ruleCount - 1);
|
2012-07-30 07:20:58 -07:00
|
|
|
rule->SetStyleSheet(nullptr);
|
2012-06-03 09:54:38 -07:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->StyleRuleRemoved(this, rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nuke child sheets list and current namespace map
|
|
|
|
for (nsCSSStyleSheet* child = mInner->mFirstChild; child; child = child->mNext) {
|
|
|
|
NS_ASSERTION(child->mParent == this, "Child sheet is not parented to this!");
|
2012-07-30 07:20:58 -07:00
|
|
|
child->mParent = nullptr;
|
|
|
|
child->mDocument = nullptr;
|
2012-06-03 09:54:38 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mInner->mFirstChild = nullptr;
|
|
|
|
mInner->mNameSpaceMap = nullptr;
|
2012-06-03 09:54:38 -07:00
|
|
|
|
|
|
|
// allow unsafe rules if the style sheet's principal is the system principal
|
|
|
|
bool allowUnsafeRules = nsContentUtils::IsSystemPrincipal(mInner->mPrincipal);
|
2012-09-05 16:10:55 -07:00
|
|
|
|
|
|
|
nsCSSParser parser(loader, this);
|
2012-06-03 09:54:38 -07:00
|
|
|
rv = parser.ParseSheet(aInput, mInner->mSheetURI, mInner->mBaseURI,
|
|
|
|
mInner->mPrincipal, 1, allowUnsafeRules);
|
|
|
|
DidDirty(); // we are always 'dirty' here since we always remove rules first
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// notify document of all new rules
|
|
|
|
if (mDocument) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t index = 0; index < mInner->mOrderedRules.Count(); ++index) {
|
2012-06-03 09:54:38 -07:00
|
|
|
nsRefPtr<css::Rule> rule = mInner->mOrderedRules.ObjectAt(index);
|
|
|
|
if (rule->GetType() == css::Rule::IMPORT_RULE &&
|
|
|
|
RuleHasPendingChildSheet(rule)) {
|
|
|
|
continue; // notify when loaded (see StyleSheetLoaded)
|
|
|
|
}
|
|
|
|
mDocument->StyleRuleAdded(this, rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:48 -07:00
|
|
|
/* virtual */ nsIURI*
|
2009-03-10 06:52:23 -07:00
|
|
|
nsCSSStyleSheet::GetOriginalURI() const
|
|
|
|
{
|
|
|
|
return mInner->mOriginalSheetURI;
|
|
|
|
}
|
2013-03-17 07:45:01 -07:00
|
|
|
|
|
|
|
/* virtual */
|
|
|
|
JSObject*
|
2013-04-25 09:29:54 -07:00
|
|
|
nsCSSStyleSheet::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-03-17 07:45:01 -07:00
|
|
|
{
|
|
|
|
return CSSStyleSheetBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|