Skip to content

Commit fe4c24a

Browse files
committed
fix type detection of embedded content
1 parent a8da610 commit fe4c24a

5 files changed

Lines changed: 33 additions & 34 deletions

File tree

packages/prettyhtml-formatter/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,12 @@ function prettierEmbeddedContent(node, level, indent, prettierOpts) {
442442
const isStyleTag = isElement(node, 'style')
443443
const isScriptTag = isElement(node, 'script')
444444
let content = toString(node)
445+
let type = node.properties.type ? `type="${node.properties.type}"` : ''
445446

446447
if (isScriptTag) {
447-
content = '<script>' + content + '</script>'
448+
content = `<script ${type}>${content}</script>`
448449
} else if (isStyleTag) {
449-
content = '<style>' + content + '</style>'
450+
content = `<style ${type}>${content}</style>`
450451
}
451452

452453
let formattedText = prettier.format(
@@ -458,12 +459,12 @@ function prettierEmbeddedContent(node, level, indent, prettierOpts) {
458459

459460
if (isScriptTag) {
460461
formattedText = formattedText
461-
.replace(/\n*<\/script\s*>/g, '')
462-
.replace(/<script\s*>\n*/g, '')
462+
.replace(/\n*<\/script\s*>/, '')
463+
.replace(/<script.*>\n*/, '')
463464
} else if (isStyleTag) {
464465
formattedText = formattedText
465-
.replace(/\n*<\/style\s*>/g, '')
466-
.replace(/<style\s*>\n*/g, '')
466+
.replace(/\n*<\/style\s*>/, '')
467+
.replace(/<style.*>\n*/, '')
467468
}
468469

469470
node.children = [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<style type="scss">
2+
$font-stack: Helvetica, sans-serif;
3+
$primary-color: #333;
4+
5+
body {
6+
font: 100% $font-stack;
7+
color: $primary-color;
8+
}
9+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<style type="scss">
2+
$font-stack: Helvetica, sans-serif;
3+
$primary-color: #333;
4+
5+
body {
6+
font: 100% $font-stack;
7+
color: $primary-color;
8+
}
9+
</style>

packages/prettyhtml-formatter/test/fixtures/style/input.html

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
<style type="text/x-scss">
22
</style>
3-
<style type="text/x-scss">
4-
$font-stack: Helvetica, sans-serif;
5-
$primary-color: #333;
6-
body {
7-
font: 100% $font-stack;
8-
color: $primary-color;
9-
}
10-
</style>
11-
<style lang="scss">
12-
$font-stack: Helvetica, sans-serif;
13-
$primary-color: #333;
14-
body {
15-
font: 100% $font-stack;
16-
color: $primary-color;
17-
}
3+
<style>
4+
body {
5+
font: 100% Helvetica, sans-serif;
6+
color: #333;
7+
}
188
</style>
199

2010
<style>

packages/prettyhtml-formatter/test/fixtures/style/output.html

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
<style type="text/x-scss"></style>
2-
<style type="text/x-scss">
3-
$font-stack: Helvetica, sans-serif;
4-
$primary-color: #333;
2+
<style>
53
body {
6-
font: 100% $font-stack;
7-
color: $primary-color;
8-
}
9-
</style>
10-
<style lang="scss">
11-
$font-stack: Helvetica, sans-serif;
12-
$primary-color: #333;
13-
body {
14-
font: 100% $font-stack;
15-
color: $primary-color;
4+
font: 100% Helvetica, sans-serif;
5+
color: #333;
166
}
177
</style>
188

0 commit comments

Comments
 (0)