summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/node.js/mockdom.js
blob: 68c088aa7779b0ecc874e73a2d550f75cb9dbf3d (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
XMLHttpRequest = function() { 
    return { 
    'open': function() {  },
    'send': function() {  }
    }
};

navigator = {
  'appName': 'mockdom',
  'userAgent': 'mockdom',
  'appVersion': '0.1',
  'language': 'en',
  'userLanguage': 'en'
}  

element = function(type) {
    type = type || "";

    return {
        'childNodes': [],
        'className': '',
        'tagName': type.toUpperCase(),
        'style': {},
        'setAttribute': function(attr, value) {
            this[attr] = value;
        },    
        'appendChild': function(element) {
            if (this.childNodes.length) {
                this.childNodes[this.childNodes.length - 1].nextSibling = element;
            }  else {
                this.firstChild = element;
            }    
            element.parentNode = this;
            this.childNodes.push(element);

        }, 
        'removeChild': function(element) {
            var i = this.childNodes.indexOf(element);
            this.childNodes.splice(i, 1);
        },    
        'addEventListener': function() {
        },
        'removeEventListener': function() {
        },
        'getElementsByTagName': function(name, externalList) {
            var uc = name.toUpperCase();
            var list = externalList || [];
            for(var i = 0; i < this.childNodes.length; i++) {
                if (this.childNodes[i].tagName == uc) {
                    list.push(this.childNodes[i]);
                }
                this.childNodes[i].getElementsByTagName(name, list);
            }
            return list;
        },
        'getElementById': function(id) {
            for(var i = 0; i < this.childNodes.length; i++) {
                if (this.childNodes[i].id == id) {
                    return this.childNodes[i];
                } else {
                    var elem = this.childNodes[i].getElementById(id);
                    if (elem) { 
                        return elem
                    }
                }
            }
        }
    }
};

document = element();
document.createElement = function(type) {
     return element(type);
};
document.createTextNode = function(text) {
    var e = element("Text");
    e.innerHTML = text;
}

document.appendChild(element("head"));
document.body = element("body");
document.appendChild(document.body);

window = {
    'addEventListener': function() {
    },
    'getSelection': function() {
        return {
            collapseToStart: function() {}
        }
    },
    document: document,
    navigator: navigator,
    location: {
        href: '#',
        port: '',
        hostname: 'openlayers.org',
        host: 'openlayers.org',
        proto: 'https'
    }    
};
document.location = window.location;

window.Function = Function;