Skip to content

Commit

Permalink
SLVS-1600 Move html encoding inside DiffTranslator to have more contr…
Browse files Browse the repository at this point in the history
…ol 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)
  • Loading branch information
gabriela-trutan-sonarsource committed Jan 10, 2025
1 parent 1304fab commit d5f4c21
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,7 @@ same 1</Paragraph>
[TestMethod]
public void TranslateHtmlToXaml_DataDiffWithAngleBracket_XMLParsable()
{
var diffTranslator = new Mock<IDiffTranslator>();

IRuleHelpXamlTranslator testSubject = CreateTestSubject(diffTranslator: diffTranslator.Object);
IRuleHelpXamlTranslator testSubject = CreateTestSubject();

var compliantText = "#include &lt;vector&gt;";
var nonCompliantText = "#include &lt;vector&gt;";
Expand All @@ -365,8 +363,6 @@ public void TranslateHtmlToXaml_DataDiffWithAngleBracket_XMLParsable()

var noncompliantXaml = @"#include &lt;vector&gt;";

diffTranslator.Setup(d => d.GetDiffXaml(compliantText, nonCompliantText)).Returns((noncompliantXaml, compliantXaml));

var htmlText = $"<pre data-diff-type=\"compliant\" data-diff-id=\"1\">{compliantText}</pre>\n<pre data-diff-type =\"noncompliant\" data-diff-id=\"1\">{nonCompliantText}</pre>";

var expectedText = @"<Section xml:space=""preserve"" Style=""{DynamicResource Pre_Section}"">
Expand All @@ -386,7 +382,7 @@ public void TranslateHtmlToXaml_SingleQuotesPresentInDiffCode_AreEscapedCorrectl

var htmlText = @"
<pre data-diff-id=""1"" data-diff-type=""noncompliant"">
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
</pre>
<pre data-diff-id=""1"" data-diff-type=""compliant"">
function f(a, g){}
Expand Down
5 changes: 4 additions & 1 deletion src/Education/XamlGenerator/DiffTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using System.ComponentModel.Composition;
using System.Text;
using System.Web;
using System.Xml;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Education/XamlGenerator/RuleHelpXamlTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d5f4c21

Please sign in to comment.