Skip to content

Commit 94010f4

Browse files
committed
Fix #545 (encoding handling, not only wrong for HTTP).
Spotless always provides decoded string to WTP.
1 parent 5acecad commit 94010f4

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

_ext/eclipse-wtp/CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.15.1`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Handling of character encodings which require more than 1 byte. Previously the WTP
8+
decoded input twice, once using the encoding configured by the user, and
9+
once again using the default platform character set ([#545](https://github.com/diffplug/spotless/issues/545)).
610

711
## [3.15.2] - 2020-03-04
812
### Fixed

_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/ContentTypeManager.java

+42
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.io.Reader;
21+
import java.nio.charset.Charset;
2122
import java.util.Arrays;
2223
import java.util.HashMap;
2324
import java.util.Map;
@@ -49,6 +50,7 @@
4950
class ContentTypeManager extends NoContentTypeSpecificHandling {
5051
private final Map<String, IContentType> id2Object;
5152
private final IContentType processorStepType;
53+
private final IContentDescription processorStepDescription;
5254

5355
/**
5456
* Content type manager as required for cleanup steps.
@@ -66,6 +68,7 @@ class ContentTypeManager extends NoContentTypeSpecificHandling {
6668
if (null == processorStepType) {
6769
throw new IllegalArgumentException("The manager does not support content type " + formatterContentTypeID);
6870
}
71+
processorStepDescription = new StringDescription(processorStepType);
6972
}
7073

7174
@Override
@@ -83,6 +86,45 @@ public IContentType findContentTypeFor(InputStream contents, String fileName) th
8386
return processorStepType;
8487
}
8588

89+
@Override
90+
public IContentDescription getDescriptionFor(InputStream contents, String fileName, QualifiedName[] options) throws IOException {
91+
return processorStepDescription;
92+
}
93+
94+
private static class StringDescription implements IContentDescription {
95+
96+
private final IContentType type;
97+
98+
public StringDescription(IContentType type) {
99+
this.type = type;
100+
}
101+
102+
@Override
103+
public boolean isRequested(QualifiedName key) {
104+
return false; //Don't use set Property
105+
}
106+
107+
@Override
108+
public String getCharset() {
109+
return Charset.defaultCharset().name(); //Spotless operates on an decoded string, meaning the input has always the "internal" encoding
110+
}
111+
112+
@Override
113+
public IContentType getContentType() {
114+
return type;
115+
}
116+
117+
@Override
118+
public Object getProperty(QualifiedName key) {
119+
return null; //Assume that the property map is empty
120+
}
121+
122+
@Override
123+
public void setProperty(QualifiedName key, Object value) {
124+
throw new IllegalArgumentException("Content description key cannot be set: " + key);
125+
}
126+
}
127+
86128
/**
87129
* The WTP uses the manager only for ID mapping, so most of the methods are not used.
88130
* Actually it has a hand stitched way for transforming the content type ID

_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImplTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,28 @@ public void formatCSS() throws Exception {
8989
testData.expected("css.html"), output);
9090
}
9191

92+
@Test
93+
public void checkNoDoubleEndoding() throws Exception {
94+
String osEncoding = System.getProperty("file.encoding");
95+
//Assure that file.encoding is not used during the clean-up.
96+
System.setProperty("file.encoding", "ISO-8859-1");
97+
//Check that WTP does not try to do UTF-8 conversion again (since done by Spotless framework)
98+
String[] input = testData.input("utf-8.html");
99+
String output = formatter.format(input[0]);
100+
System.setProperty("file.encoding", osEncoding);
101+
assertEquals("Unexpected formatting of UTF-8", testData.expected("utf-8.html"), output);
102+
}
103+
104+
@Test
105+
public void checkBOMisStripped() throws Exception {
106+
String[] input = testData.input("bom.html");
107+
String[] inputWithoutBom = testData.input("utf-8.html");
108+
//The UTF-8 BOM is interpreted as on UTF-16 character.
109+
assertEquals("BOM input invalid", input[0].length() - 1, inputWithoutBom[0].length());
110+
String output = formatter.format(input[0]);
111+
assertEquals("BOM is not stripped", testData.expected("utf-8.html"), output);
112+
}
113+
92114
@Test(expected = IllegalArgumentException.class)
93115
public void configurationChange() throws Exception {
94116
new EclipseHtmlFormatterStepImpl(new Properties());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<HTML>
3+
<HEAD>
4+
<META charset="UTF-8">
5+
<TITLE>ÄÜ€</TITLE>
6+
</HEAD>
7+
</HTML>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html>
2+
<html><head><meta charset="UTF-8"><title>ÄÜ€</title></head></html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html>
2+
<html><head><meta charset="UTF-8"><title>ÄÜ€</title></head></html>

0 commit comments

Comments
 (0)