-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtesting.js
116 lines (104 loc) · 3.2 KB
/
testing.js
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
105
106
107
108
109
110
111
112
113
114
115
116
$(function(){
/**
# * THIS FILE CONTAINS CODE USED FOR TESTING *
# Feel free to use it as a basis for your own work
# but only if you know what you're doing
**/
// Override plugin in documentation page
// so we can show every call in Firebug
$.xml2json_original = $.xml2json;
$.xml2json = function(xml, extended){
var json = $.xml2json_original(xml, extended);
if(window.console) console.log(['XML:',xml,'JSON:',json]);
return json;
};
// Set of tests showing results in simple/extended mode
$('.structure-test').each(function(){
var extended = $(this).hasClass('extended');
$('a', this).each(function(){
var ref = this.href;
$(this)
.after(
$('<div class="test-result"></div>')
.hide()
)
.after(
$('<a href="javascript:;">RUN »</a>')
.click(function(){
var result = $(this).next();
$.get(ref, function(xml){
var json = $.xml2json(xml, extended);
var text = function(node){
return node.text || (typeof node=='string'?node:'')
.replace(/"/g,""").replace(/'/g,"'");
};
var walk = function(tree){
if(!tree) return '';
var s = '';
//console.log([tree, typeof tree]);
if(typeof tree=='string'){
return '';
}
else if(tree.length && !tree.text){
for(var i=0;i<tree.length;i++)
s += '<li>['+i+']'+walk(tree[i])+'</li>';
}
else{
for(node in tree){
if(!extended && node=='text'){}
else if(node.match(/^\d+$/gi)){}
else{
var txt = text(tree[node]);
s += '<li>'+node+'';
if(txt){
s +=' [<a href="javascript:;" title="'+txt+'"'
+ ' onclick="window.prompt(\''+node+':\',this.title)"'
+ '>value</a>]';
};
s += ''+walk(tree[node])+'';
s += '</li>';
};// skip byte array of string objects
};
};
return s? (tree.length && !tree.text?' [length: '+tree.length+']':'') + '<ul>'+s+'</ul>' : '';
};
result.html(walk(json)).show('slow');
});//$.get
})//click
)//after
.after(' | TEST: ')
});//each a
});//each table
});//dom ready
//==========
// TEST 1
//==========
$(function(){
$('#butn-1').click(function(){
$('#result-1').html('Loading...');
$.get("data/simple.xml", function(xml){
var json = $.xml2json(xml);
if(window.console) console.log(['test-1 json:',json]);
$('#result-1').html('');
$.each(json['channel']['item'], function(i, obj){
$('#result-1').append('<p>Title: '+obj.title+'<br/>Link: '+obj.link+'<br/>Description: '+obj.description+'<br/></p>');
});
});
});
});
//==========
// TEST 2
//==========
$(function(){
$('#butn-2').click(function(){
$('#result-2').html('Loading...');
$.get("data/rss.xml", function(xml){
var json = $.xml2json(xml);
if(window.console) console.log(['test-2 json:',json]);
$('#result-2').html('');
$.each(json['channel']['item'], function(i, obj){
$('#result-2').append('<p>Title: '+obj.title+'<br/>Link: '+obj.link+'<br/>Description: '+obj.description+'<br/></p>');
});
});
});
});