Skip to content

Commit

Permalink
Merge pull request #4508 from kwvanderlinde/bugfix/4048-now-there-are…
Browse files Browse the repository at this point in the history
…-missing-events
  • Loading branch information
cwisniew authored Nov 30, 2023
2 parents 6c24410 + 776e07d commit d441fe3
Showing 1 changed file with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.*;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.*;

Expand Down Expand Up @@ -78,6 +79,10 @@ public class HTMLWebViewManager {
/** The bridge from Javascript to Java. */
private final JavaBridge bridge;

private EventListener listenerA = this::fixHref;

private EventListener listenerSubmit = this::getDataAndSubmit;

/** Represents a bridge from Javascript to Java. */
public class JavaBridge {

Expand Down Expand Up @@ -146,13 +151,50 @@ public void handleAddedNode(Object object) {
if (addedNode instanceof EventTarget) {
EventTarget target = (EventTarget) addedNode;
if (addedNode instanceof HTMLAnchorElement || addedNode instanceof HTMLAreaElement) {
target.addEventListener("click", HTMLWebViewManager.this::fixHref, true);
target.addEventListener("click", listenerA, true);
} else if (target instanceof HTMLFormElement) {
target.addEventListener("submit", HTMLWebViewManager.this::getDataAndSubmit, true);
target.addEventListener("submit", listenerSubmit, true);
} else if (target instanceof HTMLInputElement || target instanceof HTMLButtonElement) {
target.addEventListener("click", HTMLWebViewManager.this::getDataAndSubmit, true);
target.addEventListener("click", listenerSubmit, true);
}
}

// Add listeners to the node's descendant as they don't trigger mutation observer.
NodeList nodeList;

// Add event handlers for <a> hyperlinks.
nodeList = addedNode.getElementsByTagName("a");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget node = (EventTarget) nodeList.item(i);
node.addEventListener("click", listenerA, true);
}

// Add event handlers for hyperlinks for maps.
nodeList = addedNode.getElementsByTagName("area");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget node = (EventTarget) nodeList.item(i);
node.addEventListener("click", listenerA, true);
}

// Set the "submit" handler to get the data on submission not based on buttons
nodeList = addedNode.getElementsByTagName("form");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget target = (EventTarget) nodeList.item(i);
target.addEventListener("submit", listenerSubmit, true);
}

// Set the "submit" handler to get the data on submission based on input
nodeList = addedNode.getElementsByTagName("input");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget target = (EventTarget) nodeList.item(i);
target.addEventListener("click", listenerSubmit, true);
}
// Set the "submit" handler to get the data on submission based on button
nodeList = addedNode.getElementsByTagName("button");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget target = (EventTarget) nodeList.item(i);
target.addEventListener("click", listenerSubmit, true);
}
}
}
}
Expand Down Expand Up @@ -252,6 +294,10 @@ public void flush() {
// Delete cookies
java.net.CookieHandler.setDefault(new java.net.CookieManager());

// This may look pointless, but we need new objects on JFX <22 to avoid peering issues.
listenerA = this::fixHref;
listenerSubmit = this::getDataAndSubmit;

isFlushed = true;
}

Expand Down

0 comments on commit d441fe3

Please sign in to comment.