Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / html / CustomStateSet.cpp
blobbb3621b195faac0be77f53cfe557763b39db254b
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
21 NS_INTERFACE_MAP_END
23 CustomStateSet::CustomStateSet(HTMLElement* aTarget) : mTarget(aTarget) {}
25 // WebIDL interface
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);
37 if (aRv.Failed()) {
38 return;
41 nsTArray<RefPtr<nsAtom>>& states = mTarget->EnsureCustomStates();
42 Document* doc = mTarget->GetComposedDoc();
43 PresShell* presShell = doc ? doc->GetPresShell() : nullptr;
44 if (presShell) {
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);
51 return;
54 states.Clear();
57 bool CustomStateSet::Delete(const nsAString& aState, ErrorResult& aRv) {
58 CustomStateSet_Binding::SetlikeHelpers::Delete(this, aState, aRv);
59 if (aRv.Failed()) {
60 return false;
63 RefPtr<nsAtom> atom = NS_AtomizeMainThread(aState);
64 Document* doc = mTarget->GetComposedDoc();
65 PresShell* presShell = doc ? doc->GetPresShell() : nullptr;
66 if (presShell) {
67 presShell->CustomStatesWillChange(*mTarget);
70 bool deleted = mTarget->EnsureCustomStates().RemoveElement(atom);
72 if (presShell) {
73 presShell->CustomStateChanged(*mTarget, atom);
75 return deleted;
78 void CustomStateSet::Add(const nsAString& aState, ErrorResult& aRv) {
79 CustomStateSet_Binding::SetlikeHelpers::Add(this, aState, aRv);
80 if (aRv.Failed()) {
81 return;
84 nsTArray<RefPtr<nsAtom>>& states = mTarget->EnsureCustomStates();
85 RefPtr<nsAtom> atom = NS_AtomizeMainThread(aState);
86 if (states.Contains(atom)) {
87 return;
90 Document* doc = mTarget->GetComposedDoc();
91 PresShell* presShell = doc ? doc->GetPresShell() : nullptr;
92 if (presShell) {
93 presShell->CustomStatesWillChange(*mTarget);
96 states.AppendElement(atom);
98 if (presShell) {
99 presShell->CustomStateChanged(*mTarget, atom);
103 } // namespace mozilla::dom