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

Format script content inside HTML tag with colon #7916

Merged
merged 2 commits into from Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions changelog_unreleased/html/pr-7916.md
@@ -0,0 +1,34 @@
#### Format `script` and `style` content inside HTML tag with colon ([#7916](https://github.com/prettier/prettier/pull/7916) by [@fisker](https://github.com/fisker))

<!-- prettier-ignore -->
```html
<!--Input-->
<with:colon>
<script>function foo(){ return 1}</script>
<style>a {color: #f00}</style>
</with:colon>

<!--Prettier stable-->
<with:colon>
<script>
function foo(){ return 1}
</script>
<style>
a {color: #f00}
</style>
</with:colon>

<!--Prettier master-->
<with:colon>
<script>
function foo() {
return 1;
}
</script>
<style>
a {
color: #f00;
}
</style>
</with:colon>
```
4 changes: 3 additions & 1 deletion src/language-html/utils.js
Expand Up @@ -135,7 +135,9 @@ function isScriptLikeTag(node) {
node.type === "element" &&
(node.fullName === "script" ||
node.fullName === "style" ||
node.fullName === "svg:style")
node.fullName === "svg:style" ||
(isUnknownNamespace(node) &&
(node.name === "script" || node.fullName === "style")))
);
}

Expand Down
60 changes: 59 additions & 1 deletion tests/html_basics/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -623,6 +623,21 @@ e-wrap </html:textarea>
</script>
</with:colon>

<!-- script like -->
<with:colon>
<style>.a{color:#f00}</style>
<SCRIPT>
const func = function() { console.log('Hello, there');}
</SCRIPT>
<STYLE>.A{COLOR:#F00}</STYLE>
<html:script>const func = function() { console.log('Hello, there');}</html:script>
<html:style>.a{color:#f00}</html:style>
<svg><style>.a{color:#f00}</style></svg>
<svg><style>.a{color:#f00}</style></svg>
</with:colon>
<html:script>const func = function() { console.log('Hello, there');}</html:script>
<html:style>.a{color:#f00}</html:style>

=====================================output=====================================
<!-- unknown tag with colon -->
<div>
Expand Down Expand Up @@ -832,9 +847,52 @@ e-wrap </textarea
</h1>
</div>
<script>
const func = function() { console.log('Hello, there');}
const func = function () {
console.log("Hello, there");
};
</script>
</with:colon>

<!-- script like -->
<with:colon>
<style>
.a{color:#f00}
</style>
<script>
const func = function () {
console.log("Hello, there");
};
</script>
<style>
.A{COLOR:#F00}
</style>
<html:script
>const func = function() { console.log('Hello, there');}</html:script
>
<html:style
>.a{color:#f00}</html:style
>
<svg>
<style>
.a {
color: #f00;
}
</style>
</svg>
<svg>
<style>
.a {
color: #f00;
}
</style>
</svg>
</with:colon>
<html:script
>const func = function() { console.log('Hello, there');}</html:script
>
<html:style
>.a{color:#f00}</html:style
>

================================================================================
`;
15 changes: 15 additions & 0 deletions tests/html_basics/with-colon.html
Expand Up @@ -110,3 +110,18 @@
const func = function() { console.log('Hello, there');}
</script>
</with:colon>

<!-- script like -->
<with:colon>
<style>.a{color:#f00}</style>
<SCRIPT>
const func = function() { console.log('Hello, there');}
</SCRIPT>
<STYLE>.A{COLOR:#F00}</STYLE>
<html:script>const func = function() { console.log('Hello, there');}</html:script>
<html:style>.a{color:#f00}</html:style>
<svg><style>.a{color:#f00}</style></svg>
<svg><style>.a{color:#f00}</style></svg>
</with:colon>
<html:script>const func = function() { console.log('Hello, there');}</html:script>
<html:style>.a{color:#f00}</html:style>