Skip to content

[DomCrawler] [Form] Fields in <template> are excluded (<turbo-stream>) #53823

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
mpiot opened this issue Feb 7, 2024 · 0 comments
Closed

[DomCrawler] [Form] Fields in <template> are excluded (<turbo-stream>) #53823

mpiot opened this issue Feb 7, 2024 · 0 comments

Comments

@mpiot
Copy link
Contributor

mpiot commented Feb 7, 2024

Symfony version(s) affected

6.4.3

Description

When using Turbo-Stream, we can want to test it directly:

  1. GET on the path that give us a Turbo-Stram response
  2. Check attributes: action, method
  3. If it's a form, test to submit it

But in tests, methods like submitForm('Save'), selectButton('Save')->form(), should give us the ability to deal with, but the method exclude all fields inside <template>.

In the file src/Symfony/Component/DomCrawler/Form.php (line 421), it exclude all in <template>, by the way, it appear impossible to use the Form methods for testing easily forms inside streams.

How to reproduce

  1. Create a form inside a Turbo-Stream
  2. Call the path that return us that

Possible Solution

  1. Remove the part: [not(ancestor::template)]
  2. Replace it by [( not(ancestor::template) or ancestor::turbo-stream )]
  3. Replace it to respect the order, in test we want to avoid catch of <template> inside a <form>, no <form> inside a <template>: [not(ancestor::template/ancestor::form)]

Additional Context

No response

@mpiot mpiot added the Bug label Feb 7, 2024
@mpiot mpiot changed the title [DomCrawler] [Form] Fields in <template> are excluded [DomCrawler] [Form] Fields in <template> are excluded (<turbo-stream>) Feb 7, 2024
nicolas-grekas added a commit that referenced this issue Feb 9, 2024
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 #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 #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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants