Rhino E4X 不允许在 {表达式} 中使用 xml 文字
我使用的是rhino1_7R2。我发现以下程序会导致语法错误(非法形成的XML语法):
function b(node) {
return <span>{node}{node}</span>;
}
a=<span>foo {b(<div>bar</div>)} baz</span>;
alert(a);
但它有效:
function b(node) {
return <span>{node}{node}</span>;
}
var bar=<div>bar</div>;
a=<span>foo {b(bar)} baz</span>;
alert(a);
Rhino似乎不允许在{表达式}中使用literal xml。 首先我认为 ECMAScript 规范禁止这样做。 但在 Firefox(3.6.23) 中允许这种语法。所以这可能不是因为 ECMA 规范。
这是在 Firefox 中运行的测试页(它可以工作):
<script>
function b(node) {
return <span>{node}{node}</span>;
}
a=<span>foo {b(<div>bar</div>)} baz</span>;
alert(a);
</script>
有人能看出这是因为 Rhino 的 bug 或 Rhino 或 Ecmascript 的规范吗?
我将附上 Java/Rhino 的测试代码。问候。
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
public class Main {
public static void main(String[] args) {
Context c=Context.enter();
ScriptableObject scope = c.initStandardObjects();
String source_OK =
"function b(node) { " +
" return <span>{node}{node}</span>;" +
"}" +
"var bar=<div>bar</div>;" +
"a=<span>foo {b(bar)} baz</span>; ";
Object a=c.evaluateString(scope, source_OK, "TEST", 1, null);
System.out.println(a);
String source_NG =
"function b(node) { " +
" return <span>{node}{node}</span>;" +
"}" +
"a=<span>foo {b(<div>bar</div>)} baz</span>; ";
a=c.evaluateString(scope, source_NG, "TEST", 1, null); //Error!
System.out.println(a);
Context.exit();
}
}
I am using rhino1_7R2. I have found that the following program causes syntax error(illegally formed XML syntax):
function b(node) {
return <span>{node}{node}</span>;
}
a=<span>foo {b(<div>bar</div>)} baz</span>;
alert(a);
but it works:
function b(node) {
return <span>{node}{node}</span>;
}
var bar=<div>bar</div>;
a=<span>foo {b(bar)} baz</span>;
alert(a);
It seems to that Rhino does not allow litral xml in { expression }.
First I thought that the ECMAScript specification prohibits this.
But in Firefox(3.6.23) allowed this syntax. So it may be not because ECMA specification.
This is test page run in Firefox(it works):
<script>
function b(node) {
return <span>{node}{node}</span>;
}
a=<span>foo {b(<div>bar</div>)} baz</span>;
alert(a);
</script>
Can someone tell it is because Rhino's bug or specifications of either Rhino or Ecmascript?
I will attach test code in Java/Rhino. Regards.
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
public class Main {
public static void main(String[] args) {
Context c=Context.enter();
ScriptableObject scope = c.initStandardObjects();
String source_OK =
"function b(node) { " +
" return <span>{node}{node}</span>;" +
"}" +
"var bar=<div>bar</div>;" +
"a=<span>foo {b(bar)} baz</span>; ";
Object a=c.evaluateString(scope, source_OK, "TEST", 1, null);
System.out.println(a);
String source_NG =
"function b(node) { " +
" return <span>{node}{node}</span>;" +
"}" +
"a=<span>foo {b(<div>bar</div>)} baz</span>; ";
a=c.evaluateString(scope, source_NG, "TEST", 1, null); //Error!
System.out.println(a);
Context.exit();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我自己发现这是Rhino的一个bug:
https://bugzilla。 mozilla.org/show_bug.cgi?id=323890
我还发送了针对此错误的补丁。
Sorry, I have found that it is a bug of Rhino by myself:
https://bugzilla.mozilla.org/show_bug.cgi?id=323890
I also sended a patch for this bug.