001package org.jsoup.parser;
002
003import org.jsoup.nodes.Attributes;
004import org.jspecify.annotations.Nullable;
005
006import static org.jsoup.internal.Normalizer.lowerCase;
007import static org.jsoup.internal.Normalizer.normalize;
008
009/**
010 * Controls parser case settings, to optionally preserve tag and/or attribute name case.
011 */
012public class ParseSettings {
013    /**
014     * HTML default settings: both tag and attribute names are lower-cased during parsing.
015     */
016    public static final ParseSettings htmlDefault;
017    /**
018     * Preserve both tag and attribute case.
019     */
020    public static final ParseSettings preserveCase;
021
022    static {
023        htmlDefault = new ParseSettings(false, false);
024        preserveCase = new ParseSettings(true, true);
025    }
026
027    private final boolean preserveTagCase;
028    private final boolean preserveAttributeCase;
029
030    /**
031     * Returns true if preserving tag name case.
032     */
033    public boolean preserveTagCase() {
034        return preserveTagCase;
035    }
036
037    /**
038     * Returns true if preserving attribute case.
039     */
040    public boolean preserveAttributeCase() {
041        return preserveAttributeCase;
042    }
043
044    /**
045     * Define parse settings.
046     * @param tag preserve tag case?
047     * @param attribute preserve attribute name case?
048     */
049    public ParseSettings(boolean tag, boolean attribute) {
050        preserveTagCase = tag;
051        preserveAttributeCase = attribute;
052    }
053
054    ParseSettings(ParseSettings copy) {
055        this(copy.preserveTagCase, copy.preserveAttributeCase);
056    }
057
058    /**
059     * Normalizes a tag name according to the case preservation setting.
060     */
061    public String normalizeTag(String name) {
062        name = name.trim();
063        if (!preserveTagCase)
064            name = lowerCase(name);
065        return name;
066    }
067
068    /**
069     * Normalizes an attribute according to the case preservation setting.
070     */
071    public String normalizeAttribute(String name) {
072        name = name.trim();
073        if (!preserveAttributeCase)
074            name = lowerCase(name);
075        return name;
076    }
077
078    void normalizeAttributes(Attributes attributes) {
079        if (!preserveAttributeCase) {
080            attributes.normalize();
081        }
082    }
083
084    /** Returns the normal name that a Tag will have (trimmed and lower-cased) */
085    static String normalName(String name) {
086        return normalize(name);
087    }
088}