001package org.jsoup.nodes;
002
003import org.jsoup.internal.QuietAppendable;
004import org.jsoup.parser.Parser;
005import org.jspecify.annotations.Nullable;
006
007import java.util.List;
008
009/**
010 A comment node.
011
012 @author Jonathan Hedley, jonathan@hedley.net */
013public class Comment extends LeafNode {
014    /**
015     Create a new comment node.
016     @param data The contents of the comment
017     */
018    public Comment(String data) {
019        super(data);
020    }
021
022    @Override public String nodeName() {
023        return "#comment";
024    }
025
026    /**
027     Get the contents of the comment.
028     @return comment content
029     */
030    public String getData() {
031        return coreValue();
032    }
033
034    public Comment setData(String data) {
035        coreValue(data);
036        return this;
037    }
038
039    @Override
040    void outerHtmlHead(QuietAppendable accum, Document.OutputSettings out) {
041        accum
042            .append("<!--")
043            .append(getData())
044            .append("-->");
045    }
046
047    @Override
048    public Comment clone() {
049        return (Comment) super.clone();
050    }
051
052    /**
053     * Check if this comment looks like an XML Declaration. This is the case when the HTML parser sees an XML
054     * declaration or processing instruction. Other than doctypes, those aren't part of HTML, and will be parsed as a
055     * bogus comment.
056     * @return true if it looks like, maybe, it's an XML Declaration.
057     * @see #asXmlDeclaration()
058     */
059    public boolean isXmlDeclaration() {
060        String data = getData();
061        return isXmlDeclarationData(data);
062    }
063
064    private static boolean isXmlDeclarationData(String data) {
065        return (data.length() > 1 && (data.startsWith("!") || data.startsWith("?")));
066    }
067
068    /**
069     * Attempt to cast this comment to an XML Declaration node.
070     * @return an XML declaration if it could be parsed as one, null otherwise.
071     * @see #isXmlDeclaration()
072     */
073    public @Nullable XmlDeclaration asXmlDeclaration() {
074        String fragment = "<" + getData() + ">";
075        Parser parser = Parser.xmlParser();
076        List<Node> nodes = parser.parseFragmentInput(fragment, null, "");
077        if (!nodes.isEmpty() && nodes.get(0) instanceof XmlDeclaration)
078            return (XmlDeclaration) nodes.get(0);
079        return null;
080    }
081}