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 <michale.reiter@cyber-solutions.at> Co-authored-by: Michael Reiter <michael.reiter@metro-external.digital>
This commit is contained in:
@@ -29,15 +29,15 @@ public sealed class HtmlStringUtilities
|
||||
|
||||
public HtmlString StripHtmlTags(string html, params string[]? tags)
|
||||
{
|
||||
var doc = new HtmlDocument();
|
||||
doc.LoadHtml("<p>" + html + "</p>");
|
||||
HtmlDocument doc = new HtmlDocument();
|
||||
doc.LoadHtml(html);
|
||||
|
||||
var targets = new List<HtmlNode>();
|
||||
List<HtmlNode> targets = new List<HtmlNode>();
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user