I'd like to generate an HTML document with JSoup (version 1.15.3). The HTML document should contain script with a comparison.
This is the code to create a script element and to fill it with some JavaScript:
Element script = document.getElementsByTag("head").first().appendElement("script");
script.html("var i=0;if(i<5){alert(123);}");
The generated HTML code contains this:
<script>var i=0;if(i<5){alert(123);}</script>
In most other cases I expect that JSoup escapes characters like >
and <
, but in a script
element these characters are intended as-is and I do not want them to get replaced by >
or <
.
How can I give JSoup the hint not to escape content of a script element?
CodePudding user response:
Use DataNode
Element script = document.getElementsByTag("head").first().appendElement("script");
script.appendChild(new DataNode("var i=0;if(i<5){alert(123);}"));