From 539c85b4ec4dfe0999c3c5a1109c4e42a6316582 Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Mon, 6 Mar 2017 10:44:14 +0800 Subject: [PATCH] Bug 1341230 - Part 3: Add nsIDOMWindowUtils API to add/remove manually managed EventStates bits. r=smaug MozReview-Commit-ID: 8brJct2tkTo --HG-- extra : rebase_source : 6a9aae7f63992a74620126bf626963b3dcc00e41 --- dom/base/nsDOMWindowUtils.cpp | 57 +++++++++++++++++++++++ dom/interfaces/base/nsIDOMWindowUtils.idl | 20 ++++++++ 2 files changed, 77 insertions(+) diff --git a/dom/base/nsDOMWindowUtils.cpp b/dom/base/nsDOMWindowUtils.cpp index 1548561f8e46..e70fc1f4dcf4 100644 --- a/dom/base/nsDOMWindowUtils.cpp +++ b/dom/base/nsDOMWindowUtils.cpp @@ -4095,6 +4095,63 @@ nsDOMWindowUtils::IsTimeoutTracking(uint32_t aTimeoutId, bool* aResult) return NS_OK; } +struct StateTableEntry +{ + const char* mStateString; + EventStates mState; +}; + +static const StateTableEntry kManuallyManagedStates[] = { + // none yet; but for example: { "highlight", NS_EVENT_STATE_HIGHLIGHT }, +}; + +static EventStates +GetEventStateForString(const nsAString& aStateString) +{ + for (auto& entry : kManuallyManagedStates) { + if (aStateString.EqualsASCII(entry.mStateString)) { + return entry.mState; + } + } + return EventStates(); +} + +NS_IMETHODIMP +nsDOMWindowUtils::AddManuallyManagedState(nsIDOMElement* aElement, + const nsAString& aStateString) +{ + nsCOMPtr element = do_QueryInterface(aElement); + if (!element) { + return NS_ERROR_INVALID_ARG; + } + + EventStates state = GetEventStateForString(aStateString); + if (state.IsEmpty()) { + return NS_ERROR_INVALID_ARG; + } + + element->AddManuallyManagedStates(state); + return NS_OK; +} + +NS_IMETHODIMP +nsDOMWindowUtils::RemoveManuallyManagedState(nsIDOMElement* aElement, + const nsAString& aStateString) +{ + nsCOMPtr element = do_QueryInterface(aElement); + if (!element) { + return NS_ERROR_INVALID_ARG; + } + + EventStates state = GetEventStateForString(aStateString); + if (state.IsEmpty()) { + return NS_ERROR_INVALID_ARG; + } + + element->RemoveManuallyManagedStates(state); + return NS_OK; +} + NS_INTERFACE_MAP_BEGIN(nsTranslationNodeList) NS_INTERFACE_MAP_ENTRY(nsISupports) NS_INTERFACE_MAP_ENTRY(nsITranslationNodeList) diff --git a/dom/interfaces/base/nsIDOMWindowUtils.idl b/dom/interfaces/base/nsIDOMWindowUtils.idl index 29732c9794d1..deb28c913483 100644 --- a/dom/interfaces/base/nsIDOMWindowUtils.idl +++ b/dom/interfaces/base/nsIDOMWindowUtils.idl @@ -1976,6 +1976,26 @@ interface nsIDOMWindowUtils : nsISupports { */ boolean isTimeoutTracking(in unsigned long timeoutId); + /** + * Adds an EventStates bit to the element. + * + * The state string must be one of the following: + * * (none yet; but for example "higlighted" for NS_EVENT_STATE_HIGHLIGHTED) + * + * The supported state strings are defined in kManuallyManagedStates + * in nsDOMWindowUtils.cpp. + */ + void addManuallyManagedState(in nsIDOMElement element, + in AString state); + + /** + * Removes the specified EventStates bits from the element. + * + * See above for the strings that can be passed for |state|. + */ + void removeManuallyManagedState(in nsIDOMElement element, + in AString state); + // These consts are only for testing purposes. const long DEFAULT_MOUSE_POINTER_ID = 0; const long DEFAULT_PEN_POINTER_ID = 1;