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

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

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

Whitelist.addEnforcedAttribute介绍

[英]Add an enforced attribute to a tag. An enforced attribute will always be added to the element. If the element already has the attribute set, it will be overridden with this value.

E.g.: addEnforcedAttribute("a", "rel", "nofollow") will make all a tags output as <a href="..." rel="nofollow">
[中]向标记添加强制属性。强制属性将始终添加到元素中。如果元素已经设置了该属性,则将使用该值覆盖它。
例如:addEnforcedAttribute("a", "rel", "nofollow")将所有a标记输出为<a href="..." rel="nofollow">

代码示例

代码示例来源: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: ZapBlasterson/crushpaper

@Override
  protected Whitelist initialValue() {
    return Whitelist.relaxed()
        .addEnforcedAttribute("a", "rel", "nofollow")
        .addEnforcedAttribute("a", "target", "_blank");
  }
};

代码示例来源: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);
  
}

相关文章