From 454b8492ddde558ae37f38fe8ea59779edfcd61e Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 11 Oct 2024 14:01:49 +0530 Subject: [PATCH] Use Map#getOrDefault() method --- src/main/java/org/jsoup/safety/Safelist.java | 26 +++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/jsoup/safety/Safelist.java b/src/main/java/org/jsoup/safety/Safelist.java index cb5038d06d..da73d4c3df 100644 --- a/src/main/java/org/jsoup/safety/Safelist.java +++ b/src/main/java/org/jsoup/safety/Safelist.java @@ -12,6 +12,7 @@ Thank you to Ryan Grove (wonko.com) for the Ruby HTML cleaner http://github.com/ import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -522,15 +523,12 @@ public boolean isSafeAttribute(String tagName, Element el, Attribute attr) { TagName tag = TagName.valueOf(tagName); AttributeKey key = AttributeKey.valueOf(attr.getKey()); - Set okSet = attributes.get(tag); - if (okSet != null && okSet.contains(key)) { - if (protocols.containsKey(tag)) { - Map> attrProts = protocols.get(tag); - // ok if not defined protocol; otherwise test - return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key)); - } else { // attribute found, no protocols defined, so OK - return true; - } + Set okSet = attributes.getOrDefault(tag, Collections.emptySet()); + if (okSet.contains(key)) { + Set protocolSet = protocols.getOrDefault(tag, Collections.emptyMap()) + .getOrDefault(key, Collections.emptySet()); + // ok if not defined protocol; otherwise test + return protocolSet.isEmpty() || testValidProtocol(el, attr, protocolSet); } // might be an enforced attribute? Map enforcedSet = enforcedAttributes.get(tag); @@ -585,12 +583,10 @@ private boolean isValidAnchor(String value) { */ public Attributes getEnforcedAttributes(String tagName) { Attributes attrs = new Attributes(); - TagName tag = TagName.valueOf(tagName); - if (enforcedAttributes.containsKey(tag)) { - Map keyVals = enforcedAttributes.get(tag); - for (Map.Entry entry : keyVals.entrySet()) { - attrs.put(entry.getKey().toString(), entry.getValue().toString()); - } + Map keyVals = + enforcedAttributes.getOrDefault(TagName.valueOf(tagName), Collections.emptyMap()); + for (Map.Entry entry : keyVals.entrySet()) { + attrs.put(entry.getKey().toString(), entry.getValue().toString()); } return attrs; }