RiFi2k / format-html-in-php

Basically this vscode extension uses all your standard configurations for html formatting, and your standard configurations for format on save, etc. It more or less works exactly how vscode should already work as it pertains to HTML in PHP files.
The Unlicense
33 stars 6 forks source link

Bug when php is inside <script> #31

Closed expelliamus closed 3 years ago

expelliamus commented 5 years ago

Suppose that I have a php file which contains this:

<script>
    document.addEventListener("DOMContentLoaded", function(event) {
        GlobalVariables.taxonomy = <?= $taxonomy ?>;
    });
</script>

when I save the document I get:

<script>
    document.addEventListener("DOMContentLoaded", function(event) {
         GlobalVariables.taxonomy = < ? = $taxonomy ? > ;
    });
</script>

as you can see the extension has added spaces between the <? tag, how can I prevent this?

RiFi2k commented 5 years ago

I was able to reproduce the same issue on https://beautifier.io/ after changing the drop down to format HTML.

Could possibly be related to https://github.com/beautify-web/js-beautify/issues/1646 only need to also check for <? to also leave alone.

expelliamus commented 5 years ago

when this will be fixed? thanks

RiFi2k commented 5 years ago

Looks like they have it in the next milestone to be fixed. As soon as it's patched I'll pull the version into this extension.

https://github.com/beautify-web/js-beautify/milestone/47

RiFi2k commented 5 years ago

Related to #18

MohsinEngineer commented 4 years ago

Any update on this issue? Maybe add a temporary fix for this problem?

RiFi2k commented 4 years ago

@MohsinEngineer So since you most likely have something else handling formatting your JS, and this extension isn't really registering itself as a formatting extension anyways so something else could handle inside script tags. You could add script tags to the content_unformatted setting. If you do this then it will simply skip the content of script tags.

"html.format.contentUnformatted": "pre,code,textarea,script"
lesfen commented 4 years ago

Is there a fix coming for this anytime soon?

RiFi2k commented 4 years ago

So the problem is upstream, you can verify that by pasting the same content - https://beautifier.io

Unfortunately the project is really hurting on contributors and isn't really going out and fixing issues with their library formatting PHP script tags as a priority.

That being said if you simply exclude the contents of script tags from formatting with this extension you can have another JS formatting library handle JS inside the tags by highlighting the code and running a formatter manually, a few exist for this task. Because this extension does not need to be registered as the primary formatting extension as explained in the docs, you can register another one as primary and this will still continue to work because it doesn't hook in there.

"html.format.contentUnformatted": "pre,code,textarea,script"
RiFi2k commented 4 years ago

But I did actually figure out you can go...

index.php

<?php
$hi = "world";
?>
<html>
    <body>
        <script>
                document.addEventListener("DOMContentLoaded", function(event) {
                        GlobalVariables.taxonomy = "<?= $hi; ?>";
                });
        </script>
    </body>
</html>

Then from the cmd line I ran.

php index.php

and got

<html>
    <body>
        <script>
                document.addEventListener("DOMContentLoaded", function(event) {
                        GlobalVariables.taxonomy = "world";
                });
        </script>
    </body>
</html>

So quoting the tags works.

lesfen commented 4 years ago

I am running 1.6.2, strange that I am not getting updates. So I just uninstalled and installed again and still have 1.6.2.

Either way, your other suggestion fixed my problem "html.format.contentUnformatted": "pre,code,textarea,script"

RiFi2k commented 4 years ago

Oh, I rolled 1.6.2 like right after replying to your message so it probably slipped in before you noticed. Their update didn't do anything to mitigate the issue unfortunately. But quoting around the PHP tags should work if you don't disable, but obviously that won't be a fit for every single use case.

Glad at least we can ignore the script tags for now.

I'm thinking about another library but it's pretty slim pickings for HTML formatting, I'd welcome suggestions if anyone knows of a solid lib.

bitwiseman commented 3 years ago

The reason for this is that, in jsbeautifier templating is turned on by default in HTML and turned off by default in JavaScript. This was done to limit the potential for breaking changes to existing users.

I suppose this default should be changed to on by default inside html <script> tags, but that is longer term discussion.

The fix is to enble php to the settings for jsbeautifier.

{
  "html": {
    "js": {
        "templating": "php"
    }
  }
}

Filed #47 to fix this in this project.