Doua Beri
Doua Beri

Reputation: 10949

Closure Compiler replaces < with \x3c

I want to integrate the closure compiler into a large java application.

I have a problem. The closure compiler replaces the < with \x3c and > with \x3e

The script I'm trying to compile is

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

and closure compiler returns

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

However when I test the closure compiler on the official demo website: https://closure-compiler.appspot.com/home the < and > characters are not changed.

Is there an option to disable that?

Here is my java code:

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());



    }

}

Thanks

Upvotes: 4

Views: 967

Answers (1)

John
John

Reputation: 5468

This option is controlled by CompilerOptions#setTrustedStrings. This defaults to "false" but is set to "true" by the command-line runner. This option is useful when the compiler is used "on the fly" with uncontrolled inputs where it is possible a malicious agent could inject an script tag giving them control of the page. That is injecting user data into javascript. If this is not the case for you setting trusted strings is ok.

Upvotes: 4

Related Questions