Skip to content

Commit

Permalink
Add method SafeSetAttribute to safely write attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriele-tomassetti committed Feb 4, 2023
1 parent 3ea8a93 commit 1ea4acd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/SmartReader/NodeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Text;

namespace SmartReader
{
Expand Down Expand Up @@ -59,11 +60,7 @@ internal static IElement SetNodeTag(IElement node, string tag)
{
var attr = node.Attributes[i]!;

// the possible result of malformed HTML
if (!attr.Name.Contains("<") && !attr.Name.Contains(">"))
{
replacement.SetAttribute(attr.Name, attr.Value);
}
NodeUtility.SafeSetAttribute(replacement, attr);
}
return replacement;
}
Expand Down Expand Up @@ -622,5 +619,17 @@ internal static List<INode> GetNodeAncestors(INode node, int maxDepth = 0)
}
return next as IElement;
}

/// <summary>
/// This is a safe method to write attributes
/// because AngleSharp cannot handle attribute names that would be invalid in XML
/// </summary>
internal static void SafeSetAttribute(this IElement child, IAttr attribute)
{
if (attribute.Name.IsXmlName())
child.SetAttribute(attribute.Name, attribute.Value);
else
child.SetAttribute(attribute.Name.CleanXmlName(), attribute.Value);
}
}
}
5 changes: 1 addition & 4 deletions src/SmartReader/Readability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ internal static void SimplifyNestedElements(IElement articleContent)
var child = node.Children[0];
for (var i = 0; i < node.Attributes.Length; i++)
{
if (node.Attributes[i]!.Name.IsXmlName())
child.SetAttribute(node.Attributes[i]!.Name, node.Attributes[i]!.Value);
else
child.SetAttribute(node.Attributes[i]!.Name.CleanXmlName(), node.Attributes[i]!.Value);
NodeUtility.SafeSetAttribute(child, node.Attributes[i]);
}
node.Parent.ReplaceChild(child, node);
node = child;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartReader/TextUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ internal static string CleanXmlName(this string str)
}
else
return str;
}
}
}
}

0 comments on commit 1ea4acd

Please sign in to comment.