summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/scripting-1/the-script-element/execution-timing/testlib/testlib.js
blob: a6fd39426b9f9b26b6a8ec979117e9b010424659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Utility functions for script scheduler test
*/
(function(){ /* namespace hiding local variables like arOrderOfAllEvents from global scope */
    window.testlib = {};
    window.eventOrder = [];
    var arNumberOfScriptsParsedPerEvent=[];
    window.log = function (str){
        eventOrder.push(str);
        arNumberOfScriptsParsedPerEvent.push(document.getElementsByTagName('script').length);
    }

    window.testlib.addScript = function(source, attributes, parent, firstInParent,funcPrepare) {
        try{
            parent = parent||document.body;
            var script = document.createElement('script');
            if(funcPrepare) {
                funcPrepare(script);
            }
            if(source)script.appendChild( document.createTextNode(source) );
            for( var name in attributes){
                if(/^on/i.test(name)) {
                    script[name] = attributes[name];
                } else {
                    script.setAttribute(name, attributes[name]);
                }
            }
            if (firstInParent && parent.firstChild) {
                parent.insertBefore(script, parent.firstChild);
            } else {
                parent.appendChild(script);
            }
        } catch(e) {
            log('ERROR when adding script to DOM!');
            alert(e);
        }
        return script;
    }

    window.testlib.urlParam = function(relativeURL) {
        return location.href.replace( /\d*\.html$/, relativeURL);
    }
})();