【问题标题】:Closure Compiler replaces < with \x3c闭包编译器将 < 替换为 \x3c
【发布时间】:2016-11-08 14:50:41
【问题描述】:

我想将闭包编译器集成到一个大型 java 应用程序中。

我有问题。闭包编译器将&lt; 替换为\x3c,将&gt; 替换为\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());



    }

}

谢谢

【问题讨论】:

标签: javascript java google-closure-compiler


【解决方案1】:

此选项由CompilerOptions#setTrustedStrings 控制。这默认为“false”,但由命令行运行程序设置为“true”。当编译器在“动态”中使用不受控制的输入时,此选项很有用,恶意代理可能会注入一个脚本标签,让他们控制页面。那就是将用户数据注入到 javascript 中。如果不是这种情况,您可以设置受信任的字符串。

【讨论】:

  • 能否使用closure-compiler-npm 设置此选项,或者您能否分享任何代码sn-p 以强制它为真?当编译器用 this 替换 &lt;!-- ko .. --&gt; 时遇到问题,这会破坏 KnockoutJS,因为它不再将 HTML 注释识别为绑定指令
  • 你能为此提交一个错误吗?我认为这是 JS 版本选项的问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多