Skip to content

Commit e76111b

Browse files
authored
Merge pull request #72 from contentstack/fix/DX-707
fix: ignore td/th in case of atttrs had void:true
2 parents 05c000f + a960438 commit e76111b

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

src/main/java/com/contentstack/utils/render/DefaultOption.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
public class DefaultOption implements Option {
1616

17+
1718
/**
1819
* The function `renderOptions` takes in a JSON object and metadata and returns a string based on the
1920
* style type of the metadata.
@@ -153,10 +154,19 @@ public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback ca
153154
return "<tfoot" + strAttrs + ">" + cleanChildren + "</tfoot>";
154155
case "tr":
155156
return "<tr" + strAttrs + ">" + cleanChildren + "</tr>";
156-
case "th":
157-
return "<th" + strAttrs + ">" + cleanChildren + "</th>";
158-
case "td":
159-
return "<td" + strAttrs + ">" + cleanChildren + "</td>";
157+
case "th":{
158+
if (nodeObject.has("attrs") && nodeObject.optJSONObject("attrs").has("void") &&
159+
nodeObject.optJSONObject("attrs").optBoolean("void")) {
160+
return "";
161+
}else{
162+
return "<th" + strAttrs + ">" + cleanChildren + "</th>";}}
163+
case "td":{
164+
if (nodeObject.has("attrs") && nodeObject.optJSONObject("attrs").has("void") &&
165+
nodeObject.optJSONObject("attrs").optBoolean("void")) {
166+
return "";
167+
}else{
168+
return "<td" + strAttrs + ">" + cleanChildren + "</td>";}}
169+
160170
case "blockquote":
161171
return "<blockquote" + strAttrs + ">" + cleanChildren + "</blockquote>";
162172
case "code":

src/test/java/com/contentstack/utils/DefaultOptionTests.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.contentstack.utils;
22

33
import com.contentstack.utils.helper.Metadata;
4+
import com.contentstack.utils.interfaces.NodeCallback;
45
import com.contentstack.utils.render.DefaultOption;
6+
7+
58
import org.json.JSONObject;
9+
import org.json.JSONArray;
610
import org.jsoup.nodes.Attributes;
711
import org.junit.Assert;
812
import org.junit.BeforeClass;
913
import org.junit.FixMethodOrder;
1014
import org.junit.Test;
1115
import org.junit.runners.MethodSorters;
1216

17+
import static org.junit.Assert.assertEquals;
18+
1319
import java.io.IOException;
1420
import java.util.logging.Level;
1521
import java.util.logging.Logger;
@@ -82,5 +88,99 @@ public void testEmbeddedDefaultDisplayable() {
8288
String result = defaultOptions.renderOptions(localJsonObj.optJSONObject("_embedded_items"), metadata);
8389
Assert.assertEquals("<img src=\"\" alt=\"\" />", result);
8490
}
91+
@Test
92+
public void testRenderNodeWithVoidTd() {
93+
DefaultOption defaultOptions = new DefaultOption();
94+
JSONObject nodeObject = new JSONObject();
95+
JSONObject attrs = new JSONObject();
96+
attrs.put("void", true);
97+
nodeObject.put("attrs", attrs);
98+
nodeObject.put("children", new JSONArray());
99+
100+
NodeCallback callback = children -> {
101+
// Simple callback implementation for testing purposes
102+
StringBuilder sb = new StringBuilder();
103+
for (int i = 0; i < children.length(); i++) {
104+
sb.append(children.getJSONObject(i).getString("type"));
105+
}
106+
return sb.toString();
107+
};
108+
109+
String result = defaultOptions.renderNode("td", nodeObject, callback);
110+
Assert.assertEquals("", result);
111+
}
112+
@Test
113+
public void testRenderNodeWithVoidTh() {
114+
DefaultOption defaultOptions = new DefaultOption();
115+
JSONObject nodeObject = new JSONObject();
116+
JSONObject attrs = new JSONObject();
117+
attrs.put("void", true);
118+
nodeObject.put("attrs", attrs);
119+
nodeObject.put("children", new JSONArray());
120+
121+
NodeCallback callback = children -> {
122+
// Simple callback implementation for testing purposes
123+
StringBuilder sb = new StringBuilder();
124+
for (int i = 0; i < children.length(); i++) {
125+
sb.append(children.getJSONObject(i).getString("type"));
126+
}
127+
return sb.toString();
128+
};
129+
130+
String result = defaultOptions.renderNode("th", nodeObject, callback);
131+
Assert.assertEquals("", result);
132+
}
133+
134+
@Test
135+
public void testRenderNodeWithoutVoidTd() {
136+
DefaultOption defaultOptions = new DefaultOption();
137+
JSONObject nodeObject = new JSONObject();
138+
JSONObject attrs = new JSONObject();
139+
attrs.put("class", "example");
140+
nodeObject.put("attrs", attrs);
141+
JSONArray children = new JSONArray();
142+
JSONObject child = new JSONObject();
143+
child.put("type", "text");
144+
child.put("content", "example content");
145+
children.put(child);
146+
nodeObject.put("children", children);
147+
148+
NodeCallback callback = childrenArray -> {
149+
StringBuilder sb = new StringBuilder();
150+
for (int i = 0; i < childrenArray.length(); i++) {
151+
sb.append(childrenArray.getJSONObject(i).getString("content"));
152+
}
153+
return sb.toString();
154+
};
155+
156+
String result = defaultOptions.renderNode("td", nodeObject, callback);
157+
Assert.assertEquals("<td class=\"example\">example content</td>", result);
158+
}
159+
160+
@Test
161+
public void testRenderNodeWithoutVoidTh() {
162+
DefaultOption defaultOptions = new DefaultOption();
163+
JSONObject nodeObject = new JSONObject();
164+
JSONObject attrs = new JSONObject();
165+
attrs.put("class", "example");
166+
nodeObject.put("attrs", attrs);
167+
JSONArray children = new JSONArray();
168+
JSONObject child = new JSONObject();
169+
child.put("type", "text");
170+
child.put("content", "example content");
171+
children.put(child);
172+
nodeObject.put("children", children);
173+
174+
NodeCallback callback = childrenArray -> {
175+
StringBuilder sb = new StringBuilder();
176+
for (int i = 0; i < childrenArray.length(); i++) {
177+
sb.append(childrenArray.getJSONObject(i).getString("content"));
178+
}
179+
return sb.toString();
180+
};
181+
182+
String result = defaultOptions.renderNode("th", nodeObject, callback);
183+
Assert.assertEquals("<th class=\"example\">example content</th>", result);
184+
}
85185

86186
}

0 commit comments

Comments
 (0)