Skip to content

Commit

Permalink
Merge pull request #44 from digirati-co-uk/feature/lvp_helper
Browse files Browse the repository at this point in the history
Helper for finding LabelValuePairs by language + label
  • Loading branch information
donaldgray authored Nov 16, 2023
2 parents 34b7d93 + f113a03 commit a7174e3
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 4 deletions.
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
64 changes: 64 additions & 0 deletions src/IIIF/IIIF.Tests/Presentation/V3/LabelValuePairXTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections.Generic;
using IIIF.Presentation.V3;
using IIIF.Presentation.V3.Strings;

namespace IIIF.Tests.Presentation.V3;

public class LabelValuePairXTests
{
[Fact]
public void TryGetValue_False_NullList()
{
List<LabelValuePair> metadata = null;

var result = metadata.TryGetValue("en", "bar", out var languageMap);

result.Should().BeFalse();
languageMap.Should().BeNull();
}

[Fact]
public void TryGetValue_False_EmptyList()
{
var metadata = new List<LabelValuePair>();

var result = metadata.TryGetValue("en", "bar", out var languageMap);

result.Should().BeFalse();
languageMap.Should().BeNull();
}

[Fact]
public void TryGetValue_True_ReturnsCorrect_IfFound()
{
var metadata = new List<LabelValuePair> { new("en", "foo", "bar", "baz") };
var expected = new LanguageMap("en", new[] { "bar", "baz" });

var result = metadata.TryGetValue("en", "foo", out var languageMap);

result.Should().BeTrue();
languageMap.Should().BeEquivalentTo(expected);
}

[Fact]
public void TryGetValue_False_IfNotFoundForLanguage()
{
var metadata = new List<LabelValuePair> { new("en", "foo", "bar", "baz") };

var result = metadata.TryGetValue("en", "bar", out var languageMap);

result.Should().BeFalse();
languageMap.Should().BeNull();
}

[Fact]
public void TryGetValue_False_IfLabelForDifferentLanguage()
{
var metadata = new List<LabelValuePair> { new("en", "foo", "bar", "baz") };

var result = metadata.TryGetValue("fr", "bar", out var languageMap);

result.Should().BeFalse();
languageMap.Should().BeNull();
}
}
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] == '>';
}
39 changes: 39 additions & 0 deletions src/IIIF/IIIF/Presentation/V3/LabelValuePairX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using IIIF.Presentation.V3.Strings;
using IIIF.Utils;

namespace IIIF.Presentation.V3;

public static class LabelValuePairX
{
/// <summary>
/// Try get values associated with specified language and label.
/// </summary>
/// <param name="labelValuePairs">Collection of <see cref="labelValuePairs"/> to search</param>
/// <param name="language">Language to get values for</param>
/// <param name="label">Label to get values for</param>
/// <param name="languageMap">Found <see cref="LanguageMap"/></param>
/// <returns>true if matching value found, else false</returns>
public static bool TryGetValue(
this List<LabelValuePair>? labelValuePairs,
string language,
string label,
[NotNullWhen(true)] out LanguageMap? languageMap)
{
languageMap = null;
if (labelValuePairs.IsNullOrEmpty()) return false;

foreach (var langValues in labelValuePairs.Where(lvp => lvp.Label.ContainsKey(language)))
{
if (langValues.Label.Any(l => l.Value.Contains(label)))
{
languageMap = langValues.Value;
return true;
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/IIIF/IIIF/Presentation/V3/ResourceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ public abstract class ResourceBase : JsonLdBase, IResource
/// </summary>
[JsonProperty(Order = 1000)]
public List<ResourceBase>? PartOf { get; set; }
}
}
1 change: 0 additions & 1 deletion src/IIIF/IIIF/Presentation/V3/Strings/LabelValuePair.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using Newtonsoft.Json;

namespace IIIF.Presentation.V3.Strings;

Expand Down
3 changes: 2 additions & 1 deletion src/IIIF/IIIF/Utils/CollectionX.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace IIIF.Utils;

internal static class CollectionX
{
public static bool IsNullOrEmpty<T>(this List<T> collection)
public static bool IsNullOrEmpty<T>([NotNullWhen(false)] this List<T>? collection)
{
return collection == null || collection.Count == 0;
}
Expand Down

0 comments on commit a7174e3

Please sign in to comment.