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

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

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

Whitelist.addAttributes介绍

[英]Add a list of allowed attributes to a tag. (If an attribute is not allowed on an element, it will be removed.)

E.g.: addAttributes("a", "href", "class") allows href and class attributes on a tags.

To make an attribute valid for all tags, use the pseudo tag :all, e.g. addAttributes(":all", "class").
[中]将允许的属性列表添加到标记。(如果元素上不允许使用某个属性,则该属性将被删除。)
例如:addAttributes("a", "href", "class")允许a标记上的hrefclass属性。
要使属性对所有标记有效,请使用伪标记:all,例如addAttributes(":all", "class")

代码示例

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

"ul")
.addAttributes("a", "href", "title")
.addAttributes("blockquote", "cite")
.addAttributes("col", "span", "width")
.addAttributes("colgroup", "span", "width")
.addAttributes("img", "align", "alt", "height", "src", "title", "width")
.addAttributes("ol", "start", "type")
.addAttributes("q", "cite")
.addAttributes("table", "summary", "width")
.addAttributes("td", "abbr", "axis", "colspan", "rowspan", "width")
.addAttributes(
    "th", "abbr", "axis", "colspan", "rowspan", "scope",
    "width")
.addAttributes("ul", "type")

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

Whitelist.relaxed()
  .addTags("center", "div", "span")
  .addAttributes(":all", "style")

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

Whitelist.relaxed()
      .addTags("center", "div", "span")
      .addAttributes(":all", "style")
  );
} catch (IOException e) {

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

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

Whitelist whitelist=new Whitelist();

Cleaner cleaner = new Cleaner(whitelist);

whitelist.addAttributes("a","accesskey","dir","lang","style","tabindex","title","href");

cleaner.clean(doc);

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

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

代码示例来源: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: jungilhan/awesome-blogs-android

"span", "font",
                   "del", "strike", "s")
               .addAttributes("th", "colspan", "align", "style")
               .addAttributes("td", "colspan", "align", "style")
               .addAttributes(":all", "title", "style");
if(options.preserveRelativeLinks) {
  whitelist.preserveRelativeLinks(true);
    whitelist.addAttributes("h"+i, "id");
  whitelist.addTags(el.getTagName());
  if(!el.getAttributes().isEmpty()) {
    whitelist.addAttributes(el.getTagName(), el.getAttributes().toArray(new String[el.getAttributes().size()]));

代码示例来源:origin: com.eduworks/ew.levr.base

for (int i = 0; i < allowAttributes.length(); i++) {
  JSONObject attribute = allowAttributes.getJSONObject(i);
  wl.addAttributes(attribute.getString("element"), attribute.getString("attribute"));

代码示例来源:origin: USPTO/PatentPublicData

whitelist.addAttributes(":all", HTML_WHITELIST_ATTRIB);

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

whitelist.addAttributes(":all", ALLOWED_HTML_ATTRIBUTES);
result = Jsoup.clean(doc.body().html(), "", whitelist, settings);

代码示例来源:origin: br.com.anteros/Anteros-Bean-Validation

public void initialize(SafeHtml safeHtmlAnnotation) {
  switch ( safeHtmlAnnotation.whitelistType() ) {
    case BASIC:
      whitelist = Whitelist.basic();
      break;
    case BASIC_WITH_IMAGES:
      whitelist = Whitelist.basicWithImages();
      break;
    case NONE:
      whitelist = Whitelist.none();
      break;
    case RELAXED:
      whitelist = Whitelist.relaxed();
      break;
    case SIMPLE_TEXT:
      whitelist = Whitelist.simpleText();
      break;
  }
  whitelist.addTags( safeHtmlAnnotation.additionalTags() );
  for ( SafeHtml.Tag tag : safeHtmlAnnotation.additionalTagsWithAttributes() ) {
    whitelist.addAttributes( tag.name(), tag.attributes() );
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.hibernate-validator

@Override
public void initialize(SafeHtml safeHtmlAnnotation) {
  switch ( safeHtmlAnnotation.whitelistType() ) {
    case BASIC:
      whitelist = Whitelist.basic();
      break;
    case BASIC_WITH_IMAGES:
      whitelist = Whitelist.basicWithImages();
      break;
    case NONE:
      whitelist = Whitelist.none();
      break;
    case RELAXED:
      whitelist = Whitelist.relaxed();
      break;
    case SIMPLE_TEXT:
      whitelist = Whitelist.simpleText();
      break;
  }
  whitelist.addTags( safeHtmlAnnotation.additionalTags() );
  for ( SafeHtml.Tag tag : safeHtmlAnnotation.additionalTagsWithAttributes() ) {
    whitelist.addAttributes( tag.name(), tag.attributes() );
  }
}

代码示例来源:origin: FINRAOS/herd

whitelist.addTags(whitelistTag).addAttributes(whitelistTag, "class");

代码示例来源:origin: org.hibernate.validator/hibernate-validator

whitelist.addTags( tag.name() );
if ( tag.attributes().length > 0 ) {
  whitelist.addAttributes( tag.name(), tag.attributes() );
  whitelist.addAttributes( tag.name(), attribute.name() );

代码示例来源:origin: org.finra.herd/herd-core

whitelist.addTags(whitelistTag).addAttributes(whitelistTag, "class");

相关文章