diff --git a/Changelog.md b/Changelog.md index a634c69..cd73410 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/pom.xml b/pom.xml index a406042..8c00e61 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.contentstack.sdk utils - 1.2.10 + 1.2.11 jar Contentstack-utils Java Utils SDK for Contentstack Content Delivery API, Contentstack is a headless CMS diff --git a/src/main/java/com/contentstack/utils/render/DefaultOption.java b/src/main/java/com/contentstack/utils/render/DefaultOption.java index 35dae2a..cf6ffd3 100644 --- a/src/main/java/com/contentstack/utils/render/DefaultOption.java +++ b/src/main/java/com/contentstack/utils/render/DefaultOption.java @@ -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. @@ -153,10 +154,19 @@ public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback ca return "" + cleanChildren + ""; case "tr": return "" + cleanChildren + ""; - case "th": - return "" + cleanChildren + ""; - case "td": - return "" + cleanChildren + ""; + case "th":{ + if (nodeObject.has("attrs") && nodeObject.optJSONObject("attrs").has("void") && + nodeObject.optJSONObject("attrs").optBoolean("void")) { + return ""; + }else{ + return "" + cleanChildren + "";}} + case "td":{ + if (nodeObject.has("attrs") && nodeObject.optJSONObject("attrs").has("void") && + nodeObject.optJSONObject("attrs").optBoolean("void")) { + return ""; + }else{ + return "" + cleanChildren + "";}} + case "blockquote": return "" + cleanChildren + ""; case "code": diff --git a/src/test/java/com/contentstack/utils/DefaultOptionTests.java b/src/test/java/com/contentstack/utils/DefaultOptionTests.java index c25da3b..4055e92 100644 --- a/src/test/java/com/contentstack/utils/DefaultOptionTests.java +++ b/src/test/java/com/contentstack/utils/DefaultOptionTests.java @@ -1,8 +1,12 @@ 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; @@ -10,6 +14,8 @@ 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; @@ -82,5 +88,99 @@ public void testEmbeddedDefaultDisplayable() { String result = defaultOptions.renderOptions(localJsonObj.optJSONObject("_embedded_items"), metadata); Assert.assertEquals("\"\"", 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("example content", 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("example content", result); + } }