Skip to content

Commit

Permalink
Improve HtmlHelperService
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Dec 22, 2024
1 parent 43883aa commit 56256fb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/DNTCommon.Web.Core/DNTCommon.Web.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>DNTCommon.Web.Core provides common scenarios' solutions for ASP.NET Core applications.</Description>
<VersionPrefix>8.1.0</VersionPrefix>
<VersionPrefix>8.2.0</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -63,7 +63,7 @@
<Using Include="System.Net.Http.Headers"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.182">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.183">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 6 additions & 0 deletions src/DNTCommon.Web.Core/Http/HtmlHelperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ public async Task<string> GetUrlTitleAsync(Uri uri)
public string ReplaceImageUrlsWithEmbeddedDataImages(string html, Func<string, byte[]?> imageBuilder)
=> html.ReplaceImageUrlsWithEmbeddedDataImages(imageBuilder, _logger);

/// <summary>
/// imageUrlBuilder delegate gives you an image's src, and then you can return its new url.
/// </summary>
public string ReplaceImageUrlsWithNewImageUrls(string html, Func<string, string?> imageUrlBuilder)
=> html.ReplaceImageUrlsWithNewImageUrls(imageUrlBuilder, _logger);

/// <summary>
/// Returns HtmlAttribute's of the selected nodes.
/// </summary>
Expand Down
43 changes: 43 additions & 0 deletions src/DNTCommon.Web.Core/Http/HtmlHelperServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ public static IServiceCollection AddHtmlHelperService(this IServiceCollection se
return services;
}

/// <summary>
/// imageUrlBuilder delegate gives you an image's src, and then you can return its new url.
/// </summary>
public static string ReplaceImageUrlsWithNewImageUrls(this string html,
Func<string, string?> imageUrlBuilder,
ILogger? logger = null)
{
ArgumentNullException.ThrowIfNull(html);
ArgumentNullException.ThrowIfNull(imageUrlBuilder);

var htmlDocument = html.CreateHtmlDocument(logger);
var imageNodes = htmlDocument.DocumentNode.SelectNodes(xpath: "//img[@src]");

if (imageNodes == null)
{
return html;
}

foreach (var imageNode in imageNodes)
{
var imageSrcAttribute = imageNode.GetSrcAttribute();
var imageSrcValue = imageSrcAttribute?.Value?.Trim();

if (imageSrcAttribute is null || imageSrcValue is null ||
imageSrcValue.StartsWith(value: "file:/", StringComparison.OrdinalIgnoreCase) ||
imageSrcValue.IsBase64EncodedImage())
{
continue;
}

var newUrl = imageUrlBuilder(imageSrcValue);

if (newUrl is null)
{
continue;
}

imageSrcAttribute.Value = newUrl;
}

return htmlDocument.DocumentNode.OuterHtml;
}

/// <summary>
/// imageBuilder delegate gives you an image's src, and then you can return its equivalent data bytes to be inserted as
/// an embedded data:image
Expand Down
5 changes: 5 additions & 0 deletions src/DNTCommon.Web.Core/Http/IHtmlHelperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public interface IHtmlHelperService
/// an embedded data:image
/// </summary>
string ReplaceImageUrlsWithEmbeddedDataImages(string html, Func<string, byte[]?> imageBuilder);

/// <summary>
/// imageUrlBuilder delegate gives you an image's src, and then you can return its new url.
/// </summary>
string ReplaceImageUrlsWithNewImageUrls(string html, Func<string, string?> imageUrlBuilder);
}

0 comments on commit 56256fb

Please sign in to comment.