gpt4 book ai didi

java - Tidy 断开与非拉丁字符的链接

转载 作者:行者123 更新时间:2023-11-30 11:58:55 30 4
gpt4 key购买 nike

我使用 Java 库 Tidy 来清理 html 代码。一些代码包含带有俄文字母的链接。例如

<a href="http://example.com/Русский">link with Russian letters</a>

我知道必须对“Русский”进行转义,但我从用户那里得到了这个 html。我的工作是将其转换为 XHTML。

我认为 tidy 试图转义非拉丁字母,但结果我得到了

<a href="http://example.com/%420%443%441%441%43A%438%439">link with Russian letters</a>

这不正确。正确的版本是

<a href="http://example.com/%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9">link with Russian letters</a>

Java代码是

private static Tidy getTidy() {
if (null == tidy) {
tidy = new Tidy();
tidy.setQuiet(true);
tidy.setShowErrors(0);
tidy.setShowWarnings(false);
tidy.setXHTML(true);
tidy.setOutputEncoding("UTF-8");
}
return tidy;
}

public static String sanitizeHtml(String html, URI pageUri) {
boolean escapeMedia = false;
String ret = "";
try {
Document doc = getTidy().parseDOM(new StringReader("<body>" + html + "</body>"), null);

// here I make some processing

// string output
ByteArrayOutputStream out = new ByteArrayOutputStream();
Node node = doc.getElementsByTagName("body").item(0);
getTidy().pprint(node, out);
ret = out.toString().trim();
}
catch (Exception e) {
ret = html;
e.printStackTrace();
}

return ret;
}

最佳答案

这是一种硬编码行为,可能是一个错误。当他们应该使用 UTF-8 时,他们使用 UTF-16 来转义 URL 中的非 ASCII 字符。参见 org/w3c/tidy/AttrCheckImpl.java .

关于java - Tidy 断开与非拉丁字符的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2859984/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com