View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.html;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_MENU_TYPE_EMPTY;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
22  
23  import org.apache.commons.lang3.StringUtils;
24  
25  import com.gargoylesoftware.htmlunit.html.HtmlMenu;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
30  
31  import net.sourceforge.htmlunit.corejs.javascript.Context;
32  
33  /**
34   * The JavaScript object {@code HTMLMenuElement}.
35   *
36   * @author Ahmed Ashour
37   * @author Frank Danek
38   * @author Ronald Brill
39   */
40  @JsxClass(domClass = HtmlMenu.class)
41  public class HTMLMenuElement extends HTMLListElement {
42  
43      private String label_;
44  
45      /**
46       * Creates an instance.
47       */
48      @JsxConstructor({CHROME, FF, EDGE})
49      public HTMLMenuElement() {
50          label_ = "";
51      }
52  
53      /**
54       * Returns the value of the {@code type} property.
55       * @return the value of the {@code type} property
56       */
57      @Override
58      @JsxGetter({FF, IE})
59      public String getType() {
60          if (getBrowserVersion().hasFeature(JS_MENU_TYPE_EMPTY)) {
61              return "";
62          }
63  
64          final String type = getDomNodeOrDie().getAttribute("type");
65          if ("context".equalsIgnoreCase(type)) {
66              return "context";
67          }
68          if ("toolbar".equalsIgnoreCase(type)) {
69              return "toolbar";
70          }
71          return "list";
72      }
73  
74      /**
75       * Sets the value of the {@code type} property.
76       * @param type the value of the {@code type} property
77       */
78      @Override
79      @JsxSetter({FF, IE})
80      public void setType(final String type) {
81          if (getBrowserVersion().hasFeature(JS_MENU_TYPE_EMPTY)) {
82              if (StringUtils.isEmpty(type)) {
83                  return;
84              }
85              throw Context.reportRuntimeError("Cannot set the type property to invalid value: '" + type + "'");
86          }
87  
88          if ("context".equalsIgnoreCase(type)) {
89              getDomNodeOrDie().setAttribute("type", "context");
90              return;
91          }
92          if ("toolbar".equalsIgnoreCase(type)) {
93              getDomNodeOrDie().setAttribute("type", "toolbar");
94              return;
95          }
96  
97          getDomNodeOrDie().setAttribute("type", "list");
98      }
99  
100     /**
101      * Returns the value of the {@code label} property.
102      * @return the value of the {@code label} property
103      */
104     @JsxGetter(FF)
105     public String getLabel() {
106         return label_;
107     }
108 
109     /**
110      * Sets the value of the {@code label} property.
111      * @param label the value of the {@code label} property
112      */
113     @JsxSetter(FF)
114     public void setLabel(final String label) {
115         label_ = label;
116     }
117 }