diff --git a/src/Umbraco.Tests/Web/MediaParserTests.cs b/src/Umbraco.Tests/Templates/MediaParserTests.cs similarity index 98% rename from src/Umbraco.Tests/Web/MediaParserTests.cs rename to src/Umbraco.Tests/Templates/MediaParserTests.cs index 7c9e7576b5..f7b5933a52 100644 --- a/src/Umbraco.Tests/Web/MediaParserTests.cs +++ b/src/Umbraco.Tests/Templates/MediaParserTests.cs @@ -13,8 +13,9 @@ using System; using System.Linq; using Umbraco.Core.Models; -namespace Umbraco.Tests.Web +namespace Umbraco.Tests.Templates { + [TestFixture] public class MediaParserTests { @@ -46,7 +47,7 @@ namespace Umbraco.Tests.Web public void Ensure_Image_Sources() { //setup a mock url provider which we'll use for testing - + var mediaType = new PublishedContentType(777, "image", PublishedItemType.Media, Enumerable.Empty(), Enumerable.Empty(), ContentVariation.Nothing); var media = new Mock(); media.Setup(x => x.ContentType).Returns(mediaType); @@ -91,7 +92,7 @@ namespace Umbraco.Tests.Web } - + } } } diff --git a/src/Umbraco.Tests/Templates/UdiParserTests.cs b/src/Umbraco.Tests/Templates/UdiParserTests.cs new file mode 100644 index 0000000000..eca7f6c4f0 --- /dev/null +++ b/src/Umbraco.Tests/Templates/UdiParserTests.cs @@ -0,0 +1,28 @@ +using NUnit.Framework; +using Umbraco.Web.Templates; +using System.Linq; +using Umbraco.Core; + +namespace Umbraco.Tests.Templates +{ + [TestFixture] + public class UdiParserTests + { + [Test] + public void Returns_Udi_From_Data_Udi_Html_Attributes() + { + var input = @"

+

+ +
+

"; + + var parser = new UdiParser(); + var result = parser.ParseUdisFromDataAttributes(input).ToList(); + Assert.AreEqual(2, result.Count); + Assert.AreEqual(Udi.Parse("umb://media/D4B18427A1544721B09AC7692F35C264"), result[0]); + Assert.AreEqual(Udi.Parse("umb://media-type/B726D735E4C446D58F703F3FBCFC97A5"), result[1]); + } + + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 20b29a3147..519f36012c 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -159,6 +159,7 @@ + @@ -255,7 +256,7 @@ - + diff --git a/src/Umbraco.Web/Templates/MediaParser.cs b/src/Umbraco.Web/Templates/MediaParser.cs index 2d20602e8e..071c6a5696 100644 --- a/src/Umbraco.Web/Templates/MediaParser.cs +++ b/src/Umbraco.Web/Templates/MediaParser.cs @@ -13,21 +13,6 @@ using Umbraco.Web.Routing; namespace Umbraco.Web.Templates { - /// - /// Parses out UDIs in strings - /// - public sealed class UdiParser - { - /// - /// Parses out UDIs from an html string based on 'data-udi' html attributes - /// - /// - /// - public IEnumerable ParseUdisFromDataAttributes(string text) - { - - } - } public sealed class MediaParser { diff --git a/src/Umbraco.Web/Templates/UdiParser.cs b/src/Umbraco.Web/Templates/UdiParser.cs new file mode 100644 index 0000000000..8bb5f8c579 --- /dev/null +++ b/src/Umbraco.Web/Templates/UdiParser.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using Umbraco.Core; + +namespace Umbraco.Web.Templates +{ + /// + /// Parses out UDIs in strings + /// + public sealed class UdiParser + { + private static readonly Regex DataUdiAttributeRegex = new Regex(@"data-udi=\\?(?:""|')(?umb://[A-z0-9\-]+/[A-z0-9]+)\\?(?:""|')", + RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); + + /// + /// Parses out UDIs from an html string based on 'data-udi' html attributes + /// + /// + /// + public IEnumerable ParseUdisFromDataAttributes(string text) + { + var matches = DataUdiAttributeRegex.Matches(text); + if (matches.Count == 0) + yield break; + + foreach (Match match in matches) + { + if (match.Groups.Count == 2 && Udi.TryParse(match.Groups[1].Value, out var udi)) + yield return udi; + } + } + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 74ac3f65f3..276cdf9ebc 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -249,6 +249,7 @@ +