/** * Remove an attribute from every matched element. * @param attributeKey The attribute to remove. * @return this (for chaining) */ public Elements removeAttr(String attributeKey) { for (Element element : this) { element.removeAttr(attributeKey); } return this; }
public Node removeAttr(String attributeKey) { return originElement.removeAttr(attributeKey); }
@Override protected void configure(Element elem, String attrName, Object attrValue) { elem.removeAttr(ExtNodeConstants.ATTR_DATAREF_PREFIX_WITH_NS + attrName); elem.removeAttr(attrName); // for remove all class definition, we have to clear the // classNames too. if (attrName.equals("class")) { elem.classNames().clear(); } } },
protected void setSelectValue(Element select, String value) { for (Element opt : select.select("option")) { if (value.equals(opt.val())) { opt.attr("selected", "selected"); } else { opt.removeAttr("selected"); } } }
private void cleanStyles(Element articleContent) { // we want to clear off the style attributes in case they influence // something else. for (Element e : articleContent.getAllElements()) { e.removeAttr("style"); } }
/** * Remove attributes that are used only by Ogham: * <ul> * <li>{@link ImageInlinerConstants#INLINE_MODE_ATTR}</li> * <li>{@link ImageInlinerConstants#INLINED_ATTR}</li> * </ul> * * @param html * the html to clean * @return the cleaned html */ public static String removeOghamAttributes(String html) { Document doc = Jsoup.parse(html); Elements imgs = doc.select("img"); for (Element img : imgs) { img.removeAttr(INLINE_MODE_ATTR); img.removeAttr(INLINED_ATTR); } return doc.outerHtml(); }
/** * Transfers styles from the <code>data-cssstyle</code> attribute to the * <code>style</code> attribute. * * @param doc * the html document */ private void applyStyles(Document doc) { Elements allStyledElements = doc.getElementsByAttribute(TEMP_STYLE_ATTR); for (Element e : allStyledElements) { String newStyle = e.attr(TEMP_STYLE_ATTR); String oldStyle = e.attr(STYLE_ATTR); e.attr(STYLE_ATTR, (newStyle + "; " + oldStyle).replaceAll(";+", ";").trim()); e.removeAttr(TEMP_STYLE_ATTR); } }
/** * Transfers styles from the <code>cssstyle</code> attribute to the * <code>style</code> attribute. * * @param doc the html document */ private void applyStyles(Document doc) { Elements allStyledElements = doc.getElementsByAttribute(CSS_STYLE); for (Element e : allStyledElements) { String newStyle = e.attr(CSS_STYLE); String oldStyle = e.attr(STYLE_ATTR); e.attr(STYLE_ATTR, (newStyle + "; " + oldStyle).replace(";;", ";")); e.removeAttr(CSS_STYLE); } }
@Override protected void configure(Element elem, String attrName, Object attrValue) { if (attrValue instanceof String) { elem.removeAttr(ExtNodeConstants.ATTR_DATAREF_PREFIX_WITH_NS + attrName); elem.attr(attrName, attrValue.toString()); } else if (attrValue instanceof Number) { elem.removeAttr(ExtNodeConstants.ATTR_DATAREF_PREFIX_WITH_NS + attrName); elem.attr(attrName, attrValue.toString()); } else if (attrValue instanceof Boolean) { elem.removeAttr(ExtNodeConstants.ATTR_DATAREF_PREFIX_WITH_NS + attrName); elem.attr(attrName, attrValue.toString()); } else { String dataRefId = attrName + "_" + IdGenerator.createId(); Context context = Context.getCurrentThreadContext(); context.setData(Context.SCOPE_EXT_ATTR, dataRefId, attrValue); elem.removeAttr(attrName); elem.attr(ExtNodeConstants.ATTR_DATAREF_PREFIX_WITH_NS + attrName, dataRefId); } } },
@Override public Document runFilter(Document document) { Elements elementsWithAlign = document.getElementsByAttribute("align"); for (Element element : elementsWithAlign) { String align = element.attr("align"); if ("right".equalsIgnoreCase(align)) { element.attr("style", "text-align: right;"); } else if ("left".equalsIgnoreCase(align)) { element.attr("style", "text-align: left;"); } else if ("center".equalsIgnoreCase(align)) { element.attr("style", "text-align: center;"); } element.removeAttr("align"); } return document; } }
@Override public Document runFilter(Document document) { for (String tag : tags) { for (Element element : document.getElementsByTag(tag)) { String style = element.attr("style"); if (isNotBlank(style)) { if(style.contains("right")){ element.attr("align", "right"); }else if(style.contains("left")){ element.attr("align", "left"); }else if(style.contains("center")){ element.attr("align", "center"); } element.removeAttr("style"); } } } return document; } }
Document document = Jsoup.parse(html); Vector<String> allowedItems = new Vector<String>(); allowedItems.add("color"); allowedItems.add("font-size"); Elements e = document.getElementsByTag("span"); for (Element element : e) { String[] styles = element.attr("style").split(";"); Vector<String> filteredItems = new Vector<String>(); for (String item : styles) { String key = (item.split(":"))[0].trim().toLowerCase(); if ( allowedItems.contains(key) ){ filteredItems.add(item); } } if( filteredItems.size() == 0 ){ element.removeAttr("style"); }else{ element.attr("style",StringUtils.join(filteredItems, ";")); } }
protected void processFontColor(org.jsoup.nodes.Document document) { Elements elements = document.getElementsByTag("font"); for (Element element : elements) { String color = element.attr("color"); if (color != null) { String style = StringUtils.trim(element.attr("style")); if (style != null) { if (StringUtils.endsWith(style, ";")) { style = style + ";"; } style = style + "color:" + color; } else { style = "color:" + color; } element.attr("style", style); element.removeAttr("color"); } } } }
protected void processFontColor(org.jsoup.nodes.Document document) { Elements elements = document.getElementsByTag("font"); for (Element element : elements) { String color = element.attr("color"); if (color != null) { String style = StringUtils.trim(element.attr("style")); if (style != null) { if (StringUtils.endsWith(style, ";")) { style = style + ";"; } style = style + "color:" + color; } else { style = "color:" + color; } element.attr("style", style); element.removeAttr("color"); } } } }
@Override public Document runFilter(Document document) { for (Element span : document.getElementsByTag("span")) { String style = span.attr("style"); if (isNotBlank(style)) { String textDecoration = getSubAttributeValue(style, "text-decoration"); if ("underline".equalsIgnoreCase(textDecoration)) { span.removeAttr("style"); span.tagName("u"); } } } return document; }
Document doc = Jsoup.parse(input); NodeTraversor traversor = new NodeTraversor(new NodeVisitor() { @Override public void tail(Node node, int depth) { if (node instanceof Element) { Element e = (Element) node; e.removeAttr("width"); e.removeAttr("border"); e.removeAttr("bgcolor"); } } @Override public void head(Node node, int depth) { } }); traversor.traverse(doc.body()); String modifiedHtml = doc.toString();
private void clearFromActionAttributes(Element item) { item.attributes().asList().stream() .filter(attr -> attr.getKey().matches(ACTION_FORM_ATTRIBUTES_PATTERN)) .forEach(attr -> item.removeAttr(attr.getKey())); }
Document doc = Jsoup.parse(input); NodeTraversor traversor = new NodeTraversor(new NodeVisitor() { @Override public void tail(Node node, int depth) { if (node instanceof Element) { Element e = (Element) node; e.removeAttr("class"); e.removeAttr("style"); } } @Override public void head(Node node, int depth) { } }); traversor.traverse(doc.body()); String modifiedHtml = doc.toString();
private static void removeDisallowedAttributes(Element node) { for (Element childElement : node.children()) { removeDisallowedAttributes(childElement); } List<String> keysToRemove = new LinkedList<>(); for (Attribute attribute : node.attributes()) { if (!ATTRIBUTES_TO_RETAIN_IN_HTML.contains(attribute.getKey())) { keysToRemove.add(attribute.getKey()); } } for (String key: keysToRemove) { node.removeAttr(key); } }
/** * <p>processProperty.</p> * * @param propertyElement a {@link org.jsoup.nodes.Element} object. */ protected static void processProperty(Element propertyElement) { String xpath = propertyElement.attr("data-entity-property-xpath"); HtmlNode xpmMarkup = new HtmlCommentNode(String.format(FIELD_PATTERN, xpath)); if (propertyElement.childNodes().size() > 0) { if (!propertyXpmMarkupAlreadyGenerated(propertyElement)) { propertyElement.prepend(xpmMarkup.toHtml()); } } else { propertyElement.before(xpmMarkup.toHtml()); } propertyElement.removeAttr("data-entity-property-xpath"); }