remove unnecessary attributes from XElements

This commit is contained in:
2014-01-31 10:50:44 +01:00
parent eb053f6df4
commit c3cccc3f31
2 changed files with 42 additions and 7 deletions
@@ -126,6 +126,7 @@ namespace ReadSharp.Ports.NReadability
private static readonly Regex _PageRegex = new Regex("pag(e|ing|inat)|([^a-z]|^)pag([^a-z]|$)", RegexOptions.IgnoreCase);
private static readonly Regex _LikelyParagraphDivRegex = new Regex("text|para|parbase|paragraph|figure", RegexOptions.IgnoreCase);
private static readonly Regex _LikelyImageContainerRegex = new Regex("image|figure|photo|media", RegexOptions.IgnoreCase);
private static readonly Regex _ReplaceAttributes = new Regex("^(data-|aria-|name|height|width|item|value|contenteditable|on|target|type)", RegexOptions.IgnoreCase);
#endregion
@@ -1303,6 +1304,8 @@ namespace ReadSharp.Ports.NReadability
RemoveElements(elementsToRemove);
CleanAttributes(articleContentElement);
/* Remove br's that are directly before paragraphs. */
articleContentElement.SetInnerHtml(_BreakBeforeParagraphRegex.Replace(articleContentElement.GetInnerHtml(), "<p"));
}
@@ -1571,6 +1574,33 @@ namespace ReadSharp.Ports.NReadability
}).Traverse(rootElement);
}
internal void CleanAttributes(XElement rootElement)
{
new ElementsTraverser(
element =>
{
IEnumerable<XAttribute> attributes = element.Attributes();
List<XAttribute> attributesToRemove = new List<XAttribute>();
if (attributes == null)
{
return;
}
foreach (XAttribute attribute in attributes)
{
if (_ReplaceAttributes.IsMatch(attribute.Name.ToString()))
{
attributesToRemove.Add(attribute);
}
}
RemoveAttributes(attributesToRemove);
}).Traverse(rootElement);
}
internal string GetUserStyleClass(string prefix, String enumStr)
{
var suffixSB = new StringBuilder();
@@ -1632,6 +1662,11 @@ namespace ReadSharp.Ports.NReadability
elementsToRemove.ForEach(elementToRemove => elementToRemove.Remove());
}
private static void RemoveAttributes(IEnumerable<XAttribute> attributesToRemove)
{
attributesToRemove.ForEach(attributeToRemove => attributeToRemove.Remove());
}
private static void ResolveElementsUrls(XDocument document, string tagName, string attributeName, string url, Func<AttributeTransformationInput, AttributeTransformationResult> attributeValueTransformer)
{
if (document == null)
+7 -7
View File
@@ -138,16 +138,16 @@ namespace ReadSharp.Tests
[Fact]
public async Task TestCriticalURIs()
{
//Article result = await reader.Read(new Uri("http://wpcentral.com.feedsportal.com/c/33999/f/616880/s/35a02b5e/sc/15/l/0L0Swpcentral0N0Cgameloft0Ediscusses0Etheir0Enew0Egame0Ebrothers0Earms0E30Esons0Ewar0Eceslive/story01.htm"));
//Assert.NotEmpty(result.Content);
Article result = await reader.Read(new Uri("http://wpcentral.com.feedsportal.com/c/33999/f/616880/s/35a02b5e/sc/15/l/0L0Swpcentral0N0Cgameloft0Ediscusses0Etheir0Enew0Egame0Ebrothers0Earms0E30Esons0Ewar0Eceslive/story01.htm"));
Assert.NotEmpty(result.Content);
//result = await reader.Read(new Uri("http://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx"));
//Assert.NotEmpty(result.Content);
result = await reader.Read(new Uri("http://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx"));
Assert.NotEmpty(result.Content);
//result = await reader.Read(new Uri("http://bit.ly/KAh7FJ"));
//Assert.NotEmpty(result.Content);
result = await reader.Read(new Uri("http://bit.ly/KAh7FJ"));
Assert.NotEmpty(result.Content);
Article result = await reader.Read(new Uri("http://www.nytimes.com/2014/01/31/world/europe/ukraine-unrest.html?hp&_r=0"));
result = await reader.Read(new Uri("http://www.nytimes.com/2014/01/31/world/europe/ukraine-unrest.html?hp&_r=0"));
Assert.True(result.Images != null && result.Images.Count > 0 && result.Images.SingleOrDefault(item => item.Uri.OriginalString == "http://static01.nyt.com/images/2014/01/31/world/31ukraine-cnd01/31ukraine-cnd01-articleLarge.jpg") != null);
}