001package org.jsoup.nodes;
002
003import java.io.IOException;
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        value = data;
017    }
018
019    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 unencoded 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(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
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 polygot 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(getWholeData());
056        }
057    }
058
059    @Override
060    void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) {}
061
062    @Override
063    public String toString() {
064        return outerHtml();
065    }
066
067    @Override
068    public DataNode clone() {
069        return (DataNode) super.clone();
070    }
071}