diff --git a/src/Umbraco.Web.Common/Mvc/HtmlStringUtilities.cs b/src/Umbraco.Web.Common/Mvc/HtmlStringUtilities.cs index 90c4307807..b568d457f4 100644 --- a/src/Umbraco.Web.Common/Mvc/HtmlStringUtilities.cs +++ b/src/Umbraco.Web.Common/Mvc/HtmlStringUtilities.cs @@ -29,15 +29,15 @@ public sealed class HtmlStringUtilities public HtmlString StripHtmlTags(string html, params string[]? tags) { - var doc = new HtmlDocument(); - doc.LoadHtml("

" + html + "

"); + HtmlDocument doc = new HtmlDocument(); + doc.LoadHtml(html); - var targets = new List(); + List targets = new List(); + HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(".//*"); - HtmlNodeCollection? nodes = doc.DocumentNode.FirstChild.SelectNodes(".//*"); if (nodes != null) { - foreach (HtmlNode? node in nodes) + foreach (HtmlNode node in nodes) { // is element if (node.NodeType != HtmlNodeType.Element) @@ -45,7 +45,7 @@ public sealed class HtmlStringUtilities continue; } - var filterAllTags = tags == null || !tags.Any(); + bool filterAllTags = tags == null || !tags.Any(); if (filterAllTags || (tags?.Any(tag => string.Equals(tag, node.Name, StringComparison.CurrentCultureIgnoreCase)) ?? false)) @@ -54,9 +54,11 @@ public sealed class HtmlStringUtilities } } + // we have to reverse the list in order to not override the changes to the nodes later + targets.Reverse(); foreach (HtmlNode target in targets) { - HtmlNode content = doc.CreateTextNode(target.InnerText); + HtmlNode content = doc.CreateTextNode(target.InnerHtml); target.ParentNode.ReplaceChild(content, target); } } @@ -65,7 +67,7 @@ public sealed class HtmlStringUtilities return new HtmlString(html); } - return new HtmlString(doc.DocumentNode.FirstChild.InnerHtml.Replace(" ", " ")); + return new HtmlString(doc.DocumentNode.InnerHtml.Replace(" ", " ")); } public string Join(string separator, params object[] args)