After generating a JSoup Document with two
<script type="application/javascript">/* .. */</script>
elements, I encountered an issue.
The Problem: Whenever I use .html()
or .toString()
in JSoup, my JavaScript code gets escaped.
if (foo && bar)
turns into
if (foo && bar)
Is there a way to configure JSoup to disregard escaping for <script>
Elements??
This is how I typically create my jsoup document.
final Document doc = Document.createShell("");
final Element head = doc.head();
head.appendElement("meta").attr("charset", "utf-8");
final String myJS = ...;
head.appendElement("script").attr("type", "application/javascript").text(myJS);
Currently, my solution involves using String.replace
on .html()
and replacing a placeholder. But this method feels like a temporary fix.
head.appendElement("script").attr("type", "application/javascript").text("$MYJS$");
String s = doc.html();
s = s.replace("$MYJS$", myJS);