Skip to content

Commit

Permalink
Merge pull request #73 from contentstack/next
Browse files Browse the repository at this point in the history
Fix: Empty string in case td or th node had void:true
  • Loading branch information
nadeem-cs authored Jun 5, 2024
2 parents 1f6b9ee + 2e72a5b commit fb43398
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
A brief description of what changes project contains
## May 28, 2024

#### v1.2.11

- Fix: ignore td/th in case of attrs has void:true

## May 14, 2024

#### v1.2.10

-Enhancement: Update Asset url method added for GQL
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.contentstack.sdk</groupId>
<artifactId>utils</artifactId>
<version>1.2.10</version>
<version>1.2.11</version>
<packaging>jar</packaging>
<name>Contentstack-utils</name>
<description>Java Utils SDK for Contentstack Content Delivery API, Contentstack is a headless CMS</description>
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/contentstack/utils/render/DefaultOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

public class DefaultOption implements Option {


/**
* The function `renderOptions` takes in a JSON object and metadata and returns a string based on the
* style type of the metadata.
Expand Down Expand Up @@ -153,10 +154,19 @@ public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback ca
return "<tfoot" + strAttrs + ">" + cleanChildren + "</tfoot>";
case "tr":
return "<tr" + strAttrs + ">" + cleanChildren + "</tr>";
case "th":
return "<th" + strAttrs + ">" + cleanChildren + "</th>";
case "td":
return "<td" + strAttrs + ">" + cleanChildren + "</td>";
case "th":{
if (nodeObject.has("attrs") && nodeObject.optJSONObject("attrs").has("void") &&
nodeObject.optJSONObject("attrs").optBoolean("void")) {
return "";
}else{
return "<th" + strAttrs + ">" + cleanChildren + "</th>";}}
case "td":{
if (nodeObject.has("attrs") && nodeObject.optJSONObject("attrs").has("void") &&
nodeObject.optJSONObject("attrs").optBoolean("void")) {
return "";
}else{
return "<td" + strAttrs + ">" + cleanChildren + "</td>";}}

case "blockquote":
return "<blockquote" + strAttrs + ">" + cleanChildren + "</blockquote>";
case "code":
Expand Down
100 changes: 100 additions & 0 deletions src/test/java/com/contentstack/utils/DefaultOptionTests.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.contentstack.utils;

import com.contentstack.utils.helper.Metadata;
import com.contentstack.utils.interfaces.NodeCallback;
import com.contentstack.utils.render.DefaultOption;


import org.json.JSONObject;
import org.json.JSONArray;
import org.jsoup.nodes.Attributes;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -82,5 +88,99 @@ public void testEmbeddedDefaultDisplayable() {
String result = defaultOptions.renderOptions(localJsonObj.optJSONObject("_embedded_items"), metadata);
Assert.assertEquals("<img src=\"\" alt=\"\" />", result);
}
@Test
public void testRenderNodeWithVoidTd() {
DefaultOption defaultOptions = new DefaultOption();
JSONObject nodeObject = new JSONObject();
JSONObject attrs = new JSONObject();
attrs.put("void", true);
nodeObject.put("attrs", attrs);
nodeObject.put("children", new JSONArray());

NodeCallback callback = children -> {
// Simple callback implementation for testing purposes
StringBuilder sb = new StringBuilder();
for (int i = 0; i < children.length(); i++) {
sb.append(children.getJSONObject(i).getString("type"));
}
return sb.toString();
};

String result = defaultOptions.renderNode("td", nodeObject, callback);
Assert.assertEquals("", result);
}
@Test
public void testRenderNodeWithVoidTh() {
DefaultOption defaultOptions = new DefaultOption();
JSONObject nodeObject = new JSONObject();
JSONObject attrs = new JSONObject();
attrs.put("void", true);
nodeObject.put("attrs", attrs);
nodeObject.put("children", new JSONArray());

NodeCallback callback = children -> {
// Simple callback implementation for testing purposes
StringBuilder sb = new StringBuilder();
for (int i = 0; i < children.length(); i++) {
sb.append(children.getJSONObject(i).getString("type"));
}
return sb.toString();
};

String result = defaultOptions.renderNode("th", nodeObject, callback);
Assert.assertEquals("", result);
}

@Test
public void testRenderNodeWithoutVoidTd() {
DefaultOption defaultOptions = new DefaultOption();
JSONObject nodeObject = new JSONObject();
JSONObject attrs = new JSONObject();
attrs.put("class", "example");
nodeObject.put("attrs", attrs);
JSONArray children = new JSONArray();
JSONObject child = new JSONObject();
child.put("type", "text");
child.put("content", "example content");
children.put(child);
nodeObject.put("children", children);

NodeCallback callback = childrenArray -> {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < childrenArray.length(); i++) {
sb.append(childrenArray.getJSONObject(i).getString("content"));
}
return sb.toString();
};

String result = defaultOptions.renderNode("td", nodeObject, callback);
Assert.assertEquals("<td class=\"example\">example content</td>", result);
}

@Test
public void testRenderNodeWithoutVoidTh() {
DefaultOption defaultOptions = new DefaultOption();
JSONObject nodeObject = new JSONObject();
JSONObject attrs = new JSONObject();
attrs.put("class", "example");
nodeObject.put("attrs", attrs);
JSONArray children = new JSONArray();
JSONObject child = new JSONObject();
child.put("type", "text");
child.put("content", "example content");
children.put(child);
nodeObject.put("children", children);

NodeCallback callback = childrenArray -> {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < childrenArray.length(); i++) {
sb.append(childrenArray.getJSONObject(i).getString("content"));
}
return sb.toString();
};

String result = defaultOptions.renderNode("th", nodeObject, callback);
Assert.assertEquals("<th class=\"example\">example content</th>", result);
}

}

0 comments on commit fb43398

Please sign in to comment.