1 package org.jsoup.nodes; 2 3 import java.io.IOException; 4 5 /** 6 A data node, for contents of style, script tags etc, where contents should not show in text(). 7 8 @author Jonathan Hedley, jonathan@hedley.net */ 9 public class DataNode extends LeafNode { 10 11 /** 12 Create a new DataNode. 13 @param data data contents 14 */ DataNode(String data)15 public DataNode(String data) { 16 value = data; 17 } 18 nodeName()19 public String nodeName() { 20 return "#data"; 21 } 22 23 /** 24 Get the data contents of this node. Will be unescaped and with original new lines, space etc. 25 @return data 26 */ getWholeData()27 public String getWholeData() { 28 return coreValue(); 29 } 30 31 /** 32 * Set the data contents of this node. 33 * @param data unencoded data 34 * @return this node, for chaining 35 */ setWholeData(String data)36 public DataNode setWholeData(String data) { 37 coreValue(data); 38 return this; 39 } 40 41 @Override outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out)42 void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException { 43 /* For XML output, escape the DataNode in a CData section. The data may contain pseudo-CData content if it was 44 parsed as HTML, so don't double up Cdata. Output in polygot HTML / XHTML / XML format. */ 45 final String data = getWholeData(); 46 if (out.syntax() == Document.OutputSettings.Syntax.xml && !data.contains("<![CDATA[")) { 47 if (parentNameIs("script")) 48 accum.append("//<![CDATA[\n").append(data).append("\n//]]>"); 49 else if (parentNameIs("style")) 50 accum.append("/*<![CDATA[*/\n").append(data).append("\n/*]]>*/"); 51 else 52 accum.append("<![CDATA[").append(data).append("]]>"); 53 } else { 54 // In HTML, data is not escaped in the output of data nodes, so < and & in script, style is OK 55 accum.append(getWholeData()); 56 } 57 } 58 59 @Override outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out)60 void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) {} 61 62 @Override toString()63 public String toString() { 64 return outerHtml(); 65 } 66 67 @Override clone()68 public DataNode clone() { 69 return (DataNode) super.clone(); 70 } 71 } 72