diff --git a/src/main/java/org/jsoup/parser/Tag.java b/src/main/java/org/jsoup/parser/Tag.java index 0e50c52c39..abac6e8fcf 100644 --- a/src/main/java/org/jsoup/parser/Tag.java +++ b/src/main/java/org/jsoup/parser/Tag.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.function.Consumer; /** * Tag capabilities. @@ -306,61 +307,35 @@ protected Tag clone() { // We don't need absolute coverage here as other cases will be inferred by the HtmlTreeBuilder } - static { - // creates - for (String tagName : blockTags) { - Tag tag = new Tag(tagName, Parser.NamespaceHtml); - register(tag); - } - for (String tagName : inlineTags) { - Tag tag = new Tag(tagName, Parser.NamespaceHtml); - tag.isBlock = false; - tag.formatAsBlock = false; - register(tag); - } - - // mods: - for (String tagName : emptyTags) { - Tag tag = Tags.get(tagName); - Validate.notNull(tag); - tag.empty = true; - } - - for (String tagName : formatAsInlineTags) { - Tag tag = Tags.get(tagName); - Validate.notNull(tag); - tag.formatAsBlock = false; - } - - for (String tagName : preserveWhitespaceTags) { + private static void setupTags(String[] tagNames, Consumer tagModifier) { + for (String tagName : tagNames) { Tag tag = Tags.get(tagName); - Validate.notNull(tag); - tag.preserveWhitespace = true; + if (tag == null) { + tag = new Tag(tagName, Parser.NamespaceHtml); + Tags.put(tag.tagName, tag); + } + tagModifier.accept(tag); } + } - for (String tagName : formListedTags) { - Tag tag = Tags.get(tagName); - Validate.notNull(tag); - tag.formList = true; - } + static { + setupTags(blockTags, tag -> { + tag.isBlock = true; + tag.formatAsBlock = true; + }); - for (String tagName : formSubmitTags) { - Tag tag = Tags.get(tagName); - Validate.notNull(tag); - tag.formSubmit = true; - } + setupTags(inlineTags, tag -> { + tag.isBlock = false; + tag.formatAsBlock = false; + }); - // namespace setup + setupTags(emptyTags, tag -> tag.empty = true); + setupTags(formatAsInlineTags, tag -> tag.formatAsBlock = false); + setupTags(preserveWhitespaceTags, tag -> tag.preserveWhitespace = true); + setupTags(formListedTags, tag -> tag.formList = true); + setupTags(formSubmitTags, tag -> tag.formSubmit = true); for (Map.Entry ns : namespaces.entrySet()) { - for (String tagName : ns.getValue()) { - Tag tag = Tags.get(tagName); - Validate.notNull(tag); - tag.namespace = ns.getKey(); - } + setupTags(ns.getValue(), tag -> tag.namespace = ns.getKey()); } } - - private static void register(Tag tag) { - Tags.put(tag.tagName, tag); - } }