Skip to content

Commit 1b44209

Browse files
committed
the url was not encoded by history.replaceState() / history.pushState() (issue #523)
1 parent 2dc61bc commit 1b44209

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

src/changes/changes.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99
<body>
1010
<release version="2.69.0" date="xx, 2023" description="Bugfixes">
1111
<action type="fix" dev="rbri">
12-
Xerces detection fixed.
12+
Xerces detection fixed (regression in 2.68.0).
13+
</action>
14+
<action type="fix" dev="rbri" issue="523">
15+
Cached location.hash was not updated by history.replaceState()/history.pushState().
16+
</action>
17+
<action type="fix" dev="rbri" issue="523">
18+
The url was not encoded by history.replaceState() / history.pushState().
19+
</action>
20+
<action type="fix" dev="rbri">
21+
Inconsistent use of UrlUtils.encodeAnchor() / UrlUtils.encodeHash().
1322
</action>
1423
</release>
1524

src/main/java/com/gargoylesoftware/htmlunit/History.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
package com.gargoylesoftware.htmlunit;
1616

17+
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.URL_MINIMAL_QUERY_ENCODING;
18+
1719
import java.io.IOException;
1820
import java.io.ObjectInputStream;
1921
import java.io.Serializable;
@@ -101,11 +103,28 @@ URL getUrl() {
101103

102104
void setUrl(final URL url, final Page page) {
103105
if (url != null) {
104-
webRequest_.setUrl(url);
106+
WebWindow webWindow = null;
107+
108+
boolean minimalQueryEncoding = false;
109+
if (page != null) {
110+
webWindow = page.getEnclosingWindow();
111+
if (webWindow != null) {
112+
minimalQueryEncoding = webWindow.getWebClient()
113+
.getBrowserVersion().hasFeature(URL_MINIMAL_QUERY_ENCODING);
114+
}
115+
}
116+
117+
final URL encoded = UrlUtils.encodeUrl(url, minimalQueryEncoding,
118+
webRequest_.getCharset());
119+
webRequest_.setUrl(encoded);
105120
if (page != null) {
106-
page.getWebResponse().getWebRequest().setUrl(url);
107-
final Window win = page.getEnclosingWindow().getScriptableObject();
108-
win.getLocation().setHash(null, url.getRef(), false);
121+
page.getWebResponse().getWebRequest().setUrl(encoded);
122+
if (webWindow != null) {
123+
final Window win = webWindow.getScriptableObject();
124+
if (win != null) {
125+
win.getLocation().setHash(null, encoded.getRef(), false);
126+
}
127+
}
109128
}
110129
}
111130
}

src/test/java/com/gargoylesoftware/htmlunit/javascript/host/History2Test.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.gargoylesoftware.htmlunit.WebDriverTestCase;
3030
import com.gargoylesoftware.htmlunit.junit.BrowserRunner;
3131
import com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts;
32+
import com.gargoylesoftware.htmlunit.junit.BrowserRunner.HtmlUnitNYI;
3233
import com.gargoylesoftware.htmlunit.util.NameValuePair;
3334

3435
/**
@@ -540,6 +541,35 @@ public void replaceStateClone() throws Exception {
540541
shutDownAll();
541542
}
542543

544+
/**
545+
* @throws Exception if an error occurs
546+
*/
547+
@Test
548+
@Alerts(DEFAULT = {"href=§§URL§§", "href=§§URL§§?p=%C3%84"},
549+
FF = {"href=§§URL§§", "href=§§URL§§?p=%C4"},
550+
FF_ESR = {"href=§§URL§§", "href=§§URL§§?p=%C4"},
551+
IE = {"href=§§URL§§", "href=§§URL§§?p=\u00c4"})
552+
@HtmlUnitNYI(CHROME = {"href=§§URL§§", "href=§§URL§§?p=%C4"},
553+
EDGE = {"href=§§URL§§", "href=§§URL§§?p=%C4"})
554+
public void pushStateEncoding() throws Exception {
555+
final String html = "<html>\n"
556+
+ "<head>\n"
557+
+ "<script>\n"
558+
+ LOG_TITLE_FUNCTION
559+
+ " function test() {\n"
560+
+ " log('href=' + location.href);\n"
561+
+ " window.history.pushState(null, '', '" + URL_FIRST + "?p=\u00c4');\n"
562+
+ " log('href=' + location.href);\n"
563+
+ " }\n"
564+
+ "</script>\n"
565+
+ "</head>\n"
566+
+ "<body onload='test()'>\n"
567+
+ "</body></html>";
568+
569+
expandExpectedAlertsVariables(URL_FIRST);
570+
loadPageVerifyTitle2(html);
571+
}
572+
543573
/**
544574
* @throws Exception if an error occurs
545575
*/
@@ -613,6 +643,35 @@ public void pushStateChangeHashNoStore() throws Exception {
613643
verifyTitle2(driver, getExpectedAlerts());
614644
}
615645

646+
/**
647+
* @throws Exception if an error occurs
648+
*/
649+
@Test
650+
@Alerts(DEFAULT = {"href=§§URL§§", "href=§§URL§§?p=%C3%84"},
651+
FF = {"href=§§URL§§", "href=§§URL§§?p=%C4"},
652+
FF_ESR = {"href=§§URL§§", "href=§§URL§§?p=%C4"},
653+
IE = {"href=§§URL§§", "href=§§URL§§?p=\u00c4"})
654+
@HtmlUnitNYI(CHROME = {"href=§§URL§§", "href=§§URL§§?p=%C4"},
655+
EDGE = {"href=§§URL§§", "href=§§URL§§?p=%C4"})
656+
public void replaceStateEncoding() throws Exception {
657+
final String html = "<html>\n"
658+
+ "<head>\n"
659+
+ "<script>\n"
660+
+ LOG_TITLE_FUNCTION
661+
+ " function test() {\n"
662+
+ " log('href=' + location.href);\n"
663+
+ " window.history.replaceState(null, '', '" + URL_FIRST + "?p=\u00c4');\n"
664+
+ " log('href=' + location.href);\n"
665+
+ " }\n"
666+
+ "</script>\n"
667+
+ "</head>\n"
668+
+ "<body onload='test()'>\n"
669+
+ "</body></html>";
670+
671+
expandExpectedAlertsVariables(URL_FIRST);
672+
loadPageVerifyTitle2(html);
673+
}
674+
616675
/**
617676
* @throws Exception if an error occurs
618677
*/

0 commit comments

Comments
 (0)