8000 bug #28147 [DomCrawler] exclude fields inside "template" tags (Gorjunov) · symfony/symfony@57c1432 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57c1432

Browse files
committed
bug #28147 [DomCrawler] exclude fields inside "template" tags (Gorjunov)
This PR was merged into the 2.8 branch. Discussion ---------- [DomCrawler] exclude fields inside "template" tags | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27285 | License | MIT | Doc PR | Exclude fields values/files if fields are inside template tag. I think better to exclude values only instead of excluding fields at all (described in ticket #27285) Commits ------- 19e3e15 [DomCrawler] exclude fields inside "template" tags
2 parents f02874e + 19e3e15 commit 57c1432

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Symfony/Component/DomCrawler/Form.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,14 +433,14 @@ private function initialize()
433433
// corresponding elements are either descendants or have a matching HTML5 form attribute
434434
$formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
435435

436-
$fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%1$s] | descendant::textarea[@form=%1$s] | descendant::select[@form=%1$s] | //form[@id=%1$s]//input[not(@form)] | //form[@id=%1$s]//button[not(@form)] | //form[@id=%1$s]//textarea[not(@form)] | //form[@id=%1$s]//select[not(@form)]', $formId));
436+
$fieldNodes = $xpath->query(sprintf('( descendant::input[@form=%s] | descendant::button[@form=%1$s] | descendant::textarea[@form=%1$s] | descendant::select[@form=%1$s] | //form[@id=%1$s]//input[not(@form)] | //form[@id=%1$s]//button[not(@form)] | //form[@id=%1$s]//textarea[not(@form)] | //form[@id=%1$s]//select[not(@form)] )[not(ancestor::template)]', $formId));
437437
foreach ($fieldNodes as $node) {
438438
$this->addField($node);
439439
}
440440
} else {
441441
// do the xpath query with $this->node as the context node, to only find descendant elements
442442
// however, descendant elements with form attribute are not part of this form
443-
$fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $this->node);
443+
$fieldNodes = $xpath->query('( descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] )[not(ancestor::template)]', $this->node);
444444
foreach ($fieldNodes as $node) {
445445
$this->addField($node);
446446
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ public function te F2A6 stGetValues()
394394

395395
$form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
396396
$this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
397+
398+
$form = $this->createForm('<form><template><input type="text" name="foo" value="foo" /></template><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
399+
$this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include template fields');
400+
$this->assertFalse($form->has('foo'));
397401
}
398402

399403
public function testSetValues()
@@ -444,6 +448,10 @@ public function testGetFiles()
444448

445449
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
446450
$this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
451+
452+
$form = $this->createForm('<form method="post"><template><input type="file" name="foo"/></template><input type="text" name="bar" value="bar"/><input type="submit"/></form>');
453+
$this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include template file fields');
454+
$this->assertFalse($form->has('foo'));
447455
}
448456

449457
public function testGetPhpFiles()
@@ -857,7 +865,7 @@ protected function getFormFieldMock($name, $value = null)
857865
protected function createForm($form, $method = null, $currentUri = null)
858866
{
859867
$dom = new \DOMDocument();
860-
$dom->loadHTML('<html>'.$form.'</html>');
868+
@$dom->loadHTML('<html>'.$form.'</html>');
861869

862870
$xPath = new \DOMXPath($dom);
863871
$nodes = $xPath->query('//input | //button');

0 commit comments

Comments
 (0)
0