use of com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput in project htmlunit by HtmlUnit.
the class HTMLInputElement method click.
/**
* {@inheritDoc}
*/
@Override
public void click() throws IOException {
final HtmlInput domNode = getDomNodeOrDie();
final boolean originalState = domNode.isChecked();
domNode.click(false, false, false, false, false, true, false);
final boolean newState = domNode.isChecked();
if (originalState != newState && (domNode instanceof HtmlRadioButtonInput || domNode instanceof HtmlCheckBoxInput)) {
domNode.fireEvent(Event.TYPE_CHANGE);
}
}
use of com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput in project htmlunit by HtmlUnit.
the class CSSStyleSheet method selectsPseudoClass.
private static boolean selectsPseudoClass(final BrowserVersion browserVersion, final Condition condition, final DomElement element, final boolean fromQuerySelectorAll) {
if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
final Object sobj = element.getPage().getScriptableObject();
if (sobj instanceof HTMLDocument && ((HTMLDocument) sobj).getDocumentMode() < 8) {
return false;
}
}
final String value = condition.getValue();
switch(value) {
case "root":
return element == element.getPage().getDocumentElement();
case "enabled":
return element instanceof DisabledElement && !((DisabledElement) element).isDisabled();
case "disabled":
return element instanceof DisabledElement && ((DisabledElement) element).isDisabled();
case "focus":
final HtmlPage htmlPage = element.getHtmlPageOrNull();
if (htmlPage != null) {
final DomElement focus = htmlPage.getFocusedElement();
return element == focus;
}
return false;
case "checked":
return (element instanceof HtmlCheckBoxInput && ((HtmlCheckBoxInput) element).isChecked()) || (element instanceof HtmlRadioButtonInput && ((HtmlRadioButtonInput) element).isChecked() || (element instanceof HtmlOption && ((HtmlOption) element).isSelected()));
case "required":
return element instanceof HtmlElement && ((HtmlElement) element).isRequired();
case "optional":
return element instanceof HtmlElement && ((HtmlElement) element).isOptional();
case "first-child":
for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
if (n instanceof DomElement) {
return false;
}
}
return true;
case "last-child":
for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
if (n instanceof DomElement) {
return false;
}
}
return true;
case "first-of-type":
final String firstType = element.getNodeName();
for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
if (n instanceof DomElement && n.getNodeName().equals(firstType)) {
return false;
}
}
return true;
case "last-of-type":
final String lastType = element.getNodeName();
for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
if (n instanceof DomElement && n.getNodeName().equals(lastType)) {
return false;
}
}
return true;
case "only-child":
for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
if (n instanceof DomElement) {
return false;
}
}
for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
if (n instanceof DomElement) {
return false;
}
}
return true;
case "only-of-type":
final String type = element.getNodeName();
for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
if (n instanceof DomElement && n.getNodeName().equals(type)) {
return false;
}
}
for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
if (n instanceof DomElement && n.getNodeName().equals(type)) {
return false;
}
}
return true;
case "valid":
return element instanceof HtmlElement && ((HtmlElement) element).isValid();
case "invalid":
return element instanceof HtmlElement && !((HtmlElement) element).isValid();
case "empty":
return isEmpty(element);
case "target":
final String ref = element.getPage().getUrl().getRef();
return StringUtils.isNotBlank(ref) && ref.equals(element.getId());
case "hover":
return element.isMouseOver();
case "placeholder-shown":
if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_PLACEHOLDER_SHOWN)) {
return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
}
case "-ms-input-placeholder":
if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_MS_PLACEHHOLDER)) {
return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
}
default:
if (value.startsWith("nth-child(")) {
final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
int index = 0;
for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
if (n instanceof DomElement) {
index++;
}
}
return getNth(nth, index);
} else if (value.startsWith("nth-last-child(")) {
final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
int index = 0;
for (DomNode n = element; n != null; n = n.getNextSibling()) {
if (n instanceof DomElement) {
index++;
}
}
return getNth(nth, index);
} else if (value.startsWith("nth-of-type(")) {
final String nthType = element.getNodeName();
final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
int index = 0;
for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
if (n instanceof DomElement && n.getNodeName().equals(nthType)) {
index++;
}
}
return getNth(nth, index);
} else if (value.startsWith("nth-last-of-type(")) {
final String nthLastType = element.getNodeName();
final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
int index = 0;
for (DomNode n = element; n != null; n = n.getNextSibling()) {
if (n instanceof DomElement && n.getNodeName().equals(nthLastType)) {
index++;
}
}
return getNth(nth, index);
} else if (value.startsWith("not(")) {
final String selectors = value.substring(value.indexOf('(') + 1, value.length() - 1);
final AtomicBoolean errorOccured = new AtomicBoolean(false);
final CSSErrorHandler errorHandler = new CSSErrorHandler() {
@Override
public void warning(final CSSParseException exception) throws CSSException {
// ignore
}
@Override
public void fatalError(final CSSParseException exception) throws CSSException {
errorOccured.set(true);
}
@Override
public void error(final CSSParseException exception) throws CSSException {
errorOccured.set(true);
}
};
final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
parser.setErrorHandler(errorHandler);
try {
final SelectorList selectorList = parser.parseSelectors(selectors);
if (errorOccured.get() || selectorList == null || selectorList.size() != 1) {
throw new CSSException("Invalid selectors: " + selectors);
}
validateSelectors(selectorList, 9, element);
return !selects(browserVersion, selectorList.get(0), element, null, fromQuerySelectorAll);
} catch (final IOException e) {
throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
}
}
return false;
}
}
use of com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput in project htmlunit by HtmlUnit.
the class ComputedCSSStyleDeclaration method getEmptyHeight.
/**
* Returns the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
* elements.
*
* @return the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
* elements
*/
private int getEmptyHeight() {
if (height2_ != null) {
return height2_.intValue();
}
final DomNode node = getElement().getDomNodeOrDie();
if (!node.mayBeDisplayed()) {
height2_ = Integer.valueOf(0);
return 0;
}
final String display = getDisplay();
if (NONE.equals(display)) {
height2_ = Integer.valueOf(0);
return 0;
}
final Element elem = getElement();
final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight();
if (elem instanceof HTMLBodyElement) {
height2_ = windowHeight;
return windowHeight;
}
final boolean isInline = "inline".equals(display) && !(node instanceof HtmlInlineFrame);
// height is ignored for inline elements
final boolean explicitHeightSpecified = !isInline && !super.getHeight().isEmpty();
int defaultHeight;
if ((elem.getClass() == HTMLElement.class || elem instanceof HTMLDivElement || elem instanceof HTMLUnknownElement || elem instanceof HTMLDataElement || elem instanceof HTMLTimeElement || elem instanceof HTMLOutputElement || elem instanceof HTMLSlotElement || elem instanceof HTMLLegendElement) && StringUtils.isBlank(node.getTextContent())) {
defaultHeight = 0;
} else if (elem.getFirstChild() == null) {
if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
final BrowserVersion browser = getBrowserVersion();
if (browser.hasFeature(JS_CLIENTHEIGHT_RADIO_CHECKBOX_10)) {
defaultHeight = 10;
} else {
defaultHeight = 13;
}
} else if (node instanceof HtmlButton) {
defaultHeight = 20;
} else if (node instanceof HtmlInput && !(node instanceof HtmlHiddenInput)) {
final BrowserVersion browser = getBrowserVersion();
if (browser.hasFeature(JS_CLIENTHEIGHT_INPUT_17)) {
defaultHeight = 17;
} else if (browser.hasFeature(JS_CLIENTHEIGHT_INPUT_18)) {
defaultHeight = 18;
} else {
defaultHeight = 20;
}
} else if (node instanceof HtmlSelect) {
defaultHeight = 20;
} else if (node instanceof HtmlTextArea) {
defaultHeight = 49;
} else if (node instanceof HtmlInlineFrame) {
defaultHeight = 154;
} else {
defaultHeight = 0;
}
} else {
final String fontSize = getFontSize();
defaultHeight = getBrowserVersion().getFontHeight(fontSize);
if (node instanceof HtmlDivision || node instanceof HtmlSpan) {
String width = getStyleAttribute(WIDTH, false);
// maybe we are enclosed something that forces a width
Element parent = getElement().getParentElement();
while (width.isEmpty() && parent != null) {
width = getWindow().getComputedStyle(parent, null).getStyleAttribute(WIDTH, false);
parent = parent.getParentElement();
}
final int pixelWidth = pixelValue(width);
final String content = node.getVisibleText();
if (pixelWidth > 0 && !width.isEmpty() && StringUtils.isNotBlank(content)) {
final String[] lines = StringUtils.split(content, '\n');
int lineCount = 0;
final int fontSizeInt = Integer.parseInt(fontSize.substring(0, fontSize.length() - 2));
final FontRenderContext fontRenderCtx = new FontRenderContext(null, false, true);
for (final String line : lines) {
if (StringUtils.isBlank(line)) {
lineCount++;
} else {
// width is specified, we have to to some line breaking
final AttributedString attributedString = new AttributedString(line);
attributedString.addAttribute(TextAttribute.SIZE, fontSizeInt / 1.1);
final LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedString.getIterator(), fontRenderCtx);
lineBreakMeasurer.nextLayout(pixelWidth);
lineCount++;
while (lineBreakMeasurer.getPosition() < line.length() && lineCount < 1000) {
lineBreakMeasurer.nextLayout(pixelWidth);
lineCount++;
}
}
}
defaultHeight *= lineCount;
} else {
if (node instanceof HtmlSpan && StringUtils.isEmpty(content)) {
defaultHeight = 0;
} else {
defaultHeight *= StringUtils.countMatches(content, '\n') + 1;
}
}
}
}
final int defaultWindowHeight = elem instanceof HTMLCanvasElement ? 150 : windowHeight;
int height = pixelValue(elem, new CssValue(defaultHeight, defaultWindowHeight) {
@Override
public String get(final ComputedCSSStyleDeclaration style) {
final Element element = style.getElement();
if (element instanceof HTMLBodyElement) {
return String.valueOf(element.getWindow().getWebWindow().getInnerHeight());
}
// height is ignored for inline elements
if (isInline) {
return "";
}
return style.getStyleAttribute(HEIGHT, true);
}
});
if (height == 0 && !explicitHeightSpecified) {
height = defaultHeight;
}
height2_ = Integer.valueOf(height);
return height;
}
use of com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput in project htmlunit by HtmlUnit.
the class ComputedCSSStyleDeclaration method getCalculatedWidth.
private int getCalculatedWidth() {
if (width_ != null) {
return width_.intValue();
}
final Element element = getElement();
final DomNode node = element.getDomNodeOrDie();
if (!node.mayBeDisplayed()) {
width_ = Integer.valueOf(0);
return 0;
}
final String display = getDisplay();
if (NONE.equals(display)) {
width_ = Integer.valueOf(0);
return 0;
}
final int width;
final String styleWidth = super.getWidth();
final DomNode parent = node.getParentNode();
// width is ignored for inline elements
if (("inline".equals(display) || StringUtils.isEmpty(styleWidth)) && parent instanceof HtmlElement) {
// hack: TODO find a way to specify default values for different tags
if (element instanceof HTMLCanvasElement) {
return 300;
}
// Width not explicitly set.
final String cssFloat = getCssFloat();
if ("right".equals(cssFloat) || "left".equals(cssFloat) || ABSOLUTE.equals(getStyleAttribute(POSITION, true))) {
// We're floating; simplistic approximation: text content * pixels per character.
width = node.getVisibleText().length() * getBrowserVersion().getPixesPerChar();
} else if (BLOCK.equals(display)) {
final int windowWidth = element.getWindow().getWebWindow().getInnerWidth();
if (element instanceof HTMLBodyElement) {
width = windowWidth - 16;
} else {
// Block elements take up 100% of the parent's width.
final HTMLElement parentJS = parent.getScriptableObject();
width = pixelValue(parentJS, new CssValue(0, windowWidth) {
@Override
public String get(final ComputedCSSStyleDeclaration style) {
return style.getWidth();
}
}) - (getBorderHorizontal() + getPaddingHorizontal());
}
} else if (node instanceof HtmlSubmitInput || node instanceof HtmlResetInput || node instanceof HtmlButtonInput || node instanceof HtmlButton || node instanceof HtmlFileInput) {
// use asNormalizedText() here because getVisibleText() returns an empty string
// for submit and reset buttons
final String text = node.asNormalizedText();
// default font for buttons is a bit smaller than the body font size
width = 10 + (int) (text.length() * getBrowserVersion().getPixesPerChar() * 0.9);
} else if (node instanceof HtmlTextInput || node instanceof HtmlPasswordInput) {
final BrowserVersion browserVersion = getBrowserVersion();
if (browserVersion.hasFeature(JS_CLIENTWIDTH_INPUT_TEXT_143)) {
return 143;
}
if (browserVersion.hasFeature(JS_CLIENTWIDTH_INPUT_TEXT_173)) {
return 173;
}
// FF
width = 145;
} else if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
final BrowserVersion browserVersion = getBrowserVersion();
if (browserVersion.hasFeature(JS_CLIENTWIDTH_RADIO_CHECKBOX_10)) {
width = 10;
} else {
width = 13;
}
} else if (node instanceof HtmlTextArea) {
// wild guess
width = 100;
} else if (node instanceof HtmlImage) {
width = ((HtmlImage) node).getWidthOrDefault();
} else {
// Inline elements take up however much space is required by their children.
width = getContentWidth();
}
} else if (AUTO.equals(styleWidth)) {
width = element.getWindow().getWebWindow().getInnerWidth();
} else {
// Width explicitly set in the style attribute, or there was no parent to provide guidance.
width = pixelValue(element, new CssValue(0, element.getWindow().getWebWindow().getInnerWidth()) {
@Override
public String get(final ComputedCSSStyleDeclaration style) {
return style.getStyleAttribute(WIDTH, true);
}
});
}
width_ = Integer.valueOf(width);
return width;
}
use of com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput in project htmlunit by HtmlUnit.
the class WebClient8Test method checkingWithNoJS.
/**
* @throws Exception if something goes wrong
*/
@Test
public void checkingWithNoJS() throws Exception {
final String html = "<html>" + "<head>" + " <title>foo</title>" + "</head>" + " <body>" + " <form id='form1'>\n" + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n" + " <input type='radio' name='radio' id='radio'>Check me</input>\n" + " </form>" + " </body>" + "</html>";
try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
checkBox.setChecked(true);
final HtmlRadioButtonInput radioButton = page.getHtmlElementById("radio");
radioButton.setChecked(true);
}
}
Aggregations