Skip to content

Commit 3bb66d7

Browse files
committed
more scroll event fixes
1 parent a0ae6c0 commit 3bb66d7

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

src/main/java/org/htmlunit/javascript/host/Element.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,9 @@ private void fireScrollEvent(final Node node) {
12461246
}
12471247
else {
12481248
event = new Event(node, Event.TYPE_SCROLL);
1249+
event.setCancelable(false);
12491250
}
1251+
event.setBubbles(false);
12501252
node.fireEvent(event);
12511253
}
12521254

src/main/java/org/htmlunit/javascript/host/Window.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package org.htmlunit.javascript.host;
1616

17+
import static org.htmlunit.BrowserVersionFeatures.EVENT_SCROLL_UIEVENT;
1718
import static org.htmlunit.BrowserVersionFeatures.JS_WINDOW_SELECTION_NULL_IF_INVISIBLE;
1819
import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
1920
import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
@@ -101,11 +102,13 @@
101102
import org.htmlunit.javascript.host.dom.AbstractList.EffectOnCache;
102103
import org.htmlunit.javascript.host.dom.DOMException;
103104
import org.htmlunit.javascript.host.dom.Document;
105+
import org.htmlunit.javascript.host.dom.Node;
104106
import org.htmlunit.javascript.host.dom.Selection;
105107
import org.htmlunit.javascript.host.event.Event;
106108
import org.htmlunit.javascript.host.event.EventTarget;
107109
import org.htmlunit.javascript.host.event.MessageEvent;
108110
import org.htmlunit.javascript.host.event.MouseEvent;
111+
import org.htmlunit.javascript.host.event.UIEvent;
109112
import org.htmlunit.javascript.host.html.DocumentProxy;
110113
import org.htmlunit.javascript.host.html.HTMLCollection;
111114
import org.htmlunit.javascript.host.html.HTMLDocument;
@@ -1156,11 +1159,22 @@ public void scrollBy(final Scriptable x, final Scriptable y) {
11561159
body.setScrollLeft(body.getScrollLeft() + xOff);
11571160
body.setScrollTop(body.getScrollTop() + yOff);
11581161

1159-
final Event event = new Event(body, Event.TYPE_SCROLL);
1160-
body.fireEvent(event);
1162+
fireScrollEvent(body);
11611163
}
11621164
}
11631165

1166+
private void fireScrollEvent(final Node node) {
1167+
final Event event;
1168+
if (getBrowserVersion().hasFeature(EVENT_SCROLL_UIEVENT)) {
1169+
event = new UIEvent(node, Event.TYPE_SCROLL);
1170+
}
1171+
else {
1172+
event = new Event(node, Event.TYPE_SCROLL);
1173+
event.setCancelable(false);
1174+
}
1175+
node.fireEvent(event);
1176+
}
1177+
11641178
/**
11651179
* Scrolls the window content down by the specified number of lines.
11661180
* @param lines the number of lines to scroll down
@@ -1171,8 +1185,7 @@ public void scrollByLines(final int lines) {
11711185
if (body != null) {
11721186
body.setScrollTop(body.getScrollTop() + (19 * lines));
11731187

1174-
final Event event = new Event(body, Event.TYPE_SCROLL);
1175-
body.fireEvent(event);
1188+
fireScrollEvent(body);
11761189
}
11771190
}
11781191

@@ -1186,8 +1199,7 @@ public void scrollByPages(final int pages) {
11861199
if (body != null) {
11871200
body.setScrollTop(body.getScrollTop() + (getInnerHeight() * pages));
11881201

1189-
final Event event = new Event(body, Event.TYPE_SCROLL);
1190-
body.fireEvent(event);
1202+
fireScrollEvent(body);
11911203
}
11921204
}
11931205

@@ -1224,8 +1236,7 @@ public void scrollTo(final Scriptable x, final Scriptable y) {
12241236
body.setScrollLeft(xOff);
12251237
body.setScrollTop(yOff);
12261238

1227-
final Event event = new Event(body, Event.TYPE_SCROLL);
1228-
body.fireEvent(event);
1239+
fireScrollEvent(body);
12291240
}
12301241
}
12311242

src/main/java/org/htmlunit/javascript/host/event/Event.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ else if (TYPE_ERROR.equals(type)) {
562562
else if (
563563
TYPE_FOCUS.equals(type)
564564
|| TYPE_BLUR.equals(type)
565-
|| TYPE_SCROLL.equals(type)
566565
|| TYPE_BEFOREPRINT.equals(type)
567566
|| TYPE_AFTERPRINT.equals(type)) {
568567
bubbles_ = false;
@@ -834,7 +833,7 @@ public boolean isBubbles() {
834833
/**
835834
* @param bubbles the bubbles to set
836835
*/
837-
protected void setBubbles(final boolean bubbles) {
836+
public void setBubbles(final boolean bubbles) {
838837
bubbles_ = bubbles;
839838
}
840839

@@ -849,7 +848,7 @@ public boolean isCancelable() {
849848
/**
850849
* @param cancelable the cancelable to set
851850
*/
852-
protected void setCancelable(final boolean cancelable) {
851+
public void setCancelable(final boolean cancelable) {
853852
cancelable_ = cancelable;
854853
}
855854

src/test/java/org/htmlunit/javascript/host/event/EventTest.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,9 +1527,7 @@ public void domEventNameUsedAsFunctionName() throws Exception {
15271527
@Alerts(DEFAULT = {"[object Event]", "scroll", "false", "false", "false"},
15281528
FF = {"[object UIEvent]", "scroll", "false", "true", "false"},
15291529
FF_ESR = {"[object UIEvent]", "scroll", "false", "true", "false"})
1530-
@HtmlUnitNYI(FF = {"[object UIEvent]", "scroll", "false", "false", "false"},
1531-
FF_ESR = {"[object UIEvent]", "scroll", "false", "false", "false"})
1532-
public void scrollEvent() throws Exception {
1530+
public void scrollEventFromScrollIntoView() throws Exception {
15331531
final String html = DOCTYPE_HTML
15341532
+ "<html>\n"
15351533
+ "<head>\n"
@@ -1557,4 +1555,64 @@ public void scrollEvent() throws Exception {
15571555

15581556
loadPageVerifyTitle2(html);
15591557
}
1558+
1559+
/**
1560+
* @throws Exception if the test fails
1561+
*/
1562+
@Test
1563+
@Alerts(DEFAULT = {"[object Event]", "scroll", "true", "false", "false"},
1564+
FF = {"[object UIEvent]", "scroll", "true", "true", "false"},
1565+
FF_ESR = {"[object UIEvent]", "scroll", "true", "true", "false"})
1566+
public void scrollEventFromScrollBy() throws Exception {
1567+
final String html = DOCTYPE_HTML
1568+
+ "<html>\n"
1569+
+ "<head>\n"
1570+
+ "<script>\n"
1571+
+ LOG_TITLE_FUNCTION
1572+
+ DUMP_EVENT_FUNCTION
1573+
+ "</script>\n"
1574+
+ "</head>\n"
1575+
1576+
+ "<body>\n"
1577+
+ " <div style='height: 1000px;'></div>\n"
1578+
1579+
+ " <script>\n"
1580+
+ " document.addEventListener('scroll', function(e) { dump(e) });\n"
1581+
+ " window.scrollBy(10, 20);\n"
1582+
+ " </script>\n"
1583+
+ "</body>\n"
1584+
+ "</html>";
1585+
1586+
loadPageVerifyTitle2(html);
1587+
}
1588+
1589+
/**
1590+
* @throws Exception if the test fails
1591+
*/
1592+
@Test
1593+
@Alerts(DEFAULT = {"[object Event]", "scroll", "true", "false", "false"},
1594+
FF = {"[object UIEvent]", "scroll", "true", "true", "false"},
1595+
FF_ESR = {"[object UIEvent]", "scroll", "true", "true", "false"})
1596+
public void scrollEventFromScrollTo() throws Exception {
1597+
final String html = DOCTYPE_HTML
1598+
+ "<html>\n"
1599+
+ "<head>\n"
1600+
+ "<script>\n"
1601+
+ LOG_TITLE_FUNCTION
1602+
+ DUMP_EVENT_FUNCTION
1603+
+ "</script>\n"
1604+
+ "</head>\n"
1605+
1606+
+ "<body>\n"
1607+
+ " <div style='height: 1000px;'></div>\n"
1608+
1609+
+ " <script>\n"
1610+
+ " document.addEventListener('scroll', function(e) { dump(e) });\n"
1611+
+ " window.scrollTo(10, 20);\n"
1612+
+ " </script>\n"
1613+
+ "</body>\n"
1614+
+ "</html>";
1615+
1616+
loadPageVerifyTitle2(html);
1617+
}
15601618
}

0 commit comments

Comments
 (0)