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 org.htmlunit.html.DomElement;
18  import org.htmlunit.html.HtmlMeter;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxGetter;
22  import org.htmlunit.javascript.host.dom.NodeList;
23  
24  /**
25   * The JavaScript object {@code HTMLMeterElement}.
26   *
27   * @author Marc Guillemot
28   * @author Ronald Brill
29   * @author Ahmed Ashour
30   */
31  @JsxClass(domClass = HtmlMeter.class)
32  public class HTMLMeterElement extends HTMLElement {
33  
34      /** "Live" labels collection; has to be a member to have equality (==) working. */
35      private NodeList labels_;
36  
37      /**
38       * JavaScript constructor.
39       */
40      @Override
41      @JsxConstructor
42      public void jsConstructor() {
43          super.jsConstructor();
44      }
45  
46      /**
47       * The getter for the "value" property.
48       * @return the value
49       */
50      @JsxGetter
51      @Override
52      public Double getValue() {
53          return getAttributeAsDouble(DomElement.VALUE_ATTRIBUTE, 0);
54      }
55  
56      /**
57       * The getter for the "min" property.
58       * @return the value
59       */
60      @JsxGetter
61      public double getMin() {
62          return getAttributeAsDouble("min", 0);
63      }
64  
65      /**
66       * The getter for the "max" property.
67       * @return the value
68       */
69      @JsxGetter
70      public double getMax() {
71          return getAttributeAsDouble("max", 1);
72      }
73  
74      /**
75       * The getter for the "low" property.
76       * @return the value
77       */
78      @JsxGetter
79      public double getLow() {
80          final double val = getAttributeAsDouble("low", Double.MAX_VALUE);
81          if (val == Double.MAX_VALUE) {
82              return getMin();
83          }
84          return val;
85      }
86  
87      /**
88       * The getter for the "high" property.
89       * @return the value
90       */
91      @JsxGetter
92      public double getHigh() {
93          final double val = getAttributeAsDouble("high", Double.MIN_VALUE);
94          if (val == Double.MIN_VALUE) {
95              return getMax();
96          }
97          return val;
98      }
99  
100     /**
101      * The getter for the "optimum" property.
102      * @return the value
103      */
104     @JsxGetter
105     public double getOptimum() {
106         final double val = getAttributeAsDouble("optimum", Double.MAX_VALUE);
107         if (val == Double.MAX_VALUE) {
108             return getValue();
109         }
110         return val;
111     }
112 
113     private double getAttributeAsDouble(final String attributeName, final double defaultValue) {
114         try {
115             return Double.parseDouble(getDomNodeOrDie().getAttribute(attributeName));
116         }
117         catch (final NumberFormatException e) {
118             return defaultValue;
119         }
120     }
121 
122     /**
123      * Returns the labels associated with the element.
124      * @return the labels associated with the element
125      */
126     @JsxGetter
127     public NodeList getLabels() {
128         if (labels_ == null) {
129             labels_ = new LabelsNodeList(getDomNodeOrDie());
130         }
131         return labels_;
132     }
133 
134 }