Show HTML in WebView using Prism.js

天涯浪子 提交于 2019-12-22 14:15:36

问题


I am having trouble showing HTML in JavaFX's WebVew using Prism.js. I am able to successfully show a Java example, but the HTML example seems to want to show the HTML as it appears when you visit a site instead of just showing the syntax string. What is the correct way to display HTML in JavaFX's WebView using Prism.js?

Java Example (Displaying correctly)

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication319 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        String javaString = "/* HelloWorld.java\n"
                + " */\n"
                + "\n"
                + "public class HelloWorld\n"
                + "{\n"
                + " public static void main(String[] args) {\n"
                + "     System.out.println(\"Hello World!\");\n"
                + " }\n"
                + "}";

        String htmlString = "<!DOCTYPE html>\n"
                + "<html>\n"
                + "<head>"
                + "<link href=\"" + getClass().getResource("prism.css") + "\"" + " rel=\"stylesheet\"" + " type=\"text/css\"" + " />\n"
                + "<script src=\"" + getClass().getResource("prism.js") + "\"" + " type=\"text/javascript\"" + "></script>\n"
                + "</head>"
                + "<body>\n"
                + "\n"
                + "<h1>My First Heading</h1>\n"
                + "\n"
                + "<p>My first paragraph.</p>\n"
                + "\n"
                + "<pre>"
                + " <code class=\"language-java\">\n"
                + javaString
                + "</code>\n"
                + "</pre>\n"
                + "</body>\n"
                + "</html>";

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.setJavaScriptEnabled(true);
        engine.loadContent(htmlString);

        StackPane root = new StackPane(webView);

        Scene scene = new Scene(root, 500, 400);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

HTML Example (Not displaying correctly)

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication319 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        String htmlStringPreCode = "<!DOCTYPE html>\n"
                + "<html>\n"
                + "<head>\n"
                + "</head>\n"
                + "<body>\n"
                + "This is a Test\n"
                + "</body>\n"
                + "</html>\n";

        String htmlString = "<!DOCTYPE html>\n"
                + "<html>\n"
                + "<head>"
                + "<link href=\"" + getClass().getResource("prism.css") + "\"" + " rel=\"stylesheet\"" + " type=\"text/css\"" + " />\n"
                + "<script src=\"" + getClass().getResource("prism.js") + "\"" + " type=\"text/javascript\"" + "></script>\n"
                + "</head>"
                + "<body>\n"
                + "\n"
                + "<h1>My First Heading</h1>\n"
                + "\n"
                + "<p>My first paragraph.</p>\n"
                + "\n"
                + "<pre>"
                + " <code class=\"language-html\">\n"
                + htmlStringPreCode
                + "</code>\n"
                + "</pre>\n"
                + "</body>\n"
                + "</html>";

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.setJavaScriptEnabled(true);
        engine.loadContent(htmlString);

        StackPane root = new StackPane(webView);

        Scene scene = new Scene(root, 500, 400);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

Expected output

Java: 1.8.0_171

Prism.js Download in the picture has Java selected also


回答1:


Html code needs to be escaped first (so it can be displayed as a plain text).

See this question, both solutions work:

htmlStringPreCode = htmlStringPreCode.replace("<", "&lt;");

//or

htmlStringPreCode = "<script type=\"prism-html-markup\">" + htmlStringPreCode + "</script>";


来源:https://stackoverflow.com/questions/54079253/show-html-in-webview-using-prism-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!