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
-------

19e3e154d7 [DomCrawler] exclude fields inside "template" tags
This commit is contained in:
Fabien Potencier 2018-11-26 07:47:09 +01:00
commit 57c1432045
2 changed files with 11 additions and 3 deletions

View File

@ -433,14 +433,14 @@ class Form extends Link implements \ArrayAccess
// corresponding elements are either descendants or have a matching HTML5 form attribute
$formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
$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));
$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));
foreach ($fieldNodes as $node) {
$this->addField($node);
}
} else {
// do the xpath query with $this->node as the context node, to only find descendant elements
// however, descendant elements with form attribute are not part of this form
$fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $this->node);
$fieldNodes = $xpath->query('( descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] )[not(ancestor::template)]', $this->node);
foreach ($fieldNodes as $node) {
$this->addField($node);
}

View File

@ -394,6 +394,10 @@ class FormTest extends TestCase
$form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
$form = $this->createForm('<form><template><input type="text" name="foo" value="foo" /></template><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include template fields');
$this->assertFalse($form->has('foo'));
}
public function testSetValues()
@ -444,6 +448,10 @@ class FormTest extends TestCase
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
$this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
$form = $this->createForm('<form method="post"><template><input type="file" name="foo"/></template><input type="text" name="bar" value="bar"/><input type="submit"/></form>');
$this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include template file fields');
$this->assertFalse($form->has('foo'));
}
public function testGetPhpFiles()
@ -857,7 +865,7 @@ class FormTest extends TestCase
protected function createForm($form, $method = null, $currentUri = null)
{
$dom = new \DOMDocument();
$dom->loadHTML('<html>'.$form.'</html>');
@$dom->loadHTML('<html>'.$form.'</html>');
$xPath = new \DOMXPath($dom);
$nodes = $xPath->query('//input | //button');