Bug 1554951 [wpt PR 17043] - wake-lock: Fix invalid types test in wakelock-type.https...
[gecko.git] / dom / html / HTMLDialogElement.cpp
blob48ebceceb3cee041351e1e4495d561a183c039c6
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/HTMLDialogElement.h"
8 #include "mozilla/dom/HTMLDialogElementBinding.h"
9 #include "mozilla/dom/HTMLUnknownElement.h"
10 #include "mozilla/StaticPrefs.h"
12 // Expand NS_IMPL_NS_NEW_HTML_ELEMENT(Dialog) with pref check
13 nsGenericHTMLElement* NS_NewHTMLDialogElement(
14 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
15 mozilla::dom::FromParser aFromParser) {
16 if (!mozilla::dom::HTMLDialogElement::IsDialogEnabled()) {
17 return new mozilla::dom::HTMLUnknownElement(std::move(aNodeInfo));
20 return new mozilla::dom::HTMLDialogElement(std::move(aNodeInfo));
23 namespace mozilla {
24 namespace dom {
26 HTMLDialogElement::~HTMLDialogElement() {}
28 NS_IMPL_ELEMENT_CLONE(HTMLDialogElement)
30 bool HTMLDialogElement::IsDialogEnabled() {
31 return StaticPrefs::dom_dialog_element_enabled();
34 void HTMLDialogElement::Close(
35 const mozilla::dom::Optional<nsAString>& aReturnValue) {
36 if (!Open()) {
37 return;
39 if (aReturnValue.WasPassed()) {
40 SetReturnValue(aReturnValue.Value());
42 ErrorResult ignored;
43 SetOpen(false, ignored);
44 ignored.SuppressException();
45 RefPtr<AsyncEventDispatcher> eventDispatcher = new AsyncEventDispatcher(
46 this, NS_LITERAL_STRING("close"), CanBubble::eNo);
47 eventDispatcher->PostDOMEvent();
50 void HTMLDialogElement::Show() {
51 if (Open()) {
52 return;
54 ErrorResult ignored;
55 SetOpen(true, ignored);
56 ignored.SuppressException();
59 void HTMLDialogElement::ShowModal(ErrorResult& aError) {
60 if (!IsInComposedDoc() || Open()) {
61 aError.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
62 return;
65 SetOpen(true, aError);
66 aError.SuppressException();
69 JSObject* HTMLDialogElement::WrapNode(JSContext* aCx,
70 JS::Handle<JSObject*> aGivenProto) {
71 return HTMLDialogElement_Binding::Wrap(aCx, this, aGivenProto);
74 } // namespace dom
75 } // namespace mozilla