Skip to content
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

Unclosed p tag if there is a linebreak before [/group] #85

Closed
erikyo opened this issue Aug 13, 2021 · 8 comments
Closed

Unclosed p tag if there is a linebreak before [/group] #85

erikyo opened this issue Aug 13, 2021 · 8 comments

Comments

@erikyo
Copy link

erikyo commented Aug 13, 2021

hi @pwkip,

today a user of the wp forum made me discover that if you put a empty line before the row with [/group] in the cf7 mail template some non-validable code will be outputted.

cf7 mail template test:

[group g]
<span>yomama</span>

[/group]

[submit "submit"]

will return:

<div data-id="g" data-orig_data_id="g"  data-class="wpcf7cf_group">
<span>yomama</span></p>
</div>
<p><input type="submit" value="submit" class="wpcf7-form-control wpcf7-submit" /></p>

Not critical but this makes my plugin buggy because I use DOMDocument() to parse the form and unclosed tags are not allowed. So please fix :P

(wp 5.8 - cf7 5.4.2 - cf7cf 2.0.4)

@pwkip
Copy link
Owner

pwkip commented Apr 16, 2022

Hi @erikyo thanks for the bug report. However, I don't believe this is something that should be fixed on my side. groups are converted to divs by the plugin. The p tag is part of the auto-p magic CF7 performs.

If you write this code:

<div>
<span>yomama</span>

</div>

It will lead to the same problem.
Resulting HTML code:

<div>
    <span>yomama</span></p>
</div>

So the problem appears either with or without conditional fields.

@pwkip pwkip closed this as completed Apr 16, 2022
@erikyo
Copy link
Author

erikyo commented Nov 2, 2022

No, unfortunately I think the problem lies in this code because removing that part the issue disappear:

$form_parts = preg_split('/(\[\/?group(?:\]|\s.*?\]))/',$form, -1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
ob_start();
$stack = array();
foreach ($form_parts as $form_part) {
if (substr($form_part,0,7) == '[group ') {
$tag_parts = explode(' ',rtrim($form_part,']'));
array_shift($tag_parts);
$tag_id = $tag_parts[0];
$tag_html_type = 'div';
$tag_html_data = array();
foreach ($tag_parts as $i => $tag_part) {
if ($i==0) continue;
$tag_part = explode(':',$tag_part);
if ($tag_part[0] == 'inline') $tag_html_type = 'span';
else if ($tag_part[0] == 'clear_on_hide') $tag_html_data[] = 'data-clear_on_hide';
else if ($tag_part[0] == 'disable_on_hide' && WPCF7CF_IS_PRO) $tag_html_data[] = 'data-disable_on_hide';
else if ($tag_part[0] == 'class') $tag_html_data[] = 'class="'.($tag_part[1]??'').'"';
}
array_push($stack,$tag_html_type);
echo '<'.$tag_html_type.' data-id="'.$tag_id.'" data-orig_data_id="'.$tag_id.'" '.implode(' ',$tag_html_data).' data-class="wpcf7cf_group">';
} else if ($form_part == '[/group]') {
echo '</'.array_pop($stack).'>';
} else {
echo $form_part;
}
}

Anyway, even it was somewhat related to wordpress it need to be escaped... I tried without your plugin enabled and the issue doesn't happen and the generated html is fine. Please, I don't want to keep anymore a fallback (which turned out to be somewhat fruitless) to fix an issue that come before my plugin... so please fix it, if you need a hand just ask! 😉

@pwkip
Copy link
Owner

pwkip commented Nov 10, 2022

ok, will check again

@pwkip pwkip reopened this Nov 10, 2022
@erikyo
Copy link
Author

erikyo commented Nov 11, 2022

Thanks! i can explain why this happens in addition.

without the wpcf7cf_properties:

<p>[group g]<br />
 <span>yomama</span></p>
<p>[/group]</p>

with:

<div data-id="g" data-orig_data_id="g" data-class="wpcf7cf_group">
<span>yomama</span></p>
</div>

It seems that the regex does not work as intended when the line is breaked with a <br/>

@pwkip
Copy link
Owner

pwkip commented Dec 11, 2022

Hi @erikyo, I tried again with a fresh installation of WP with only CF7 5.6.4 installed, and the problem remains.

If I create a form with this code:

<div>
<span>yomama</span>

</div>

it results in this HTML:

<div>
  <span>yomama</span></p>
</div>

The last version of CF7 (5.7) seems to output valid HTML, but it f***s things up with unnecessary p tags.

<div> 
  <p><br />
    <span>yomama</span>
  </p>
</div>

I'm thinking about disabling the cf7_autop functionality of CF7 altogether when Conditional fields is active. I could do this by hooking into the wpcf7_autop_or_not filter late enough, and always return false. Hope this will not cause too many conflicts...

@erikyo
Copy link
Author

erikyo commented Dec 11, 2022

You're right, the issue is partially due to autop, but I guess that disabling it isn't the right way to fix it... the problem is that [group] is not a true html tag, and this allows the tags to be crossed before the regex split... maybe it should be converted to a div before autop?

@pwkip
Copy link
Owner

pwkip commented Dec 11, 2022

Well.. it is converted to <div> before autop. See my above example. CF7 messes up the HTML later. I could probably go and trim whitespaces and whatnot, but with the pace that CF7 is introducing core changes, I feel like it's okay to disable autop when you install conditional fields. I could introduce it as a new "feature" of conditional fields :)

@erikyo
Copy link
Author

erikyo commented Dec 11, 2022

Ah ok! I just tried again (with the last cf7 version - 5.7) and let's just say it solved it for me, because what matters was that the generated code was valid. Isn't very nice btw, but at least it is valid.

I can confirm that your first example renders like below, so for me the issue is solved (and wasn't in your repo). Thanks for the support!

<p><br>
  <span>yomama</span>
</p>

@erikyo erikyo closed this as completed Dec 11, 2022
pwkip added a commit that referenced this issue Dec 11, 2022
* Fully tested with Contact Form 7 version 5.7 (when WP_DEBUG is false). Note: due to some errors in Contact Form 7 version 5.7 you will need to make sure WP_DEBUG is set to false in your wp-config.php file.
* disable automatic insertion of paragraphs. Recent CF7 updates have changed the autop mechanism, causing too many problems with parsing the HTML code. If you still want to use autop (at your own risk) copy this code to your functions.php file: `add_filter( 'wpcf7_autop_or_not', '__return_true', 41, 0);`. Untill further notice Conditional Fields and Conditional Fields for Contact Form 7 will not use the automatic paragraph feature of CF7. Always try to write clean and valid HTML code. [GH issue 85](#85)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants