All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gargoylesoftware.htmlunit.html.HtmlInput Maven / Gradle / Ivy

There is a newer version: 2.70.0
Show newest version
/*
 * Copyright (c) 2002-2011 Gargoyle Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gargoylesoftware.htmlunit.html;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;

import com.gargoylesoftware.htmlunit.BrowserVersionFeatures;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.WebAssert;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import com.gargoylesoftware.htmlunit.javascript.host.Event;
import com.gargoylesoftware.htmlunit.util.NameValuePair;

/**
 * Wrapper for the HTML element "input".
 *
 * @version $Revision: 6204 $
 * @author Mike Bowler
 * @author David K. Taylor
 * @author Christian Sell
 * @author David D. Kilzer
 * @author Marc Guillemot
 * @author Daniel Gredler
 * @author Ahmed Ashour
 */
public abstract class HtmlInput extends HtmlElement implements DisabledElement, SubmittableElement,
    FormFieldWithNameHistory {
    /** The HTML tag represented by this element. */
    public static final String TAG_NAME = "input";

    private String defaultValue_;
    private String originalName_;
    private Collection previousNames_ = Collections.emptySet();

    /**
     * Creates an instance.
     *
     * @param page the page that contains this element
     * @param attributes the initial attributes
     */
    public HtmlInput(final SgmlPage page, final Map attributes) {
        this(null, TAG_NAME, page, attributes);
    }

    /**
     * Creates an instance.
     *
     * @param namespaceURI the URI that identifies an XML namespace
     * @param qualifiedName the qualified name of the element type to instantiate
     * @param page the page that contains this element
     * @param attributes the initial attributes
     */
    public HtmlInput(final String namespaceURI, final String qualifiedName, final SgmlPage page,
            final Map attributes) {
        super(namespaceURI, qualifiedName, page, attributes);
        defaultValue_ = getValueAttribute();
        originalName_ = getNameAttribute();
    }

    /**
     * Sets the content of the "value" attribute, executing onchange handlers if appropriate.
     * This method returns the page contained by this element's window after the value is set,
     * which may or may not be the same as the original page.
     * @param newValue the new content
     * @return the page contained by this element's window after the value is set
     */
    public Page setValueAttribute(final String newValue) {
        WebAssert.notNull("newValue", newValue);
        setAttribute("value", newValue);

        return executeOnChangeHandlerIfAppropriate(this);
    }

    /**
     * {@inheritDoc}
     */
    public NameValuePair[] getSubmitKeyValuePairs() {
        return new NameValuePair[]{new NameValuePair(getNameAttribute(), getValueAttribute())};
    }

    /**
     * Returns the value of the attribute "type". Refer to the
     * HTML 4.01
     * documentation for details on the use of this attribute.
     *
     * @return the value of the attribute "type" or an empty string if that attribute isn't defined
     */
    public final String getTypeAttribute() {
        return getAttribute("type");
    }

    /**
     * Returns the value of the attribute "name". Refer to the
     * HTML 4.01
     * documentation for details on the use of this attribute.
     *
     * @return the value of the attribute "name" or an empty string if that attribute isn't defined
     */
    public final String getNameAttribute() {
        return getAttribute("name");
    }

    /**
     * 

Return the value of the attribute "value". Refer to the * HTML 4.01 * documentation for details on the use of this attribute.

* * @return the value of the attribute "value" or an empty string if that attribute isn't defined */ public final String getValueAttribute() { return getAttribute("value"); } /** * Returns the value of the attribute "checked". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "checked" or an empty string if that attribute isn't defined */ public final String getCheckedAttribute() { return getAttribute("checked"); } /** * {@inheritDoc} */ public final String getDisabledAttribute() { return getAttribute("disabled"); } /** * {@inheritDoc} */ public final boolean isDisabled() { return hasAttribute("disabled"); } /** * Returns the value of the attribute "readonly". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "readonly" * or an empty string if that attribute isn't defined. */ public final String getReadOnlyAttribute() { return getAttribute("readonly"); } /** * Returns the value of the attribute "size". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "size" * or an empty string if that attribute isn't defined. */ public final String getSizeAttribute() { return getAttribute("size"); } /** * Returns the value of the attribute "maxlength". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "maxlength" * or an empty string if that attribute isn't defined. */ public final String getMaxLengthAttribute() { return getAttribute("maxlength"); } /** * Gets the max length if defined, Integer.MAX_VALUE if none. * @return the max length */ protected int getMaxLength() { final String maxLength = getMaxLengthAttribute(); if (maxLength.length() == 0) { return Integer.MAX_VALUE; } try { return Integer.parseInt(maxLength.trim()); } catch (final NumberFormatException e) { return Integer.MAX_VALUE; } } /** * Returns the value of the attribute "src". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "src" * or an empty string if that attribute isn't defined. */ public final String getSrcAttribute() { return getAttribute("src"); } /** * Returns the value of the attribute "alt". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "alt" * or an empty string if that attribute isn't defined. */ public final String getAltAttribute() { return getAttribute("alt"); } /** * Returns the value of the attribute "usemap". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "usemap" * or an empty string if that attribute isn't defined. */ public final String getUseMapAttribute() { return getAttribute("usemap"); } /** * Returns the value of the attribute "tabindex". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "tabindex" * or an empty string if that attribute isn't defined. */ public final String getTabIndexAttribute() { return getAttribute("tabindex"); } /** * Returns the value of the attribute "accesskey". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "accesskey" * or an empty string if that attribute isn't defined. */ public final String getAccessKeyAttribute() { return getAttribute("accesskey"); } /** * Returns the value of the attribute "onfocus". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "onfocus" * or an empty string if that attribute isn't defined. */ public final String getOnFocusAttribute() { return getAttribute("onfocus"); } /** * Returns the value of the attribute "onblur". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "onblur" * or an empty string if that attribute isn't defined. */ public final String getOnBlurAttribute() { return getAttribute("onblur"); } /** * Returns the value of the attribute "onselect". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "onselect" * or an empty string if that attribute isn't defined. */ public final String getOnSelectAttribute() { return getAttribute("onselect"); } /** * Returns the value of the attribute "onchange". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "onchange" * or an empty string if that attribute isn't defined. */ public final String getOnChangeAttribute() { return getAttribute("onchange"); } /** * Returns the value of the attribute "accept". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "accept" * or an empty string if that attribute isn't defined. */ public final String getAcceptAttribute() { return getAttribute("accept"); } /** * Returns the value of the attribute "align". Refer to the * HTML 4.01 * documentation for details on the use of this attribute. * * @return the value of the attribute "align" * or an empty string if that attribute isn't defined. */ public final String getAlignAttribute() { return getAttribute("align"); } /** * {@inheritDoc} * @see SubmittableElement#reset() */ public void reset() { setValueAttribute(defaultValue_); } /** * {@inheritDoc} Also sets the value attribute when emulating Netscape browsers. * @see SubmittableElement#setDefaultValue(String) * @see HtmlFileInput#setDefaultValue(String) */ public void setDefaultValue(final String defaultValue) { final boolean modifyValue = getPage().getWebClient().getBrowserVersion() .hasFeature(BrowserVersionFeatures.HTMLINPUT_DEFAULT_IS_CHECKED); setDefaultValue(defaultValue, modifyValue); } /** * Sets the default value, optionally also modifying the current value. * @param defaultValue the new default value * @param modifyValue Whether or not to set the current value to the default value */ protected void setDefaultValue(final String defaultValue, final boolean modifyValue) { defaultValue_ = defaultValue; if (modifyValue) { setValueAttribute(defaultValue); } } /** * {@inheritDoc} * @see SubmittableElement#getDefaultValue() */ public String getDefaultValue() { return defaultValue_; } /** * {@inheritDoc} The default implementation is empty; only checkboxes and radio buttons * really care what the default checked value is. * @see SubmittableElement#setDefaultChecked(boolean) * @see HtmlRadioButtonInput#setDefaultChecked(boolean) * @see HtmlCheckBoxInput#setDefaultChecked(boolean) */ public void setDefaultChecked(final boolean defaultChecked) { // Empty. } /** * {@inheritDoc} The default implementation returns false; only checkboxes and * radio buttons really care what the default checked value is. * @see SubmittableElement#isDefaultChecked() * @see HtmlRadioButtonInput#isDefaultChecked() * @see HtmlCheckBoxInput#isDefaultChecked() */ public boolean isDefaultChecked() { return false; } /** * Sets the "checked" attribute, returning the page that occupies this input's window after setting * the attribute. Note that the returned page may or may not be the original page, depending on * the presence of JavaScript event handlers, etc. * * @param isChecked true if this element is to be selected * @return the page that occupies this input's window after setting the attribute */ public Page setChecked(final boolean isChecked) { // By default this returns the current page. Derived classes will override. return getPage(); } /** * Sets the "readOnly" attribute. * * @param isReadOnly true if this element is read only */ public void setReadOnly(final boolean isReadOnly) { if (isReadOnly) { setAttribute("readOnly", "readOnly"); } else { removeAttribute("readOnly"); } } /** * Returns true if this element is currently selected. * @return true if this element is currently selected */ public boolean isChecked() { return hasAttribute("checked"); } /** * Returns true if this element is read only. * @return true if this element is read only */ public boolean isReadOnly() { return hasAttribute("readOnly"); } /** * Simulate clicking this input with a pointing device. The x and y coordinates * of the pointing device will be sent to the server. * * @param

the page type * @param x the x coordinate of the pointing device at the time of clicking * @param y the y coordinate of the pointing device at the time of clicking * @return the page that is loaded after the click has taken place * @exception IOException If an io error occurs * @exception ElementNotFoundException If a particular XML element could not be found in the DOM model */ @SuppressWarnings("unchecked") public

P click(final int x, final int y) throws IOException, ElementNotFoundException { // By default this is no different than a click without coordinates. return (P) click(); } /** * Executes the onchange script code for this element if this is appropriate. * This means that the element must have an onchange script, script must be enabled * and the change in the element must not have been triggered by a script. * * @param htmlElement the element that contains the onchange attribute * @return the page that occupies this window after this method completes (may or * may not be the same as the original page) */ static Page executeOnChangeHandlerIfAppropriate(final HtmlElement htmlElement) { final SgmlPage page = htmlElement.getPage(); final JavaScriptEngine engine = htmlElement.getPage().getWebClient().getJavaScriptEngine(); if (engine.isScriptRunning()) { return page; } final ScriptResult scriptResult = htmlElement.fireEvent(Event.TYPE_CHANGE); if (page.getWebClient().getWebWindows().contains(page.getEnclosingWindow())) { return page.getEnclosingWindow().getEnclosedPage(); // may be itself or a newly loaded one } else if (scriptResult != null) { // current window doesn't exist anymore return scriptResult.getNewPage(); } return page; } /** * {@inheritDoc} */ @Override public void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue) { if ("name".equals(qualifiedName)) { if (previousNames_.isEmpty()) { previousNames_ = new HashSet(); } previousNames_.add(attributeValue); } super.setAttributeNS(namespaceURI, qualifiedName, attributeValue); } /** * {@inheritDoc} */ public String getOriginalName() { return originalName_; } /** * {@inheritDoc} */ public Collection getPreviousNames() { return previousNames_; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy