Skip to content

Instantly share code, notes, and snippets.

@weitzj
Created May 11, 2014 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weitzj/a35438be0e4162180fa0 to your computer and use it in GitHub Desktop.
Save weitzj/a35438be0e4162180fa0 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Node;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
/**
* User: Jan Weitz
* Date: 11/05/14
* Time: 19:24
*/
public class LargeTagNames extends Application {
private static final String TEST_LOWERCASE_HTML = "" +
"<html>\n" +
" <head>\n" +
" <title>MyTitle</title>\n" +
" </head>\n" +
" <body>\n" +
" <div class=\"lowerCaseClass\">\n" +
" All Tags are UPPERCASE!\n" +
" </div>\n" +
" </body>\n" +
"</html>";
public static void main(String[] args) {
Application.launch(LargeTagNames.class);
}
@Override
public void start(Stage primaryStage) throws Exception {
WebView webView = new WebView();
//Show the pageSource from the webEngine in stdout once the page is loaded.
webView.getEngine().getLoadWorker().stateProperty().addListener(
(ov, oldState, newState) -> {
if (Worker.State.SUCCEEDED == newState) {
System.out.println("############################################################################");
System.out.println("ORIGINAL CONTENT:\n" + TEST_LOWERCASE_HTML);
System.out.println("\n\n############################################################################");
System.out.println("WEBVIEW DOCUMENT:\n" + nodeToString(webView.getEngine().getDocument()));
System.out.println("\n\n############################################################################");
System.out.println("JAVASCRIPT OUTERHTML:\n" + webView.getEngine().executeScript("document.documentElement.outerHTML"));
Platform.runLater(() -> Platform.exit());
}
}
);
webView.getEngine().loadContent(TEST_LOWERCASE_HTML);
primaryStage.setScene(new Scene(webView));
}
private static String nodeToString(final Node node) {
if (null == node) return null;
StringWriter writer = new StringWriter();
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
@weitzj
Copy link
Author

weitzj commented May 11, 2014

Output:

############################################################################
ORIGINAL CONTENT:
<html>
  <head>
    <title>MyTitle</title>
  </head>
  <body>
    <div class="lowerCaseClass">
      All Tags are UPPERCASE!
    </div>
  </body>
</html>


############################################################################
WEBVIEW DOCUMENT:
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD>
    <TITLE>MyTitle</TITLE>
  </HEAD>
  <BODY>
    <DIV class="lowerCaseClass">
      All Tags are UPPERCASE!
    </DIV>

</BODY></HTML>


############################################################################
JAVASCRIPT OUTERHTML:
<html><head>
    <title>MyTitle</title>
  </head>
  <body>
    <div class="lowerCaseClass">
      All Tags are UPPERCASE!
    </div>

</body></html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment