Search in sources :

Example 1 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project java-design-patterns by iluwatar.

the class AlbumPage method changeAlbumYear.

/**
 * Selects the select's option value based on the year value given
 *
 * @param year the new year value to set
 * @return {@link AlbumPage}
 */
public AlbumPage changeAlbumYear(int year) {
    HtmlSelect albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
    HtmlOption yearOption = albumYearSelectOption.getOptionByValue(Integer.toString(year));
    albumYearSelectOption.setSelectedAttribute(yearOption, true);
    return this;
}
Also used : HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption)

Example 2 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.

the class HTMLOptionElement method jsConstructor.

/**
 * JavaScript constructor.
 * @param newText the text
 * @param newValue the value
 * @param defaultSelected Whether the option is initially selected
 * @param selected the current selection state of the option
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final Object newText, final String newValue, final boolean defaultSelected, final boolean selected) {
    final SgmlPage page = (SgmlPage) getWindow().getWebWindow().getEnclosedPage();
    AttributesImpl attributes = null;
    if (defaultSelected) {
        attributes = new AttributesImpl();
        attributes.addAttribute(null, "selected", "selected", null, "selected");
    }
    final HtmlOption htmlOption = (HtmlOption) page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME).createElement(page, HtmlOption.TAG_NAME, attributes);
    htmlOption.setSelected(selected);
    setDomNode(htmlOption);
    if (!Undefined.isUndefined(newText)) {
        final String newTextString = Context.toString(newText);
        htmlOption.appendChild(new DomText(page, newTextString));
        htmlOption.setLabelAttribute(newTextString);
    }
    if (!"undefined".equals(newValue)) {
        htmlOption.setValueAttribute(newValue);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) DomText(com.gargoylesoftware.htmlunit.html.DomText) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 3 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.

the class HTMLOptionElement method setSelected.

/**
 * Sets the value of the {@code selected} property.
 * @param selected the new selected property
 */
@JsxSetter
public void setSelected(final boolean selected) {
    final HtmlOption optionNode = (HtmlOption) getDomNodeOrNull();
    final HtmlSelect enclosingSelect = optionNode.getEnclosingSelect();
    if (!selected && optionNode.isSelected() && enclosingSelect != null && enclosingSelect.getSize() <= 1 && !enclosingSelect.isMultipleSelectEnabled()) {
        enclosingSelect.getOption(0).setSelectedFromJavaScript(true);
        return;
    }
    optionNode.setSelectedFromJavaScript(selected);
}
Also used : HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 4 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.

the class HTMLOptionsCollection method put.

/**
 * Sets the index property.
 * @param index the index
 * @param start the scriptable object that was originally invoked for this property
 * @param newValue the new value
 */
@Override
public void put(final int index, final Scriptable start, final Object newValue) {
    if (newValue == null) {
        // Remove the indexed option.
        htmlSelect_.removeOption(index);
    } else {
        final HTMLOptionElement option = (HTMLOptionElement) newValue;
        final HtmlOption htmlOption = (HtmlOption) option.getDomNodeOrNull();
        if (index >= getLength()) {
            setLength(index);
            // Add a new option at the end.
            htmlSelect_.appendOption(htmlOption);
        } else {
            // Replace the indexed option.
            htmlSelect_.replaceOption(index, htmlOption);
        }
    }
}
Also used : HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption)

Example 5 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.

the class HTMLOptionsCollection method setLength.

/**
 * Changes the number of options: removes options if the new length
 * is less than the current one else add new empty options to reach the
 * new length.
 * @param newLength the new length property value
 */
@JsxSetter
public void setLength(final int newLength) {
    if (newLength < 0) {
        if (getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_IGNORE_NEGATIVE_LENGTH)) {
            return;
        }
        throw Context.reportRuntimeError("Length is negative");
    }
    final int currentLength = htmlSelect_.getOptionSize();
    if (currentLength > newLength) {
        htmlSelect_.setOptionSize(newLength);
    } else {
        final SgmlPage page = htmlSelect_.getPage();
        final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME);
        for (int i = currentLength; i < newLength; i++) {
            final HtmlOption option = (HtmlOption) factory.createElement(page, HtmlOption.TAG_NAME, null);
            htmlSelect_.appendOption(option);
        }
    }
}
Also used : SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Aggregations

HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)17 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)8 HtmlSelect (com.gargoylesoftware.htmlunit.html.HtmlSelect)8 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)3 Test (org.junit.Test)3 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)2 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)2 HashSet (java.util.HashSet)2 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)1 CSSException (com.gargoylesoftware.css.parser.CSSException)1 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)1 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)1 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)1 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)1 ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)1 DisabledElement (com.gargoylesoftware.htmlunit.html.DisabledElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1