2007-08-06 08:27:19 -07: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 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-08-06 08:27:19 -07:00
|
|
|
|
|
|
|
#include "nsStyledElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsAttrValue.h"
|
2012-09-30 09:40:24 -07:00
|
|
|
#include "nsAttrValueInlines.h"
|
2012-11-14 14:10:08 -08:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-08-06 08:27:19 -07:00
|
|
|
#include "nsMutationEvent.h"
|
|
|
|
#include "nsDOMCSSDeclaration.h"
|
2009-04-08 13:52:37 -07:00
|
|
|
#include "nsDOMCSSAttrDeclaration.h"
|
2007-08-06 08:27:19 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIDocument.h"
|
2011-03-10 18:48:57 -08:00
|
|
|
#include "mozilla/css/StyleRule.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"
|
2010-02-23 20:37:47 -08:00
|
|
|
#include "nsIDOMMutationEvent.h"
|
2010-08-05 14:59:36 -07:00
|
|
|
#include "nsXULElement.h"
|
2011-07-15 03:31:34 -07:00
|
|
|
#include "nsContentUtils.h"
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2011-03-10 18:48:57 -08:00
|
|
|
namespace css = mozilla::css;
|
2012-11-14 14:10:08 -08:00
|
|
|
using namespace mozilla::dom;
|
2011-03-10 18:48:57 -08:00
|
|
|
|
2007-08-06 08:27:19 -07:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsIContent methods
|
|
|
|
|
|
|
|
nsIAtom*
|
2011-04-07 22:27:57 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::GetClassAttributeName() const
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
|
|
|
return nsGkAtoms::_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
2011-04-07 22:27:57 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::GetIDAttributeName() const
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
|
|
|
return nsGkAtoms::id;
|
|
|
|
}
|
|
|
|
|
2010-06-03 18:09:20 -07:00
|
|
|
nsIAtom*
|
2011-04-07 22:27:57 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::DoGetID() const
|
2010-06-03 18:09:20 -07:00
|
|
|
{
|
2011-04-07 19:29:50 -07:00
|
|
|
NS_ASSERTION(HasID(), "Unexpected call");
|
2010-06-03 18:09:20 -07:00
|
|
|
|
2012-11-14 14:10:08 -08:00
|
|
|
// The nullcheck here is needed because Element::UnsetAttr calls
|
2010-06-03 18:09:20 -07:00
|
|
|
// out to various code between removing the attribute and we get a chance to
|
2011-04-07 19:29:50 -07:00
|
|
|
// ClearHasID().
|
2010-06-03 18:09:20 -07:00
|
|
|
|
|
|
|
const nsAttrValue* attr = mAttrsAndChildren.GetAttr(nsGkAtoms::id);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return attr ? attr->GetAtomValue() : nullptr;
|
2010-06-03 18:09:20 -07:00
|
|
|
}
|
|
|
|
|
2007-08-06 08:27:19 -07:00
|
|
|
const nsAttrValue*
|
2011-04-07 22:27:57 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::DoGetClasses() const
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2008-09-10 20:22:20 -07:00
|
|
|
NS_ASSERTION(HasFlag(NODE_MAY_HAVE_CLASS), "Unexpected call");
|
2007-09-01 09:14:53 -07:00
|
|
|
return mAttrsAndChildren.GetAttr(nsGkAtoms::_class);
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::ParseAttribute(int32_t aNamespaceID,
|
2011-04-07 22:27:57 -07:00
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
if (aAttribute == nsGkAtoms::style) {
|
2011-04-07 19:29:50 -07:00
|
|
|
SetMayHaveStyle();
|
2011-10-17 07:59:28 -07:00
|
|
|
ParseStyleAttribute(aValue, aResult, false);
|
|
|
|
return true;
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
if (aAttribute == nsGkAtoms::_class) {
|
2007-09-18 01:38:24 -07:00
|
|
|
SetFlags(NODE_MAY_HAVE_CLASS);
|
2007-08-06 08:27:19 -07:00
|
|
|
aResult.ParseAtomArray(aValue);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
2010-06-03 18:09:20 -07:00
|
|
|
if (aAttribute == nsGkAtoms::id) {
|
|
|
|
// Store id as an atom. id="" means that the element has no id,
|
|
|
|
// not that it has an emptystring as the id.
|
|
|
|
RemoveFromIdTable();
|
|
|
|
if (aValue.IsEmpty()) {
|
2011-04-07 19:29:50 -07:00
|
|
|
ClearHasID();
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-06-03 18:09:20 -07:00
|
|
|
}
|
|
|
|
aResult.ParseAtom(aValue);
|
2011-04-07 19:29:50 -07:00
|
|
|
SetHasID();
|
2010-06-03 18:09:20 -07:00
|
|
|
AddToIdTable(aResult.GetAtomValue());
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-06-03 18:09:20 -07:00
|
|
|
}
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsStyledElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
|
2010-06-03 18:09:20 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::UnsetAttr(int32_t aNameSpaceID,
|
2011-04-07 22:27:57 -07:00
|
|
|
nsIAtom* aAttribute,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNotify)
|
2010-06-03 18:09:20 -07:00
|
|
|
{
|
2011-05-25 10:58:23 -07:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2010-06-03 18:09:20 -07:00
|
|
|
if (aAttribute == nsGkAtoms::id && aNameSpaceID == kNameSpaceID_None) {
|
|
|
|
// Have to do this before clearing flag. See RemoveFromIdTable
|
|
|
|
RemoveFromIdTable();
|
|
|
|
}
|
2010-09-01 15:48:24 -07:00
|
|
|
|
2012-11-14 14:10:08 -08:00
|
|
|
return Element::UnsetAttr(aNameSpaceID, aAttribute, aNotify);
|
2010-09-03 15:39:29 -07:00
|
|
|
}
|
2010-06-03 18:09:20 -07:00
|
|
|
|
2010-09-03 15:39:29 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::AfterSetAttr(int32_t aNamespaceID,
|
2011-04-07 22:27:57 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-02-13 18:00:56 -08:00
|
|
|
const nsAttrValue* aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNotify)
|
2010-09-03 15:39:29 -07:00
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None && !aValue &&
|
|
|
|
aAttribute == nsGkAtoms::id) {
|
|
|
|
// The id has been removed when calling UnsetAttr but we kept it because
|
|
|
|
// the id is used for some layout stuff between UnsetAttr and AfterSetAttr.
|
|
|
|
// Now. the id is really removed so it would not be safe to keep this flag.
|
2011-04-07 19:29:50 -07:00
|
|
|
ClearHasID();
|
2010-06-03 18:09:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 14:10:08 -08:00
|
|
|
return Element::AfterSetAttr(aNamespaceID, aAttribute, aValue, aNotify);
|
2010-06-03 18:09:20 -07:00
|
|
|
}
|
|
|
|
|
2012-04-14 06:09:19 -07:00
|
|
|
nsresult
|
2011-04-07 22:27:57 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::SetInlineStyleRule(css::StyleRule* aStyleRule,
|
2012-04-05 10:14:54 -07:00
|
|
|
const nsAString* aSerialized,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNotify)
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2011-04-07 19:29:50 -07:00
|
|
|
SetMayHaveStyle();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool modification = false;
|
2012-02-13 18:00:56 -08:00
|
|
|
nsAttrValue oldValue;
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasListeners = aNotify &&
|
2007-08-06 08:27:19 -07: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-13 18:00:56 -08:00
|
|
|
nsAutoString oldValueStr;
|
2007-08-06 08:27:19 -07:00
|
|
|
modification = GetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
|
|
|
oldValueStr);
|
2012-02-13 18:00:56 -08:00
|
|
|
if (modification) {
|
|
|
|
oldValue.SetTo(oldValueStr);
|
|
|
|
}
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
else if (aNotify && IsInDoc()) {
|
|
|
|
modification = !!mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
}
|
|
|
|
|
2012-04-05 10:14:54 -07:00
|
|
|
nsAttrValue attrValue(aStyleRule, aSerialized);
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2010-02-23 20:37:47 -08:00
|
|
|
// XXXbz do we ever end up with ADDITION here? I doubt it.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t modType = modification ?
|
|
|
|
static_cast<uint8_t>(nsIDOMMutationEvent::MODIFICATION) :
|
|
|
|
static_cast<uint8_t>(nsIDOMMutationEvent::ADDITION);
|
2010-02-23 20:37:47 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return SetAttrAndNotify(kNameSpaceID_None, nsGkAtoms::style, nullptr,
|
2012-02-13 18:00:56 -08:00
|
|
|
oldValue, attrValue, modType, hasListeners,
|
2012-02-13 18:00:56 -08:00
|
|
|
aNotify, kDontCallAfterSetAttr);
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
2011-03-10 18:48:57 -08:00
|
|
|
css::StyleRule*
|
2011-04-07 22:27:57 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::GetInlineStyleRule()
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2011-04-07 19:29:50 -07:00
|
|
|
if (!MayHaveStyle()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-09-18 01:38:24 -07:00
|
|
|
}
|
2007-08-06 08:27:19 -07:00
|
|
|
const nsAttrValue* attrVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
|
|
|
|
if (attrVal && attrVal->Type() == nsAttrValue::eCSSStyleRule) {
|
|
|
|
return attrVal->GetCSSStyleRuleValue();
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
// Others and helpers
|
|
|
|
|
2012-11-10 15:30:15 -08:00
|
|
|
nsICSSDeclaration*
|
2013-01-11 00:42:59 -08:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::Style()
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2012-11-14 14:10:08 -08:00
|
|
|
Element::nsDOMSlots *slots = DOMSlots();
|
2007-08-06 08:27:19 -07:00
|
|
|
|
|
|
|
if (!slots->mStyle) {
|
|
|
|
// Just in case...
|
2011-10-17 07:59:28 -07:00
|
|
|
ReparseStyleAttribute(true);
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2011-11-05 03:32:52 -07:00
|
|
|
slots->mStyle = new nsDOMCSSAttributeDeclaration(this, false);
|
2011-04-07 19:29:50 -07:00
|
|
|
SetMayHaveStyle();
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
2010-08-05 14:59:36 -07:00
|
|
|
return slots->mStyle;
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::ReparseStyleAttribute(bool aForceInDataDoc)
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2011-04-07 19:29:50 -07:00
|
|
|
if (!MayHaveStyle()) {
|
2007-09-18 01:38:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-08-06 08:27:19 -07:00
|
|
|
const nsAttrValue* oldVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
|
|
|
|
if (oldVal && oldVal->Type() != nsAttrValue::eCSSStyleRule) {
|
|
|
|
nsAttrValue attrValue;
|
|
|
|
nsAutoString stringValue;
|
|
|
|
oldVal->ToString(stringValue);
|
2010-02-23 20:37:46 -08:00
|
|
|
ParseStyleAttribute(stringValue, attrValue, aForceInDataDoc);
|
2007-08-06 08:27:19 -07: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-07 22:27:57 -07:00
|
|
|
nsStyledElementNotElementCSSInlineStyle::ParseStyleAttribute(const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aForceInDataDoc)
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = OwnerDoc();
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2011-10-18 04:19:44 -07:00
|
|
|
if (aForceInDataDoc ||
|
|
|
|
!doc->IsLoadedAsData() ||
|
|
|
|
doc->IsStaticDocument()) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isCSS = true; // assume CSS until proven otherwise
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2010-02-23 20:37:46 -08:00
|
|
|
if (!IsInNativeAnonymousSubtree()) { // native anonymous content
|
|
|
|
// always assumes CSS
|
2007-08-06 08:27:19 -07: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 09:40:24 -07:00
|
|
|
if (isCSS && aResult.ParseStyleAttribute(aValue, this)) {
|
|
|
|
return;
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult.SetTo(aValue);
|
|
|
|
}
|