diff --git a/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs b/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs index 410fdc65..94789114 100644 --- a/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs +++ b/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs @@ -8,7 +8,6 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Linq; namespace HtmlAgilityPack { @@ -368,20 +367,14 @@ public void Remove(string name) throw new ArgumentNullException("name"); } - List listToRemove = new List(); - for (int i = 0; i < items.Count; i++) + for (int i = items.Count - 1; i >= 0; i--) { HtmlAttribute att = items[i]; if (String.Equals(att.Name, name, StringComparison.OrdinalIgnoreCase)) { - listToRemove.Add(i); + RemoveAt(i); } } - - foreach(var i in listToRemove.OrderByDescending(x => x)) - { - RemoveAt(i); - } } /// diff --git a/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs b/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs index 79178af3..97146975 100644 --- a/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs +++ b/src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocumentTests.cs @@ -564,6 +564,24 @@ public void OuterHtmlHasBeenCalled_RemoveCalled_SubsequentOuterHtmlCallsAreBroke Assert.AreEqual("", doc.DocumentNode.OuterHtml); } + [Test] + public void TestRemoveAttribute() + { + var output = @"

This is new heading

"; + + string html = "

This is new heading

"; + + var htmlDoc = new HtmlDocument(); + htmlDoc.LoadHtml(html); + + var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1"); + + h1Node.Attributes.Remove("a"); + h1Node.Attributes.Remove("b"); + + Assert.AreEqual(h1Node.OuterHtml, output); + } + [Test] public void TestAttributeDeEntitizeValue() {