Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - namespace Root.Core.Models.Fields
 - {
 - using System.Net;
 - using HtmlAgilityPack;
 - using Microsoft.AspNetCore.Html;
 - public readonly struct MarkupField
 - {
 - private readonly string _markup;
 - public MarkupField(string markup)
 - {
 - _markup = markup;
 - }
 - public static implicit operator MarkupField(string input)
 - {
 - return new MarkupField(input);
 - }
 - public static implicit operator string(MarkupField input)
 - {
 - return input.ToString();
 - }
 - public static implicit operator HtmlString(MarkupField input)
 - {
 - return new HtmlString(input.ToString());
 - }
 - public HtmlString Value
 - {
 - get
 - {
 - return new HtmlString(_markup);
 - }
 - }
 - public override string ToString()
 - {
 - return WebUtility.HtmlEncode(_markup);
 - }
 - public HtmlString OpenExternalsLinksInNewTabsOrWindows
 - {
 - get
 - {
 - HtmlDocument doc = new HtmlDocument();
 - doc.LoadHtml(_markup);
 - HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a[@href]");
 - if (nodes == null)
 - {
 - return new HtmlString(_markup);
 - }
 - foreach (HtmlNode node in (nodes))
 - {
 - string url = node.GetAttributeValue("href", string.Empty);
 - if (!url.StartsWith("/"))
 - {
 - string target = node.GetAttributeValue("target", string.Empty);
 - if (string.IsNullOrWhiteSpace(target))
 - {
 - node.Attributes.Add(doc.CreateAttribute("target", "_blank"));
 - }
 - }
 - }
 - return new HtmlString(doc.DocumentNode.OuterHtml);
 - }
 - }
 - }
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment