Skip to content

Commit 4ae363c

Browse files
committed
Do not format text content of <style> and <script>
`<style>` is usually used for CSS, and `<script>` is usually used for JavaScript (or some other scripting language). Given the user has probably manually formatted this code, we should avoid formatting the text content by default. Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent a82b215 commit 4ae363c

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/settings/XMLFormattingOptions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public class XMLFormattingOptions extends org.eclipse.lemminx.settings.LSPFormat
5454
"screen", //
5555
"synopsis", //
5656
"pre", //
57-
"xd:pre");
57+
"xd:pre", //
58+
"style", //
59+
"script");
5860

5961
private boolean legacy;
6062
private int maxLineWidth;

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/wtp/TestPartitionFormatterXML.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public void testPreserveCDATAFormat2() throws Exception {
164164
formatAndAssertEquals("testfiles/xml/usecdata2.xml", "testfiles/xml/usecdata2-fmt.xml");
165165
}
166166

167+
@Test
168+
public void testPreserveCDATAFormat3() throws Exception {
169+
// <script> is treated specially, because it likely contains formatted source code
170+
formatAndAssertEquals("testfiles/xml/usecdata3.xml", "testfiles/xml/usecdata3-fmt.xml");
171+
}
172+
167173
@Test
168174
@Disabled
169175
public void testSplitAttributesFormat() throws Exception {

org.eclipse.lemminx/src/test/resources/wtp/testfiles/xml/usecdata2-fmt.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script>
1+
<script-code>
22
nospacebetweenmeandcdata<![CDATA[
33
function matchwo(a,b)
44
{
@@ -12,4 +12,4 @@ else
1212
}
1313
}
1414
]]>nospacebetweenmeandcdata
15-
</script>
15+
</script-code>

org.eclipse.lemminx/src/test/resources/wtp/testfiles/xml/usecdata2.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script>
1+
<script-code>
22
nospacebetweenmeandcdata<![CDATA[
33
function matchwo(a,b)
44
{
@@ -12,4 +12,4 @@ else
1212
}
1313
}
1414
]]>nospacebetweenmeandcdata
15-
</script>
15+
</script-code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
nospacebetweenmeandcdata<![CDATA[
3+
function matchwo(a,b)
4+
{
5+
if (a < b && a < 0) then
6+
{
7+
return 1
8+
}
9+
else
10+
{
11+
return 0
12+
}
13+
}
14+
]]>nospacebetweenmeandcdata
15+
</script>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
nospacebetweenmeandcdata<![CDATA[
3+
function matchwo(a,b)
4+
{
5+
if (a < b && a < 0) then
6+
{
7+
return 1
8+
}
9+
else
10+
{
11+
return 0
12+
}
13+
}
14+
]]>nospacebetweenmeandcdata
15+
</script>

0 commit comments

Comments
 (0)