-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DomCrawler] Attach label to form fields #18322
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
Conversation
* | ||
* @return \DOMNode|null The \DOMElement of the associated label tag or null if none was found | ||
*/ | ||
private function findAssociatedLabel(\DOMElement $node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered moving this implementation to the FormField's getLabel()
method? You wouldn't have to add a new argument to FormField's constructor, and the logic wouldn't be called if the label wasn't accessed. ownerDocument
property is available on the $node
that's already passed to the FormField.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. I'll refactor it.
efd3577
to
93f8d8f
Compare
Sorry @stof, I realised while you where commenting it. It should be fixed now. |
return $labels->item(0); | ||
} | ||
|
||
if ($this->node->hasAttribute('id')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the id-based logic should have higher priority
What about something like this: <form>
<div>
<label for="my_field">Input the result of 3 + 4:</label>
</div>
<div>
<input type="text" id="my_field" name="my_field" />
</div>
</form> |
@xabbuh it will work as far as the label is within the form node computed by the crawler. In this case, parenting would fail but the label will still be found by the If it is suitable, I can add a new test to prove this although it seems to be working on my local. |
@carlosV2 You are right. I misread the way the XPath object is created. |
Thank you @carlosV2. |
While building a software to extract information from a webpage, I need to fill a form with hints on the fields labels.
Example:
I thought it would be handy to have the field labels attached to each field.
So given the previous form you could now do: