gecko-dev/layout/style/nsDOMCSSAttrDeclaration.h
Olli Pettay abf54817b9 Bug 1428246 - The attributeChangedCallback is fired twice for the *first* style attribute change, r=peterv
The idea with this patch is that style code will first call
InlineStyleDeclarationWillChange before style declaration has changed, and SetInlineStyleDeclaration once it has changed.

In order to be able to report old attribute value, InlineStyleDeclarationWillChange reads the value and also calls AttributeWillChange (so that DOMMutationObserser can grab the old value). Later SetInlineStyleDeclaration passes the old value to
SetAttrAndNotify so that mutation events and attributeChanged callbacks are handled correctly.

Because of performance, declaration can't be cloned for reading the old value. And that is why the recently-added callback is used to detect when declaration is about to change (bug 1466963 and followup bug 1468665).

To keep the expected existing behavior, even if declaration isn't changed, but just a new declaration was created (since there wasn't any), we need to still run all these
willchange/set calls. That is when the code has 'if (created)' checks.

Since there are several declaration implementation and only nsDOMCSSAttributeDeclaration needs the about-to-change callback, GetPropertyChangeClosure is the one to initialize the callback closure, and the struct which is then passes as data to the closure.

Apparently we lost mutation event testing on style attribute when the pref was added, so test_style_attr_listener.html is modified to test both pref values.

--HG--
extra : rebase_source : 9e605d43f22e650ac3912fbfb41abb8d5a2a0c8f
2018-06-26 12:54:00 +03:00

87 lines
2.6 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* DOM object for element.style */
#ifndef nsDOMCSSAttributeDeclaration_h
#define nsDOMCSSAttributeDeclaration_h
#include "mozilla/Attributes.h"
#include "mozilla/dom/DocGroup.h"
#include "nsDOMCSSDeclaration.h"
class nsSMILValue;
struct RawServoUnlockedDeclarationBlock;
namespace mozilla {
namespace dom {
class DomGroup;
class Element;
} // namespace dom
} // namespace mozilla
class nsDOMCSSAttributeDeclaration final : public nsDOMCSSDeclaration
{
public:
typedef mozilla::dom::Element Element;
nsDOMCSSAttributeDeclaration(Element* aContent, bool aIsSMILOverride);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsDOMCSSAttributeDeclaration,
nsICSSDeclaration)
mozilla::DeclarationBlock* GetOrCreateCSSDeclaration(
Operation aOperation, mozilla::DeclarationBlock** aCreated) final;
nsDOMCSSDeclaration::ParsingEnvironment
GetParsingEnvironment(nsIPrincipal* aSubjectPrincipal) const final;
mozilla::css::Rule* GetParentRule() override
{
return nullptr;
}
nsINode* GetParentObject() override
{
return mElement;
}
nsresult SetSMILValue(const nsCSSPropertyID aPropID, const nsSMILValue&);
nsresult SetPropertyValue(const nsCSSPropertyID aPropID,
const nsAString& aValue,
nsIPrincipal* aSubjectPrincipal) override;
static void MutationClosureFunction(void* aData);
void
GetPropertyChangeClosure(DeclarationBlockMutationClosure* aClosure,
MutationClosureData* aClosureData) final
{
aClosure->function = MutationClosureFunction;
aClosure->data = aClosureData;
aClosureData->mClosure = MutationClosureFunction;
aClosureData->mElement = mElement;
}
protected:
~nsDOMCSSAttributeDeclaration();
virtual nsresult SetCSSDeclaration(mozilla::DeclarationBlock* aDecl,
MutationClosureData* aClosureData) override;
virtual nsIDocument* DocToUpdate() override;
RefPtr<Element> mElement;
/* If true, this indicates that this nsDOMCSSAttributeDeclaration
* should interact with mContent's SMIL override style rule (rather
* than the inline style rule).
*/
const bool mIsSMILOverride;
};
#endif /* nsDOMCSSAttributeDeclaration_h */