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