~testplan-team/testplan/source-collection

« back to all changes in this revision

Viewing changes to htmlunit-2.6/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLOptionElement.java

  • Committer: edA-qa mort-ora-y
  • Date: 2010-04-07 10:54:57 UTC
  • Revision ID: eda-qa@disemia.com-20100407105457-g46bvbsrjqtjujab
updating hmltunit src

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2002-2009 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 java.util.HashSet;
 
18
import java.util.Set;
 
19
 
 
20
import org.xml.sax.helpers.AttributesImpl;
 
21
 
 
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;
 
29
 
 
30
/**
 
31
 * The JavaScript object that represents an option.
 
32
 *
 
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
 
39
 */
 
40
public class HTMLOptionElement extends FormChild {
 
41
 
 
42
    private static final long serialVersionUID = 947015932373556314L;
 
43
 
 
44
    private static final Set<String> namesIEAttributeAlwaysAvailable_ = new HashSet<String>();
 
45
 
 
46
    static {
 
47
        final String[] names = {"id", "value", "selected"};
 
48
        for (final String name : names) {
 
49
            namesIEAttributeAlwaysAvailable_.add(name);
 
50
        }
 
51
    }
 
52
 
 
53
    /**
 
54
     * Creates an instance.
 
55
     */
 
56
    public HTMLOptionElement() {
 
57
        // Empty.
 
58
    }
 
59
 
 
60
    /**
 
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
 
67
     */
 
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");
 
75
        }
 
76
 
 
77
        final HtmlOption htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement(
 
78
                page, HtmlOption.TAG_NAME, attributes);
 
79
        htmlOption.setSelected(selected);
 
80
        setDomNode(htmlOption);
 
81
 
 
82
        if (newText != null && !newText.equals("undefined")) {
 
83
            htmlOption.appendChild(new DomText(page, newText));
 
84
        }
 
85
        if (newValue != null && !newValue.equals("undefined")) {
 
86
            htmlOption.setValueAttribute(newValue);
 
87
        }
 
88
    }
 
89
 
 
90
    /**
 
91
     * Returns the value of the "value" property.
 
92
     * @return the value property
 
93
     */
 
94
    public String jsxGet_value() {
 
95
        String value = getDomNodeOrNull().getAttribute("value");
 
96
        if (value == DomElement.ATTRIBUTE_NOT_DEFINED && getBrowserVersion().isFirefox()) {
 
97
            value = getDomNodeOrNull().getText();
 
98
        }
 
99
        return value;
 
100
    }
 
101
 
 
102
    /**
 
103
     * Sets the value of the "value" property.
 
104
     * @param newValue the value property
 
105
     */
 
106
    public void jsxSet_value(final String newValue) {
 
107
        getDomNodeOrNull().setValueAttribute(newValue);
 
108
    }
 
109
 
 
110
    /**
 
111
     * Returns the value of the "text" property.
 
112
     * @return the text property
 
113
     */
 
114
    @Override
 
115
    public String jsxGet_text() {
 
116
        return getDomNodeOrNull().getText();
 
117
    }
 
118
 
 
119
    /**
 
120
     * {@inheritDoc}
 
121
     */
 
122
    @Override
 
123
    public HtmlOption getDomNodeOrNull() {
 
124
        return (HtmlOption) super.getDomNodeOrNull();
 
125
    }
 
126
 
 
127
    /**
 
128
     * Sets the value of the "text" property.
 
129
     * @param newText the text property
 
130
     */
 
131
    public void jsxSet_text(final String newText) {
 
132
        getDomNodeOrNull().setText(newText);
 
133
    }
 
134
 
 
135
    /**
 
136
     * Returns the value of the "selected" property.
 
137
     * @return the text property
 
138
     */
 
139
    public boolean jsxGet_selected() {
 
140
        return getDomNodeOrNull().isSelected();
 
141
    }
 
142
 
 
143
    /**
 
144
     * Sets the value of the "selected" property.
 
145
     * @param selected the new selected property
 
146
     */
 
147
    public void jsxSet_selected(final boolean selected) {
 
148
        getDomNodeOrNull().setSelected(selected);
 
149
    }
 
150
 
 
151
    /**
 
152
     * Returns the value of the "defaultSelected" property.
 
153
     * @return the text property
 
154
     */
 
155
    public boolean jsxGet_defaultSelected() {
 
156
        return getDomNodeOrNull().isDefaultSelected();
 
157
    }
 
158
 
 
159
    /**
 
160
     * Returns the value of the "label" property.
 
161
     * @return the label property
 
162
     */
 
163
    public String jsxGet_label() {
 
164
        return getDomNodeOrNull().getLabelAttribute();
 
165
    }
 
166
 
 
167
    /**
 
168
     * Sets the value of the "label" property.
 
169
     * @param label the new label property
 
170
     */
 
171
    public void jsxSet_label(final String label) {
 
172
        getDomNodeOrNull().setLabelAttribute(label);
 
173
    }
 
174
 
 
175
    /**
 
176
     * {@inheritDoc}
 
177
     */
 
178
    @Override
 
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());
 
187
            return att;
 
188
        }
 
189
 
 
190
        return response;
 
191
    }
 
192
}