From f2cb3ec10557c97945749ed3cfbda3bd6339b9eb Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Fri, 31 May 2024 18:48:47 +0530 Subject: [PATCH 1/3] fix: ignore td/th in case of atttrs had void:true --- .../utils/render/DefaultOption.java | 18 +++- .../utils/DefaultOptionTests.java | 100 ++++++++++++++++++ 2 files changed, 114 insertions(+), 4 deletions(-) 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); + } } From 0c46a9865c5a55ee966380f5ee7f0696c1930fd9 Mon Sep 17 00:00:00 2001 From: Nadeem <110535104+nadeem-cs@users.noreply.github.com> Date: Mon, 3 Jun 2024 18:02:59 +0530 Subject: [PATCH 2/3] Update Changelog.md --- Changelog.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 From 131951233c50bc7c0ec1520ca104f263e37d7c65 Mon Sep 17 00:00:00 2001 From: Nadeem <110535104+nadeem-cs@users.noreply.github.com> Date: Mon, 3 Jun 2024 18:03:40 +0530 Subject: [PATCH 3/3] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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