简体   繁体   English

JavaFX WebView无法正确呈现CSS / HTML文本

[英]JavaFX WebView doesn't Render CSS/HTML Text Correctly

As the Title indicates, I'm attempting to write CSS/HTML that will render in a WebView object, but the HTML I'm writing isn't rendering correctly. 如标题所示,我试图编写将在WebView对象中呈现的CSS / HTML,但是正在编写的HTML不能正确呈现。

This is the sample text I'm using for testing: 这是我用于测试的示例文本:

test.html test.html

<style>
.rainbow {
  background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
  background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
  color:transparent;
  -webkit-background-clip: text;
  background-clip: text;
}
</style>

<table border=1>
<tr><th>Header 1</th><th>Header 2</th></tr>
<tr><td>Entry 1-1</td><td>Entry 1-2</td></tr>
<tr><td>Entry 2-1</td><td>Entry 2-2</td></tr>
</table>

<p>This is a personal manifesto that I'm going to write here and hope that noone sees it because I'm really not certain that anyone is going to be happy if they see it. Prepare yourself, because this is going to be super controversial and politicial.</p>

*Deep breath*

<h1><span class="rainbow">Big Bang Theory Sucks!</span></h1>

If I just load this into a simple text document .html and load it with my web browser, it works just fine: 如果我只是将其加载到一个简单的文本文档.html ,并通过Web浏览器加载它,那么它将正常工作:

超级有争议的东西

However, the following simple JavaFX code renders the html incorrectly: 但是,以下简单的JavaFX代码会错误地呈现html:

Main.java Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = FXMLLoader.load(getClass().getResource("/application/WebViewAndTextInput.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();

            TextArea textArea = (TextArea) root.getLeft();
            WebView webView = (WebView) root.getRight();
            webView.getEngine().loadContent(textArea.getText());
            textArea.textProperty().addListener((e) -> {
                webView.getEngine().loadContent(textArea.getText());
            });

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

WebViewAndTextInput.fxml WebViewAndTextInput.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.web.WebView?>


<BorderPane prefHeight="450.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
   <right>
      <WebView prefHeight="-1.0" prefWidth="400.0" />
   </right>
   <left>
      <TextArea prefWidth="300.0" text="&lt;span style=&quot;color:#ff0000&quot;&gt;Hello World!&lt;/span&gt;" wrapText="true">
         <font>
            <Font name="Courier New" size="14.0" />
         </font>
      </TextArea>
   </left>
</BorderPane>

Result: 结果:

现在,经过审查的有争议的内容!

This is obviously disastrous, as now, my super controversial opinions are being censored by the incorrectly rendered text! 这显然是灾难性的,因为现在,我的超级有争议的观点被错误地呈现的文字所审查! Do I need to make a change to the WebView object to properly render this text, or is this a bug in WebView's rendering engine? 我是否需要更改WebView对象才能正确呈现此文本,或者这是WebView呈现引擎中的错误吗?

This might be a bug, but it may be in the used userAgent. 这可能是一个错误,但可能在使用的userAgent中。 You can chek your current userAgent by simply printing its information like; 您可以通过简单地打印其信息来检查您当前的userAgent;

System.out.println(web.getEngine().getUserAgent());

Based on the userAgent you might be able to determine what need to be done to make things work the way you want, and what limitations the current userAgenr has which makes things (not) work they way they do. 基于userAgent,您可能能够确定需要做什么才能使事情按您希望的方式运行,以及当前userAgenr的局限性使事情(或不使事情)按其方式工作。

As extra, this SO post explains a bit about userAgent retrieval. 另外,这篇SO帖子还介绍了有关userAgent检索的内容。 And this blog post might provide a bit more information and usefull tools on a simular useragent related problem. 并且此博客文章可能会提供有关模拟用户代理相关问题的更多信息和有用的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM