Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93f8d8f

Browse files
author
Carlos Ortega Huetos
committed
Mode responsability to FormField object
1 parent081314c commit93f8d8f

File tree

4 files changed

+51
-76
lines changed

4 files changed

+51
-76
lines changed

‎src/Symfony/Component/DomCrawler/Field/FormField.php‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ abstract class FormField
2222
* @var \DOMElement
2323
*/
2424
protected$node;
25-
/**
26-
* @var \DOMElement
27-
*/
28-
protected$label;
2925
/**
3026
* @var string
3127
*/
@@ -50,13 +46,11 @@ abstract class FormField
5046
/**
5147
* Constructor.
5248
*
53-
* @param \DOMElement $node The node associated with this field
54-
* @param \DOMElement|null $label The label associated with this field
49+
* @param \DOMElement $node The node associated with this field
5550
*/
56-
publicfunction__construct(\DOMElement$node,\DOMElement$label =null)
51+
publicfunction__construct(\DOMElement$node)
5752
{
5853
$this->node =$node;
59-
$this->label =$label;
6054
$this->name =$node->getAttribute('name');
6155
$this->xpath =new \DOMXPath($node->ownerDocument);
6256

@@ -70,7 +64,21 @@ public function __construct(\DOMElement $node, \DOMElement $label = null)
7064
*/
7165
publicfunctiongetLabel()
7266
{
73-
return$this->label;
67+
$xpath =new \DOMXPath($this->node->ownerDocument);
68+
69+
$labels =$xpath->query('ancestor::label[1]',$this->node);
70+
if ($labels->length >0) {
71+
return$labels->item(0);
72+
}
73+
74+
if ($this->node->hasAttribute('id')) {
75+
$labels =$xpath->query(sprintf('descendant::label[@for="%s"]',$this->node->getAttribute('id')));
76+
if ($labels->length >0) {
77+
return$labels->item(0);
78+
}
79+
}
80+
81+
return;
7482
}
7583

7684
/**

‎src/Symfony/Component/DomCrawler/Form.php‎

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -454,49 +454,22 @@ private function addField(\DOMElement $node)
454454
}
455455

456456
$nodeName =$node->nodeName;
457-
$label =$this->findAssociatedLabel($node);
458457
if ('select' ==$nodeName ||'input' ==$nodeName &&'checkbox' ==strtolower($node->getAttribute('type'))) {
459-
$this->set(newField\ChoiceFormField($node,$label));
458+
$this->set(newField\ChoiceFormField($node));
460459
}elseif ('input' ==$nodeName &&'radio' ==strtolower($node->getAttribute('type'))) {
461460
// there may be other fields with the same name that are no choice
462461
// fields already registered (see https://github.com/symfony/symfony/issues/11689)
463462
if ($this->has($node->getAttribute('name')) &&$this->get($node->getAttribute('name'))instanceof ChoiceFormField) {
464463
$this->get($node->getAttribute('name'))->addChoice($node);
465464
}else {
466-
$this->set(newField\ChoiceFormField($node,$label));
465+
$this->set(newField\ChoiceFormField($node));
467466
}
468467
}elseif ('input' ==$nodeName &&'file' ==strtolower($node->getAttribute('type'))) {
469-
$this->set(newField\FileFormField($node,$label));
468+
$this->set(newField\FileFormField($node));
470469
}elseif ('input' ==$nodeName && !in_array(strtolower($node->getAttribute('type')),array('submit','button','image'))) {
471-
$this->set(newField\InputFormField($node,$label));
470+
$this->set(newField\InputFormField($node));
472471
}elseif ('textarea' ==$nodeName) {
473-
$this->set(newField\TextareaFormField($node,$label));
472+
$this->set(newField\TextareaFormField($node));
474473
}
475474
}
476-
477-
/**
478-
* Finds the associated label to a given field node.
479-
*
480-
* @param \DOMElement $node The \DOMElement instance of the field
481-
*
482-
* @return \DOMNode|null The \DOMElement of the associated label tag or null if none was found
483-
*/
484-
privatefunctionfindAssociatedLabel(\DOMElement$node)
485-
{
486-
$xpath =new \DOMXPath($this->node->ownerDocument);
487-
488-
$labels =$xpath->query('ancestor::label[1]',$node);
489-
if ($labels->length >0) {
490-
return$labels->item(0);
491-
}
492-
493-
if ($node->hasAttribute('id')) {
494-
$labels =$xpath->query(sprintf('descendant::label[@for="%s"]',$node->getAttribute('id')));
495-
if ($labels->length >0) {
496-
return$labels->item(0);
497-
}
498-
}
499-
500-
return;
501-
}
502475
}

‎src/Symfony/Component/DomCrawler/Tests/Field/FormFieldTest.php‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,37 @@ public function testGetSetHasValue()
3636
$this->assertTrue($field->hasValue(),'->hasValue() always returns true');
3737
}
3838

39-
publicfunctiontestGetLabel()
39+
publicfunctiontestLabelReturnsNullIfNoneIsDefined()
4040
{
41-
$node =$this->createNode('input','',array());
42-
$label =$this->createNode('label','',array());
41+
$dom =new \DOMDocument();
42+
$dom->loadHTML('<html><form><input type="text" name="foo" value="foo" /><input type="submit" /></form></html>');
4343

44-
$field =newInputFormField($node);
45-
$this->assertNull($field->getLabel(),'->getLabel() returns null if none associated');
44+
$field =newInputFormField($dom->getElementById('foo'));
45+
$this->assertNull($field->getLabel(),'->getLabel() returns null if no label is defined');
46+
}
47+
48+
publicfunctiontestLabelIsAssignedByForAttribute()
49+
{
50+
$dom =new \DOMDocument();
51+
$dom->loadHTML('<html><form>
52+
<label for="foo">Foo label</label>
53+
<input type="text" id="foo" name="foo" value="foo" />
54+
<input type="submit" />
55+
</form></html>');
56+
57+
$field =newInputFormField($dom->getElementById('foo'));
58+
$this->assertEquals('Foo label',$field->getLabel()->textContent,'->getLabel() returns the associated label');
59+
}
60+
61+
publicfunctiontestLabelIsAssignedByParentingRelation()
62+
{
63+
$dom =new \DOMDocument();
64+
$dom->loadHTML('<html><form>
65+
<label for="foo">Foo label<input type="text" id="foo" name="foo" value="foo" /></label>
66+
<input type="submit" />
67+
</form></html>');
4668

47-
$field =newInputFormField($node,$label);
48-
$this->assertEquals($label,$field->getLabel(),'->getLabel() returns thefield label');
69+
$field =newInputFormField($dom->getElementById('foo'));
70+
$this->assertEquals('Foolabel',$field->getLabel()->textContent,'->getLabel() returns theparent label');
4971
}
5072
}

‎src/Symfony/Component/DomCrawler/Tests/FormTest.php‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -970,32 +970,4 @@ public function testgetPhpValuesWithEmptyTextarea()
970970
$form =newForm($nodes->item(0),'http://example.com');
971971
$this->assertEquals($form->getPhpValues(),array('example' =>''));
972972
}
973-
974-
publicfunctiontestLabelNotAssignedIfNoneIsDefined()
975-
{
976-
$form =$this->createForm('<form><input type="text" id="foo" name="foo" value="foo" /><input type="submit" /></form>');
977-
978-
$this->assertNull($form->get('foo')->getLabel(),'->getLabel() returns null if no label is defined');
979-
}
980-
981-
publicfunctiontestLabelIsAssignedByForAttribute()
982-
{
983-
$form =$this->createForm('<form>
984-
<label for="foo">Foo label</label>
985-
<input type="text" id="foo" name="foo" value="foo" />
986-
<input type="submit" />
987-
</form>');
988-
989-
$this->assertEquals('Foo label',$form->get('foo')->getLabel()->textContent,'->getLabel() returns the associated label');
990-
}
991-
992-
publicfunctiontestLabelIsAssignedByParentingRelation()
993-
{
994-
$form =$this->createForm('<form>
995-
<label for="foo">Foo label<input type="text" id="foo" name="foo" value="foo" /></label>
996-
<input type="submit" />
997-
</form>');
998-
999-
$this->assertEquals('Foo label',$form->get('foo')->getLabel()->textContent,'->getLabel() returns the parent label');
1000-
}
1001973
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp