8000 feat: handle form attribute by maelanleborgne · Pull Request #7 · zenstruck/dom · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: handle form attribute #7

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

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from

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
0