javascript - Closure Compiler 将 < 替换为\x3c

标签 javascript java google-closure-compiler

我想将闭包编译器集成到一个大型 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());



    }

}

谢谢

最佳答案

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

关于javascript - Closure Compiler 将 < 替换为\x3c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38225928/

相关文章:

java - 从映射对象中获取键和值并将它们发送到不同的服务器

java - 使用 tomcat6 安装 Web 应用程序时出现问题

java - GWT 中的多个 javascript 异常处理

javascript - 在输入文本字段时设置单选按钮值

java - 回文法及比较

javascript - Annotate Singleton objects in JavaScript for the Google Closure Compiler, 或 "dangerous use of the global this object"警告

javascript - 闭包编译器不重命名属性和方法(高级编译)

javascript - 使用闭包编译器编译两个js文件

javascript - 创建新图像时,image.width 返回 0

javascript - 将 id 与点击元素 id 或动态内容的最简洁父 id 相匹配