001package org.jsoup.nodes;
002
003import org.jsoup.internal.QuietAppendable;
004
005/**
006 A data node, for contents of style, script tags etc, where contents should not show in text().
007
008 @author Jonathan Hedley, jonathan@hedley.net */
009public class DataNode extends LeafNode {
010
011    /**
012     Create a new DataNode.
013     @param data data contents
014     */
015    public DataNode(String data) {
016        super(data);
017    }
018
019    @Override public String nodeName() {
020        return "#data";
021    }
022
023    /**
024     Get the data contents of this node. Will be unescaped and with original new lines, space etc.
025     @return data
026     */
027    public String getWholeData() {
028        return coreValue();
029    }
030
031    /**
032     * Set the data contents of this node.
033     * @param data un-encoded data
034     * @return this node, for chaining
035     */
036    public DataNode setWholeData(String data) {
037        coreValue(data);
038        return this;
039    }
040
041    @Override
042    void outerHtmlHead(QuietAppendable accum, Document.OutputSettings out) {
043        /* For XML output, escape the DataNode in a CData section. The data may contain pseudo-CData content if it was
044        parsed as HTML, so don't double up Cdata. Output in polyglot HTML / XHTML / XML format. */
045        final String data = getWholeData();
046        if (out.syntax() == Document.OutputSettings.Syntax.xml && !data.contains("<![CDATA[")) {
047            if (parentNameIs("script"))
048                accum.append("//<![CDATA[\n").append(data).append("\n//]]>");
049            else if (parentNameIs("style"))
050                accum.append("/*<![CDATA[*/\n").append(data).append("\n/*]]>*/");
051            else
052                accum.append("<![CDATA[").append(data).append("]]>");
053        } else {
054            // In HTML, data is not escaped in the output of data nodes, so < and & in script, style is OK
055            accum.append(data);
056        }
057    }
058
059    @Override
060    public DataNode clone() {
061        return (DataNode) super.clone();
062    }
063}