forked from westonruter/webforms2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.pl
executable file
·269 lines (238 loc) · 5.39 KB
/
pack.pl
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
259
260
261
262
263
264
265
266
267
268
269
use File::Copy;
@commonVarNames = qw(
xhr
processAttr
parent
attrName
attr
distance
evt
btnType
attrValue
addEvt
moveEvt
isValid
refNode
removeEvt
attrs
sibling
minAttrNode
maxAttrNode
rtNestedDepth
repetitionTemplates
eventSet
isTimeRelated
IDAttrValue
formElements
handleRepetitionEvent
funcBody
deprecatedAttrs
indexAttr
attrVal
reTemplateName
results
invalidIndicator
doCheckPrecision
typeArg
IDAttrName
xmlDoc
elementArg
code
funcMatches
hasChecked
canBubbleArg
valueAttrNode
repeatTemplateAttr
doMaxLengthCheck
isRequired
handlerAttr
isDateRelated
matches
onremovedAttr
isNumberRelated
currentBlockCount
count
isRadioOrCheckbox
isNotEqual
isFileInput
isTemplate
elNames
allElements
oninvalidAttr
attrExpr
hasIndex
addBtns
thisAttrValue
cancelableArg
btns
doCheckRange
doRequiredCheck
zeroPoint
invalidIndicators
indicatorTimeoutId
indicatorIntervalId
addInvalidIndicator
createLI
clearInvalidIndicators
cloneNode_customAttrs
cloneNode_skippedAttrs
cloneNode_rtEventHandlerAttrs
getRepetitionBlock
getHtmlTemplate
getFormElements
getElementsByTagNames
getElementsByTagNamesAndAttribute
arrayHasItem
getElementStyle
createMiscFunctions
ISO8601RegExp
parseISO8601
validateDateTimeType
zeroPad
dateToISO8601
namespaceURIArg
initAutofocusElement
isInitialized
applyValidityInterface
controlCheckValidity
controlSetCustomValidity
formCheckValidity
createValidityState
loadDataURI
numberRegExp
onsubmitValidityHandler
constructRepetitionBlock
clickRepetitionButton
constructRepetitionButton
constructRepetitionTemplate
sortNodes
updateValidityState
urlRegExp
valueToWF2Type
emailRegExp
hasInvalidState
libpath
DOMException
isInitialized
updateAddButtons
updateMoveButtons
property
visitedParents
rePattern
patternAttr
hasInvalidIndicator
prefillSelectElements
prefillFormElements
hasElementExtensions
hasGettersAndSetters
);
#onDOMContentLoaded
push @commonVarNames, '(?<=2\.|\s\s)cloneNode'; #\$wf2\.|\s
push @commonVarNames, '(?<=2\.|\s\s)createElement';
push @commonVarNames, '(?<=2\.|\s\s)addRepetitionBlock(?!:1)';
push @commonVarNames, '(?<=2\.|\s\s)addRepetitionBlockByIndex(?!:1)';
push @commonVarNames, '(?<=2\.|\s\s)removeRepetitionBlock(?!:1)';
push @commonVarNames, '(?<=2\.|\s\s)moveRepetitionBlock(?!:1)';
%replacements = (
'RepetitionElement.REPETITION_NONE' => 0,
'RepetitionElement.REPETITION_TEMPLATE' => 1,
'RepetitionElement.REPETITION_BLOCK' => 2,
'XPathResult.ORDERED_NODE_SNAPSHOT_TYPE' => 7
);
open IN, "webforms2_src.js";
$source = join "", <IN>;
close IN;
$source =~ m{^(.+?)(\s+if\(!window\.\$wf2\).+$)}s;
$header = $1;
$code = $2;
#strip all comments
open NOCOM, ">webforms2-nocomments.js";
$nocoms = $code;
$nocoms =~ s{\s*(?<![\.:])//(?!:).*(?=\n)}{}g; #remove all single-line comments
$nocoms =~ s{/\*.+?\*/}{}gs; #remove all multi-line comments
$nocoms =~ s{[ \t]+(?=\n)}{}g; #remove line terminating whitespace
$nocoms =~ s{\n(?=\n)}{}g; #remove all empty lines
$thisHeader = $header;
$thisHeader =~ s{\bwebforms.+?js\b}{webforms2-nocomments.js};
print NOCOM $thisHeader . "\n" . $nocoms;
close NOCOM;
open NOWHITE, ">webforms2-nocomments-nowhitespace.js";
$nowhite = $nocoms;
$nowhite =~ s{ +}{ }g;
$nowhite =~ s{ +,}{,}g;
$nowhite =~ s{^\s+}{}gm; #remove all non-newline whitespace
$thisHeader = $header;
$thisHeader =~ s{\bwebforms.+?js\b}{webforms2-nocomments-nowhitespace.js};
print NOWHITE $thisHeader . "\n" . $nowhite;
close NOWHITE;
open MAINS, ">webforms2.js";
$thisHeader = $header;
$thisHeader =~ s{\bwebforms.+?js\b}{webforms2.js};
print MAINS $thisHeader . "\n\n" . $nowhite;
close MAINS;
#shorten long variable names
open SHORT, ">webforms2-nocomments-nowhitespace-shortnames.js";
$short = $nocoms;
$count = 0;
foreach(@commonVarNames){
$re = /\W/ ? $_ : '(?<!\')\b' . $_;
print "$re\n";
$short =~ s{$re\b}{sprintf('_%x', $count)}eg; #(?<!')\b$_\b
$count++;
}
foreach(keys %replacements){
$short =~ s{\b$_\b}{$replacements{$_}}eg;
}
$short =~ s{ +}{ }g;
$short =~ s{ +,}{,}g;
$short =~ s{^\s+}{}gm; #remove all non-newline whitespace
my @shortLines = split /\n/, $short;
my $newShort = '';
my $lineSize = 0;
#for(my $i = 0; $i < @shortLines; $i++){
foreach(@shortLines){
$lineSize += length($_);
if($lineSize > 1024){
$lineSize = 0;
$newShort .= $_ . "\n";
}
else {
$newShort .= $_;
}
}
$thisHeader = $header;
$thisHeader =~ s{\bwebforms.+?js\b}{webforms2-nocomments-nowhitespace-shortnames.js};
print SHORT $thisHeader . "\n\n" . $newShort;
close SHORT;
#open MAINS, ">webforms2-p.js";
#$thisHeader = $header;
#$thisHeader =~ s{\bwebforms.+?js\b}{webforms2-p.js};
#print MAINS $thisHeader . "\n\n" . $newShort;
#close MAINS;
#use Dean Edward's packer to condense the code
open PACKED, ">webforms2-p.js";
$packed = jsPack($short);
$thisHeader = $header;
$thisHeader =~ s{\bwebforms.+?js\b}{webforms2-p.js};
print PACKED $thisHeader . "\n\n" . $packed;
close PACKED;
sub jsPack {
open TEMP, ">~topack.js";
print TEMP shift;
close TEMP;
chdir('./packer.perl/');
my $packed = `perl jsPacker.pl -q -f -e0 -i ../~topack.js`; #-e62 -f #-e62
chdir('..');
unlink "~topack.js";
return $packed;
}
exit;
#$source =~ s{(/\*\@cc_on \@\*/\n/\*\@if \(\@_win32\).+?/\*\@end \@\*/)}
# {MSIE_ONDOMCONTENTLOADED();}s;
#(?=/\*\s+cssQuery)
#$source =~ s{var RepetitionElement.+?(?=/\*\@cc_on \@\*/)}
# {SOURCE1}es;
#$source =~ s{var RepetitionElement.+?(?=/\*\@cc_on \@\*/)}
# {SOURCE1}es;
#$source =~ s{(?<=/\*\@cc_on \@\*/\n/\*\@if \(\@_win32\))(.+?)(?=/\*\@end \@\*/)}
# {SOURCE2}es;