From a3895e1b323d9e207fdb4186196619c3c2e1a284 Mon Sep 17 00:00:00 2001 From: Chad Killingsworth Date: Fri, 25 Sep 2015 08:20:13 -0500 Subject: [PATCH] Revert "Revert "Remove UTF-8 BOM from source files"" This reverts commit 0b3c460ff0361694678ed6f0e51dc7eaa2710927. --- src/com/google/javascript/jscomp/SourceFile.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/com/google/javascript/jscomp/SourceFile.java b/src/com/google/javascript/jscomp/SourceFile.java index 600228642a8..8845b4969ae 100644 --- a/src/com/google/javascript/jscomp/SourceFile.java +++ b/src/com/google/javascript/jscomp/SourceFile.java @@ -52,6 +52,7 @@ */ public class SourceFile implements StaticSourceFile, Serializable { private static final long serialVersionUID = 1L; + private static final String UTF8_BOM = String.format("%c", CommandLineRunner.UTF8_BOM_CODE); /** A JavaScript source code provider. The value should * be cached so that the source text stays consistent throughout a single @@ -169,7 +170,15 @@ String getCodeNoCache() { } private void setCode(String sourceCode) { - code = sourceCode; + this.setCode(sourceCode, false); + } + + private void setCode(String sourceCode, boolean removeUtf8Bom) { + if (removeUtf8Bom && sourceCode != null && sourceCode.startsWith(UTF8_BOM)) { + code = sourceCode.substring(UTF8_BOM.length()); + } else { + code = sourceCode; + } } public String getOriginalPath() { @@ -543,7 +552,7 @@ public synchronized String getCode() throws IOException { if (cachedCode == null) { cachedCode = Files.toString(file, this.getCharset()); - super.setCode(cachedCode); + super.setCode(cachedCode, this.getCharset() == StandardCharsets.UTF_8); } return cachedCode; } @@ -636,7 +645,7 @@ public synchronized String getCode() throws IOException { if (cachedCode == null) { cachedCode = Resources.toString(url, this.getCharset()); - super.setCode(cachedCode); + super.setCode(cachedCode, this.getCharset() == StandardCharsets.UTF_8); } return cachedCode; }