exectest.html 3.93 KB
Newer Older
1 2 3 4 5 6
<html>
<body>
Script execution test
<div id="b1"></div>
<div id="b2"></div>
<div id="b3"></div>
7
<script id="emptyscript"></script>
8
<script id="put_text_script">2+2</script>
9 10 11 12 13 14
<script>
function ok(b, msg) {
    return external.ok(b, msg);
}

var executed = false;
15
var extern_cnt = 0;
16
var exec_cnt;
17

18 19 20
function testWriteScript() {
    var len = document.getElementsByTagName('div').length;
    ok(len === 3, "init length = " + len);
21

22 23 24 25 26
    document.write("<script>"
        + "var len = document.getElementsByTagName('div').length;"
        + "ok(len === 3, 'unexpected length ' + len);"
        + "executed = true;"
        + "<" + "/script>");
27

28
    len = document.getElementsByTagName('script').length;
29
    ok(len === 4, "script col length = " + len);
30 31 32 33 34 35 36 37 38 39 40
    ok(executed, "written script not executed");
}

function testPutSrc() {
    var elem = document.getElementById("emptyscript");

    ok(extern_cnt === 0, "extern_cnt = " + extern_cnt + " expected 0");
    elem.src = "externscr.js";
    ok(extern_cnt === 0, "extern_cnt = " + extern_cnt + " expected 0");
}

41 42 43 44 45 46 47 48 49 50 51 52
function testPutText() {
    var elem = document.getElementById("put_text_script");

    exec_cnt = 0;
    elem.text = "exec_cnt++;";
    ok(exec_cnt === 1, "put_text_script exec_cnt " + exec_cnt);

    exec_cnt = 0;
    elem.text = "exec_cnt++;";
    ok(exec_cnt === 1, "put_text_script exec_cnt " + exec_cnt);
}

53 54 55
try {
    testWriteScript();
    testPutSrc();
56
    testPutText();
57
}catch(e) {
58
    ok(false, "Got an exception " + e.message);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
}

function testPutSrc2() {
    var elem, elem2;

    elem = document.createElement("script");
    elem.src = "externscr.js";
    ok(extern_cnt === 1, "extern_cnt = " + extern_cnt + " expected 1");
    document.body.appendChild(elem);
    ok(extern_cnt === 2, "extern_cnt = " + extern_cnt + " expected 2");

    elem = document.createElement("script");
    document.body.appendChild(elem);
    ok(extern_cnt === 2, "extern_cnt = " + extern_cnt + " expected 2");
    elem.src = "externscr.js";
    ok(extern_cnt === 3, "extern_cnt = " + extern_cnt + " expected 3");

    elem2 = document.createElement("div");
    elem = document.createElement("script");
    elem.src = "externscr.js";
    ok(extern_cnt === 3, "extern_cnt = " + extern_cnt + " expected 3");
    elem2.appendChild(elem);
    document.body.appendChild(elem2);
    ok(extern_cnt === 4, "extern_cnt = " + extern_cnt + " expected 4");
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
function testPutText2() {
    var elem;

    elem = document.createElement("script");
    exec_cnt = 0;
    elem.text = "exec_cnt++;";
    ok(exec_cnt === 0, "script exec_cnt " + exec_cnt);

    document.body.appendChild(elem);
    ok(exec_cnt === 1, "script exec_cnt " + exec_cnt);
    exec_cnt = 0;
    elem.text = "exec_cnt++;";
    ok(exec_cnt === 1, "script exec_cnt " + exec_cnt);

    elem.src = "externscr.js";
    ok(extern_cnt === 5, "extern_cnt = " + extern_cnt + " expected 5");

    exec_cnt = 0;
    ok(extern_cnt === 5, "> extern_cnt = " + extern_cnt + " expected 5");
    elem.text = "exec_cnt++;"; /* forces evaluation, but src will be executed */
    ok(exec_cnt === 0, "script exec_cnt " + exec_cnt);
    ok(extern_cnt === 6, "< extern_cnt = " + extern_cnt + " expected 6");

    elem = document.createElement("script");
    exec_cnt = 0;
    elem.text = "exec_cnt++;";
    ok(exec_cnt === 0, "script exec_cnt " + exec_cnt);
    elem.src = "externscr.js";
    ok(extern_cnt === 6, "extern_cnt = " + extern_cnt + " expected 6");

    document.body.appendChild(elem);
    ok(exec_cnt === 0, "script exec_cnt " + exec_cnt);
    ok(extern_cnt === 7, "extern_cnt = " + extern_cnt + " expected 7");
}

120 121 122 123
document.body.onload = function() {
    ok(extern_cnt === 1, "extern_cnt = " + extern_cnt);
    try {
        testPutSrc2();
124
        testPutText2();
125
    }catch(e) {
126
        ok(false, "got an exception " + e.message);
127 128 129 130 131 132 133
    }

    external.reportSuccess();
}
</script>
<script>
    ok(extern_cnt === 1, "extern_cnt = " + extern_cnt);
134 135 136 137 138 139
</script>
<div id="a1"></div>
<div id="a2"></div>
<div id="b3"></div>
</body>
</html>