Closure Compiler将<替换为\x3c。

4

我希望将闭包编译器集成到一个大型的Java应用程序中。

我遇到了一个问题。闭包编译器将<替换为\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());



    }

}

谢谢


输出字符集?https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/CompilerOptions.java#L1674-L1679 - Chad Killingsworth
@ChadKillingsworth 不是。我使用了StandardCharsets.UTF_8,结果一样。 - Doua Beri
这些表示相同字符,为什么会有问题? - John
1个回答

4

这个选项由CompilerOptions#setTrustedStrings控制。默认值为“false”,但由命令行运行器设置为“true”。当编译器与可能存在恶意代理注入脚本标记控制页面的未受控输入实时使用时,此选项很有用。也就是将用户数据注入到JavaScript中。如果您不需要考虑这种情况,则可以设置信任字符串。


这个选项能否使用closure-compiler-npm设置,或者您能否分享任何代码片段来强制将其设置为true?当编译器用此替换<!-- ko .. -->时,我遇到了问题,因为KnockoutJS不再将HTML注释识别为绑定指令。 - ladar
你能为此提交一个错误报告吗?我认为这可能是与JS版本选项有关的问题。 - John

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接