-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.indentxml.js
191 lines (173 loc) · 6.18 KB
/
jquery.indentxml.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*!
* jquery.indentxml v0.0.1
* https://github.com/molnarp/jquery.indentxml/
*
* Copyright 2013 Peter Molnar
* Released under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Date: 2013-07-16T16:22Z
*/
(function ( $ ) {
// States:
var
BASE = 0,
IN_ELEMENT = 1,
IN_OPEN_ELEMENT = 2,
IN_SELF_CLOSED_ELEMENT = 3,
IN_CLOSE_ELEMENT = 4,
DOUBLE_QUOTE = 5,
SINGLE_QUOTE = 6,
COMMENT = 7,
PI = 8,
PI_QUESTION_MARK = 9;
/**
* Indents XML code.
*
* @param String input The input code as text.
* @returns The reindented XML code.
*/
$.indentxml = function(/*String*/ input) {
input = $.trim(input);
var formatted = "";
var tab = " ";
var nextLevel = 0, currentLevel = 0, levelStack = 0;
var state = BASE, stack = BASE;
$.each(input.split("\n"), function(index, line) {
// Save previously computed level
currentLevel = nextLevel;
// Parse current line and compute level of next line
var pos = 0;
var c = "";
while (pos < line.length) {
// Get next character, quoted characters are parsed as a whole
c = readChar(line, pos);
pos += c.length;
switch (state) {
case 0: // BASE
if (c === "<") {
state = IN_ELEMENT;
nextLevel += 2; // continuation, open
}
break;
case 1: // IN_ELEMENT
if (c === "/") {
state = IN_CLOSE_ELEMENT;
currentLevel -= 1;
nextLevel -= 1;
}
else if (c === "!") {
state = COMMENT;
}
else if (c === "?") {
state = PI;
}
else {
state = IN_OPEN_ELEMENT;
}
break;
case 2: // IN_OPEN_ELEMENT
if (c === "\"") {
stack = state;
levelStack = nextLevel;
nextLevel = 0;
state = DOUBLE_QUOTE;
}
else if (c === "'") {
stack = state;
levelStack = nextLevel;
nextLevel = 0;
state = SINGLE_QUOTE;
}
else if (c === "/") {
state = IN_SELF_CLOSED_ELEMENT;
nextLevel -= 1; // stop
}
else if (c === ">") {
state = BASE;
nextLevel -= 1; // continuation
}
break;
case 3: // IN_SELF_CLOSED_ELEMENT
if (c === ">") {
state = BASE;
nextLevel -= 1; // continuation, close
}
break;
case 4: // IN_CLOSE_ELEMENT
if (c === ">") {
state = BASE;
nextLevel -= 2; // continuation, close
}
break;
case 5: // DOUBLE_QUOTE
if (c === "\"") {
state = stack;
nextLevel = levelStack;
}
break;
case 6: // SINGLE_QUOTE
if (c === "'") {
state = stack;
nextLevel = levelStack;
}
break;
case 7: // COMMENT
if (c === ">") {
state = BASE;
nextLevel -= 2;
}
break;
case 8: // PI
if (c === "\"") {
stack = state;
levelStack = nextLevel;
nextLevel = 0;
state = DOUBLE_QUOTE;
}
else if (c === "'") {
stack = state;
levelStack = nextLevel;
nextLevel = 0;
state = SINGLE_QUOTE;
}
else if (c === "?") {
state = PI_QUESTION_MARK;
nextLevel -= 1; // continuation
}
break;
case 9:
if (c === ">") {
state = BASE;
nextLevel -= 1; // continuation
} else {
state = PI;
}
break;
}
}
// Append current line with indentation from the last line
var i;
for (i = 0; i < currentLevel; ++i) {
formatted += tab;
}
formatted += $.trim(line);
formatted += "\n";
});
return formatted;
};
function readChar(input, pos) {
var c = input.charAt(pos);
if (c !== '&') {
return c;
}
var i;
for (i = 1; i < (input.length - pos); ++i) {
c += input.charAt(pos + i);
if (input.charAt(pos + i) === ';') {
break;
}
}
return c;
};
} ( jQuery ));