public void remove() { attributes.remove(attr.getKey()); } }
@Override protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) { if ("src".equalsIgnoreCase(attr.getKey())) { String src = attr.getValue(); if (StrUtils.isNotBlank(src) && src.toLowerCase().startsWith("javascript")) { return false; } } return ("img".equals(tagName) && "src".equals(attr.getKey()) && attr.getValue().startsWith("data:;base64")) || super.isSafeAttribute(tagName, el, attr); } }
private void copyAttributes(org.jsoup.nodes.Node source, Element el) { for (Attribute attribute : source.attributes()) { // valid xml attribute names are: ^[a-zA-Z_:][-a-zA-Z0-9_:.] String key = attribute.getKey().replaceAll("[^-a-zA-Z0-9_:.]", ""); if (key.matches("[a-zA-Z_:][-a-zA-Z0-9_:.]*")) el.setAttribute(key, attribute.getValue()); } }
public Entry<String, String> next() { return new Attribute(attr.getKey().substring(dataPrefix.length()), attr.getValue()); }
private void getWholeDeclaration(Appendable accum, Document.OutputSettings out) throws IOException { for (Attribute attribute : attributes()) { if (!attribute.getKey().equals(nodeName())) { // skips coreValue (name) accum.append(' '); attribute.html(accum, out); } } }
/** * Finds any namespaces defined in this element. Returns any tag prefix. */ private String updateNamespaces(org.jsoup.nodes.Element el) { // scan the element for namespace declarations // like: xmlns="blah" or xmlns:prefix="blah" Attributes attributes = el.attributes(); for (Attribute attr : attributes) { String key = attr.getKey(); String prefix; if (key.equals(xmlnsKey)) { prefix = ""; } else if (key.startsWith(xmlnsPrefix)) { prefix = key.substring(xmlnsPrefix.length()); } else { continue; } namespacesStack.peek().put(prefix, attr.getValue()); } // get the element prefix if any int pos = el.tagName().indexOf(":"); return pos > 0 ? el.tagName().substring(0, pos) : ""; }
@Override public boolean matches(Element root, Element element) { List<org.jsoup.nodes.Attribute> values = element.attributes().asList(); for (org.jsoup.nodes.Attribute attribute : values) { if (lowerCase(attribute.getKey()).startsWith(keyPrefix)) return true; } return false; }
/** Set a new attribute, or replace an existing one by key. @param attribute attribute with case sensitive key @return these attributes, for chaining */ public Attributes put(Attribute attribute) { Validate.notNull(attribute); put(attribute.getKey(), attribute.getValue()); attribute.parent = this; return this; }
private boolean testValidProtocol(Element el, Attribute attr, Set<Protocol> protocols) { // try to resolve relative urls to abs, and optionally update the attribute so output html has abs. // rels without a baseuri get removed String value = el.absUrl(attr.getKey()); if (value.length() == 0) value = attr.getValue(); // if it could not be made abs, run as-is to allow custom unknown protocols if (!preserveRelativeLinks) attr.setValue(value); for (Protocol protocol : protocols) { String prot = protocol.toString(); if (prot.equals("#")) { // allows anchor links if (isValidAnchor(value)) { return true; } else { continue; } } prot += ":"; if (lowerCase(value).startsWith(prot)) { return true; } } return false; }
if (!html.hasAttr(attribute.getKey())) html.attributes().put(attribute); Element body = stack.get(1); for (Attribute attribute : startTag.getAttributes()) { if (!body.hasAttr(attribute.getKey())) body.attributes().put(attribute); if (!StringUtil.inSorted(attr.getKey(), Constants.InBodyStartInputAttribs)) inputAttribs.put(attr);
private Map<String, String> getAttributes(Element element) { Map<String, String> attributes = new HashMap<>(); for (Attribute attribute : element.attributes()) { if(!attribute.getValue().isEmpty()) attributes.put(attribute.getKey(), attribute.getValue()); } return attributes; }
void processAttributes(Node node) { Attributes attributes = myState.myAttributes; if (myOptions.outputAttributesIdAttr || !myOptions.outputAttributesNamesRegex.isEmpty()) { final org.jsoup.nodes.Attributes nodeAttributes = node.attributes(); boolean idDone = false; if (myOptions.outputAttributesIdAttr) { String id = nodeAttributes.get("id"); if (id == null || id.isEmpty()) { id = nodeAttributes.get("name"); } if (id != null && !id.isEmpty()) { attributes.replaceValue("id", id); idDone = true; } } if (!myOptions.outputAttributesNamesRegex.isEmpty()) { for (org.jsoup.nodes.Attribute attribute : nodeAttributes) { if (idDone && (attribute.getKey().equals("id") || attribute.getKey().equals("name"))) { continue; } if (attribute.getKey().matches(myOptions.outputAttributesNamesRegex)) { attributes.replaceValue(attribute.getKey(), attribute.getValue()); } } } } }
/** * Clears the children and attributes of the given element. * * @param design * the element to be cleared */ public static void clearElement(Element design) { Attributes attr = design.attributes(); for (Attribute a : attr.asList()) { attr.remove(a.getKey()); } List<Node> children = new ArrayList<>(); children.addAll(design.childNodes()); for (Node node : children) { node.remove(); } }
public void dumpHtmlTree(FormattingAppendable out, Node node) { out.line().append(node.nodeName()); for (org.jsoup.nodes.Attribute attribute : node.attributes().asList()) { out.append(' ').append(attribute.getKey()).append("=\"").append(attribute.getValue()).append("\""); } out.line().indent(); for (Node child : node.childNodes()) { dumpHtmlTree(out, child); } out.unIndent(); }
private void copyAttributes(org.jsoup.nodes.Node source, Element el) { for (Attribute attribute : source.attributes()) { // valid xml attribute names are: ^[a-zA-Z_:][-a-zA-Z0-9_:.] String key = attribute.getKey().replaceAll("[^-a-zA-Z0-9_:.]", ""); if (key.matches("[a-zA-Z_:][-a-zA-Z0-9_:.]*")) el.setAttribute(key, attribute.getValue()); } }
private Attributes computeAttributes() { AttributesImpl attributes = new AttributesImpl(); for (Attribute attr : element.attributes()) { attributes.addAttribute(null, attr.getKey(), null, null, attr.getValue()); } return attributes; }
public static DataSourceAttribute from(Attribute attr) { String key = attr.getKey(); String keyPostfix = key .substring(key.indexOf(ATTRIBUTE_SELECTOR + ATTRIBUTE_SEPARATOR) + ATTRIBUTE_DATA_OFFSET); String[] attrParts = keyPostfix.split(ATTRIBUTE_SEPARATOR); DataSourceAttribute builder = DataSourceAttribute.of(AtributeType.from(attrParts[0])); if (attrParts.length == 2) { builder.namespace = attrParts[1]; } builder.value = attr.getValue(); return builder; }
Document doc = Jsoup.parseBodyFragment(aText); Elements el = doc.getAllElements(); for (Element e : el) { Attributes at = e.attributes(); for (Attribute a : at) { e.removeAttr(a.getKey()); } } if(Jsoup.isValid(doc.body().html(), theLegalWhitelist)) return true; else return false;