6
6
import com .contentstack .utils .node .MarkType ;
7
7
import org .apache .commons .text .StringEscapeUtils ;
8
8
import org .json .JSONObject ;
9
- import org .jsoup .Jsoup ;
10
- import org .jsoup .nodes .Document ;
11
9
12
10
import java .util .*;
13
11
@@ -104,79 +102,77 @@ private String escapeInjectHtml(JSONObject nodeObj, String nodeType) {
104
102
public String renderNode (String nodeType , JSONObject nodeObject , NodeCallback callback ) {
105
103
String strAttrs = strAttrs (nodeObject );
106
104
String children = callback .renderChildren (nodeObject .optJSONArray ("children" ));
107
- // Jsoup sanitization
108
- Document sanitizedChildren = Jsoup .parse (children );
109
- String cleanChildren = sanitizedChildren .body ().html ();
105
+
110
106
switch (nodeType ) {
111
107
case "p" :
112
- return "<p" + strAttrs + ">" + cleanChildren + "</p>" ;
108
+ return "<p" + strAttrs + ">" + children + "</p>" ;
113
109
case "a" :
114
- return "<a" + strAttrs + " href=\" " + escapeInjectHtml (nodeObject , "href" ) + "\" >" + cleanChildren + "</a>" ;
110
+ return "<a" + strAttrs + " href=\" " + escapeInjectHtml (nodeObject , "href" ) + "\" >" + children + "</a>" ;
115
111
case "img" :
116
112
String assetLink = getNodeStr (nodeObject , "asset-link" );
117
113
if (!assetLink .isEmpty ()) {
118
114
JSONObject attrs = nodeObject .optJSONObject ("attrs" );
119
115
if (attrs .has ("link" )) {
120
- return "<a href=\" " + escapeInjectHtml (nodeObject , "link" ) + "\" >" + "<img" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "asset-link" ) + "\" />" + cleanChildren + "</a>" ;
116
+ return "<a href=\" " + escapeInjectHtml (nodeObject , "link" ) + "\" >" + "<img" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "asset-link" ) + "\" />" + children + "</a>" ;
121
117
}
122
- return "<img" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "asset-link" ) + "\" />" + cleanChildren ;
118
+ return "<img" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "asset-link" ) + "\" />" + children ;
123
119
}
124
- return "<img" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "src" ) + "\" />" + cleanChildren ;
120
+ return "<img" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "src" ) + "\" />" + children ;
125
121
case "embed" :
126
- return "<iframe" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "src" ) + "\" " + cleanChildren + "</iframe>" ;
122
+ return "<iframe" + strAttrs + " src=\" " + escapeInjectHtml (nodeObject , "src" ) + "\" " + children + "</iframe>" ;
127
123
case "h1" :
128
- return "<h1" + strAttrs + ">" + cleanChildren + "</h1>" ;
124
+ return "<h1" + strAttrs + ">" + children + "</h1>" ;
129
125
case "h2" :
130
- return "<h2" + strAttrs + ">" + cleanChildren + "</h2>" ;
126
+ return "<h2" + strAttrs + ">" + children + "</h2>" ;
131
127
case "h3" :
132
- return "<h3" + strAttrs + ">" + cleanChildren + "</h3>" ;
128
+ return "<h3" + strAttrs + ">" + children + "</h3>" ;
133
129
case "h4" :
134
- return "<h4" + strAttrs + ">" + cleanChildren + "</h4>" ;
130
+ return "<h4" + strAttrs + ">" + children + "</h4>" ;
135
131
case "h5" :
136
- return "<h5" + strAttrs + ">" + cleanChildren + "</h5>" ;
132
+ return "<h5" + strAttrs + ">" + children + "</h5>" ;
137
133
case "h6" :
138
- return "<h6" + strAttrs + ">" + cleanChildren + "</h6>" ;
134
+ return "<h6" + strAttrs + ">" + children + "</h6>" ;
139
135
case "ol" :
140
- return "<ol" + strAttrs + ">" + cleanChildren + "</ol>" ;
136
+ return "<ol" + strAttrs + ">" + children + "</ol>" ;
141
137
case "ul" :
142
- return "<ul" + strAttrs + ">" + cleanChildren + "</ul>" ;
138
+ return "<ul" + strAttrs + ">" + children + "</ul>" ;
143
139
case "li" :
144
- return "<li" + strAttrs + ">" + cleanChildren + "</li>" ;
140
+ return "<li" + strAttrs + ">" + children + "</li>" ;
145
141
case "hr" :
146
142
return "<hr" + strAttrs + " />" ;
147
143
case "table" :
148
- return "<table " + strAttrs + ">" + cleanChildren + "</table>" ;
144
+ return "<table " + strAttrs + ">" + children + "</table>" ;
149
145
case "thead" :
150
- return "<thead " + strAttrs + ">" + cleanChildren + "</thead>" ;
146
+ return "<thead " + strAttrs + ">" + children + "</thead>" ;
151
147
case "tbody" :
152
- return "<tbody" + strAttrs + ">" + cleanChildren + "</tbody>" ;
148
+ return "<tbody" + strAttrs + ">" + children + "</tbody>" ;
153
149
case "tfoot" :
154
- return "<tfoot" + strAttrs + ">" + cleanChildren + "</tfoot>" ;
150
+ return "<tfoot" + strAttrs + ">" + children + "</tfoot>" ;
155
151
case "tr" :
156
- return "<tr" + strAttrs + ">" + cleanChildren + "</tr>" ;
152
+ return "<tr" + strAttrs + ">" + children + "</tr>" ;
157
153
case "th" :{
158
154
if (nodeObject .has ("attrs" ) && nodeObject .optJSONObject ("attrs" ).has ("void" ) &&
159
155
nodeObject .optJSONObject ("attrs" ).optBoolean ("void" )) {
160
156
return "" ;
161
157
}else {
162
- return "<th" + strAttrs + ">" + cleanChildren + "</th>" ;}}
158
+ return "<th" + strAttrs + ">" + children + "</th>" ;}}
163
159
case "td" :{
164
160
if (nodeObject .has ("attrs" ) && nodeObject .optJSONObject ("attrs" ).has ("void" ) &&
165
161
nodeObject .optJSONObject ("attrs" ).optBoolean ("void" )) {
166
162
return "" ;
167
163
}else {
168
- return "<td" + strAttrs + ">" + cleanChildren + "</td>" ;}}
164
+ return "<td" + strAttrs + ">" + children + "</td>" ;}}
169
165
170
166
case "blockquote" :
171
- return "<blockquote" + strAttrs + ">" + cleanChildren + "</blockquote>" ;
167
+ return "<blockquote" + strAttrs + ">" + children + "</blockquote>" ;
172
168
case "code" :
173
- return "<code" + strAttrs + ">" + cleanChildren + "</code>" ;
169
+ return "<code" + strAttrs + ">" + children + "</code>" ;
174
170
case "reference" :
175
171
return "" ;
176
172
case "fragment" :
177
- return "<fragment" + strAttrs + ">" + cleanChildren + "</fragment>" ;
173
+ return "<fragment" + strAttrs + ">" + children + "</fragment>" ;
178
174
default :
179
- return cleanChildren ;
175
+ return children ;
180
176
}
181
177
}
182
178
@@ -197,16 +193,8 @@ String strAttrs(JSONObject nodeObject) {
197
193
for (String key : attrsObject .keySet ()) {
198
194
Object objValue = attrsObject .opt (key );
199
195
String value = objValue .toString ();
200
-
201
- StringBuilder escapedValue = new StringBuilder ();
202
- for (char ch : value .toCharArray ()) {
203
- if (ch == '&' || ch == '<' || ch == '>' || ch == '"' || ch == '\'' ) {
204
- escapedValue .append ("&#" ).append ((int ) ch ).append (';' );
205
- } else {
206
- escapedValue .append (ch );
207
- }
208
- }
209
- value = escapedValue .toString ();
196
+ // Escape HTML entities using StringEscapeUtils
197
+ value = StringEscapeUtils .escapeHtml4 (value );
210
198
// If style is available, do styling calculations
211
199
if (Objects .equals (key , "style" )) {
212
200
String resultStyle = stringifyStyles (attrsObject .optJSONObject ("style" ));
0 commit comments