Skip to content

Commit

Permalink
Handle cases where sanitiseHtml empty
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldgray committed Nov 16, 2023
1 parent a20ba40 commit f113a03
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/IIIF/IIIF.Tests/Presentation/HtmlSanitiserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ public class HtmlSanitiserTests
public void SanitiseHtml_ReturnsGivenString_IfNullOrEmpty(string val)
=> val.SanitiseHtml().Should().Be(val);

[Theory]
[InlineData(true)]
[InlineData(false)]
public void SanitiseHtml_ReturnsEmptyString_IfGivenInvalidHtml(bool ignoreNonHtml)
{
const string input = "<div>invalid html</div>";
const string expected = "";

var actual = input.SanitiseHtml(ignoreNonHtml);

actual.Should().Be(expected);
}

[Fact]
public void SanitiseHtml_Trims_Whitespace_FromBeginningAndEnd_IfIgnoreNonHtmlFalse()
{
Expand Down
4 changes: 3 additions & 1 deletion src/IIIF/IIIF/Presentation/HtmlSanitiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static string SanitiseHtml(this string propertyValue, bool ignoreNonHtml

var workingString = Sanitizer.Sanitize(propertyValue.Trim());

if (string.IsNullOrEmpty(workingString)) return workingString;
if (IsHtmlString(workingString)) return workingString;

if (!HtmlSanitizerOptions.AllowedTags.Contains(nonHtmlWrappingTag))
Expand All @@ -85,5 +86,6 @@ public static string SanitiseHtml(this string propertyValue, bool ignoreNonHtml
return Sanitizer.Sanitize(workingString);
}

private static bool IsHtmlString(string candidate) => candidate[0] == '<' && candidate[^1] == '>';
private static bool IsHtmlString(string candidate)
=> candidate[0] == '<' && candidate[^1] == '>';
}

0 comments on commit f113a03

Please sign in to comment.