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

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

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

Whitelist.addProtocols介绍

[英]Add allowed URL protocols for an element's URL attribute. This restricts the possible values of the attribute to URLs with the defined protocol.

E.g.: addProtocols("a", "href", "ftp", "http", "https")

To allow a link to an in-page URL anchor (i.e. <a href="#anchor">, add a #:
E.g.: addProtocols("a", "href", "#")
[中]为元素的URL属性添加允许的URL协议。这将属性的可能值限制为具有已定义协议的URL。
例如:addProtocols("a", "href", "ftp", "http", "https")
要允许链接到页面内URL锚(即<a href="#anchor">,请添加#
例如:addProtocols("a", "href", "#")

代码示例

代码示例来源: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: org.jsoup/jsoup

.addAttributes("ul", "type")
.addProtocols("a", "href", "ftp", "http", "https", "mailto")
.addProtocols("blockquote", "cite", "http", "https")
.addProtocols("cite", "cite", "http", "https")
.addProtocols("img", "src", "http", "https")
.addProtocols("q", "cite", "http", "https")

代码示例来源: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: 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: org.hibernate.validator/hibernate-validator

whitelist.addProtocols( tag.name(), attribute.name(), attribute.protocols() );

相关文章