How can I set the encoding of a JavaFX WebView?

I'm having an encoding issue with JavaFX's WebView. When loading a UTF-8 encoded file, special characters are displayed incorrectly (e.g. รข€™ is displayed instead of ). Here's an SSCCE:

WebViewTest.java

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

public class WebViewTest extends Application {

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

    @Override
    public void start(Stage stage) {
        WebView webView = new WebView();
        webView.getEngine().load(getClass().getResource("/test.html").toExternalForm());

        Scene scene = new Scene(webView, 500, 500);
        stage.setScene(scene);
        stage.setTitle("WebView Test");
        stage.show();
    }

}

test.html

<!DOCTYPE html>
<html>
  <body>
      <p>RIGHT SINGLE QUOTATION MARK: ’</p>
  </body>
</html>

Output of file -bi test.html

src:$ file -bi test.html
text/plain; charset=utf-8

Result:
WebView encoding issue

The same thing happens in Windows using Java 17 and the latest JavaFX (I used Linux and Java 8 for the demonstration).

I've tried:

  • Declaring the charset in the HTML: <meta charset="UTF-8">

    (works, but I'm making an editor program, so I don't have control over the HTML)

  • Using the JVM argument -Dfile.encoding=UTF-8 (doesn't work)

  • Setting the charset using reflection (doesn't work, and throws an exception in newer Java versions):

    System.setProperty("file.encoding","UTF-8");
    Field charset = Charset.class.getDeclaredField("defaultCharset");
    charset.setAccessible(true);
    charset.set(null,null);
    
  • Declaring the charset after the page loads using the DOM API (doesn't work):

    webView.getEngine().getLoadWorker().stateProperty().addListener((o, oldState, newState) -> {
        if(newState == Worker.State.SUCCEEDED) {
            Document document = webView.getEngine().getDocument();
            Element meta = document.createElement("meta");
            meta.setAttribute("charset", "UTF-8");
    
            document.getElementsByTagName("html").item(0).appendChild(meta);
        }
    });
    
  • Using WebEngine.loadContent(String) instead of load(String) (wouldn't work; relative links would be broken)

It appears that WebView ignores file encodings, and uses ISO-8859-1 unless a charset is specified in the HTML.



from Recent Questions - Stack Overflow https://ift.tt/3GiW3PS
https://ift.tt/3Kez7Uz

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)