-
-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not all attributes are removed with Remove() if there are multiple attributes of the same name #461
Comments
Quick analysis The removal logic is broken: html-agility-pack/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs Lines 358 to 365 in def26b2
Removing an element at position As the loop is iterating in incrementing fashion, removing an element will therefore cause the loop to ignore/skip over the element following a removed element ¹, unless the iterator variable However, instead of clumsily adjusting the iterator variable ², i would like to suggest a somewhat simpler fix: Let the for loop iterate in decrementing fashion, thus it won't skip any elements naturally. for (int i = items.Count - 1; i >= 0; i--)
{
HtmlAttribute att = items[i];
if (String.Equals(att.Name, name, StringComparison.OrdinalIgnoreCase))
{
RemoveAt(i);
}
} But in the end, either way (whether it is using a decrementing loop, or wether it is keeping the incrementing loop with an additional ¹ This can be seen in the dotnetfiddle demo provided by the original report. Removal of the ² Manipulating an iterator variable inside a loop body is in my opinion not helping code readibility and maintainability when there are other simple ways that avoid scattering operations manipulating the iterator variable across the loop body. |
Hello @kawaicheung , Thank you for reporting, we will look at it. Thank also @elgonzo for your detailed answer. The current issue is a very common mistake. We will check if we will apply the fix you provided or create a list of thing to remove which is both usually 2 good solutions for this issue. Best Regards, Jon Sponsorship Performance Libraries Runtime Evaluation |
Hello @kawaicheung , The v1.11.40 has been released. My developer preferred to do it a little bit differently but the current issue should be fixed. Let me know if everything is now working correctly. Best Regards, Jon |
1. Description
When using
Remove()
on theHtmlAttributeCollection
where there are multiple attributes of the same name, only some get removed.In the fiddle below, the attribute
class
appears five times. Upon callingRemove("class")
on the attribute collection, I would expect all five would be removed but instead two still remain.2. Fiddle or Project
(https://dotnetfiddle.net/zrfFnI)
4. Any further technical details
Add any relevant detail can help us, such as:
The text was updated successfully, but these errors were encountered: