-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirtyXML.js
112 lines (93 loc) · 2.9 KB
/
dirtyXML.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
// XML Manipulation Using Regex
// to modify parameters universally use js proxy
function XM(str){
if(!(this instanceof XM)){
return new XM(str);
}
var str = str || '';
// capturing group (from non-capturing)
function C(str) {
return str.replace('?:','')
}
// required group (from optional)
function R(str) {
return str.replace(')?', ')')
}
var all = String.raw`[\s\S]*?`;
var ns = String.raw`(?:\S+?:)?`;
var attr = String.raw`(?:\s+[\s\S]*?)?`;
this.fill = function(ele,content){
var reg = new RegExp(String.raw`<${C(ns)}${ele}${C(attr)}>${all}<\/${ns}${ele}>`);
str = str.replace(reg, `<$1${ele}$2>${content}<\/$1${ele}>`);
return this;
}
this.replace = function(ele,content){
var reg = new RegExp(String.raw`<${ns}${ele}${attr}>${all}<\/${ns}${ele}>`);
str = str.replace(reg, content);
return this;
}
this.prepend = function(ele,content){
var reg = new RegExp(String.raw`<${C(ns)}${ele}${C(attr)}>`);
str = str.replace(reg, `<$1${ele}$2>${content}`);
return this;
}
this.append = function(ele,content){
var reg = new RegExp(String.raw`<\/${C(ns)}${ele}>`);
str = str.replace(reg, `${content}<\/$1${ele}>`);
return this;
}
this.before = function(ele,content){
var reg = new RegExp(String.raw`<${C(ns)}${ele}${C(attr)}>`);
str = str.replace(reg, `${content}<$1${ele}$2>`);
return this;
}
this.after = function(ele,content){
var reg = new RegExp(String.raw`<\/${C(ns)}${ele}>`);
str = str.replace(reg, `<\/$1${ele}>${content}`);
return this;
}
this.remove = function(...eles){
eles.forEach((ele)=>{
var reg = new RegExp(String.raw`<${ns}${ele}${attr}>${all}<\/${ns}${ele}>`);
str = str.replace(reg, '');
})
return this;
}
this.empty = function(ele){
var reg = new RegExp(String.raw`<${C(ns)}${ele}${C(attr)}>${all}<\/${ns}${ele}>`);
str = str.replace(reg, `<$1${ele}$2><\/$1${ele}>`);
return this;
}
this.get = function(ele){
var reg = new RegExp(String.raw`<${ns}${ele}${attr}>(${all})<\/${ns}${ele}>`);
return str.match(reg)?.[1];
}
this.getAll = function(ele){
var reg = new RegExp(String.raw`<${ns}${ele}${attr}>(${all})<\/${ns}${ele}>`, 'g');
return [...str.matchAll(reg)].map((i)=>i[1]);
}
this.render = function(){
return str;
}
}
function X(obj) {
var xml = '';
for (var key in obj) {
if (obj[key] instanceof Array) {
for (var arr in obj[key]) {
xml += '<'+key+'>';
xml += X(new Object(obj[key][arr]));
xml += '</'+key+'>';
}
} else {
xml += '<'+key+'>';
if (typeof obj[key] == "object") {
xml += X(new Object(obj[key]));
} else {
xml += obj[key];
}
xml += '</'+key+'>';
}
}
return xml
}