From: <rb...@us...> - 2018-01-22 18:44:17
|
Revision: 15089 http://sourceforge.net/p/htmlunit/code/15089 Author: rbri Date: 2018-01-22 18:44:13 +0000 (Mon, 22 Jan 2018) Log Message: ----------- doing Ctrl+Click on an anchor now opens a new window also if the href is javascript based Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor2Test.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2018-01-22 16:58:07 UTC (rev 15088) +++ trunk/htmlunit/src/changes/changes.xml 2018-01-22 18:44:13 UTC (rev 15089) @@ -8,6 +8,9 @@ <body> <release version="2.30" date="xx, 2018" description="Bugfixes, URLSearchParams implemented, start adding support of user defined iterators"> + <action type="fix" dev="rbri"> + Doing Ctrl+Click on an anchor now opens a new window also if the href is javascript based. + </action> <action type="add" dev="rbri"> JavaScript: Setter for history.scrollRestoration added. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor.java 2018-01-22 16:58:07 UTC (rev 15088) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor.java 2018-01-22 18:44:13 UTC (rev 15089) @@ -131,12 +131,18 @@ builder.append(ch); } - if (hasFeature(ANCHOR_IGNORE_TARGET_FOR_JS_HREF)) { + if (hasFeature(ANCHOR_IGNORE_TARGET_FOR_JS_HREF) && !shiftKey && !ctrlKey) { page.executeJavaScript(builder.toString(), "javascript url", getStartLineNumber()); } else { - final WebWindow win = page.getWebClient().openTargetWindow(page.getEnclosingWindow(), - page.getResolvedTarget(getTargetAttribute()), "_self"); + final String target; + if (shiftKey || ctrlKey) { + target = "_blank"; + } + else { + target = page.getResolvedTarget(getTargetAttribute()); + } + final WebWindow win = page.getWebClient().openTargetWindow(page.getEnclosingWindow(), target, "_self"); Page enclosedPage = win.getEnclosedPage(); if (enclosedPage == null) { win.getWebClient().getPage(win, new WebRequest(WebClient.URL_ABOUT_BLANK)); Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor2Test.java 2018-01-22 16:58:07 UTC (rev 15088) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlAnchor2Test.java 2018-01-22 18:44:13 UTC (rev 15089) @@ -42,6 +42,7 @@ * @author Marc Guillemot * @author Stefan Anzinger * @author Ahmed Ashour + * @author Ronald Brill */ @RunWith(BrowserRunner.class) public class HtmlAnchor2Test extends SimpleWebTestCase { @@ -640,4 +641,123 @@ assertEquals("First", secondPage.getTitleText()); } + /** + * @throws Exception if the test fails + */ + @Test + public void clickCtrlShift() throws Exception { + final String first + = "<html><head><title>First</title></head><body>\n" + + " <a href='" + URL_SECOND + "' id='a2'>link to foo2</a>\n" + + "</body></html>"; + final String second + = "<html><head><title>Second</title></head><body></body></html>"; + + final WebClient client = getWebClient(); + final List<String> collectedAlerts = new ArrayList<>(); + client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); + + final MockWebConnection webConnection = new MockWebConnection(); + webConnection.setResponse(URL_FIRST, first); + webConnection.setResponse(URL_SECOND, second); + client.setWebConnection(webConnection); + + assertEquals(1, getWebClient().getTopLevelWindows().size()); + final HtmlPage page = client.getPage(URL_FIRST); + final HtmlAnchor anchor = page.getHtmlElementById("a2"); + + final HtmlPage secondPage = anchor.click(true, true, false); + assertEquals(2, getWebClient().getTopLevelWindows().size()); + assertEquals("First", secondPage.getTitleText()); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void clickShiftJavascript() throws Exception { + final String first + = "<html><head><title>First</title></head><body>\n" + + " <a href='javascript: window.location=\"" + URL_SECOND + "\"' id='a2'>link to foo2</a>\n" + + "</body></html>"; + final String second + = "<html><head><title>Second</title></head><body></body></html>"; + + final WebClient client = getWebClient(); + final List<String> collectedAlerts = new ArrayList<>(); + client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); + + final MockWebConnection webConnection = new MockWebConnection(); + webConnection.setResponse(URL_FIRST, first); + webConnection.setResponse(URL_SECOND, second); + client.setWebConnection(webConnection); + + assertEquals(1, getWebClient().getTopLevelWindows().size()); + final HtmlPage page = client.getPage(URL_FIRST); + final HtmlAnchor anchor = page.getHtmlElementById("a2"); + + final HtmlPage secondPage = anchor.click(true, false, false); + assertEquals(2, getWebClient().getTopLevelWindows().size()); + assertEquals("Second", secondPage.getTitleText()); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void clickCtrlJavascript() throws Exception { + final String first + = "<html><head><title>First</title></head><body>\n" + + " <a href='javascript: window.location=\"" + URL_SECOND + "\"' id='a2'>link to foo2</a>\n" + + "</body></html>"; + final String second + = "<html><head><title>Second</title></head><body></body></html>"; + + final WebClient client = getWebClient(); + final List<String> collectedAlerts = new ArrayList<>(); + client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); + + final MockWebConnection webConnection = new MockWebConnection(); + webConnection.setResponse(URL_FIRST, first); + webConnection.setResponse(URL_SECOND, second); + client.setWebConnection(webConnection); + + assertEquals(1, getWebClient().getTopLevelWindows().size()); + final HtmlPage page = client.getPage(URL_FIRST); + final HtmlAnchor anchor = page.getHtmlElementById("a2"); + + final HtmlPage secondPage = anchor.click(false, true, false); + assertEquals(2, getWebClient().getTopLevelWindows().size()); + assertEquals("First", secondPage.getTitleText()); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void clickShiftCtrlJavascript() throws Exception { + final String first + = "<html><head><title>First</title></head><body>\n" + + " <a href='javascript: window.location=\"" + URL_SECOND + "\"' id='a2'>link to foo2</a>\n" + + "</body></html>"; + final String second + = "<html><head><title>Second</title></head><body></body></html>"; + + final WebClient client = getWebClient(); + final List<String> collectedAlerts = new ArrayList<>(); + client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); + + final MockWebConnection webConnection = new MockWebConnection(); + webConnection.setResponse(URL_FIRST, first); + webConnection.setResponse(URL_SECOND, second); + client.setWebConnection(webConnection); + + assertEquals(1, getWebClient().getTopLevelWindows().size()); + final HtmlPage page = client.getPage(URL_FIRST); + final HtmlAnchor anchor = page.getHtmlElementById("a2"); + + final HtmlPage secondPage = anchor.click(true, true, false); + assertEquals(2, getWebClient().getTopLevelWindows().size()); + assertEquals("First", secondPage.getTitleText()); + } } |