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