JDK-8199014 : WebView: Links to anchors do not work when using loadContent()
  • Type: Bug
  • Component: javafx
  • Sub-Component: web
  • Affected Version: 8,9,10,openjfx11
  • Priority: P4
  • Status: In Progress
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2018-03-03
  • Updated: 2018-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Description
FULL PRODUCT VERSION :
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
MAC OS X 10.13.3
also affects Windows 7, others not tested

A DESCRIPTION OF THE PROBLEM :
Anchor links do not work in WebView component when HTML is loaded using the WebEngine.loadContent() method. When storing the identical HTML to a file and using WebEngine.load() with the file's URL works as expected.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run attached source code. Click on the "Introduction" link.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
WebView should scroll to the line containing the anchor #introduction.
ACTUAL -
Nothing happens.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No errors or Eceptions are reported.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	private static String generateContent() {
		StringBuilder sb = new StringBuilder();
		sb.append("<!DOCTYPE html>\n" + 
				"<html lang=\"en\">\n" + 
				"<head>\n" + 
				"<meta charset=\"utf-8\">\n" + 
				"<p>Jump to <a href=\"#introduction\">Introduction</a></p>\n");
		for (int i=1;i<100;i++) {
			sb.append("Line "+i+"<br>\n");
		}
		sb.append("<h1 id=\"introduction\">Introduction</h1>\n");
		for (int i=100;i<200;i++) {
			sb.append("Line "+i+"<br>\n");
		}
		sb.append("</body>\n" + 
				"</html>\n");
		return sb.toString();
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		ScrollPane scrollPane = new ScrollPane();
		WebView webView = new WebView();
		WebEngine webEngine = webView.getEngine();

		scrollPane.setContent(webView);
		scrollPane.setFitToWidth(true);
		scrollPane.setFitToHeight(true);

		Scene scene = new Scene(webView);
		stage.setScene(scene);
		stage.show();
		
		webEngine.loadContent(generateContent());
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Storing HTML to a file and using WebEngine.load(URL) instead. But depending on the application, this could reuqire new permissions or introduce new vulnerabilities into the application code.


Comments
Lowering the priority to P4: this is a less common case that WebView has not ever handled correctly and there is a workaround.
16-03-2018

Analyzed this issue. WebKit code internally relies on baseurl to navigate to the fragment identifier specified in the href. As the baseurl is empty in this case, the fragment navigation does not happen.This is the general WebEngine code in almost all browsers. Tried few other cases where baseurl would not be set. -html string passed as input to iframe using srcdoc attribute instead of passing html document file through src attribute. This case too does not work in popular browsers. - Few email clients which support inserting html are known to work given that they preprocess the content to convert links suitably. If we need to support this from FX, changes in WebKit would have to be made to handle the scenario of navigation even if baseurl is not set. ----------------------- Also, apart from the workaround of storing the html in a file, one more option could be to make use of scrollintoview on a click handler for anchor. Something like this <a href="#introduction" onclick="gotoElement()">Introduction</a> and in the script function gotosection() { document.getElementById("introduction").scrollIntoView(); } -------------------- Considering the rarity of the said scenario and available workarounds, this does not seem to be a critical issue in my opinion.
16-03-2018

Issue is reproducible in all JDK 8,9,10,11. Windows 10 - JDK results ------------------------------------ 8u172 : Fail 11-ea+03 : Fail ------------------------------------ When same content is loaded in HTML file and then loaded into webview using 'WebEngine.load()', anchor feature is working.
05-03-2018