JS格式化XML

格式化XML

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
String.prototype.removeLineEnd = function () {
return this.replace(/(<.+?\s+?)(?:\n\s*?(.+?=".*?"))/g, '$1 $2')
}

function getPrefix(prefixIndex) {
var span = ' ';
var output = [];
for (var i = 0; i < prefixIndex; ++i) {
output.push(span);
}

return output.join('');
}

var format_xml_fun = function (text) {
//去掉多余的空格
text = '\n' + text.replace(/(<\w+)(\s.*?>)/g, function ($0, name, props) {
return name + ' ' + props.replace(/\s+(\w+=)/g, " $1");
}).replace(/>\s*?</g, ">\n<");

//把注释编码
text = text.replace(/\n/g, '\r').replace(/<!--(.+?)-->/g, function ($0, text) {
return '<!--' + escape(text) + '-->';
}).replace(/\r/g, '\n');

//调整格式
var rgx = /\n(<(([^?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
var nodeStack = [];
var output = text.replace(rgx, function ($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2) {
var isClosed = (isCloseFull1 === '/') || (isCloseFull2 === '/') || (isFull1 === '/') || (isFull2 === '/');
var prefix = '';
if (isBegin === '!') {
prefix = getPrefix(nodeStack.length);
} else {
if (isBegin !== '/') {
prefix = getPrefix(nodeStack.length);
if (!isClosed) {
nodeStack.push(name);
}
} else {
nodeStack.pop();
prefix = getPrefix(nodeStack.length);
}

}
return '\n' + prefix + all;
});

var prefixSpace = -1;
var outputText = output.substring(1);

//把注释还原并解码,调格式
outputText = outputText.replace(/\n/g, '\r').replace(/(\s*)<!--(.+?)-->/g, function ($0, prefix, text) {
if (prefix.charAt(0) === '\r')
prefix = prefix.substring(1);
text = unescape(text).replace(/\r/g, '\n');
return '\n' + prefix + '<!--' + text.replace(/^\s*/mg, prefix) + '-->';
});

return outputText.replace(/\s+$/g, '').replace(/\r/g, '\r\n');
}

export const format_xml = format_xml_fun