From: <rb...@us...> - 2013-07-25 17:30:49
|
Revision: 8421 http://sourceforge.net/p/htmlunit/code/8421 Author: rbri Date: 2013-07-25 17:30:45 +0000 (Thu, 25 Jul 2013) Log Message: ----------- XPathResult types STRING_TYPE, NUMBER_TYPE and BOOLEAN_TYPE don't work (patch by Chuck Dumont) Issue 1527 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResult.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResultTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-07-24 19:05:25 UTC (rev 8420) +++ trunk/htmlunit/src/changes/changes.xml 2013-07-25 17:30:45 UTC (rev 8421) @@ -8,6 +8,9 @@ <body> <release version="2.13" date="???" description="Bugfixes"> + <action type="fix" dev="rbri" issue="1527" due-to="Chuck Dumont"> + XPathResult types STRING_TYPE, NUMBER_TYPE and BOOLEAN_TYPE don't work. + </action> <action type="fix" dev="rbri" issue="1520"> Method document.createStyleSheet is more compatible with the IE; now the associated link node is also created and inserted at the right place in the header. The new link Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResult.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResult.java 2013-07-24 19:05:25 UTC (rev 8420) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResult.java 2013-07-25 17:30:45 UTC (rev 8421) @@ -20,6 +20,7 @@ import net.sourceforge.htmlunit.corejs.javascript.Context; +import com.gargoylesoftware.htmlunit.html.DomAttr; import com.gargoylesoftware.htmlunit.html.DomNode; import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; @@ -33,6 +34,8 @@ * * @version $Revision$ * @author Ahmed Ashour + * @author Chuck Dumont + * @author Ronald Brill */ @JsxClass(browsers = @WebBrowser(FF)) public class XPathResult extends SimpleScriptable { @@ -222,7 +225,15 @@ if (resultType_ != NUMBER_TYPE) { throw Context.reportRuntimeError("Cannot get numberValue for type: " + resultType_); } - return ((Number) result_.get(0)).doubleValue(); + final String asString = asString(); + Double answer; + try { + answer = Double.parseDouble(asString); + } + catch (final NumberFormatException e) { + answer = Double.NaN; + } + return answer; } /** @@ -234,7 +245,7 @@ if (resultType_ != BOOLEAN_TYPE) { throw Context.reportRuntimeError("Cannot get booleanValue for type: " + resultType_); } - return ((Boolean) result_.get(0)).booleanValue(); + return Boolean.parseBoolean(asString()); } /** @@ -246,6 +257,17 @@ if (resultType_ != STRING_TYPE) { throw Context.reportRuntimeError("Cannot get stringValue for type: " + resultType_); } - return (String) result_.get(0); + return asString(); } + + private String asString() { + final Object resultObj = result_.get(0); + if (resultObj instanceof DomAttr) { + return ((DomAttr) resultObj).getValue(); + } + if (resultObj instanceof DomNode) { + return ((DomNode) resultObj).asText(); + } + return resultObj.toString(); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResultTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResultTest.java 2013-07-24 19:05:25 UTC (rev 8420) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XPathResultTest.java 2013-07-25 17:30:45 UTC (rev 8421) @@ -30,6 +30,8 @@ * @version $Revision$ * @author Ahmed Ashour * @author Marc Guillemot + * @author Chuck Dumont + * @author Ronald Brill */ @RunWith(BrowserRunner.class) public class XPathResultTest extends SimpleWebTestCase { @@ -193,4 +195,82 @@ loadPageWithAlerts(html); } + + /** + * @throws Exception if the test fails + */ + @Test + @Browsers(FF) + @Alerts({"bar", "foo", "foo" }) + public void stringType() throws Exception { + final String html = "<html><head><title attr=\"bar\">foo</title><script>\n" + + " function test() {\n" + + " var result = document.evaluate('//title/@attr', document, null, " + + "XPathResult.STRING_TYPE, null);\n" + + " alert(result.stringValue);\n" + + " result = document.evaluate('//title', document, null, " + + "XPathResult.STRING_TYPE, null);\n" + + " alert(result.stringValue);\n" + + " var result = document.evaluate('//title/text()', document, null, " + + "XPathResult.STRING_TYPE, null);\n" + + " alert(result.stringValue);\n" + + "}" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Browsers(FF) + @Alerts({ "true", "true", "true", "true" }) + public void numberType() throws Exception { + final String html = "<html><head><title attr=\"1234\">4321.5</title><span>foo</span><script>\n" + + " function test() {\n" + + " var result = document.evaluate('//title/@attr', document, null, " + + "XPathResult.NUMBER_TYPE, null);\n" + + " alert(result.numberValue === 1234);\n" + + " result = document.evaluate('//title', document, null, " + + "XPathResult.NUMBER_TYPE, null);\n" + + " alert(result.numberValue === 4321.5);\n" + + " result = document.evaluate('//title/text()', document, null, " + + "XPathResult.NUMBER_TYPE, null);\n" + + " alert(result.numberValue === 4321.5);\n" + + " result = document.evaluate('//span', document, null, " + + "XPathResult.NUMBER_TYPE, null);\n" + + " alert(isNaN(result.numberValue));\n" + + " }\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Browsers(FF) + @Alerts({ "true", "true", "true" }) + public void booleanType() throws Exception { + final String html = "<html><head><title>foo</title><span attr=\"true\">true</span><script>\n" + + " function test() {\n" + + " var result = document.evaluate('//title', document, null, " + + "XPathResult.BOOLEAN_TYPE, null);\n" + + " alert(result.booleanValue === false);\n" + + " result = document.evaluate('//span', document, null, " + + "XPathResult.BOOLEAN_TYPE, null);\n" + + " alert(result.booleanValue === true);\n" + + " result = document.evaluate('//span/@attr', document, null, " + + "XPathResult.BOOLEAN_TYPE, null);\n" + + " alert(result.booleanValue === true);\n" + + " }\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts(html); + } } |