beautifier / js-beautify

Beautifier for javascript
https://beautifier.io
MIT License
8.58k stars 1.37k forks source link

Indentation issue inside php tags on html/php #1972

Open tenzap opened 2 years ago

tenzap commented 2 years ago

Description

Part of the PHP code changed while this wasn't expected: (it was indented, it lost indentation).

Command: html-beautify -r -t --templating=php --end-with-newline=true -b expand "file"

Input

The code looked like this before beautification:

<?php $this->load->view('js_init/users/js_users');
if ($users->num_rows() === 0):
    if ($_POST)
    {
        echo "<p><i><?php echo lang('tni_user_not_found'); ?></i></p>";
    }
    else
    {
        echo "<p><i><?php echo lang('tni_user_search_empty'); ?></i></p>";
    }
else: ?>
<p>aa</p>
<?php endif; ?>

Expected Output

The code should have looked like this after beautification:

<?php $this->load->view('js_init/users/js_users');
if ($users->num_rows() === 0):
    if ($_POST)
    {
        echo "<p><i><?php echo lang('tni_user_not_found'); ?></i></p>";
    }
    else
    {
        echo "<p><i><?php echo lang('tni_user_search_empty'); ?></i></p>";
    }
else: ?>
<p>aa</p>
<?php endif; ?>

Actual Output

The code actually looked like this after beautification:

<?php $this->load->view('js_init/users/js_users');
if ($users->num_rows() === 0):
    if ($_POST)
    {
        echo "<p><i><?php echo lang('tni_user_not_found'); ?></i></p>";
}
else
{
echo "<p><i><?php echo lang('tni_user_search_empty'); ?></i></p>";
}
else: ?>
<p>aa</p>
<?php endif; ?>

Steps to Reproduce

Environment

OS: linux debian 11

Settings

default + command: html-beautify -r -t --templating=php --end-with-newline=true -b expand "file"

tenzap commented 2 years ago

I just saw that there is such a terrible <?php inside the text of the echo. That is the culprit. If I remove it, then there is no indentation issue.

So the problem is linked to the presence of some <?php ?> nested inside a another <?php ?> in this case it is hidden in a PHP string