简体   繁体   English

Closure Compiler修改字符串中的尾括号

[英]Closure Compiler modifying end bracket in string

I try to run the following Javascript code through closure compiler: 我尝试通过闭包编译器运行以下Javascript代码:

var firstRun = eval("<?--#exec cmd_argument='command'-->");

And it comes out as: 结果如下:

var a=eval(" <?--#exec cmd_argument='command'--\x3e")

Since it is an SSI command I need i to not convert > to \\x3e . 由于这是一个SSI命令,我需要我不要将>转换为\\x3e I have tried using --charset=utf-8 and US-ascii as input to Closure Compiler. 我尝试使用--charset=utf-8US-ascii作为Closure Compiler的输入。 I have also tried using \> in the string. 我也尝试在字符串中使用\> Whatever I do it outputs > as \\x3e , which seems very odd, since it does not do the same to <. 无论我做什么,它都将>输出为\\x3e ,这似乎很奇怪,因为它对<并没有作用。

The input command to closure compiler I use is: 我使用的闭包编译器的输入命令是:

java -jar compiler.jar --js file.js --js_output_file newfile.js

Even if there is a compiler option to avoid that happening, my view is that this is not a good practice, as you are relying on how precisely the compiler will output something (ie your SSI string in this case) that can have many different representations (as you can see the original string is changed). 即使有编译器选项可以避免这种情况发生,但我认为这不是一个好习惯,因为您依赖于编译器将输出具有多种不同表示形式的某些内容(在这种情况下,即SSI字符串)的精确度(如您所见,原始字符串已更改)。

The compiler does this to avoid the string --> that will end an HTML comment in case it's embedded in HTML code. 编译器这样做是为了避免字符串-->在嵌入HTML代码的情况下结束HTML注释。 It used to be common practice to wrap JavaScript in an HTML comment to avoid error in browsers that didn't support JavaScript. 过去通常将JavaScript封装在HTML注释中,以避免在不支持JavaScript的浏览器中出错。


Possible solution: can you separately include your SSI string so that it will not go through the compilation process? 可能的解决方案:是否可以单独包括SSI字符串,以使它不会通过编译过程? That way you can guarantee it's not going to be affected by the compiler. 这样,您可以保证它不会受到编译器的影响。

This could be done either by including the SSI string in another uncompiled JS file or by prepending the uncompiled SSI string in the compiled JS output. 可以通过在另一个未编译的 JS文件中包含SSI字符串,或在已编译的 JS输出中添加未编译的 SSI字符串来完成此操作。 Something like this: 像这样:

var MY_GLOBAL_SSI_STRING = "<?--#exec cmd_argument='command'-->";
// compiled code follows...

Then you can refer to MY_GLOBAL_SSI_STRING from your (to be compiled) code like this: 然后,您可以像这样从您的(待编译)代码中引用MY_GLOBAL_SSI_STRING:

var firstRun = eval(goog.global['MY_GLOBAL_SSI_STRING']);

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM