Skip to content

Conversation

maelanleborgne
Copy link

@maelanleborgne maelanleborgne commented Jun 19, 2025

Closes zenstruck/browser#166

Pretty rough implementation of a feature that is central for our project tests, and that we currently circumvent by abusing reflection methods. It would be very nice to have this feature shipped in the library.

I'll let GPT describe the PR :

Description

This pull request refactors the Form and Element classes to improve handling of form elements, particularly those with explicit form attributes. It updates the logic for identifying a form associated with an element.

Refactoring and Enhancements:

  • src/Dom/Node/Form.php: Added the findNodesForForm method to refine how form-related nodes are identified. This method accounts for both direct descendants and nodes explicitly referencing the form via the form attribute. Updated fields, buttons, and submitButtons methods to use findNodesForForm for improved accuracy.
  • src/Dom/Node/Form/Element.php: Enhanced the form method to prioritize elements with explicit form attributes before falling back to the closest form ancestor.

Test Coverage:

  • tests/Node/FormTest.php: Added unit tests to validate the behavior of forms with and without IDs, ensuring correct identification of associated fields and buttons.

Comment on lines 60 to 62
->reduce(function (Crawler $crawler) {
return !$crawler->getNode(0)?->attributes?->getNamedItem('form');
});
Copy link
Author

Choose a reason for hiding this comment

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

There is probably a better way to filter nodes than using reduce but my understanding of xpath is almost 0. If we can use Nodes::filter matching only on current nodes (not descendants) instead it would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support input with form attribute
1 participant