mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
38 lines
1.1 KiB
Java
38 lines
1.1 KiB
Java
|
|
package com.commafeed.backend.feeds;
|
||
|
|
|
||
|
|
import org.apache.commons.lang.StringUtils;
|
||
|
|
import org.jsoup.Jsoup;
|
||
|
|
import org.jsoup.nodes.Document.OutputSettings;
|
||
|
|
import org.jsoup.nodes.Entities.EscapeMode;
|
||
|
|
import org.jsoup.safety.Whitelist;
|
||
|
|
|
||
|
|
public class FeedUtils {
|
||
|
|
|
||
|
|
public static String handleContent(String content) {
|
||
|
|
if (StringUtils.isNotBlank(content)) {
|
||
|
|
content = trimUnicodeSurrogateCharacters(content);
|
||
|
|
Whitelist whitelist = Whitelist.relaxed();
|
||
|
|
whitelist.addEnforcedAttribute("a", "target", "_blank");
|
||
|
|
|
||
|
|
whitelist.addTags("iframe");
|
||
|
|
whitelist.addAttributes("iframe", "src", "height", "width",
|
||
|
|
"allowfullscreen", "frameborder");
|
||
|
|
|
||
|
|
content = Jsoup.clean(content, "", whitelist,
|
||
|
|
new OutputSettings().escapeMode(EscapeMode.base));
|
||
|
|
}
|
||
|
|
return content;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String trimUnicodeSurrogateCharacters(String text) {
|
||
|
|
StringBuilder sb = new StringBuilder();
|
||
|
|
for (int i = 0; i < text.length(); i++) {
|
||
|
|
char ch = text.charAt(i);
|
||
|
|
if (!Character.isHighSurrogate(ch) && !Character.isLowSurrogate(ch)) {
|
||
|
|
sb.append(ch);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return sb.toString();
|
||
|
|
}
|
||
|
|
}
|