2007-08-06 15:27:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set tw=80 expandtab softtabstop=2 ts=2 sw=2: */
|
2012-05-21 11:12:37 +00: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-08-06 15:27:19 +00:00
|
|
|
|
|
|
|
#include "nsStyledElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsAttrValue.h"
|
2012-09-30 16:40:24 +00:00
|
|
|
#include "nsAttrValueInlines.h"
|
2013-10-02 20:09:18 +00:00
|
|
|
#include "mozilla/dom/ElementInlines.h"
|
2014-02-27 10:51:15 +00:00
|
|
|
#include "mozilla/InternalMutationEvent.h"
|
2007-08-06 15:27:19 +00:00
|
|
|
#include "nsDOMCSSDeclaration.h"
|
2009-04-08 20:52:37 +00:00
|
|
|
#include "nsDOMCSSAttrDeclaration.h"
|
2007-08-06 15:27:19 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIDocument.h"
|
2011-03-11 02:48:57 +00:00
|
|
|
#include "mozilla/css/StyleRule.h"
|
2010-03-02 20:59:32 +00:00
|
|
|
#include "nsCSSParser.h"
|
2010-06-28 22:49:35 +00:00
|
|
|
#include "mozilla/css/Loader.h"
|
2010-02-24 04:37:47 +00:00
|
|
|
#include "nsIDOMMutationEvent.h"
|
2010-08-05 21:59:36 +00:00
|
|
|
#include "nsXULElement.h"
|
2011-07-15 10:31:34 +00:00
|
|
|
#include "nsContentUtils.h"
|
2012-08-30 17:58:24 +00:00
|
|
|
#include "nsStyleUtil.h"
|
2007-08-06 15:27:19 +00:00
|
|
|
|
2013-11-19 19:49:52 +00:00
|
|
|
using namespace mozilla;
|
2012-11-14 22:10:08 +00:00
|
|
|
using namespace mozilla::dom;
|
2011-03-11 02:48:57 +00:00
|
|
|
|
2007-08-06 15:27:19 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsIContent methods
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::ParseAttribute(int32_t aNamespaceID,
|
2011-04-08 05:27:57 +00:00
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
2007-08-06 15:27:19 +00:00
|
|
|
{
|
2014-05-30 07:36:53 +00:00
|
|
|
if (aAttribute == nsGkAtoms::style && aNamespaceID == kNameSpaceID_None) {
|
|
|
|
SetMayHaveStyle();
|
|
|
|
ParseStyleAttribute(aValue, aResult, false);
|
|
|
|
return true;
|
2007-08-06 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsStyledElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
|
2012-04-14 13:09:19 +00:00
|
|
|
nsresult
|
2011-04-08 05:27:57 +00:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::SetInlineStyleRule(css::StyleRule* aStyleRule,
|
2012-04-05 17:14:54 +00:00
|
|
|
const nsAString* aSerialized,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aNotify)
|
2007-08-06 15:27:19 +00:00
|
|
|
{
|
2011-04-08 02:29:50 +00:00
|
|
|
SetMayHaveStyle();
|
2011-09-29 06:19:26 +00:00
|
|
|
bool modification = false;
|
2012-02-14 02:00:56 +00:00
|
|
|
nsAttrValue oldValue;
|
2007-08-06 15:27:19 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasListeners = aNotify &&
|
2007-08-06 15:27:19 +00:00
|
|
|
nsContentUtils::HasMutationListeners(this,
|
|
|
|
NS_EVENT_BITS_MUTATION_ATTRMODIFIED,
|
|
|
|
this);
|
|
|
|
|
|
|
|
// There's no point in comparing the stylerule pointers since we're always
|
|
|
|
// getting a new stylerule here. And we can't compare the stringvalues of
|
|
|
|
// the old and the new rules since both will point to the same declaration
|
|
|
|
// and thus will be the same.
|
|
|
|
if (hasListeners) {
|
|
|
|
// save the old attribute so we can set up the mutation event properly
|
|
|
|
// XXXbz if the old rule points to the same declaration as the new one,
|
|
|
|
// this is getting the new attr value, not the old one....
|
2012-02-14 02:00:56 +00:00
|
|
|
nsAutoString oldValueStr;
|
2007-08-06 15:27:19 +00:00
|
|
|
modification = GetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
|
|
|
oldValueStr);
|
2012-02-14 02:00:56 +00:00
|
|
|
if (modification) {
|
|
|
|
oldValue.SetTo(oldValueStr);
|
|
|
|
}
|
2007-08-06 15:27:19 +00:00
|
|
|
}
|
|
|
|
else if (aNotify && IsInDoc()) {
|
|
|
|
modification = !!mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
}
|
|
|
|
|
2012-04-05 17:14:54 +00:00
|
|
|
nsAttrValue attrValue(aStyleRule, aSerialized);
|
2007-08-06 15:27:19 +00:00
|
|
|
|
2010-02-24 04:37:47 +00:00
|
|
|
// XXXbz do we ever end up with ADDITION here? I doubt it.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t modType = modification ?
|
|
|
|
static_cast<uint8_t>(nsIDOMMutationEvent::MODIFICATION) :
|
|
|
|
static_cast<uint8_t>(nsIDOMMutationEvent::ADDITION);
|
2010-02-24 04:37:47 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return SetAttrAndNotify(kNameSpaceID_None, nsGkAtoms::style, nullptr,
|
2012-02-14 02:00:56 +00:00
|
|
|
oldValue, attrValue, modType, hasListeners,
|
2012-02-14 02:00:56 +00:00
|
|
|
aNotify, kDontCallAfterSetAttr);
|
2007-08-06 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 02:48:57 +00:00
|
|
|
css::StyleRule*
|
2011-04-08 05:27:57 +00:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::GetInlineStyleRule()
|
2007-08-06 15:27:19 +00:00
|
|
|
{
|
2011-04-08 02:29:50 +00:00
|
|
|
if (!MayHaveStyle()) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2007-09-18 08:38:24 +00:00
|
|
|
}
|
2007-08-06 15:27:19 +00:00
|
|
|
const nsAttrValue* attrVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
|
|
|
|
if (attrVal && attrVal->Type() == nsAttrValue::eCSSStyleRule) {
|
|
|
|
return attrVal->GetCSSStyleRuleValue();
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2007-08-06 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
// Others and helpers
|
|
|
|
|
2012-11-10 23:30:15 +00:00
|
|
|
nsICSSDeclaration*
|
2013-01-11 08:42:59 +00:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::Style()
|
2007-08-06 15:27:19 +00:00
|
|
|
{
|
2012-11-14 22:10:08 +00:00
|
|
|
Element::nsDOMSlots *slots = DOMSlots();
|
2007-08-06 15:27:19 +00:00
|
|
|
|
|
|
|
if (!slots->mStyle) {
|
|
|
|
// Just in case...
|
2011-10-17 14:59:28 +00:00
|
|
|
ReparseStyleAttribute(true);
|
2007-08-06 15:27:19 +00:00
|
|
|
|
2011-11-05 10:32:52 +00:00
|
|
|
slots->mStyle = new nsDOMCSSAttributeDeclaration(this, false);
|
2011-04-08 02:29:50 +00:00
|
|
|
SetMayHaveStyle();
|
2007-08-06 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 21:59:36 +00:00
|
|
|
return slots->mStyle;
|
2007-08-06 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::ReparseStyleAttribute(bool aForceInDataDoc)
|
2007-08-06 15:27:19 +00:00
|
|
|
{
|
2011-04-08 02:29:50 +00:00
|
|
|
if (!MayHaveStyle()) {
|
2007-09-18 08:38:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-08-06 15:27:19 +00:00
|
|
|
const nsAttrValue* oldVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
|
|
|
|
if (oldVal && oldVal->Type() != nsAttrValue::eCSSStyleRule) {
|
|
|
|
nsAttrValue attrValue;
|
|
|
|
nsAutoString stringValue;
|
|
|
|
oldVal->ToString(stringValue);
|
2010-02-24 04:37:46 +00:00
|
|
|
ParseStyleAttribute(stringValue, attrValue, aForceInDataDoc);
|
2007-08-06 15:27:19 +00:00
|
|
|
// Don't bother going through SetInlineStyleRule, we don't want to fire off
|
|
|
|
// mutation events or document notifications anyway
|
|
|
|
nsresult rv = mAttrsAndChildren.SetAndTakeAttr(nsGkAtoms::style, attrValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-04-08 05:27:57 +00:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::ParseStyleAttribute(const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aForceInDataDoc)
|
2007-08-06 15:27:19 +00:00
|
|
|
{
|
2011-10-18 10:53:36 +00:00
|
|
|
nsIDocument* doc = OwnerDoc();
|
2007-08-06 15:27:19 +00:00
|
|
|
|
2013-11-08 23:44:39 +00:00
|
|
|
if (!nsStyleUtil::CSPAllowsInlineStyle(nullptr, NodePrincipal(),
|
2012-08-30 17:58:24 +00:00
|
|
|
doc->GetDocumentURI(), 0, aValue,
|
|
|
|
nullptr))
|
|
|
|
return;
|
|
|
|
|
2011-10-18 11:19:44 +00:00
|
|
|
if (aForceInDataDoc ||
|
|
|
|
!doc->IsLoadedAsData() ||
|
|
|
|
doc->IsStaticDocument()) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isCSS = true; // assume CSS until proven otherwise
|
2007-08-06 15:27:19 +00:00
|
|
|
|
2010-02-24 04:37:46 +00:00
|
|
|
if (!IsInNativeAnonymousSubtree()) { // native anonymous content
|
|
|
|
// always assumes CSS
|
2007-08-06 15:27:19 +00:00
|
|
|
nsAutoString styleType;
|
|
|
|
doc->GetHeaderData(nsGkAtoms::headerContentStyleType, styleType);
|
|
|
|
if (!styleType.IsEmpty()) {
|
|
|
|
static const char textCssStr[] = "text/css";
|
|
|
|
isCSS = (styleType.EqualsIgnoreCase(textCssStr, sizeof(textCssStr) - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-30 16:40:24 +00:00
|
|
|
if (isCSS && aResult.ParseStyleAttribute(aValue, this)) {
|
|
|
|
return;
|
2007-08-06 15:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult.SetTo(aValue);
|
|
|
|
}
|