-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconvertE4X.js
More file actions
258 lines (237 loc) · 7.51 KB
/
convertE4X.js
File metadata and controls
258 lines (237 loc) · 7.51 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// https://github.com/Infocatcher/Custom_Buttons/tree/master/Convert_E4X
// https://forum.mozilla-russia.org/viewtopic.php?id=56442
// http://custombuttons.sf.net/forum/viewtopic.php?f=2&t=365
// Convert E4X button for Custom Buttons
// Code for "code" section
// (c) Infocatcher 2012-2013, 2016-2017
// version 0.1.0b2 - 2017-03-15
// Tries convert deprecated E4X to string literals.
// Click on button and then click on another button with E4X in code (or click on opened page with *.js file).
var btn = this;
btn.checked = true;
addEventListener("click", function getButton(e) {
var trg = e.target;
var codes;
if(trg.localName == "toolbarbutton" && /^custombuttons-button\d+$/.test(trg.id))
codes = [trg.cbCommand, trg.cbInitCode];
else if(
trg.ownerDocument.defaultView == content
&& /^(?:application\/(?:x-)?javascript|text\/plain)$/.test(trg.ownerDocument.contentType)
)
codes = [trg.ownerDocument.body.textContent];
if(!codes)
return;
removeEventListener(e.type, getButton, true);
try {
e.preventDefault();
e.stopPropagation();
}
catch(e) { // TypeError: 'preventDefault' called on an object that does not implement interface Event
Components.utils.reportError(e);
}
btn.checked = false;
if(trg != btn) codes.forEach(function(code) {
code = convertE4XCode(code);
if(!code) // Nothing to convert
return;
var uri = "data:text/plain;charset=utf-8," + encodeURIComponent(code);
gBrowser.selectedTab = gBrowser.addTab(uri, makeURI("about:blank"));
});
}, true);
function convertE4XCode(s) {
var _debug = false;
convertE4XCode = convertCode;
return convertCode(s);
function _log(s) {
_debug && LOG(s);
};
function convertCode(s) {
if(!s || s == "/*CODE*/" || s == "/*Initialization Code*/")
return "";
var convertedNote = "/* Converted with https://github.com/Infocatcher/Custom_Buttons/tree/master/Convert_E4X */\n";
if(s.indexOf(convertedNote) != -1)
return "";
var orig = s;
// Replace old parsers
// Note: declarations of old perser functions aren't removed!
s = s.replace(/(?:[\w$]+\s*\.\s*)*(?:makeXML|parseFromXML)(\s*)\(/g, "e4xConv_parseXULFromString$1(");
// Convert node.@attr = ...
s = s.replace(/(\.\s*)@([\w$]+)\s*=\s*([^;]+)/g, '$1setAttribute("$2", $3)');
// Other node.@attr usage
s = s.replace(/(\.\s*)@([\w$]+)/g, '$1getAttribute("$2")');
s = s
// <> ... </>
.replace(/<>([\s\S]+?)<\/>(?:\s*\.\s*toXMLString\s*\(\))?/g, function(s, e4x) {
if(e4x.substr(0, 9) == "<![CDATA[")
return s;
_log("<> ... </>\n" + s);
return convertE4X(e4x);
})
// <![CDATA[ ... ]]>
.replace(/(?:<>)?<!\[CDATA\[([\s\S]+?)\]\]>(?:<\/>)?(?:\s*\.\s*toString\s*\(\))?/g, function(s, cdata) {
if(inE4X(RegExp.leftContext) || alreadyConverted(cdata))
return s;
_log("<![CDATA[ ... ]]>\n" + s);
return convertCDATA(cdata);
})
// <tag ... />
.replace(/<\w+\s([^>]+?)\/>(?:\s*\.\s*toXMLString\s*\(\))?/g, function(s) {
var lc = RegExp.leftContext;
var rc = RegExp.rightContext;
if(inE4X(lc) || inString(lc) || alreadyConverted(s) || /^(?:\\n)?\\[\n\r]/.test(rc))
return s;
_log("<tag ... />\n" + s);
return convertE4X(s);
})
// <tag> ... </tag>
//.replace(/<(\w+)(?:[^>]*[^>\/])?>([\s\S]*?)<\/\1>(?:\s*\.\s*toXMLString\s*\(\))?/g, function(s) {
.replace(getTagsPattern(), function(s) {
var lc = RegExp.leftContext;
if(inE4X(lc) || inString(lc) || alreadyConverted(s))
return s;
_log("<tag> ... </tag>\n" + s);
return convertE4X(s);
})
// All .toXMLString() calls are useless
// Example:
// window.openDialog("data:application/vnd.mozilla.xul+xml," + encodeURIComponent(dialog.toXMLString()), ...
.replace(/\s*\.\s*toXMLString\s*\(\)/g, "");
if(s != orig) {
var codeHeader = /^(?:\/\*(?:CODE|Initialization Code)\*\/\n?)?/;
if(/\WXML\s*\.\s*\w/.test(orig))
s = s.replace(codeHeader, "$&var XML = window.XML || {};\n\n");
s = s.replace(codeHeader, "$&" + convertedNote);
var addFuncs = "";
if(s.indexOf("e4xConv_parseXULFromString") != -1)
addFuncs += "\n" + trimFunc(e4xConv_parseXULFromString);
if(s.indexOf("e4xConv_encodeHTML") != -1)
addFuncs += "\n" + trimFunc(e4xConv_encodeHTML);
if(addFuncs)
s += "\n" + addFuncs;
return s;
}
return "";
}
function getTagsPattern() {
// I'm too lazy to write a real tags parser, sorry...
const recursion = 7;
const tagStart = "<(\\w+)(?:[^>]*[^>\\/])?>";
function getSub(level) {
if(++level > recursion)
return "[\\s\\S]*?";
return "(?:"
+ "<\\w+[^>]*\\/>"
+ "|" + tagStart + getSub(level) + "<\\/\\" + level + ">"
+ "|[\\s\\S]"
+ ")*?";
}
var pattern = tagStart
+ getSub(1)
+ "<\\/\\1>"
+ "(?:\\s*\\.\\s*toXMLString\\s*\\(\\))?";
var re = new RegExp(pattern, "g");
getTagsPattern = function() {
return re;
};
return re;
}
function convertCDATA(s) {
return "'" + escapeString(s.replace(/['\\]/g, "\\$&")) + "'";
}
function inE4X(prev) {
return />\s*(?:(?:\\n)?\\[\n\r]\s*)?$/.test(prev);
}
function inString(prev) {
// Very basic check to not convert something like
// node.innerHTML = '<div>...</div>';
return /['"]$/.test(prev);
}
function alreadyConverted(s) {
return /['">][ \t]*(?:\\n)?\\[\n\r]/.test(s);
}
function convertE4X(s) {
var cdates = [];
var rndCd = rnd();
// Leave CDATA as is
s = s.replace(/<!\[CDATA\[[\s\S]+?\]\]>/g, function(s) {
//cdates.push("\\x3" + s.substr(1)); // Very simple way to break CDATA
cdates.push(s);
return rndCd;
});
// Extract codes
var codes = [];
var rndExpr = rnd();
// { expressions }
s = s.replace(/(=\s*)?\{([^\}]*)\}/g, function(s, eq, code, offset, str) {
if(
!eq
&& /\s[-:\w]+\s*=\s*(?:"[^"]*|'[^']*)$/.test(str.substr(0, offset)) // foo="something{
&& /^(?:[^"]*"|[^']*')(?:\s|\/?>)/.test(str.substr(offset)) // something"
)
return s; // Looks like we're inside attribute, leave as is
code = "e4xConv_encodeHTML(" + code + (eq ? ", true" : "") + ")";
var q = eq ? '"' : "";
code = q + "' + " + code + " + '" + q;
codes.push(code);
return (eq || "") + rndExpr;
});
// Restore CDATA
var i = 0;
s = s.replace(new RegExp(rndCd, "g"), function(s) {
return cdates[i++];
});
s = escapeString(s);
// Restore codes
var i = 0;
s = s.replace(new RegExp(rndExpr, "g"), function(s) {
return codes[i++];
});
return "'" + s + "'";
}
function escapeString(s) {
return s
.replace(/['\\]/g, "\\$&")
.replace(/$/mg, "\\n\\")
.replace(/^\\n(\\[\n\r])/, "$1")
.slice(0, -3);
}
function trimFunc(fn) {
var fnCode = "" + fn;
var spaces = /([ \t]+)\}$/.test(fnCode) && RegExp.$1;
if(!spaces)
return fnCode;
return fnCode.replace(new RegExp("^" + spaces, "mg"), "");
}
function rnd() {
return Date.now()
+ Math.random().toFixed(14).substr(2)
+ Math.random().toFixed(14).substr(2);
}
function e4xConv_parseXULFromString(xul) {
xul = xul.replace(/>\s+</g, "><");
try {
return new DOMParser().parseFromString(xul, "application/xml").documentElement;
}
catch(e) {
// See http://custombuttons.sourceforge.net/forum/viewtopic.php?f=5&t=3720
// + https://forum.mozilla-russia.org/viewtopic.php?pid=732243#p732243
var dummy = document.createElement("dummy");
dummy.innerHTML = xul.trimLeft();
return dummy.firstChild;
}
}
function e4xConv_encodeHTML(s, isAttr) {
s = String(s)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
if(isAttr) {
s = s
.replace(/\t/g, "	")
.replace(/\n/g, "
")
.replace(/\r/g, "
");
}
return s;
}
}