org.jsoup.safety.Whitelist.addTags()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中org.jsoup.safety.Whitelist.addTags()方法的一些代码示例,展示了Whitelist.addTags()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Whitelist.addTags()方法的具体详情如下:
包路径:org.jsoup.safety.Whitelist
类名称:Whitelist
方法名:addTags

Whitelist.addTags介绍

[英]Add a list of allowed elements to a whitelist. (If a tag is not allowed, it will be removed from the HTML.)
[中]将允许的元素列表添加到白名单。(如果不允许标记,它将从HTML中删除。)

代码示例

代码示例来源:origin: org.jsoup/jsoup

/**
 This whitelist allows only simple text formatting: <code>b, em, i, strong, u</code>. All other HTML (tags and
 attributes) will be removed.
 @return whitelist
 */
public static Whitelist simpleText() {
  return new Whitelist()
      .addTags("b", "em", "i", "strong", "u")
      ;
}

代码示例来源:origin: org.jsoup/jsoup

/**
 This whitelist allows the same text tags as {@link #basic}, and also allows <code>img</code> tags, with appropriate
 attributes, with <code>src</code> pointing to <code>http</code> or <code>https</code>.
 @return whitelist
 */
public static Whitelist basicWithImages() {
  return basic()
      .addTags("img")
      .addAttributes("img", "align", "alt", "height", "src", "title", "width")
      .addProtocols("img", "src", "http", "https")
      ;
}

代码示例来源:origin: org.jsoup/jsoup

/**
 <p>
 This whitelist allows a fuller range of text nodes: <code>a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li,
 ol, p, pre, q, small, span, strike, strong, sub, sup, u, ul</code>, and appropriate attributes.
 </p>
 <p>
 Links (<code>a</code> elements) can point to <code>http, https, ftp, mailto</code>, and have an enforced
 <code>rel=nofollow</code> attribute.
 </p>
 <p>
 Does not allow images.
 </p>
 @return whitelist
 */
public static Whitelist basic() {
  return new Whitelist()
      .addTags(
          "a", "b", "blockquote", "br", "cite", "code", "dd", "dl", "dt", "em",
          "i", "li", "ol", "p", "pre", "q", "small", "span", "strike", "strong", "sub",
          "sup", "u", "ul")
      .addAttributes("a", "href")
      .addAttributes("blockquote", "cite")
      .addAttributes("q", "cite")
      .addProtocols("a", "href", "ftp", "http", "https", "mailto")
      .addProtocols("blockquote", "cite", "http", "https")
      .addProtocols("cite", "cite", "http", "https")
      .addEnforcedAttribute("a", "rel", "nofollow")
      ;
}

代码示例来源:origin: k9mail/k-9

HtmlSanitizer() {
  Whitelist whitelist = Whitelist.relaxed()
      .addTags("font", "hr", "ins", "del", "center", "map", "area")
      .addAttributes("font", "color", "face", "size")
      .addAttributes("table", "align", "background", "bgcolor", "border", "cellpadding", "cellspacing",
          "width")
      .addAttributes("tr", "align", "background", "bgcolor", "valign")
      .addAttributes("th",
          "align", "background", "bgcolor", "colspan", "headers", "height", "nowrap", "rowspan", "scope",
          "sorted", "valign", "width")
      .addAttributes("td",
          "align", "background", "bgcolor", "colspan", "headers", "height", "nowrap", "rowspan", "scope",
          "valign", "width")
      .addAttributes("map", "name")
      .addAttributes("area", "shape", "coords", "href", "alt")
      .addProtocols("area", "href", "http", "https")
      .addAttributes("img", "usemap")
      .addAttributes(":all", "class", "style", "id", "dir")
      .addProtocols("img", "src", "http", "https", "cid", "data")
      .addProtocols("a", "href", "tel", "sip", "bitcoin", "ethereum", "rtsp");
  cleaner = new Cleaner(whitelist);
  headCleaner = new HeadCleaner();
}

代码示例来源:origin: org.jsoup/jsoup

.addTags(
    "a", "b", "blockquote", "br", "caption", "cite", "code", "col",
    "colgroup", "dd", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6",

代码示例来源:origin: tomoya92/pybbs

content = Jsoup.clean(content, Whitelist.relaxed().addTags("code", "pre").addAttributes("code", "class"));
Document parse = Jsoup.parse(content);
Elements tableElements = parse.select("table");

代码示例来源:origin: ron190/jsql-injection

.replaceAll("<input.*>", "<div style=\"text-align:center;border:1px solid black;width:100px;\">input</div>"),
Whitelist.relaxed()
  .addTags("center", "div", "span")
  .addAttributes(":all", "style")

代码示例来源:origin: ron190/jsql-injection

.replaceAll("<input.*>", "<div style=\"text-align:center;border:1px solid black;width:100px;\">input</div>"),
  Whitelist.relaxed()
    .addTags("center", "div", "span")
    .addAttributes(":all", "style")
);

代码示例来源:origin: stackoverflow.com

String cleanXmlAndRemoveUnwantedTags(String textToEscape) {
  Whitelist whitelist = Whitelist.none();
  whitelist.addTags(allowedTags);

  OutputSettings outputSettings = new OutputSettings()
          .syntax(OutputSettings.Syntax.xml)
          .charset(StandardCharsets.UTF_8)
          .prettyPrint(false);

  String safe = Jsoup.clean(textToEscape, "", whitelist, outputSettings);
  return safe;
}

代码示例来源:origin: stackoverflow.com

Whitelist whitelist = Whitelist.none();
 whitelist
   .addTags("a")
   .addAttributes("a", "href")
   .addProtocols("a", "href", "http", "https", "mailto");
 String safeText = Jsoup.clean(untrustedText, whitelist);

代码示例来源:origin: stackoverflow.com

public String clean(String unsafe){
   Whitelist whitelist = Whitelist.none();
   whitelist.addTags(new String[]{"p","br","ul"});
   String safe = Jsoup.clean(unsafe, whitelist);
   return StringEscapeUtils.unescapeXml(safe);
}

代码示例来源:origin: stackoverflow.com

String html = "<html><head></head><body><div style='padding-top:25px;' onclick='javascript.alert('hi');'>This is a sample div <span class='sampleclass'>This is a simple span</span></div></body></html>";

Whitelist wl = Whitelist.simpleText();
wl.addTags("div", "span"); // add additional tags here as necessary
String clean = Jsoup.clean(html, wl);
System.out.println(clean);

代码示例来源:origin: stackoverflow.com

String html = "<body><p class='default'> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> <b>Hello World</b> </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> , Testing </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> <i><b>Font </b></i> </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> Style </span> <span style='color: #000000; font-size: 21pt; font-family: MyriadPro-Bold;'> <i>Check</i> </span> <span style='color: #000000; font-size: 10pt; font-family: MyriadPro-Bold;'> </span> </p></body>";

Whitelist wl = Whitelist.simpleText();
wl.addTags("b", "i"); // add additional tags here as necessary
String clean = Jsoup.clean(html, wl);
System.out.println(clean);

代码示例来源:origin: stackoverflow.com

String html = "<div id='someId' style='color: #000000'><p class='someClass'>Some Text</p><img src='images/someimage.jpg' alt='' /><a href='somelink.html'>Some Link Text</a></div>";
Whitelist whitelist = Whitelist.simpleText(); // Whitelist.simpleText() allows b, em, i, strong, u. Use Whitelist.none() instead if you want to start clean.
whitelist.addTags("div", "p");
String clean = Jsoup.clean(html, whitelist);
System.out.println(clean);

代码示例来源:origin: stackoverflow.com

Whitelist myCustomWhitelist = new Whitelist();
myCustomWhitelist.addTags("b", "em", ...);

代码示例来源:origin: zhangyd-c/OneBlog

/**
 * 自定义的白名单
 *
 * @return
 */
private static Whitelist custome() {
  return Whitelist.none().addTags("p", "strong", "pre", "code", "span", "blockquote", "br").addAttributes("span", "class");
}

代码示例来源:origin: info.magnolia/magnolia-module-rssaggregator

/**
 * Format and sanitize description input.
 *
 * @param origDescription original description as delivered by feed fetcher.
 * @param abbreviation gives the amount of characters used for abbreviation.
 * @return sanitized HTML output string.
 */
public static String formatDescription(String origDescription, Integer abbreviation) {
  // replace Java linebreaks with HTML
  //origDescription = StringUtils.replace(origDescription, "\n", "<br/>");
  // check if abbreviation is wanted
  if (abbreviation != null && abbreviation > 0) {
    origDescription = StringUtils.abbreviate(origDescription, abbreviation);
  }
  // Sanitize HTML input
  Whitelist whitelist = Whitelist.basicWithImages();
  whitelist.addTags("h1", "h2", "h3", "h4", "h5", "h6", "div");
  // table tags
  whitelist.addTags("table", "tbody", "td", "tfoot", "th", "thead", "tr");
  return Jsoup.clean(origDescription, whitelist);
}

代码示例来源:origin: inception-project/inception

public static String cleanHighlight(String aHighlight) {
    Whitelist wl = new Whitelist();
    wl.addTags("em");
    Document dirty = Jsoup.parseBodyFragment(aHighlight, "");
    Cleaner cleaner = new Cleaner(wl);
    Document clean = cleaner.clean(dirty);
    clean.select("em").tagName("mark");

    return clean.body().html();
  }
}

代码示例来源:origin: com.vaadin/vaadin-rich-text-editor-flow

String sanitize(String html) {
  return org.jsoup.Jsoup.clean(html,
          org.jsoup.safety.Whitelist.basic()
          .addTags("img", "h1", "h2", "h3", "s")
          .addAttributes("img", "align", "alt", "height", "src", "title", "width")
          .addAttributes(":all", "style")
          .addProtocols("img", "src", "data"));
}

代码示例来源:origin: IQSS/dataverse

/**
 * Wrapper around Jsoup clean method with the basic White list
 *   http://jsoup.org/cookbook/cleaning-html/whitelist-sanitizer
 * @param unsafe
 * @return 
 */
public static String sanitizeBasicHTML(String unsafe){
  
  if (unsafe == null){
    return null;
  }
  // basic includes: a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li, ol, p, pre, q, small, span, strike, strong, sub, sup, u, ul
  //Whitelist wl = Whitelist.basic().addTags("img", "h1", "h2", "h3", "kbd", "hr", "s", "del");  
  Whitelist wl = Whitelist.basicWithImages().addTags( "h1", "h2", "h3", "kbd", "hr", "s", "del","map","area").addAttributes("img", "usemap")
      .addAttributes("map", "name").addAttributes("area", "shape","coords","href","title","alt")
      .addEnforcedAttribute("a", "target", "_blank");
  return Jsoup.clean(unsafe, wl);
  
}

相关文章