From c3cccc3f31b10e780029e6f452fbec5f9e48beb2 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Fri, 31 Jan 2014 10:50:44 +0100 Subject: [PATCH] remove unnecessary attributes from XElements --- .../NReadability/NReadabilityTranscoder.cs | 35 +++++++++++++++++++ ReadSharp.Tests/ReadTests.cs | 14 ++++---- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/PortablePorts/NReadability/NReadabilityTranscoder.cs b/PortablePorts/NReadability/NReadabilityTranscoder.cs index bb58c0a..7f5d53f 100644 --- a/PortablePorts/NReadability/NReadabilityTranscoder.cs +++ b/PortablePorts/NReadability/NReadabilityTranscoder.cs @@ -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(), " + { + IEnumerable attributes = element.Attributes(); + List attributesToRemove = new List(); + + 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 attributesToRemove) + { + attributesToRemove.ForEach(attributeToRemove => attributeToRemove.Remove()); + } + private static void ResolveElementsUrls(XDocument document, string tagName, string attributeName, string url, Func attributeValueTransformer) { if (document == null) diff --git a/ReadSharp.Tests/ReadTests.cs b/ReadSharp.Tests/ReadTests.cs index 8bbd176..4204969 100644 --- a/ReadSharp.Tests/ReadTests.cs +++ b/ReadSharp.Tests/ReadTests.cs @@ -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); }