View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.html;
16  
17  import org.htmlunit.html.HtmlDialog;
18  import org.htmlunit.javascript.JavaScriptEngine;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxFunction;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  import org.htmlunit.javascript.configuration.JsxSetter;
24  
25  /**
26   * The JavaScript object {@code HTMLDialogElement}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   *
31   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement">MDN Documentation</a>
32   */
33  @JsxClass(domClass = HtmlDialog.class)
34  public class HTMLDialogElement extends HTMLElement {
35  
36      /**
37       * JavaScript constructor.
38       */
39      @Override
40      @JsxConstructor
41      public void jsConstructor() {
42          super.jsConstructor();
43      }
44  
45      /**
46       * Returns the {@code open} property.
47       * @return the {@code open} property
48       */
49      @JsxGetter
50      public boolean isOpen() {
51          return ((HtmlDialog) getDomNodeOrDie()).isOpen();
52      }
53  
54      /**
55       * Sets the {@code open} property.
56       * @param newValue the new value to set
57       */
58      @JsxSetter
59      public void setOpen(final Object newValue) {
60          final boolean bool = JavaScriptEngine.toBoolean(newValue);
61  
62          ((HtmlDialog) getDomNodeOrDie()).setOpen(bool);
63      }
64  
65      /**
66       * Displays the dialog non-modally.
67       */
68      @JsxFunction
69      public void show() {
70          final HtmlDialog dialog = (HtmlDialog) getDomNodeOrDie();
71  
72          if (dialog.isOpen()) {
73              if (dialog.isModal()) {
74                  throw JavaScriptEngine.reportRuntimeError("InvalidStateError: Dialog is already open.");
75              }
76          }
77  
78          dialog.show();
79      }
80  
81      /**
82       * Closes the dialog.
83       * @param returnValue the return value
84       */
85      @JsxFunction
86      public void close(final Object returnValue) {
87          if (returnValue == null || JavaScriptEngine.isUndefined(returnValue)) {
88              ((HtmlDialog) getDomNodeOrDie()).close("");
89          }
90  
91          ((HtmlDialog) getDomNodeOrDie()).close(JavaScriptEngine.toString(returnValue));
92      }
93  
94      /**
95       * Displays the dialog modally.
96       */
97      @JsxFunction
98      public void showModal() {
99          final HtmlDialog dialog = (HtmlDialog) getDomNodeOrDie();
100 
101         if (dialog.isOpen()) {
102             if (!dialog.isModal()) {
103                 throw JavaScriptEngine.reportRuntimeError("InvalidStateError: Dialog is already open.");
104             }
105         }
106 
107         dialog.showModal();
108     }
109 
110     /**
111      * Returns the {@code returnValue} property.
112      * @return the {@code returnValue} property
113      */
114     @JsxGetter
115     public String getReturnValue() {
116         return ((HtmlDialog) getDomNodeOrDie()).getReturnValue();
117     }
118 
119     /**
120      * Sets the {@code returnValue} property.
121      * @param newValue the new value to set
122      */
123     @JsxSetter
124     public void setReturnValue(final Object newValue) {
125         if (newValue == null || JavaScriptEngine.isUndefined(newValue)) {
126             ((HtmlDialog) getDomNodeOrDie()).setReturnValue("");
127         }
128 
129         ((HtmlDialog) getDomNodeOrDie()).setReturnValue(JavaScriptEngine.toString(newValue));
130     }
131 }