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

WebView does not render contents of locally stored MarkDown (*.md) files

XMLWordPrintable

    • web
    • x86_64
    • windows

      ADDITIONAL SYSTEM INFORMATION :
      Setup #1
      - Windows 8.1, x86-64
      - Java 15.0.1
      - Gluon OpenJFX 16-ea+6
      - Gluon OpenJFX 11.0.2

      Setup #2
      - MacOS x86-64,
      - Gluon OpenJFX 16-ea+6 is used

      A DESCRIPTION OF THE PROBLEM :
      WebView perfectly shows content of MarkDown files stored at remote locations (e.g. https://github.com/openjdk/jfx/edit/master/README.md).

      When the same file is stored locally, WebView dows not render its content. Instead, a blank (white) screen is shown.

      When instead a HTML file is used, file rendering works regardless of its location. Same is valid for TXT files, those are also rendered as expeced, regardless of their location.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      (1) Setup a standard JavaFX application
      (2) Create a WebView instance
      (3) Get the WebView instances WebViewEngine
      (4) Load the local "MarkDown" file OR load a remote located version

      Live example:
      https://github.com/Oliver-Loeffler/webview-markdown-issue

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      After instructing the WebViewEngine to load a locally stored MarkDown file for a WebView,
      the contents (plain text) of the MarkDown file shall be displayed in the WebView window.
      Ideally, if the window not large enough, scrollbars shall appear.
      ACTUAL -
      After instructing the WebViewEngine to load a locally stored MarkDown file for a WebView,
      no visible content is displayed in the WebView window. Window shows an area of white color only.
      No text is shown at all.

      When MarkDown content is loaded from a remote location, then text content is displayed.
      No scrollbars are shown when content takes more space as available on screen.

      ---------- BEGIN SOURCE ----------
      /*
       * Required modules:
       *
       * 'javafx.controls', 'javafx.web'
       *
       * JavaFX version used:
       * 11.0.2
       * 16-ea+6
       *
       * Java version used:
       * 15.0.1
       *
       *
       */

      package webview.test;

      import java.io.IOException;
      import java.net.URL;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.scene.Scene;
      import javafx.scene.control.Alert;
      import javafx.scene.control.Alert.AlertType;
      import javafx.scene.control.Button;
      import javafx.scene.control.Label;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.Priority;
      import javafx.scene.layout.VBox;
      import javafx.scene.web.WebEngine;
      import javafx.scene.web.WebView;
      import javafx.stage.Stage;

      public class App extends Application {

      private WebView webView;

      private TextField resourceName;

      private void load(String url) {
      WebEngine engine = webView.getEngine();
      engine.load("about:blank");
      engine.load(url);

      resourceName.setText("Resource to be loaded: " + url);
      }

      @Override
      public void start(Stage primaryStage) throws Exception {
      primaryStage.setTitle("JavaFX WebView demo");

              String javaVersion = System.getProperty("java.version");
              String javafxVersion = System.getProperty("javafx.version");
              Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
              
              resourceName = new TextField();
              resourceName.setMaxWidth(Double.MAX_VALUE);
                      
              VBox box = new VBox();
              box.getChildren().add(l);
              box.getChildren().add(resourceName);
              
              webView = new WebView();
             
              webView.setMinSize(800, 600);
              VBox.setVgrow(webView, Priority.ALWAYS);
                      
              Button remoteHtml = new Button("Load remote Web site.");
              box.getChildren().add(remoteHtml);
              remoteHtml.setOnAction(evt->load("https://openjfx.io/"));
              
              URL localHtmlFile = Paths.get("testfiles/HelloWorld.html").toUri().toURL();
              Button localHtml = new Button("Load local HTML file.");
              box.getChildren().add(localHtml);
              localHtml.setOnAction(evt->load(localHtmlFile.toString()));
              
              Button remoteMarkDown = new Button("Load remote Markdown file.");
              box.getChildren().add(remoteMarkDown);
              remoteMarkDown.setOnAction(evt->load("https://raw.githubusercontent.com/openjdk/jfx/master/README.md"));
              
              Path localMdFile = Paths.get("testfiles/HelloWorld.MD");
              URL localMarkDownFile = localMdFile.toUri().toURL();
              Button localMarkDown = new Button("Load local Markdown (MD) file.");
              box.getChildren().add(localMarkDown);
              localMarkDown.setOnAction(evt->load(localMarkDownFile.toString()));
              
              URL localTextFile = Paths.get("testfiles/HelloWorld.TXT").toUri().toURL();
              Button localText = new Button("Load local TXT file.");
              box.getChildren().add(localText);
              localText.setOnAction(evt->load(localTextFile.toString()));
             
              Button localMdFileQuickView = new Button("View local MD file");
              localMdFileQuickView.setOnAction(evt->{
               Alert alert = new Alert(AlertType.INFORMATION);
               alert.setHeaderText("QuickView of local MD file: " + localMdFile.toAbsolutePath());
              
               try {
      String contents = Files.readString(localMdFile);
      alert.setContentText(contents);
      } catch (IOException e) {
      alert.setContentText("Failed to read local MD file");
      }
              
               Platform.runLater(()->alert.showAndWait());
              
              });
              box.getChildren().add(localMdFileQuickView);
              box.getChildren().add(webView);
              
              Scene scene = new Scene(box, 800, 700);
              primaryStage.setScene(scene);
              
              primaryStage.show();
              
              
      }


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

      /*
       * testfiles/HelloWorld.html
       *
       * <html>
       * <body>
       * <h1>Hello HTML!</h1>
       * <p>
       * This HTML content is loaded from a local file system resource.
       * </p>
       * </body>
       * </html>
       *
       */

      /*
       * testfiles/HelloWorld.MD
       *
       * # Markdown
       *
       * Markdown can be quite useful.
       *
       * ## Chapter 1
       *
       * Some text may follow here.
       *
       * ## Chapter 2
       *
       * This markdown is loaded from local file system.
       *
       *
       */

      /*
       * testfiles/HelloWorld.TXT
       *
       * # Markdown
       *
       * Markdown can be quite useful.
       *
       * ## Chapter 1
       *
       * Some text may follow here.
       *
       * ## Chapter 2
       *
       * This markdown is loaded from local file system.
       *
       *
       */
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      (1) A possible method would be to create a local copy of the local Markdown file with an extension TXT instead of MD.
      (2) Then loading the TXT file instead the MD file

      FREQUENCY : always


        1. App.java
          3 kB
          Praveen Narayanaswamy

            gpattnaik Gopal Pattnaik
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: