Skip to content

Commit 795d855

Browse files
Merge pull request #342 from FromDoppler/DOP-1397-dynamic-process
DOP-1397 - add sanitize content for dynamic component
2 parents 8b5a0c6 + 7a3bff1 commit 795d855

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

Doppler.HtmlEditorApi.Test/Domain/DopplerHtmlDocumentTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public void GetTrackableUrls_should_return_empty_list_when_there_are_only_social
261261
}
262262

263263
[Fact]
264+
264265
public void GetTrackableUrls_should_return_list_of_trackable_urls()
265266
{
266267
// Arrange
@@ -307,6 +308,67 @@ public void GetTrackableUrls_should_not_map_field_names()
307308
link => Assert.Equal("ftp://|*|3*|*", link));
308309
}
309310

311+
[Fact]
312+
public void SanitizeDynamicContentNode_should_modify_html_dynamic_content_when_has_two_or_more_child_node_and_replace_image_src()
313+
{
314+
// Arrange
315+
var input = $@"<dynamiccontent action=""abandoned_cart"" items=""2"">
316+
<div role=""container"">
317+
<section>
318+
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">
319+
<img
320+
src=""https://cdn.fromdoppler.com/unlayer-editor/assets/cart_v2.svg""
321+
alt=""product image""
322+
/>
323+
</a>
324+
</section>
325+
<section>
326+
<span>[[[DC:TITLE]]]</span>
327+
<span >[[[DC:PRICE]]]</span>
328+
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">Comprar</a>
329+
</section>
330+
</div>
331+
<div role=""container"">
332+
<section>
333+
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">
334+
<img
335+
src=""https://cdn.fromdoppler.com/unlayer-editor/assets/cart_v2.svg""
336+
alt=""product image""
337+
/>
338+
</a>
339+
</section>
340+
<section>
341+
<span>[[[DC:TITLE]]]</span>
342+
<span >[[[DC:PRICE]]]</span>
343+
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">Comprar</a>
344+
</section>
345+
</div></dynamiccontent>";
346+
var htmlDocument = new DopplerHtmlDocument(input);
347+
htmlDocument.GetDopplerContent();
348+
var sanitizedContent = $@"<dynamiccontent action=""abandoned_cart"" items=""2"">
349+
<div role=""container"">
350+
<section>
351+
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">
352+
<img src=""[[[DC:IMAGE]]]"" alt=""product image"">
353+
</a>
354+
</section>
355+
<section>
356+
<span>[[[DC:TITLE]]]</span>
357+
<span>[[[DC:PRICE]]]</span>
358+
<a role=""link"" href=""[[[DC:URL]]]"" target=""_blank"">Comprar</a>
359+
</section>
360+
</div>
361+
</dynamiccontent>";
362+
363+
// Act
364+
htmlDocument.SanitizeDynamicContentNode();
365+
var content = htmlDocument.GetDopplerContent();
366+
367+
// Assert
368+
Assert.Equal(sanitizedContent, content);
369+
}
370+
371+
310372
[Fact]
311373
public void GetTrackableUrls_remove_encoding_on_sanitize_to_save()
312374
{

Doppler.HtmlEditorApi/Controllers/CampaignsController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ private async Task<DopplerHtmlDocument> ExtractHtmlDomFromCampaignContent(string
236236
htmlDocument.ReplaceFieldNameTagsByFieldIdTags(dopplerFieldsProcessor.GetFieldIdOrNull);
237237
htmlDocument.RemoveUnknownFieldIdTags(dopplerFieldsProcessor.FieldIdExist);
238238
htmlDocument.SanitizeTrackableLinks();
239+
htmlDocument.SanitizeDynamicContentNode();
239240
return htmlDocument;
240241
}
241242

Doppler.HtmlEditorApi/Domain/DopplerHtmlDocument.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ public void SanitizeTrackableLinks()
8989
}
9090
}
9191

92+
/// <summary>
93+
/// Sanitize the HTML code exported by Unlayer for the right processing of Doppler.
94+
/// - Remove repeated cart item tags
95+
/// - Replace img src value by [[[DC:IMAGE]]]
96+
/// </summary>
97+
public void SanitizeDynamicContentNode()
98+
{
99+
var dynamicContentNode = _contentNode.SelectSingleNode("//dynamiccontent");
100+
if (dynamicContentNode != null)
101+
{
102+
var divNodes = dynamicContentNode.SelectNodes("div");
103+
if (divNodes != null && divNodes.Count > 1)
104+
{
105+
for (var i = 1; i < divNodes.Count; i++)
106+
{
107+
dynamicContentNode.RemoveChild(divNodes[i]);
108+
}
109+
}
110+
111+
var imgNode = dynamicContentNode.SelectSingleNode(".//img");
112+
imgNode?.SetAttributeValue("src", "[[[DC:IMAGE]]]");
113+
}
114+
}
115+
92116
public void RemoveHarmfulTags()
93117
{
94118
var harmfulTags = _rootNode.SelectNodes(@"//script|//embed|//iframe|//meta[contains(@http-equiv,'refresh')]").EmptyIfNull();

cspell-project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dplr
2525
mseditor
2626
mydplr
2727
socialshare
28+
dynamiccontent
2829

2930
# Technical terms
3031
Liskov

0 commit comments

Comments
 (0)