From b3237c498b074a9b1fc5d178dcaa218d1c36da70 Mon Sep 17 00:00:00 2001 From: CyberReiter <90895378+CyberReiter@users.noreply.github.com> Date: Sun, 24 Jul 2022 00:53:04 +0200 Subject: [PATCH] Fix StripHtmlTagsMethod by adding Regex (#12607) * #umbraco12400 fixing StripHtmlTagsMethod by adding Regex * changing logic by removing .FirstChild calls * fixing line endings * final fix * removing unused using Co-authored-by: Michael Reiter Co-authored-by: Michael Reiter --- .../Mvc/HtmlStringUtilities.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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)