From: <asa...@us...> - 2017-05-13 14:20:28
|
Revision: 14442 http://sourceforge.net/p/htmlunit/code/14442 Author: asashour Date: 2017-05-13 14:20:25 +0000 (Sat, 13 May 2017) Log Message: ----------- Fix element.isDisplayed() Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlInput.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAreaElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLButtonElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFieldSetElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFormElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLObjectElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLOutputElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLSelectElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLTextAreaElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/ValidityState.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -808,7 +808,7 @@ ((HtmlPage) enclosedPage).setFocusedElement(activeElement.getDomNodeOrDie(), true); } } - else if (jsWindow instanceof Window2) { + else if (jsWindow != null) { final HTMLElement2 activeElement = ((HTMLDocument2) Window2.getDocument(jsWindow)).getActiveElement(); if (activeElement != null) { Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -753,6 +753,9 @@ final Object scriptableObject = ((DomNode) node).getScriptableObject(); if (scriptableObject instanceof HTMLElement) { final HTMLElement elem = (HTMLElement) scriptableObject; + if (elem.isHidden()) { + return false; + } final CSSStyleDeclaration style = elem.getWindow().getComputedStyle(elem, null); if (DisplayStyle.NONE.value().equals(style.getDisplay())) { return false; Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -1353,4 +1353,21 @@ public boolean isAltPressed() { return altPressed_; } + + /** + * Returns whether this element satisfies all form validation constraints set. + * @return whether this element satisfies all form validation constraints set + */ + public boolean isValid() { + return !isRequiredSupported() || getAttribute("required") == ATTRIBUTE_NOT_DEFINED + || !getAttribute("value").isEmpty(); + } + + /** + * Returns whether this {@link HtmlInput} supports the {@code required} constraint. + * @return whether this {@link HtmlInput} supports the {@code required} constraint + */ + protected boolean isRequiredSupported() { + return false; + } } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlInput.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlInput.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlInput.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -767,18 +767,9 @@ } /** - * Returns whether this element satisfies all form validation constraints set. - * @return whether this element satisfies all form validation constraints set + * {@inheritDoc} */ - public boolean isValid() { - return !isRequiredSupported() || getAttribute("required") == ATTRIBUTE_NOT_DEFINED - || !getValueAttribute().isEmpty(); - } - - /** - * Returns whether this {@link HtmlInput} supports the {@code required} constraint. - * @return whether this {@link HtmlInput} supports the {@code required} constraint - */ + @Override protected boolean isRequiredSupported() { return true; } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAreaElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAreaElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAreaElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -108,4 +108,23 @@ area.focus(); } } + + /** + * Returns the {@code coords} attribute. + * @return the {@code coords} attribute + */ + @JsxGetter + public String getCoords() { + return getDomNodeOrDie().getAttribute("coords"); + } + + /** + * Sets the {@code coords} attribute. + * @param coords {@code coords} attribute + */ + @JsxSetter + public void setCoords(final String coords) { + getDomNodeOrDie().setAttribute("coords", coords); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLButtonElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLButtonElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLButtonElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -105,4 +105,14 @@ public TextRange createTextRange() { return super.createTextRange(); } + + /** + * Checks whether the element has any constraints and whether it satisfies them. + * @return if the element is valid + */ + @JsxFunction + public boolean checkValidity() { + return getDomNodeOrDie().isValid(); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFieldSetElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFieldSetElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFieldSetElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -23,6 +23,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter; @@ -90,4 +91,14 @@ } return (HTMLFormElement) getScriptableFor(form); } + + /** + * Checks whether the element has any constraints and whether it satisfies them. + * @return if the element is valid + */ + @JsxFunction + public boolean checkValidity() { + return getDomNodeOrDie().isValid(); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFormElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFormElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLFormElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -518,4 +518,14 @@ } return result; } + + /** + * Checks whether the element has any constraints and whether it satisfies them. + * @return if the element is valid + */ + @JsxFunction + public boolean checkValidity() { + return getDomNodeOrDie().isValid(); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLObjectElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLObjectElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLObjectElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -31,6 +31,7 @@ import com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter; import com.gargoylesoftware.htmlunit.javascript.host.ActiveXObjectImpl; @@ -353,4 +354,14 @@ } return (HTMLFormElement) getScriptableFor(form); } + + /** + * Checks whether the element has any constraints and whether it satisfies them. + * @return if the element is valid + */ + @JsxFunction + public boolean checkValidity() { + return getDomNodeOrDie().isValid(); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLOutputElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLOutputElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLOutputElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -20,6 +20,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlOutput; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter; import com.gargoylesoftware.htmlunit.javascript.host.dom.AbstractList; @@ -73,4 +74,13 @@ return labels_; } + /** + * Checks whether the element has any constraints and whether it satisfies them. + * @return if the element is valid + */ + @JsxFunction + public boolean checkValidity() { + return getDomNodeOrDie().isValid(); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLSelectElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLSelectElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLSelectElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -320,4 +320,13 @@ return labels_; } + /** + * Checks whether the element has any constraints and whether it satisfies them. + * @return if the element is valid + */ + @JsxFunction + public boolean checkValidity() { + return getDomNodeOrDie().isValid(); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLTextAreaElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLTextAreaElement.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLTextAreaElement.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -355,4 +355,14 @@ public TextRange createTextRange() { return super.createTextRange(); } + + /** + * Checks whether the element has any constraints and whether it satisfies them. + * @return if the element is valid + */ + @JsxFunction + public boolean checkValidity() { + return getDomNodeOrDie().isValid(); + } + } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/ValidityState.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/ValidityState.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/ValidityState.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -21,6 +21,7 @@ import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; /** * A JavaScript object for {@code ValidityState}. @@ -37,4 +38,84 @@ public ValidityState() { } + /** + * Returns whether the customer validity message is set or not. + * @return whether the customer validity message is set or not + */ + @JsxGetter + public boolean isCustomError() { + return false; + } + + /** + * Returns whether the element value does not match its {@code pattern} attribute. + * @return whether the element value does not match its {@code pattern} attribute + */ + @JsxGetter + public boolean isPatternMismatch() { + return false; + } + + /** + * Returns whether the element value is greater than its {@code max} attribute. + * @return whether the element value is greater than its {@code max} attribute + */ + @JsxGetter + public boolean isRangeOverlow() { + return false; + } + + /** + * Returns whether the element value is less than its {@code min} attribute. + * @return whether the element value is less than its {@code min} attribute + */ + @JsxGetter + public boolean isRangeUnderflow() { + return false; + } + + /** + * Returns whether the element value is invalid per its {@code step} attribute. + * @return whether the element value is invalid per its {@code step} attribute + */ + @JsxGetter + public boolean isStepMismatch() { + return false; + } + + /** + * Returns whether the element value exceeds its {@code maxLength} attribute. + * @return whether the element value exceeds its {@code maxLength} attribute + */ + public boolean isTooLong() { + return false; + } + + /** + * Returns whether the element value is invalid per its {@code type} attribute. + * @return whether the element value is invalid per its {@code type} attribute + */ + @JsxGetter + public boolean isTypeMismatch() { + return false; + } + + /** + * Returns whether the element (with a {@code required} attribute) has no value. + * @return whether the element (with a {@code required} attribute) has no value + */ + @JsxGetter + public boolean isValueMissing() { + return false; + } + + /** + * Returns whether the element value is valid. + * @return whether the element value is valid + */ + @JsxGetter + public boolean isValid() { + return false; + } + } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java 2017-05-12 17:30:22 UTC (rev 14441) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElementTest.java 2017-05-13 14:20:25 UTC (rev 14442) @@ -23,6 +23,7 @@ import org.junit.runner.RunWith; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; import com.gargoylesoftware.htmlunit.BrowserRunner; import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; @@ -4666,4 +4667,22 @@ loadPageWithAlerts2(html); } + /** + * @throws Exception if the test fails + */ + @Test + public void isDisplayed() throws Exception { + final String html = + "<html>\n" + + "<body>\n" + + " <div id='div1' hidden>\n" + + " <div id='child' />\n" + + " </div>\n" + + "</body></html>"; + + final WebDriver driver = loadPage2(html); + final WebElement element = driver.findElement(By.id("child")); + assertFalse(element.isDisplayed()); + } + } |