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