From d5f4c21c2614f4779115f4a1f74d08d2bb5eb206 Mon Sep 17 00:00:00 2001 From: Gabriela Trutan Date: Fri, 10 Jan 2025 18:37:32 +0100 Subject: [PATCH] SLVS-1600 Move html encoding inside DiffTranslator to have more control on the received html content and for better testability (e.g. html encoding and XML writer encoding sometimes lead to invalid characters, when XmlWriter encodes special characters from html escape characters) --- .../XamlGenerator/RuleHelpXamlTranslatorTests.cs | 8 ++------ src/Education/XamlGenerator/DiffTranslator.cs | 5 ++++- src/Education/XamlGenerator/RuleHelpXamlTranslator.cs | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Education.UnitTests/XamlGenerator/RuleHelpXamlTranslatorTests.cs b/src/Education.UnitTests/XamlGenerator/RuleHelpXamlTranslatorTests.cs index 7dce4fbb4..e9f2e4f0f 100644 --- a/src/Education.UnitTests/XamlGenerator/RuleHelpXamlTranslatorTests.cs +++ b/src/Education.UnitTests/XamlGenerator/RuleHelpXamlTranslatorTests.cs @@ -354,9 +354,7 @@ same 1 [TestMethod] public void TranslateHtmlToXaml_DataDiffWithAngleBracket_XMLParsable() { - var diffTranslator = new Mock(); - - IRuleHelpXamlTranslator testSubject = CreateTestSubject(diffTranslator: diffTranslator.Object); + IRuleHelpXamlTranslator testSubject = CreateTestSubject(); var compliantText = "#include <vector>"; var nonCompliantText = "#include <vector>"; @@ -365,8 +363,6 @@ public void TranslateHtmlToXaml_DataDiffWithAngleBracket_XMLParsable() var noncompliantXaml = @"#include <vector>"; - diffTranslator.Setup(d => d.GetDiffXaml(compliantText, nonCompliantText)).Returns((noncompliantXaml, compliantXaml)); - var htmlText = $"
{compliantText}
\n
{nonCompliantText}
"; var expectedText = @"
@@ -386,7 +382,7 @@ public void TranslateHtmlToXaml_SingleQuotesPresentInDiffCode_AreEscapedCorrectl var htmlText = @"
-    function f(a, g){} // Noncompliant: 'f' returns 'b' on two different return statements
+function f(a, g){} // Noncompliant: 'f' returns 'b' on two different return statements
 
 function f(a, g){}
diff --git a/src/Education/XamlGenerator/DiffTranslator.cs b/src/Education/XamlGenerator/DiffTranslator.cs
index 4d7edacfa..607099d9e 100644
--- a/src/Education/XamlGenerator/DiffTranslator.cs
+++ b/src/Education/XamlGenerator/DiffTranslator.cs
@@ -20,6 +20,7 @@
 
 using System.ComponentModel.Composition;
 using System.Text;
+using System.Web;
 using System.Xml;
 using DiffPlex.DiffBuilder;
 using DiffPlex.DiffBuilder.Model;
@@ -51,7 +52,9 @@ public DiffTranslator(IXamlWriterFactory xamlWriterFactory)
 
         public (string noncompliantXaml, string compliantXaml) GetDiffXaml(string noncompliantHtml, string compliantHtml)
         {
-            var resultDiff = SideBySideDiffBuilder.Diff(oldText: noncompliantHtml, newText: compliantHtml, ignoreWhiteSpace: false);
+            var encodedNonCompliantHtml = HttpUtility.HtmlEncode(noncompliantHtml);
+            var encodedCompliantHtml = HttpUtility.HtmlEncode(compliantHtml);
+            var resultDiff = SideBySideDiffBuilder.Diff(oldText: encodedNonCompliantHtml, newText: encodedCompliantHtml, ignoreWhiteSpace: false);
 
             var highlightedNonCompliant = HighlightLines(resultDiff.OldText.Lines, StyleResourceNames.NonCompliant_Diff, StyleResourceNames.Sub_NonCompliant_Diff);
             var highlightedCompliant = HighlightLines(resultDiff.NewText.Lines, StyleResourceNames.Compliant_Diff, StyleResourceNames.Sub_Compliant_Diff);
diff --git a/src/Education/XamlGenerator/RuleHelpXamlTranslator.cs b/src/Education/XamlGenerator/RuleHelpXamlTranslator.cs
index 3b94d734f..1f22cd3df 100644
--- a/src/Education/XamlGenerator/RuleHelpXamlTranslator.cs
+++ b/src/Education/XamlGenerator/RuleHelpXamlTranslator.cs
@@ -601,7 +601,7 @@ private void ReplaceDiffs(StringBuilder sb)
                         continue;
                     }
 
-                    var diffXaml = diffTranslator.GetDiffXaml(HttpUtility.HtmlEncode(diffCodes[noncompliantKey]), HttpUtility.HtmlEncode(diffCodes[compliantKey]));
+                    var diffXaml = diffTranslator.GetDiffXaml(diffCodes[noncompliantKey], diffCodes[compliantKey]);
 
                     sb.Replace(noncompliantKey, diffXaml.noncompliantXaml);
                     sb.Replace(compliantKey, diffXaml.compliantXaml);