8000 [DomCrawler] Attach label to form fields by carlosV2 · Pull Request #18322 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Mode responsability to FormField object
  • Loading branch information
Carlos Ortega Huetos committed Apr 7, 2016
commit 93f8d8f3f72857c06a131a8583deb91a3bd6d37f
26 changes: 17 additions & 9 deletions src/Symfony/Component/DomCrawler/Field/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ abstract class FormField
* @var \DOMElement
*/
protected $node;
/**
* @var \DOMElement
*/
protected $label;
/**
* @var string
*/
Expand All @@ -50,13 +46,11 @@ abstract class FormField
/**
* Constructor.
*
* @param \DOMElement $node The node associated with this field
* @param \DOMElement|null $label The label associated with this field
* @param \DOMElement $node The node associated with this field
*/
public function __construct(\DOMElement $node, \DOMElement $label = null)
public function __construct(\DOMElement $node)
{
$this->node = $node;
$this->label = $label;
$this->name = $node->getAttribute('name');
$this->xpath = new \DOMXPath($node->ownerDocument);

Expand All @@ -70,7 +64,21 @@ public function __construct(\DOMElement $node, \DOMElement $label = null)
*/
public function getLabel()
{
return $this->label;
$xpath = new \DOMXPath($this->node->ownerDocument);

$labels = $xpath->query('ancestor::label[1]', $this->node);
if ($labels->length > 0) {
return $labels->item(0);
}

if ($this->node->hasAttribute('id')) {
Copy link
Member

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

$labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id')));
if ($labels->length > 0) {
return $labels->item(0);
}
}

return;
}

/**
Expand Down
37 changes: 5 additions & 32 deletions src/Symfony/Component/DomCrawler/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,49 +454,22 @@ private function addField(\DOMElement $node)
}

$nodeName = $node->nodeName;
$label = $this->findAssociatedLabel($node);
if ('select' == $nodeName || 'input' == $nodeName && 'checkbox' == strtolower($node->getAttribute('type'))) {
$this->set(new Field\ChoiceFormField($node, $label));
$this->set(new Field\ChoiceFormField($node));
} elseif ('input' == $nodeName && 'radio' == strtolower($node->getAttribute('type'))) {
// there may be other fields with the same name that are no choice
// fields already registered (see https://github.com/symfony/symfony/issues/11689)
if ($this->has($node->getAttribute('name')) && $this->get($node->getAttribute('name')) instanceof ChoiceFormField) {
$this->get($node->getAttribute('name'))->addChoice($node);
} else {
$this->set(new Field\ChoiceFormField($node, $label));
$this->set(new Field\ChoiceFormField($node));
}
} elseif ('input' == $nodeName && 'file' == strtolower($node->getAttribute('type'))) {
$this->set(new Field\FileFormField($node, $label));
$this->set(new Field\FileFormField($node));
} elseif ('input' == $nodeName && !in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image'))) {
$this->set(new Field\InputFormField($node, $label));
$this->set(new Field\InputFormField($node));
} elseif ('textarea' == $nodeName) {
$this->set(new Field\TextareaFormField($node, $label));
$this->set(new Field\TextareaFormField($node));
}
}

/**
* Finds the associated label to a given field node.
*
* @param \DOMElement $node The \DOMElement instance of the field
*
* @return \DOMNode|null The \DOMElement of the associated label tag or null if none was found
*/
private function findAssociatedLabel(\DOMElement $node)
{
$xpath = new \DOMXPath($this->node->ownerDocument);

$labels = $xpath->query('ancestor::label[1]', $node);
if ($labels->length > 0) {
return $labels->item(0);
}

if ($node->hasAttribute('id')) {
$labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $node->getAttribute('id')));
if ($labels->length > 0) {
return $labels->item(0);
}
}

return;
}
}
36 changes: 29 additions & 7 deletions src/Symfony/Component/DomCrawler/Tests/Field/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,37 @@ public function testGetSetHasValue()
$this->assertTrue($field->hasValue(), '->hasValue() always returns true');
}

public function testGetLabel()
public function testLabelReturnsNullIfNoneIsDefined()
{
$node = $this->createNode('input', '', array());
$label = $this->createNode('label', '', array());
$dom = new \DOMDocument();
$dom->loadHTML('<html><form><input type="text" id="foo" name="foo" value="foo" /><input type="submit" /></form></html>');

$field = new InputFormField($node);
$this->assertNull($field->getLabel(), '->getLabel() returns null if none associated');
$field = new InputFormField($dom->getElementById('foo'));
$this->assertNull($field->getLabel(), '->getLabel() returns null if no label is defined');
}

public function testLabelIsAssignedByForAttribute()
{
$dom = new \DOMDocument();
$dom->loadHTML('<html><form>
<label for="foo">Foo label</label>
<input type="text" id="foo" name="foo" value="foo" />
<input type="submit" />
</form></html>');

$field = new InputFormField($dom->getElementById('foo'));
$this->assertEquals('Foo label', $field->getLabel()->textContent, '->getLabel() returns the associated label');
}

public function testLabelIsAssignedByParentingRelation()
{
$dom = new \DOMDocument();
$dom->loadHTML('<html><form>
<label for="foo">Foo label<input type="text" id="foo" name="foo" value="foo" /></label>
<input type="submit" />
</form></html>');

$field = new InputFormField($node, $label);
$this->assertEquals($label, $field->getLabel(), '->getLabel() returns the field label');
$field = new InputFormField($dom->getElementById('foo'));
$this->assertEquals('Foo label', $field->getLabel()->textContent, '->getLabel() returns the parent label');
}
}
28 changes: 0 additions & 28 deletions src/Symfony/Component/DomCrawler/Tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -970,32 +970,4 @@ public function testgetPhpValuesWithEmptyTextarea()
$form = new Form($nodes->item(0), 'http://example.com');
$this->assertEquals($form->getPhpValues(), array('example' => ''));
}

public function testLabelNotAssignedIfNoneIsDefined()
{
$form = $this->createForm('<form><input type="text" id="foo" name="foo" value="foo" /><input type="submit" /></form>');

$this->assertNull($form->get('foo')->getLabel(), '->getLabel() returns null if no label is defined');
}

public function testLabelIsAssignedByForAttribute()
{
$form = $this->createForm('<form>
<label for="foo">Foo label</label>
<input type="text" id="foo" name="foo" value="foo" />
<input type="submit" />
</form>');

$this->assertEquals('Foo label', $form->get('foo')->getLabel()->textContent, '->getLabel() returns the associated label');
}

public function testLabelIsAssignedByParentingRelation()
{
$form = $this->createForm('<form>
<label for="foo">Foo label<input type="text" id="foo" name="foo" value="foo" /></label>
<input type="submit" />
</form>');

$this->assertEquals('Foo label', $form->get('foo')->getLabel()->textContent, '->getLabel() returns the parent label');
}
}
0