2
* Copyright (c) 2002-2009 Gargoyle Software Inc.
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
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.
15
package com.gargoylesoftware.htmlunit.javascript.host.html;
17
import java.util.HashSet;
20
import org.xml.sax.helpers.AttributesImpl;
22
import com.gargoylesoftware.htmlunit.html.DomElement;
23
import com.gargoylesoftware.htmlunit.html.DomText;
24
import com.gargoylesoftware.htmlunit.html.HTMLParser;
25
import com.gargoylesoftware.htmlunit.html.HtmlOption;
26
import com.gargoylesoftware.htmlunit.html.HtmlPage;
27
import com.gargoylesoftware.htmlunit.javascript.host.Attr;
28
import com.gargoylesoftware.htmlunit.javascript.host.FormChild;
31
* The JavaScript object that represents an option.
33
* @version $Revision: 4649 $
34
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
35
* @author David K. Taylor
36
* @author Chris Erskine
37
* @author Marc Guillemot
38
* @author Ahmed Ashour
40
public class HTMLOptionElement extends FormChild {
42
private static final long serialVersionUID = 947015932373556314L;
44
private static final Set<String> namesIEAttributeAlwaysAvailable_ = new HashSet<String>();
47
final String[] names = {"id", "value", "selected"};
48
for (final String name : names) {
49
namesIEAttributeAlwaysAvailable_.add(name);
54
* Creates an instance.
56
public HTMLOptionElement() {
61
* JavaScript constructor. This must be declared in every JavaScript file because
62
* the rhino engine won't walk up the hierarchy looking for constructors.
63
* @param newText the text
64
* @param newValue the value
65
* @param defaultSelected Whether the option is initially selected
66
* @param selected the current selection state of the option
68
public void jsConstructor(final String newText, final String newValue,
69
final boolean defaultSelected, final boolean selected) {
70
final HtmlPage page = (HtmlPage) getWindow().getWebWindow().getEnclosedPage();
71
AttributesImpl attributes = null;
72
if (defaultSelected) {
73
attributes = new AttributesImpl();
74
attributes.addAttribute(null, "selected", "selected", null, "selected");
77
final HtmlOption htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement(
78
page, HtmlOption.TAG_NAME, attributes);
79
htmlOption.setSelected(selected);
80
setDomNode(htmlOption);
82
if (newText != null && !newText.equals("undefined")) {
83
htmlOption.appendChild(new DomText(page, newText));
85
if (newValue != null && !newValue.equals("undefined")) {
86
htmlOption.setValueAttribute(newValue);
91
* Returns the value of the "value" property.
92
* @return the value property
94
public String jsxGet_value() {
95
String value = getDomNodeOrNull().getAttribute("value");
96
if (value == DomElement.ATTRIBUTE_NOT_DEFINED && getBrowserVersion().isFirefox()) {
97
value = getDomNodeOrNull().getText();
103
* Sets the value of the "value" property.
104
* @param newValue the value property
106
public void jsxSet_value(final String newValue) {
107
getDomNodeOrNull().setValueAttribute(newValue);
111
* Returns the value of the "text" property.
112
* @return the text property
115
public String jsxGet_text() {
116
return getDomNodeOrNull().getText();
123
public HtmlOption getDomNodeOrNull() {
124
return (HtmlOption) super.getDomNodeOrNull();
128
* Sets the value of the "text" property.
129
* @param newText the text property
131
public void jsxSet_text(final String newText) {
132
getDomNodeOrNull().setText(newText);
136
* Returns the value of the "selected" property.
137
* @return the text property
139
public boolean jsxGet_selected() {
140
return getDomNodeOrNull().isSelected();
144
* Sets the value of the "selected" property.
145
* @param selected the new selected property
147
public void jsxSet_selected(final boolean selected) {
148
getDomNodeOrNull().setSelected(selected);
152
* Returns the value of the "defaultSelected" property.
153
* @return the text property
155
public boolean jsxGet_defaultSelected() {
156
return getDomNodeOrNull().isDefaultSelected();
160
* Returns the value of the "label" property.
161
* @return the label property
163
public String jsxGet_label() {
164
return getDomNodeOrNull().getLabelAttribute();
168
* Sets the value of the "label" property.
169
* @param label the new label property
171
public void jsxSet_label(final String label) {
172
getDomNodeOrNull().setLabelAttribute(label);
179
public Object jsxFunction_getAttributeNode(final String attributeName) {
180
final Object response = super.jsxFunction_getAttributeNode(attributeName);
181
if (response == null && getBrowserVersion().isIE()
182
&& namesIEAttributeAlwaysAvailable_.contains(attributeName)) {
183
final Attr att = new Attr();
184
att.setPrototype(getPrototype(Attr.class));
185
att.setParentScope(getWindow());
186
att.init(attributeName, getDomNodeOrDie());