Skip to content

Commit

Permalink
Merge pull request #342 from FromDoppler/DOP-1397-dynamic-process
Browse files Browse the repository at this point in the history
DOP-1397 - add sanitize content for dynamic component
  • Loading branch information
ck-makingsense authored Feb 6, 2024
2 parents 8b5a0c6 + 7a3bff1 commit 795d855
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Doppler.HtmlEditorApi.Test/Domain/DopplerHtmlDocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public void GetTrackableUrls_should_return_empty_list_when_there_are_only_social
}

[Fact]

public void GetTrackableUrls_should_return_list_of_trackable_urls()
{
// Arrange
Expand Down Expand Up @@ -307,6 +308,67 @@ public void GetTrackableUrls_should_not_map_field_names()
link => Assert.Equal("ftp://|*|3*|*", link));
}

[Fact]
public void SanitizeDynamicContentNode_should_modify_html_dynamic_content_when_has_two_or_more_child_node_and_replace_image_src()
{
// Arrange
var input = $@"<dynamiccontent action=""abandoned_cart"" items=""2"">
<div role=""container"">
<section>
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">
<img
src=""https://cdn.fromdoppler.com/unlayer-editor/assets/cart_v2.svg""
alt=""product image""
/>
</a>
</section>
<section>
<span>[[[DC:TITLE]]]</span>
<span >[[[DC:PRICE]]]</span>
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">Comprar</a>
</section>
</div>
<div role=""container"">
<section>
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">
<img
src=""https://cdn.fromdoppler.com/unlayer-editor/assets/cart_v2.svg""
alt=""product image""
/>
</a>
</section>
<section>
<span>[[[DC:TITLE]]]</span>
<span >[[[DC:PRICE]]]</span>
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">Comprar</a>
</section>
</div></dynamiccontent>";
var htmlDocument = new DopplerHtmlDocument(input);
htmlDocument.GetDopplerContent();
var sanitizedContent = $@"<dynamiccontent action=""abandoned_cart"" items=""2"">
<div role=""container"">
<section>
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">
<img src=""[[[DC:IMAGE]]]"" alt=""product image"">
</a>
</section>
<section>
<span>[[[DC:TITLE]]]</span>
<span>[[[DC:PRICE]]]</span>
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">Comprar</a>
</section>
</div>
</dynamiccontent>";

// Act
htmlDocument.SanitizeDynamicContentNode();
var content = htmlDocument.GetDopplerContent();

// Assert
Assert.Equal(sanitizedContent, content);
}


[Fact]
public void GetTrackableUrls_remove_encoding_on_sanitize_to_save()
{
Expand Down
1 change: 1 addition & 0 deletions Doppler.HtmlEditorApi/Controllers/CampaignsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ private async Task<DopplerHtmlDocument> ExtractHtmlDomFromCampaignContent(string
htmlDocument.ReplaceFieldNameTagsByFieldIdTags(dopplerFieldsProcessor.GetFieldIdOrNull);
htmlDocument.RemoveUnknownFieldIdTags(dopplerFieldsProcessor.FieldIdExist);
htmlDocument.SanitizeTrackableLinks();
htmlDocument.SanitizeDynamicContentNode();
return htmlDocument;
}

Expand Down
24 changes: 24 additions & 0 deletions Doppler.HtmlEditorApi/Domain/DopplerHtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ public void SanitizeTrackableLinks()
}
}

/// <summary>
/// Sanitize the HTML code exported by Unlayer for the right processing of Doppler.
/// - Remove repeated cart item tags
/// - Replace img src value by [[[DC:IMAGE]]]
/// </summary>
public void SanitizeDynamicContentNode()
{
var dynamicContentNode = _contentNode.SelectSingleNode("//dynamiccontent");
if (dynamicContentNode != null)
{
var divNodes = dynamicContentNode.SelectNodes("div");
if (divNodes != null && divNodes.Count > 1)
{
for (var i = 1; i < divNodes.Count; i++)
{
dynamicContentNode.RemoveChild(divNodes[i]);
}
}

var imgNode = dynamicContentNode.SelectSingleNode(".//img");
imgNode?.SetAttributeValue("src", "[[[DC:IMAGE]]]");
}
}

public void RemoveHarmfulTags()
{
var harmfulTags = _rootNode.SelectNodes(@"//script|//embed|//iframe|//meta[contains(@http-equiv,'refresh')]").EmptyIfNull();
Expand Down
1 change: 1 addition & 0 deletions cspell-project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dplr
mseditor
mydplr
socialshare
dynamiccontent

# Technical terms
Liskov
Expand Down

0 comments on commit 795d855

Please sign in to comment.