Skip to content

GetAttributeNode extension methods added to Element #1223

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

Merged
merged 1 commit into from
Apr 15, 2025

Conversation

arekdygas
Copy link
Contributor

Types of Changes

Prerequisites

Please make sure you can check the following two boxes:

  • I have read the CONTRIBUTING document
  • My code follows the code style of this project

Contribution Type

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue, please reference the issue id)
  • New feature (non-breaking change which adds functionality, make sure to open an associated issue first)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

Description

GetAttributeNode(name) and GetAttributeNode(namespace, localName) added to IElement. Resolves #1221.

@CLAassistant
Copy link

CLAassistant commented Apr 10, 2025

CLA assistant check
All committers have signed the CLA.

@FlorianRappl
Copy link
Contributor

In general this is great, but it would be breaking in a way that users that implemented from IElement cannot just update. They would need to change (there are new implementations required; so it's not ABI compatible as well).

A nice way out here would be to instead use something like ElementExtensions.cs (maybe a new file such as DomElementExtensions.cs with the class being decorated as [DomExposed("Element")]) and place the two methods as extension methods there.

For instance:

[DomName("getAttributeNode")]
public static IAttr? GetAttributeNode(this IElement element, String name)
{
    if (NamespaceUri.Is(NamespaceNames.HtmlUri))
    {
        name = name.HtmlLower();
    }

    return Attributes.GetNamedItem(name);
}

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds two new methods, GetAttributeNode, to the IElement interface and its implementation, allowing attribute retrieval by name or by namespace and local name.

  • Added GetAttributeNode(string name) and GetAttributeNode(string? namespaceUri, string localName) in the Element implementation.
  • Updated the IElement interface to expose the new methods with corresponding documentation and DOM names.
  • Added tests in DOMActions.cs to verify behavior for namespaced and non-namespaced attribute retrieval.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/AngleSharp/Dom/Internal/Element.cs Added implementations for GetAttributeNode methods with handling for HTML namespace.
src/AngleSharp/Dom/IElement.cs Defined interface for new GetAttributeNode methods with XML documentation and DOM names.
src/AngleSharp.Core.Tests/Library/DOMActions.cs Added tests to validate the behavior of the new attribute retrieval methods.

@arekdygas
Copy link
Contributor Author

Yes - you're right. I'll try to fix it tomorrow.

@arekdygas
Copy link
Contributor Author

Do you think new file would be better than using already existing ElementExtensions.cs? The latter is not decorated with DomExposedAttribute now, but adding it and implementing these two methods should work as expected, as only methods decorated with DomNameAttribute are used during extensions processing. New file would be a bit cleaner, but I'm not sure if it's a good idea to have two classes extending Element, with the only difference, that one contains only members that could be exposed to JS.

What do you think?

@FlorianRappl
Copy link
Contributor

I agree. A new file would be cleaner imho, too.

@arekdygas arekdygas changed the title GetAttributeNode methods added to IElement GetAttributeNode extension methods added to Element Apr 15, 2025
@arekdygas
Copy link
Contributor Author

Done. Let me know if it looks ok.

Copy link
Contributor

@FlorianRappl FlorianRappl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks for the wonderful work!

@FlorianRappl FlorianRappl merged commit b278c49 into AngleSharp:devel Apr 15, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getAttributeNode and getAttributeNodeNS are missing
3 participants