使用jQuery解析html时,zombie.js调用堆栈大小超出

时间:2014-07-08 21:10:32

标签: jquery node.js zombie.js

我一直在收到错误消息:" RangeError:超出最大调用堆栈大小"当使用nodejs包" zombie"和jQuery。我已经尽可能地提炼了代码以重新创建错误。

这是一个文件zombie.js,可以使用命令" node zombie.js"运行nodejs:

var Browser = require("zombie");
browser = new Browser();
browser.visit("http://localhost:8000/background.html", function(error) {
    console.log("error: " + error);
});

这里是background.html:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
  var source = "<html><body>Hello, world!</body></html>";
  source = $(source);
</script>

错误发生在以下行:

source = $(source);

可能导致这种情况的原因是什么?我该如何解决?

编辑: 正如P.scheit建议的那样,我尝试在启用调试的情况下运行,并使用几行输出来显示程序中的位置。这是输出:

Zombie: Opened window http://localhost:8000/background.html 
Zombie: GET http://localhost:8000/background.html => 200
Zombie: Loaded document http://localhost:8000/background.html
Zombie: GET http://localhost:8000/jquery.min.js => 200
Before the line: var source = "<html><body>Hello, world!</body></html>";
After the line: var source = "<html><body>Hello, world!</body></html>";
Before the line: source = $(source);
Maximum call stack size exceeded undefined
error: RangeError: Maximum call stack size exceeded
Zombie: Event loop is empty

编辑2:

我使用--trace选项运行节点,看起来这是反复发生的事情,直到调用堆栈的大小太大:

39:  HTML5Parser.phases.base.processEndTag+23(this=0x308bc44a629 <an Object>, 0x308bc456959 <String[4]: html>) {
40:   HTML5Parser.phases.inBody.endTagHtml+23(this=0x308bc44a629 <an Object>, 0x308bc456959 <String[4]: html>) {
41:    HTML5Parser.phases.inBody.endTagBody+23(this=0x308bc44a629 <an Object>, 0x308bc456959 <String[4]: html>) {
42:     HTML5Parser.phases.base.inScope+27(this=0x308bc44a629 <an Object>, 0xf26eb08fd29 <String[4]: body>, 0x15b97b404121 <undefined>) {
43:      +27(this=0x308bc43eb71 <JS Object>, 0x15b97b404121 <undefined>) {
44:       toUpperCase+38(this=0xf26eb08fc21 <String[3]: div>) {
44:       } -> 0x308bc4573c9 <String[3]: DIV>
44:       +20(this=0x308bc43eb71 <JS Object>, 0x15b97b404121 <undefined>) {
45:        toUpperCase+38(this=0xf26eb08fc21 <String[3]: div>) {
45:        } -> 0x308bc4573e9 <String[3]: DIV>
44:       } -> 0x308bc4573e9 <String[3]: DIV>
43:      } -> 0x308bc4573c9 <String[3]: DIV>
43:      +27(this=0x308bc43eb71 <JS Object>, 0x15b97b404121 <undefined>) {
44:       toUpperCase+38(this=0xf26eb08fc21 <String[3]: div>) {
44:       } -> 0x308bc457409 <String[3]: DIV>
44:       +20(this=0x308bc43eb71 <JS Object>, 0x15b97b404121 <undefined>) {
45:        toUpperCase+38(this=0xf26eb08fc21 <String[3]: div>) {
45:        } -> 0x308bc457429 <String[3]: DIV>
44:       } -> 0x308bc457429 <String[3]: DIV>
43:      } -> 0x308bc457409 <String[3]: DIV>
43:      toLowerCase+38(this=0x308bc457409 <String[3]: DIV>) {
43:      } -> 0x308bc457449 <String[3]: div>
43:      +27(this=0x308bc43eb71 <JS Object>, 0x15b97b404121 <undefined>) {
44:       toUpperCase+38(this=0xf26eb08fc21 <String[3]: div>) {
44:       } -> 0x308bc457469 <String[3]: DIV>
44:       +20(this=0x308bc43eb71 <JS Object>, 0x15b97b404121 <undefined>) {
45:        toUpperCase+38(this=0xf26eb08fc21 <String[3]: div>) {
45:        } -> 0x308bc457489 <String[3]: DIV>
44:       } -> 0x308bc457489 <String[3]: DIV>
43:      } -> 0x308bc457469 <String[3]: DIV>
43:      toLowerCase+38(this=0x308bc457469 <String[3]: DIV>) {
43:      } -> 0x308bc4574a9 <String[3]: div>
43:      indexOf+51(this=0x308bc9a79a1 <JS Array[17]>, 0x308bc4574a9 <String[3]: div>, 0x15b97b404121 <undefined>) {
44:       UseSparseVariant+38(this=0x15b97b404121 <undefined>, 0x308bc9a79a1 <JS Array[17]>, 17, 0x15b97b404141 <true>) {
44:       } -> 0x15b97b404161 <false>
43:      } -> -1
42:     } -> 0x15b97b404161 <false>
42:     Parser.parse_error+23(this=0x308bc43f769 <an HTML5Parser>, 0xa03be62aab9 <String[18]: unexpected-end-tag>, 0x308bc4574c9 <an Object>) {
42:     } -> 0x15b97b404121 <undefined>
41:    } -> 0x15b97b404121 <undefined>
41:    HTML5Parser.phases.base.processEndTag+23(this=0x308bc44a629 <an Object>, 0x308bc456959 <String[4]: html>) {
42:     HTML5Parser.phases.inBody.endTagHtml+23(this=0x308bc44a629 <an Object>, 0x308bc456959 <String[4]: html>) {
43:      HTML5Parser.phases.inBody.endTagBody+23(this=0x308bc44a629 <an Object>, 0x308bc456959 <String[4]: html>) {
44:       HTML5Parser.phases.base.inScope+27(this=0x308bc44a629 <an Object>, 0xf26eb08fd29 <String[4]: body>, 0x15b97b404121 <undefined>) {

正如您所看到的,这是一个循环,在调用堆栈中以更深的方式结束。这会重复数千次,直到调用堆栈大小太大。

0 个答案:

没有答案