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: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Mats Palmgren <mats.palmgren@bredband.net>
|
|
|
|
* Daniel Kraft <d@domob.eu>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsStyledElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsAttrValue.h"
|
|
|
|
#include "nsGenericElement.h"
|
|
|
|
#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"
|
|
|
|
#include "nsICSSStyleRule.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"
|
2007-08-06 11:00:01 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_SVG
|
2007-08-06 08:27:19 -07:00
|
|
|
#include "nsIDOMSVGStylable.h"
|
2007-08-06 11:00:01 -07:00
|
|
|
#endif
|
2007-08-06 08:27:19 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsIContent methods
|
|
|
|
|
|
|
|
nsIAtom*
|
|
|
|
nsStyledElement::GetClassAttributeName() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
|
|
|
nsStyledElement::GetIDAttributeName() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::id;
|
|
|
|
}
|
|
|
|
|
2010-06-03 18:09:20 -07:00
|
|
|
nsIAtom*
|
|
|
|
nsStyledElement::DoGetID() const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(HasFlag(NODE_HAS_ID), "Unexpected call");
|
|
|
|
|
|
|
|
// The nullcheck here is needed because nsGenericElement::UnsetAttr calls
|
|
|
|
// out to various code between removing the attribute and we get a chance to
|
|
|
|
// clear the NODE_HAS_ID flag.
|
|
|
|
|
|
|
|
const nsAttrValue* attr = mAttrsAndChildren.GetAttr(nsGkAtoms::id);
|
|
|
|
|
|
|
|
return attr ? attr->GetAtomValue() : nsnull;
|
|
|
|
}
|
|
|
|
|
2007-08-06 08:27:19 -07:00
|
|
|
const nsAttrValue*
|
2008-09-10 20:22:20 -07:00
|
|
|
nsStyledElement::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
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStyledElement::ParseAttribute(PRInt32 aNamespaceID, nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue, nsAttrValue& aResult)
|
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
if (aAttribute == nsGkAtoms::style) {
|
2007-09-18 01:38:24 -07:00
|
|
|
SetFlags(NODE_MAY_HAVE_STYLE);
|
2010-02-23 20:37:46 -08:00
|
|
|
ParseStyleAttribute(aValue, aResult, PR_FALSE);
|
2007-08-06 08:27:19 -07:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
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()) {
|
|
|
|
UnsetFlags(NODE_HAS_ID);
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
aResult.ParseAtom(aValue);
|
|
|
|
SetFlags(NODE_HAS_ID);
|
|
|
|
AddToIdTable(aResult.GetAtomValue());
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsStyledElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
|
2010-06-03 18:09:20 -07:00
|
|
|
nsresult
|
|
|
|
nsStyledElement::UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
|
|
|
|
PRBool aNotify)
|
|
|
|
{
|
|
|
|
PRBool isId = PR_FALSE;
|
|
|
|
if (aAttribute == nsGkAtoms::id && aNameSpaceID == kNameSpaceID_None) {
|
|
|
|
// Have to do this before clearing flag. See RemoveFromIdTable
|
|
|
|
RemoveFromIdTable();
|
|
|
|
isId = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = nsGenericElement::UnsetAttr(aNameSpaceID, aAttribute, aNotify);
|
|
|
|
|
|
|
|
if (isId) {
|
|
|
|
UnsetFlags(NODE_HAS_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-08-06 08:27:19 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyledElement::SetInlineStyleRule(nsICSSStyleRule* aStyleRule, PRBool aNotify)
|
|
|
|
{
|
2007-09-18 01:38:24 -07:00
|
|
|
SetFlags(NODE_MAY_HAVE_STYLE);
|
2007-08-06 08:27:19 -07:00
|
|
|
PRBool modification = PR_FALSE;
|
|
|
|
nsAutoString oldValueStr;
|
|
|
|
|
|
|
|
PRBool hasListeners = aNotify &&
|
|
|
|
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....
|
|
|
|
modification = GetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
|
|
|
oldValueStr);
|
|
|
|
}
|
|
|
|
else if (aNotify && IsInDoc()) {
|
|
|
|
modification = !!mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
}
|
|
|
|
|
2010-08-12 23:03:23 -07:00
|
|
|
nsAttrValue attrValue(aStyleRule, nsnull);
|
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.
|
|
|
|
PRUint8 modType = modification ?
|
|
|
|
static_cast<PRUint8>(nsIDOMMutationEvent::MODIFICATION) :
|
|
|
|
static_cast<PRUint8>(nsIDOMMutationEvent::ADDITION);
|
|
|
|
|
2007-08-06 08:27:19 -07:00
|
|
|
return SetAttrAndNotify(kNameSpaceID_None, nsGkAtoms::style, nsnull,
|
2010-02-23 20:37:47 -08:00
|
|
|
oldValueStr, attrValue, modType, hasListeners,
|
2008-12-03 02:39:21 -08:00
|
|
|
aNotify, nsnull);
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsICSSStyleRule*
|
|
|
|
nsStyledElement::GetInlineStyleRule()
|
|
|
|
{
|
2007-09-18 01:38:24 -07:00
|
|
|
if (!HasFlag(NODE_MAY_HAVE_STYLE)) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
2007-08-06 08:27:19 -07:00
|
|
|
const nsAttrValue* attrVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
|
|
|
|
|
|
|
|
if (attrVal && attrVal->Type() == nsAttrValue::eCSSStyleRule) {
|
|
|
|
return attrVal->GetCSSStyleRuleValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStyledElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
|
|
|
PRBool aCompileEventHandlers)
|
|
|
|
{
|
|
|
|
nsresult rv = nsStyledElementBase::BindToTree(aDocument, aParent,
|
|
|
|
aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-06-03 18:09:20 -07:00
|
|
|
if (aDocument && HasFlag(NODE_HAS_ID) && !GetBindingParent()) {
|
|
|
|
aDocument->AddToIdTable(this, DoGetID());
|
|
|
|
}
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2010-06-03 18:09:20 -07:00
|
|
|
if (!IsXUL()) {
|
|
|
|
// XXXbz if we already have a style attr parsed, this won't do
|
|
|
|
// anything... need to fix that.
|
|
|
|
ReparseStyleAttribute(PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
2010-06-03 18:09:20 -07:00
|
|
|
void
|
|
|
|
nsStyledElement::UnbindFromTree(PRBool aDeep, PRBool aNullParent)
|
|
|
|
{
|
|
|
|
RemoveFromIdTable();
|
|
|
|
|
|
|
|
nsStyledElementBase::UnbindFromTree(aDeep, aNullParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-06 08:27:19 -07:00
|
|
|
// ---------------------------------------------------------------
|
|
|
|
// Others and helpers
|
|
|
|
|
2010-08-05 14:59:36 -07:00
|
|
|
nsIDOMCSSStyleDeclaration*
|
|
|
|
nsStyledElement::GetStyle(nsresult* retval)
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2010-08-05 14:59:36 -07:00
|
|
|
nsXULElement* xulElement = nsXULElement::FromContent(this);
|
|
|
|
if (xulElement) {
|
|
|
|
nsresult rv = xulElement->EnsureLocalStyle();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*retval = rv;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-06 08:27:19 -07:00
|
|
|
nsGenericElement::nsDOMSlots *slots = GetDOMSlots();
|
|
|
|
|
|
|
|
if (!slots->mStyle) {
|
|
|
|
// Just in case...
|
2008-02-19 09:52:00 -08:00
|
|
|
ReparseStyleAttribute(PR_TRUE);
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2009-09-02 17:28:37 -07:00
|
|
|
slots->mStyle = new nsDOMCSSAttributeDeclaration(this
|
|
|
|
#ifdef MOZ_SMIL
|
|
|
|
, PR_FALSE
|
|
|
|
#endif // MOZ_SMIL
|
|
|
|
);
|
2007-09-18 01:38:24 -07:00
|
|
|
SetFlags(NODE_MAY_HAVE_STYLE);
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
2010-08-05 14:59:36 -07:00
|
|
|
*retval = NS_OK;
|
|
|
|
return slots->mStyle;
|
2007-08-06 08:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-02-19 09:52:00 -08:00
|
|
|
nsStyledElement::ReparseStyleAttribute(PRBool aForceInDataDoc)
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2007-09-18 01:38:24 -07:00
|
|
|
if (!HasFlag(NODE_MAY_HAVE_STYLE)) {
|
|
|
|
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
|
2010-02-23 20:37:46 -08:00
|
|
|
nsStyledElement::ParseStyleAttribute(const nsAString& aValue,
|
2008-02-19 09:52:00 -08:00
|
|
|
nsAttrValue& aResult,
|
|
|
|
PRBool aForceInDataDoc)
|
2007-08-06 08:27:19 -07:00
|
|
|
{
|
2010-02-23 20:37:46 -08:00
|
|
|
nsIDocument* doc = GetOwnerDoc();
|
2007-08-06 08:27:19 -07:00
|
|
|
|
2009-12-10 20:02:13 -08:00
|
|
|
if (doc && (aForceInDataDoc ||
|
|
|
|
!doc->IsLoadedAsData() ||
|
|
|
|
doc->IsStaticDocument())) {
|
2007-08-06 08:27:19 -07:00
|
|
|
PRBool isCSS = PR_TRUE; // assume CSS until proven otherwise
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCSS) {
|
2010-03-02 13:00:53 -08:00
|
|
|
mozilla::css::Loader* cssLoader = doc->CSSLoader();
|
2010-03-02 12:59:32 -08:00
|
|
|
nsCSSParser cssParser(cssLoader);
|
2007-08-06 08:27:19 -07:00
|
|
|
if (cssParser) {
|
2010-02-23 20:37:46 -08:00
|
|
|
nsCOMPtr<nsIURI> baseURI = GetBaseURI();
|
2007-08-06 08:27:19 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsICSSStyleRule> rule;
|
2010-03-02 12:59:32 -08:00
|
|
|
cssParser.ParseStyleAttribute(aValue, doc->GetDocumentURI(),
|
|
|
|
baseURI,
|
|
|
|
NodePrincipal(),
|
|
|
|
getter_AddRefs(rule));
|
2007-08-06 08:27:19 -07:00
|
|
|
if (rule) {
|
2010-08-12 23:03:23 -07:00
|
|
|
aResult.SetTo(rule, &aValue);
|
2007-08-06 08:27:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult.SetTo(aValue);
|
|
|
|
}
|