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 static org.htmlunit.BrowserVersionFeatures.JS_MENU_TYPE_PASS;
18  
19  import org.htmlunit.html.DomElement;
20  import org.htmlunit.html.HtmlMenu;
21  import org.htmlunit.javascript.JavaScriptEngine;
22  import org.htmlunit.javascript.configuration.JsxClass;
23  import org.htmlunit.javascript.configuration.JsxConstructor;
24  import org.htmlunit.javascript.configuration.JsxGetter;
25  import org.htmlunit.javascript.configuration.JsxSetter;
26  
27  /**
28   * The JavaScript object {@code HTMLMenuElement}.
29   *
30   * @author Ahmed Ashour
31   * @author Frank Danek
32   * @author Ronald Brill
33   *
34   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLMenuElement">MDN Documentation</a>
35   */
36  @JsxClass(domClass = HtmlMenu.class)
37  public class HTMLMenuElement extends HTMLElement {
38  
39      /**
40       * JavaScript constructor.
41       */
42      @Override
43      @JsxConstructor
44      public void jsConstructor() {
45          super.jsConstructor();
46      }
47  
48      /**
49       * Returns the value of the {@code type} property.
50       * @return the value of the {@code type} property
51       */
52      protected String getType() {
53          final String type = getDomNodeOrDie().getAttributeDirect("type");
54          if (getBrowserVersion().hasFeature(JS_MENU_TYPE_PASS)) {
55              return type;
56          }
57  
58          if ("context".equalsIgnoreCase(type)) {
59              return "context";
60          }
61          if ("toolbar".equalsIgnoreCase(type)) {
62              return "toolbar";
63          }
64  
65          return "list";
66      }
67  
68      /**
69       * Sets the value of the {@code type} property.
70       * @param type the value of the {@code type} property
71       */
72      protected void setType(final String type) {
73          if (getBrowserVersion().hasFeature(JS_MENU_TYPE_PASS)) {
74              getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, type);
75              return;
76          }
77  
78          if ("context".equalsIgnoreCase(type)) {
79              getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, "context");
80              return;
81          }
82          if ("toolbar".equalsIgnoreCase(type)) {
83              getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, "toolbar");
84              return;
85          }
86  
87          getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, "list");
88      }
89  
90      /**
91       * Returns the value of the {@code compact} attribute.
92       * @return the value of the {@code compact} attribute
93       */
94      @JsxGetter
95      public boolean isCompact() {
96          return getDomNodeOrDie().hasAttribute("compact");
97      }
98  
99      /**
100      * Sets the value of the {@code compact} attribute.
101      * @param compact the value of the {@code compact} attribute
102      */
103     @JsxSetter
104     public void setCompact(final Object compact) {
105         if (JavaScriptEngine.toBoolean(compact)) {
106             getDomNodeOrDie().setAttribute("compact", "");
107         }
108         else {
109             getDomNodeOrDie().removeAttribute("compact");
110         }
111     }
112 }