1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "CustomStateSet.h"
8 #include "mozilla/dom/ElementInternalsBinding.h"
9 #include "mozilla/dom/HTMLElement.h"
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/PresShell.h"
13 namespace mozilla::dom
{
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CustomStateSet
, mTarget
);
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(CustomStateSet
)
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(CustomStateSet
)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CustomStateSet
)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23 CustomStateSet::CustomStateSet(HTMLElement
* aTarget
) : mTarget(aTarget
) {}
26 nsISupports
* CustomStateSet::GetParentObject() const {
27 return ToSupports(mTarget
);
30 JSObject
* CustomStateSet::WrapObject(JSContext
* aCx
,
31 JS::Handle
<JSObject
*> aGivenProto
) {
32 return CustomStateSet_Binding::Wrap(aCx
, this, aGivenProto
);
35 void CustomStateSet::Clear(ErrorResult
& aRv
) {
36 CustomStateSet_Binding::SetlikeHelpers::Clear(this, aRv
);
41 nsTArray
<RefPtr
<nsAtom
>>& states
= mTarget
->EnsureCustomStates();
42 Document
* doc
= mTarget
->GetComposedDoc();
43 PresShell
* presShell
= doc
? doc
->GetPresShell() : nullptr;
45 presShell
->CustomStatesWillChange(*mTarget
);
46 // Iterate over each state to ensure each one is invalidated.
47 while (!states
.IsEmpty()) {
48 RefPtr
<nsAtom
> atom
= states
.PopLastElement();
49 presShell
->CustomStateChanged(*mTarget
, atom
);
57 bool CustomStateSet::Delete(const nsAString
& aState
, ErrorResult
& aRv
) {
58 CustomStateSet_Binding::SetlikeHelpers::Delete(this, aState
, aRv
);
63 RefPtr
<nsAtom
> atom
= NS_AtomizeMainThread(aState
);
64 Document
* doc
= mTarget
->GetComposedDoc();
65 PresShell
* presShell
= doc
? doc
->GetPresShell() : nullptr;
67 presShell
->CustomStatesWillChange(*mTarget
);
70 bool deleted
= mTarget
->EnsureCustomStates().RemoveElement(atom
);
73 presShell
->CustomStateChanged(*mTarget
, atom
);
78 void CustomStateSet::Add(const nsAString
& aState
, ErrorResult
& aRv
) {
79 CustomStateSet_Binding::SetlikeHelpers::Add(this, aState
, aRv
);
84 nsTArray
<RefPtr
<nsAtom
>>& states
= mTarget
->EnsureCustomStates();
85 RefPtr
<nsAtom
> atom
= NS_AtomizeMainThread(aState
);
86 if (states
.Contains(atom
)) {
90 Document
* doc
= mTarget
->GetComposedDoc();
91 PresShell
* presShell
= doc
? doc
->GetPresShell() : nullptr;
93 presShell
->CustomStatesWillChange(*mTarget
);
96 states
.AppendElement(atom
);
99 presShell
->CustomStateChanged(*mTarget
, atom
);
103 } // namespace mozilla::dom