首页 > 解决方案 > Treat inline script tags with type="text/plain" as JavaScript

问题描述

How can I make PhpStorm treat the script tags with the type="text/plain" property as JavaScript? For example the following fragment:

<script type="text/plain">
alert("test");
</script>

I have tried adding a language injection with the following config, which makes code highlighting work, but code style doesn't work. Every time I reformat the code, it just appends a new line at the bottom of the script tag. It should indent the alert by 4 spaces, just like it does with a script tag that doesn't have the type="text/plain" property.

PhpStorm language injection

The reason I'm using type="text/plain" is because I'm using a GDPR JavaScript plugin, that only loads the JavaScript after the user has given consent to marketing for example.

标签: javascriptintellij-ideaphpstormwebstorm

解决方案


The text/plain type seems to be hardcoded (special handling by the IDE): even though you can make Language Injection to work (the code will be displayed using JavaScript syntax colors) it still shows up as "Plain Text fragment" when Alt + Enter Quick Fix menu is invoked inside. Plus that "extra line at the end" and "no indenting" issues...

Therefore try another type, e.g. text/script (Language Injection rule works just fine here).

If you have concerns that some browsers may actually execute it (because of the script part after the text/) then try another custom type, e.g. text/script-template, text/text etc.


推荐阅读