-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.js
89 lines (89 loc) · 3.1 KB
/
parser.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
var Parser = {
stack: [],
position: 0,
wf: true,
current: '',
posMark: '\u25AE',
tokenize: function(str) {
return str.split('<');
},
/**
* The parse function does not do a true XML well-formedness check.
* In fact, it only does an extremely dumb, though fast, check that
* tags are properly nested. Takes a string containing the XML doc.
*/
parse: function(doc) {
this.stack = [];
this.position = 0;
this.wf = true;
var stream = this.tokenize(doc);
for (var i = 0; i < stream.length; i++) {
var token = stream[i];
this.position += 4;
var len = token.length;
if (token.indexOf('>') > 0) {
//get tag internals
token = token.substring(0, token.indexOf('>')).trim();
//ignore PIs and comments
if (token.charAt(0) == '?' || token.charAt(0) == '!') {
continue;
}
//if it's a close tag, pop the element off the stack
if (token.charAt(0) == '/') {
token = token.substring(1).trim();
if (this.stack[this.stack.length - 1] == token) {
this.stack.pop();
this.current = this.stack[this.stack.length - 1];
} else {
this.wf = false;
break;
}
continue;
}
//if it's a self-closing tag, ignore it
if (token.charAt(token.length - 1) == '/') {
continue;
}
//if it's an open tag, put the name on the stack
if (token.indexOf(' ') > 0) {
this.stack.push(token.substring(0, token.indexOf(' ')));
} else {
this.stack.push(token);
}
this.current = this.stack[this.stack.length - 1];
} else {
if (token.length > 0) {
token = token.trim();
if (token.indexOf(' ') >= 0) {
token = token.substring(0, token.indexOf(' '));
this.current = token;
} else if (token.length > 0) {
this.current = token;
}
}
}
this.position += len;
}
if (this.stack.length == 0 && this.wf == true) {
this.wf = true;
}
return this.wf;
},
// Parse up to the point in the doc string given by the location
parseToLocation: function(doc, location) {
var state = this.parse(doc.substring(0, location));
return this.current;
},
// Parse the contents of an element, given by the id parameter
parseElementContents: function(id) {
return this.parse(jQuery(id).text());
},
/*
* Parse the contents of an element, given by the id parameter up to the first
* occurrence of the character given by the posMark member variable.
*/
parseElementContentsToLocation: function(id) {
var doc = jQuery(id).text();
return this.parseToLocation(doc, doc.indexOf(this.posMark));
}
};