View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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  @JsxClass(domClass = HtmlMenu.class)
35  public class HTMLMenuElement extends HTMLElement {
36  
37      /**
38       * JavaScript constructor.
39       */
40      @Override
41      @JsxConstructor
42      public void jsConstructor() {
43          super.jsConstructor();
44      }
45  
46      /**
47       * Returns the value of the {@code type} property.
48       * @return the value of the {@code type} property
49       */
50      protected String getType() {
51          final String type = getDomNodeOrDie().getAttributeDirect("type");
52          if (getBrowserVersion().hasFeature(JS_MENU_TYPE_PASS)) {
53              return type;
54          }
55  
56          if ("context".equalsIgnoreCase(type)) {
57              return "context";
58          }
59          if ("toolbar".equalsIgnoreCase(type)) {
60              return "toolbar";
61          }
62  
63          return "list";
64      }
65  
66      /**
67       * Sets the value of the {@code type} property.
68       * @param type the value of the {@code type} property
69       */
70      protected void setType(final String type) {
71          if (getBrowserVersion().hasFeature(JS_MENU_TYPE_PASS)) {
72              getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, type);
73              return;
74          }
75  
76          if ("context".equalsIgnoreCase(type)) {
77              getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, "context");
78              return;
79          }
80          if ("toolbar".equalsIgnoreCase(type)) {
81              getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, "toolbar");
82              return;
83          }
84  
85          getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, "list");
86      }
87  
88      /**
89       * Returns the value of the {@code compact} attribute.
90       * @return the value of the {@code compact} attribute
91       */
92      @JsxGetter
93      public boolean isCompact() {
94          return getDomNodeOrDie().hasAttribute("compact");
95      }
96  
97      /**
98       * Sets the value of the {@code compact} attribute.
99       * @param compact the value of the {@code compact} attribute
100      */
101     @JsxSetter
102     public void setCompact(final Object compact) {
103         if (JavaScriptEngine.toBoolean(compact)) {
104             getDomNodeOrDie().setAttribute("compact", "");
105         }
106         else {
107             getDomNodeOrDie().removeAttribute("compact");
108         }
109     }
110 }