2016-07-06 65 views
2

我想将闭包编译器集成到一个大的Java应用程序中。Closure编译器替换<用 x3c

我有一个问题。封闭编译器替换<\x3c>\x3e

脚本我试图编译是

$('#something').append($('<div></div>').text('hi')); 

和关闭编译器返回

$("#something").append($("\x3cdiv\x3e\x3c/div\x3e").text("hi")); 

但是当我测试的编译器关闭在官方演示网站上:https://closure-compiler.appspot.com/home <和>字符不变。

是否有一个选项来禁用?

这里是我的Java代码:

import java.util.ArrayList; 
import java.util.List; 
import java.util.logging.Level; 

import com.google.javascript.jscomp.CheckLevel; 
import com.google.javascript.jscomp.CompilationLevel; 
import com.google.javascript.jscomp.Compiler; 
import com.google.javascript.jscomp.CompilerOptions; 
import com.google.javascript.jscomp.PropertyRenamingPolicy; 
import com.google.javascript.jscomp.SourceFile; 
import com.google.javascript.jscomp.VariableRenamingPolicy; 
import com.google.javascript.jscomp.WarningLevel; 

public class ClosureCompiler { 

    public static void main(String args[]) { 

     String code = "$('#something').append($('<div></div>').text('hi'));"; 
     String outputFilename = "a.txt"; 

     Compiler.setLoggingLevel(Level.OFF); 
     Compiler compiler = new Compiler(); 

     CompilerOptions compilerOptions = new CompilerOptions(); 
     compilerOptions.checkGlobalThisLevel = CheckLevel.OFF; 
     compilerOptions.closurePass = false; 
     compilerOptions.coalesceVariableNames = true; 
     compilerOptions.collapseVariableDeclarations = true; 
     compilerOptions.convertToDottedProperties = true; 
     compilerOptions.deadAssignmentElimination = true; 
     compilerOptions.flowSensitiveInlineVariables = true; 
     compilerOptions.foldConstants = true; 
     compilerOptions.labelRenaming = true; 
     compilerOptions.removeDeadCode = true; 
     compilerOptions.optimizeArgumentsArray = true; 

     compilerOptions.setAssumeClosuresOnlyCaptureReferences(false); 
     compilerOptions.setInlineFunctions(CompilerOptions.Reach.LOCAL_ONLY); 
     compilerOptions.setInlineVariables(CompilerOptions.Reach.LOCAL_ONLY); 
     compilerOptions.setRenamingPolicy(VariableRenamingPolicy.LOCAL, PropertyRenamingPolicy.OFF); 
     compilerOptions.setRemoveUnusedVariables(CompilerOptions.Reach.LOCAL_ONLY); 

     System.out.println(compilerOptions.convertToDottedProperties); 

     CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(compilerOptions); 

     WarningLevel.DEFAULT.setOptionsForWarningLevel(compilerOptions); 

     List<SourceFile> primaryJavascriptFiles = new ArrayList<SourceFile>(); 
     primaryJavascriptFiles.add(SourceFile.fromCode(outputFilename, code)); 

     compiler.compile(new ArrayList<SourceFile>(), primaryJavascriptFiles, compilerOptions); 

     System.out.println(compiler.toSource()); 



    } 

} 

感谢

+0

输出字符集? https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/CompilerOptions.java#L1674-L1679 –

+0

@ChadKillingsworth nope。我把StandardCharsets.UTF_8和它相同 –

+0

这些代表相同的字符,为什么它是一个问题? – John

回答

2

此选项是CompilerOptions#setTrustedStrings控制。默认为“false”,但被命令行运行器设置为“true”。当编译器在不受控制的输入中“随时”使用时,该选项非常有用,因为恶意代理可能会注入一个脚本标记,使他们能够控制页面。这是注入用户数据到JavaScript。如果您不是这样设置可信的字符串是可以的。

+0

这个选项可以使用closure-compiler-npm来设置,还是可以分享任何代码片段来强制它为真?编译器正在替换'''<! - ko .. - >''',因为这会破坏KnockoutJS,因为它不再将HTML注释识别为绑定指令 – ladar