Skip to content

Commit

Permalink
Simplify the Tag setup code
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Oct 12, 2023
1 parent 1c54718 commit aab1dc1
Showing 1 changed file with 24 additions and 49 deletions.
73 changes: 24 additions & 49 deletions src/main/java/org/jsoup/parser/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
* Tag capabilities.
Expand Down Expand Up @@ -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<Tag> 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<String, String[]> 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);
}
}

0 comments on commit aab1dc1

Please sign in to comment.