8000 bug #53826 [DomCrawler][Form] Fix the exclusion of <template> (mpiot) · priyadi/symfony@cff8011 · GitHub
[go: up one dir, main page]

Skip to content

Commit cff8011

Browse files
bug symfony#53826 [DomCrawler][Form] Fix the exclusion of <template> (mpiot)
This PR was merged into the 6.4 branch. Discussion ---------- [DomCrawler][Form] Fix the exclusion of <template> | Q | A | ------------- | --- | Branch? | 6.4 <!-- see below --> | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#53823 | License | MIT In the DomCrawler, the way to get fields via xPath avoid to find elements inside a `<template>` element: ```html <form> <template> <input type="text" name="not_selected_because_in_template" /> </template> <input type="text" name="selected_because_out_of_template" /> </form> ``` But, it appear it also exclude form elements if the `<form>` is in a `<template>` (eg: in a `<turbo-stream>`), that bug-fix keep the previous behavior but allow the following: ```html <turbo-stream> <template> <form> <input type="text" name="selected_because_in_turbo_stream_1" /> <input type="text" name="selected_because_in_turbo_stream_2" /> </form> </template> </turbo-stream> ``` As mentioned in symfony#53823, an alternative should be to use `[not(ancestor::template/ancestor::form)]` in the xPath to allow: ```html <template> <form> <input type="text" name="selected_because_parent_is_form_1" /> <input type="text" name="selected_because_parent_is_form_2" /> </form> </template> ``` Commits ------- db3a9dd [DomCrawler] [Form] Fix the exclusion of <template>
2 parents 3d832bd + db3a9dd commit cff8011

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Symfony/Component/DomCrawler/Form.php

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

427-
$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));
427+
$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) or ancestor::turbo-stream )]', < 10000 span class="pl-s1">$formId));
428428
foreach ($fieldNodes as $node) {
429429
$this->addField($node);
430430
}
431431
} else {
432432
// do the xpath query with $this->node as the context node, to only find descendant elements
433433
// however, descendant elements with form attribute are not part of this form
434-
$fieldNodes = $xpath->query('( descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] )[not(ancestor::template)]', $this->node);
434+
$fieldNodes = $xpath->query('( descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] )[( not(ancestor::template) or ancestor::turbo-stream )]', $this->node);
435435
foreach ($fieldNodes as $node) {
436436
$this->addField($node);
437437
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ public function testGetValues()
432432
$form = $this->createForm('<form><template><input type="text" name="foo" value="foo" /></template><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
433433
$this->assertEquals(['bar' => 'bar'], $form->getValues(), '->getValues() does not include template fields');
434434
$this->assertFalse($form->has('foo'));
435+
436+
$form = $this->createForm('<turbo-stream><template><form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><select multiple="multiple" name="baz[]"></select><input type="submit" /></form></template></turbo-stream>');
437+
$this->assertEquals(['foo[bar]' => 'foo', 'bar' => 'bar', 'baz' => []], $form->getValues(), '->getValues() returns all form field values from template field inside a turbo-stream');
435438
}
436439

437440
public function testSetValues()
@@ -486,6 +489,9 @@ public function testGetFiles()
486489
$form = $this->createForm('<form method="post"><template><input type="file" name="foo"/></template><input type="text" name="bar" value="bar"/><input type="submit"/></form>');
487490
$this->assertEquals([], $form->getFiles(), '->getFiles() does not include template file fields');
488491
$this->assertFalse($form->has('foo'));
492+
493+
$form = $this->createForm('<turbo-stream><template><form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form></template></turbo-stream>');
494+
$this->assertEquals(['foo[bar]' => ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0]], $form->getFiles(), '->getFiles() return files fields from template inside turbo-stream');
489495
}
490496

491497
public function testGetPhpFiles()

0 commit comments

Comments
 (0)
0