File: HTMLDetailsElement.cpp

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (265 lines) | stat: -rw-r--r-- 8,272 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* -*- 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/. */

#include "mozilla/dom/HTMLDetailsElement.h"

#include "mozilla/BuiltInStyleSheets.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/dom/HTMLDetailsElementBinding.h"
#include "mozilla/dom/HTMLSummaryElement.h"
#include "mozilla/dom/ShadowRoot.h"
#include "nsContentUtils.h"
#include "nsTextNode.h"

NS_IMPL_NS_NEW_HTML_ELEMENT(Details)

namespace mozilla::dom {

HTMLDetailsElement::~HTMLDetailsElement() = default;

NS_IMPL_ELEMENT_CLONE(HTMLDetailsElement)

HTMLDetailsElement::HTMLDetailsElement(already_AddRefed<NodeInfo>&& aNodeInfo)
    : nsGenericHTMLElement(std::move(aNodeInfo)) {
  SetupShadowTree();
}

HTMLSummaryElement* HTMLDetailsElement::GetFirstSummary() const {
  // XXX: Bug 1245032: Might want to cache the first summary element.
  for (nsIContent* child = nsINode::GetFirstChild(); child;
       child = child->GetNextSibling()) {
    if (auto* summary = HTMLSummaryElement::FromNode(child)) {
      return summary;
    }
  }
  return nullptr;
}

void HTMLDetailsElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
                                      const nsAttrValue* aValue,
                                      const nsAttrValue* aOldValue,
                                      nsIPrincipal* aMaybeScriptedPrincipal,
                                      bool aNotify) {
  if (aNameSpaceID == kNameSpaceID_None) {
    if (aName == nsGkAtoms::open) {
      bool wasOpen = !!aOldValue;
      bool isOpen = !!aValue;
      if (wasOpen != isOpen) {
        auto stringForState = [](bool aOpen) {
          return aOpen ? u"open"_ns : u"closed"_ns;
        };
        nsAutoString oldState;
        if (mToggleEventDispatcher) {
          oldState.Truncate();
          static_cast<ToggleEvent*>(mToggleEventDispatcher->mEvent.get())
              ->GetOldState(oldState);
          mToggleEventDispatcher->Cancel();
        } else {
          oldState.Assign(stringForState(wasOpen));
        }
        RefPtr<ToggleEvent> toggleEvent = CreateToggleEvent(
            u"toggle"_ns, oldState, stringForState(isOpen), Cancelable::eNo);
        mToggleEventDispatcher =
            new AsyncEventDispatcher(this, toggleEvent.forget());
        mToggleEventDispatcher->PostDOMEvent();

        if (isOpen) {
          CloseOtherElementsIfNeeded();
        }
        SetStates(ElementState::OPEN, isOpen);
      }
    } else if (aName == nsGkAtoms::name) {
      CloseElementIfNeeded();
    }
  }

  return nsGenericHTMLElement::AfterSetAttr(
      aNameSpaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
}

nsresult HTMLDetailsElement::BindToTree(BindContext& aContext,
                                        nsINode& aParent) {
  nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
  NS_ENSURE_SUCCESS(rv, rv);

  CloseElementIfNeeded();

  return NS_OK;
}

void HTMLDetailsElement::SetupShadowTree() {
  const bool kNotify = false;
  AttachAndSetUAShadowRoot(NotifyUAWidgetSetup::No);
  RefPtr<ShadowRoot> sr = GetShadowRoot();
  if (NS_WARN_IF(!sr)) {
    return;
  }

  nsNodeInfoManager* nim = OwnerDoc()->NodeInfoManager();
  RefPtr<NodeInfo> slotNodeInfo = nim->GetNodeInfo(
      nsGkAtoms::slot, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);
  sr->AppendBuiltInStyleSheet(BuiltInStyleSheet::Details);
  {
    RefPtr<nsGenericHTMLElement> slot =
        NS_NewHTMLSlotElement(do_AddRef(slotNodeInfo));
    if (NS_WARN_IF(!slot)) {
      return;
    }
    slot->SetAttr(kNameSpaceID_None, nsGkAtoms::name,
                  u"internal-main-summary"_ns, kNotify);
    sr->AppendChildTo(slot, kNotify, IgnoreErrors());

    RefPtr<NodeInfo> summaryNodeInfo = nim->GetNodeInfo(
        nsGkAtoms::summary, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);
    RefPtr<nsGenericHTMLElement> summary =
        NS_NewHTMLSummaryElement(summaryNodeInfo.forget());
    if (NS_WARN_IF(!summary)) {
      return;
    }

    nsAutoString defaultSummaryText;
    nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                            "DefaultSummary", OwnerDoc(),
                                            defaultSummaryText);
    RefPtr<nsTextNode> description = new (nim) nsTextNode(nim);
    description->SetText(defaultSummaryText, kNotify);
    summary->AppendChildTo(description, kNotify, IgnoreErrors());

    slot->AppendChildTo(summary, kNotify, IgnoreErrors());
  }
  {
    RefPtr<nsGenericHTMLElement> slot =
        NS_NewHTMLSlotElement(slotNodeInfo.forget());
    if (NS_WARN_IF(!slot)) {
      return;
    }
    if (StaticPrefs::layout_css_details_content_enabled()) {
      slot->SetPseudoElementType(PseudoStyleType::detailsContent);
    }
    sr->AppendChildTo(slot, kNotify, IgnoreErrors());
  }
}

void HTMLDetailsElement::AsyncEventRunning(AsyncEventDispatcher* aEvent) {
  if (mToggleEventDispatcher == aEvent) {
    mToggleEventDispatcher = nullptr;
  }
}

JSObject* HTMLDetailsElement::WrapNode(JSContext* aCx,
                                       JS::Handle<JSObject*> aGivenProto) {
  return HTMLDetailsElement_Binding::Wrap(aCx, this, aGivenProto);
}

bool HTMLDetailsElement::IsValidCommandAction(Command aCommand) const {
  return nsGenericHTMLElement::IsValidCommandAction(aCommand) ||
         (StaticPrefs::dom_element_commandfor_on_details_enabled() &&
          (aCommand == Command::Toggle || aCommand == Command::Close ||
           aCommand == Command::Open));
}

bool HTMLDetailsElement::HandleCommandInternal(Element* aSource,
                                               Command aCommand,
                                               ErrorResult& aRv) {
  if (nsGenericHTMLElement::HandleCommandInternal(aSource, aCommand, aRv)) {
    return true;
  }

  MOZ_ASSERT(StaticPrefs::dom_element_commandfor_on_details_enabled());
  if (aCommand == Command::Toggle) {
    ToggleOpen();
    return true;
  }
  if (aCommand == Command::Close) {
    if (Open()) {
      SetOpen(false, IgnoreErrors());
    }
    return true;
  }
  if (aCommand == Command::Open) {
    if (!Open()) {
      SetOpen(true, IgnoreErrors());
    }
    return true;
  }

  return false;
}

void HTMLDetailsElement::CloseElementIfNeeded() {
  if (!StaticPrefs::dom_details_group_enabled()) {
    return;
  }

  if (!Open()) {
    return;
  }

  if (!HasName()) {
    return;
  }

  RefPtr<nsAtom> name = GetParsedAttr(nsGkAtoms::name)->GetAsAtom();

  RefPtr<Document> doc = OwnerDoc();
  bool oldFlag = doc->FireMutationEvents();
  doc->SetFireMutationEvents(false);

  nsINode* root = SubtreeRoot();
  for (nsINode* cur = root; cur; cur = cur->GetNextNode(root)) {
    if (!cur->HasName()) {
      continue;
    }
    if (auto* other = HTMLDetailsElement::FromNode(cur)) {
      if (other != this && other->Open() &&
          other->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, name,
                             eCaseMatters)) {
        SetOpen(false, IgnoreErrors());
        break;
      }
    }
  }

  doc->SetFireMutationEvents(oldFlag);
}

void HTMLDetailsElement::CloseOtherElementsIfNeeded() {
  if (!StaticPrefs::dom_details_group_enabled()) {
    return;
  }

  MOZ_ASSERT(Open());

  if (!HasName()) {
    return;
  }

  RefPtr<nsAtom> name = GetParsedAttr(nsGkAtoms::name)->GetAsAtom();

  RefPtr<Document> doc = OwnerDoc();
  bool oldFlag = doc->FireMutationEvents();
  doc->SetFireMutationEvents(false);

  nsINode* root = SubtreeRoot();
  for (nsINode* cur = root; cur; cur = cur->GetNextNode(root)) {
    if (!cur->HasName()) {
      continue;
    }
    if (auto* other = HTMLDetailsElement::FromNode(cur)) {
      if (other != this && other->Open() &&
          other->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, name,
                             eCaseMatters)) {
        RefPtr<HTMLDetailsElement> otherDetails = other;
        otherDetails->SetOpen(false, IgnoreErrors());
        break;
      }
    }
  }

  doc->SetFireMutationEvents(oldFlag);
}

}  // namespace mozilla::dom