Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8199014

WebView: Links to anchors do not work when using loadContent()

XMLWordPrintable

    • web
    • generic
    • generic

      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.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: