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 f75dfd8 + f857321 commit cc6b822
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 57 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);
}

}
12 changes: 6 additions & 6 deletions src/test/resources/file.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"display-type": "display",
"asset-uid": "blt51f558a28ef555ca",
"content-type-uid": "sys_assets",
"asset-link": "https://images.contentstack.io/v3/assets/***REMOVED***/blt51f558a28ef555ca/6643371882c344cb839ec1a8/ben.jpeg",
"asset-link": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt51f558a28ef555ca/6643371882c344cb839ec1a8/ben.jpeg",
"asset-name": "Designer.png",
"asset-type": "image/jpeg",
"type": "asset",
Expand Down Expand Up @@ -47,7 +47,7 @@
{
"node": {
"title": "Designer.png",
"url": "https://images.contentstack.io/v3/assets/***REMOVED***/blt51f558a28ef555ca/66545ff10258be81fc639ae5/Virginia_Woolf_Headshot.jpg",
"url": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt51f558a28ef555ca/66545ff10258be81fc639ae5/Virginia_Woolf_Headshot.jpg",
"system": {
"uid": "blt51f558a28ef555ca",
"content_type_uid": "sys_assets"
Expand All @@ -63,7 +63,7 @@
{
"node": {
"title": "W._E._B._Du_Bois_Headshot.jpg",
"url": "https://images.contentstack.io/v3/assets/***REMOVED***/blt897c06ebf8c6353a/66545fe479abeeb60a3d4e35/Mark_Twain_Headshot.jpg",
"url": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt897c06ebf8c6353a/66545fe479abeeb60a3d4e35/Mark_Twain_Headshot.jpg",
"system": {
"content_type_uid": "sys_assets",
"uid": "blt897c06ebf8c6353a"
Expand All @@ -73,7 +73,7 @@
{
"node": {
"title": "iot-icon.png",
"url": "https://images.contentstack.io/v3/assets/***REMOVED***/bltbd81ff2ed42f1317/6654501e466a1c9b3edecb21/iot-icon.png",
"url": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/bltbd81ff2ed42f1317/6654501e466a1c9b3edecb21/iot-icon.png",
"system": {
"content_type_uid": "sys_assets",
"uid": "bltbd81ff2ed42f1317"
Expand All @@ -94,7 +94,7 @@
"display-type": "display",
"asset-uid": "blt897c06ebf8c6353a",
"content-type-uid": "sys_assets",
"asset-link": "https://images.contentstack.io/v3/assets/***REMOVED***/blt897c06ebf8c6353a/6644340e015b1c2d2b56ca48/W._E._B._Du_Bois_Headshot.jpg",
"asset-link": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt897c06ebf8c6353a/6644340e015b1c2d2b56ca48/W._E._B._Du_Bois_Headshot.jpg",
"asset-name": "W._E._B._Du_Bois_Headshot.jpg",
"asset-type": "image/jpeg",
"type": "asset",
Expand Down Expand Up @@ -126,7 +126,7 @@
"display-type": "display",
"asset-uid": "bltbd81ff2ed42f1317",
"content-type-uid": "sys_assets",
"asset-link": "https://images.contentstack.io/v3/assets/***REMOVED***/bltbd81ff2ed42f1317/664425e3a3f9df03dfc0e5e6/5216292.jpg",
"asset-link": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/bltbd81ff2ed42f1317/664425e3a3f9df03dfc0e5e6/5216292.jpg",
"asset-name": "iot-icon.png",
"asset-type": "image/jpeg",
"type": "asset",
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/issue/jsonfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@
}
],
"multi_line": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"single_line": "Single line textbox",
"suraj_123": [
{
"single_line": "Multi line textbox",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"markdown": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"number": 45346346
}
Expand Down Expand Up @@ -670,12 +670,12 @@
}
],
"multi_line": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"single_line": "Single line textbox",
"suraj_123": [
{
"single_line": "Multi line textbox",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"markdown": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"number": 45346346
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/multiple_rich_text_content.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@
}
}],
"multi_line": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"single_line": "Single line textbox",
"suraj_123": [{
"single_line": "Multi line textbox",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"markdown": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"number": 45346346

Expand Down Expand Up @@ -586,11 +586,11 @@
}
}],
"multi_line": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"rich_text_editor": "<img data-image=\"199k9jsr64s5\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/c9d4efe393ee4c5a/5e157cf29da2de41a1ac2ff1/220px-Bastet_Istanbul_museum.JPG\" data-sys-asset-uid=\"c9d4efe393ee4c5a\" alt=\"220px-Bastet_Istanbul_museum.JPG\">",
"single_line": "Single line textbox",
"suraj_123": [{
"single_line": "Multi line textbox",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"***REMOVED***/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"rich_text_editor": "<img data-image=\"mzeant040dnw\" src=\"https://stag-images.contentstack.io/v3/assets/77263d300aee3e6b/863ce52b0c65d670/5e157d0d1ec50841d6bccdb4/220px-House_sparrow04.jpg\" data-sys-asset-uid=\"863ce52b0c65d670\" alt=\"220px-House_sparrow04.jpg\">",
"markdown": "Multi line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textboxMulti line textbox",
"number": 45346346

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/reports/article2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@
"display-type": "display",
"asset-uid": "blt1e8458b798a67cf5",
"content-type-uid": "sys_assets",
"asset-link": "***REMOVED***/v3/assets/***REMOVED***/blt1e8458b798a67cf5/650b78874f22f8000d8c82e5/couldnt_submit_shipment.png",
"asset-link": "https://azure-na-images.contentstack.com/v3/assets/blta7903c6b840b702d/blt1e8458b798a67cf5/650b78874f22f8000d8c82e5/couldnt_submit_shipment.png",
"asset-name": "couldnt_submit_shipment.png",
"asset-type": "image/png",
"type": "asset",
Expand Down Expand Up @@ -1597,7 +1597,7 @@
"file_size": "97657",
"tags": [],
"filename": "couldnt_submit_shipment.png",
"url": "***REMOVED***/v3/assets/***REMOVED***/blt1e8458b798a67cf5/650b78874f22f8000d8c82e5/couldnt_submit_shipment.png",
"url": "https://azure-na-images.contentstack.com/v3/assets/blta7903c6b840b702d/blt1e8458b798a67cf5/650b78874f22f8000d8c82e5/couldnt_submit_shipment.png",
"ACL": [],
"parent_uid": "blt7c541342b35c3564",
"_version": 1,
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/reports/article3.json
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@
"display-type": "display",
"asset-uid": "blt80fc6f2ccca9406a",
"content-type-uid": "sys_assets",
"asset-link": "***REMOVED***/v3/assets/***REMOVED***/blt80fc6f2ccca9406a/64fa50d5b979ea000d97a176/Big_and_Bulky.png",
"asset-link": "https://azure-na-images.contentstack.com/v3/assets/blta7903c6b840b702d/blt80fc6f2ccca9406a/64fa50d5b979ea000d97a176/Big_and_Bulky.png",
"asset-name": "Big_and_Bulky.png",
"asset-type": "image/png",
"type": "asset",
Expand Down Expand Up @@ -1935,7 +1935,7 @@
"file_size": "136896",
"tags": [],
"filename": "Big_and_Bulky.png",
"url": "***REMOVED***/v3/assets/***REMOVED***/blt80fc6f2ccca9406a/64fa50d5b979ea000d97a176/Big_and_Bulky.png",
"url": "https://azure-na-images.contentstack.com/v3/assets/blta7903c6b840b702d/blt80fc6f2ccca9406a/64fa50d5b979ea000d97a176/Big_and_Bulky.png",
"ACL": [],
"parent_uid": "blt7fdffe41992d7f16",
"_version": 1,
Expand Down
Loading

0 comments on commit cc6b822

Please sign in to comment.