【发布时间】:2015-10-25 17:12:53
【问题描述】:
我创建了一个 JSoup 文档,其中包含两个 <script type="application/javascript">/* .. */</script> 元素。
问题:当我调用 .html() 或 .toString() 时,JSoup 会转义我的 JavaScript。
if (foo && bar)
得到
if (foo && bar)
是否可以将 JSoup 配置为在转义时忽略 <script> 元素??
这(基本上)是我创建 jsoup 文档的方式。
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);
我目前的解决方法是在.html() 上用String.replace 替换占位符。但这有点 hacky。
head.appendElement("script").attr("type", "application/javascript").text("$MYJS$");
String s = doc.html();
s = s.replace("$MYJS$", myJS);
【问题讨论】:
-
我已经更新了我的答案。
标签: javascript java jsoup