Debian Codegraph @

rjsmin.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: ascii -*-
3 u"""
4 =====================
5 Javascript Minifier
6 =====================
7
8 rJSmin is a javascript minifier written in python.
9
10 The minifier is based on the semantics of `jsmin.c by Douglas Crockford`_\\.
11
12 :Copyright:
13
14 Copyright 2011 - 2025
15 Andr\xe9 Malo or his licensors, as applicable
16
17 :License:
18
19 Licensed under the Apache License, Version 2.0 (the "License");
20 you may not use this file except in compliance with the License.
21 You may obtain a copy of the License at
22
23 http://www.apache.org/licenses/LICENSE-2.0
24
25 Unless required by applicable law or agreed to in writing, software
26 distributed under the License is distributed on an "AS IS" BASIS,
27 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 See the License for the specific language governing permissions and
29 limitations under the License.
30
31 The module is a re-implementation aiming for speed, so it can be used at
32 runtime (rather than during a preprocessing step). Usually it produces the
33 same results as the original ``jsmin.c``. It differs in the following ways:
34
35 - there is no error detection: unterminated string, regex and comment
36 literals are treated as regular javascript code and minified as such.
37 - Control characters inside string and regex literals are left untouched; they
38 are not converted to spaces (nor to \\n)
39 - Newline characters are not allowed inside string and regex literals, except
40 for line continuations in string literals (ECMA-5).
41 - "return /regex/" is recognized correctly.
42 - More characters are allowed before regexes.
43 - Line terminators after regex literals are handled more sensibly
44 - "+ +" and "- -" sequences are not collapsed to '++' or '--'
45 - Newlines before ! operators are removed more sensibly
46 - (Unnested) template literals are supported (ECMA-6)
47 - Comments starting with an exclamation mark (``!``) can be kept optionally
48 - rJSmin does not handle streams, but only complete strings. (However, the
49 module provides a "streamy" interface).
50
51 Since most parts of the logic are handled by the regex engine it's way faster
52 than the original python port of ``jsmin.c`` by Baruch Even. The speed factor
53 varies between about 6 and 55 depending on input and python version (it gets
54 faster the more compressed the input already is). Compared to the
55 speed-refactored python port by Dave St.Germain the performance gain is less
56 dramatic but still between 3 and 50 (for huge inputs). See the docs/BENCHMARKS
57 file for details.
58
59 rjsmin.c is a reimplementation of rjsmin.py in C and speeds it up even more.
60
61 Supported python versions are 2.7 and 3.6+.
62
63 .. _jsmin.c by Douglas Crockford:
64 http://www.crockford.com/javascript/jsmin.c
65 """
66 __author__ = u"Andr\xe9 Malo"
67 __license__ = "Apache License, Version 2.0"
68 __version__ = '1.2.5'
69 __all__ = ['jsmin']
70
71 import functools as _ft
72 import re as _re
73
74
75 def _make_jsmin(python_only=False):
76 """
77 Generate JS minifier based on `jsmin.c by Douglas Crockford`_
78
79 .. _jsmin.c by Douglas Crockford:
80 http://www.crockford.com/javascript/jsmin.c
81
82 Parameters:
83 python_only (bool):
84 Use only the python variant. If true, the c extension is not even
85 tried to be loaded.
86
87 Returns:
88 callable: Minifier
89 """
90 # pylint: disable = unused-variable, possibly-unused-variable
91 # pylint: disable = too-many-locals, too-many-statements
92
93 if not python_only:
94 try:
95 import _rjsmin # pylint: disable = import-outside-toplevel
96 except ImportError:
97 pass
98 else:
99 # Ensure that the C version is in sync
100 # https://github.com/ndparker/rjsmin/issues/11
101 if getattr(_rjsmin, '__version__', None) == __version__:
102 return _rjsmin.jsmin
103 try:
104 xrange # pylint: disable = used-before-assignment
105 except NameError:
106 xrange = range # pylint: disable = redefined-builtin
107
108 space_chars = r'[\000-\011\013\014\016-\040]'
109
110 line_comment = r'(?://[^\r\n]*)'
111 space_comment = r'(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)'
112 space_comment_nobang = r'(?:/\*(?!!)[^*]*\*+(?:[^/*][^*]*\*+)*/)'
113 bang_comment = r'(?:/\*![^*]*\*+(?:[^/*][^*]*\*+)*/)'
114
115 string1 = r"(?:'[^'\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|\r)[^'\\\r\n]*)*')"
116 string1 = string1.replace("'", r'\047') # portability
117 string2 = r'(?:"[^"\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|\r)[^"\\\r\n]*)*")'
118 string3 = r'(?:`[^`\\]*(?:\\(?:[^\r\n]|\r?\n|\r)[^`\\]*)*`)'
119 string3 = string3.replace('`', r'\140') # portability
120 strings = r'(?:%s|%s|%s)' % (string1, string2, string3)
121
122 charclass = r'(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\])'
123 nospecial = r'[^/\\\[\r\n]'
124 regex = r'(?:/(?![\r\n/*])%s*(?:(?:\\[^\r\n]|%s)%s*)*/[a-z]*)' % (
125 nospecial, charclass, nospecial
126 )
127 space = r'(?:%s|%s)' % (space_chars, space_comment)
128 newline = r'(?:%s?[\r\n])' % line_comment
129
130 def fix_charclass(result):
131 """ Fixup string of chars to fit into a regex char class """
132 pos = result.find('-')
133 if pos >= 0:
134 result = r'%s%s-' % (result[:pos], result[pos + 1:])
135
136 def sequentize(string):
137 """
138 Notate consecutive characters as sequence
139
140 (1-4 instead of 1234)
141 """
142 first, last, result = None, None, []
143 for char in map(ord, string):
144 if last is None:
145 first = last = char
146 elif last + 1 == char:
147 last = char
148 else:
149 result.append((first, last))
150 first = last = char
151 if last is not None:
152 result.append((first, last))
153 return ''.join(['%s%s%s' % (
154 chr(first),
155 last > first + 1 and '-' or '',
156 last != first and chr(last) or ''
157 ) for first, last in result]) # noqa
158
159 return _re.sub(
160 r"([\000-\040'`])", # ' and ` for better portability
161 lambda m: '\\%03o' % ord(m.group(1)), (
162 sequentize(result)
163 .replace('\\', '\\\\')
164 .replace('[', '\\[')
165 .replace(']', '\\]')
166 )
167 )
168
169 def id_literal_(what):
170 """ Make id_literal like char class """
171 match = _re.compile(what).match
172 result = ''.join([
173 chr(c) for c in xrange(127) if not match(chr(c))
174 ])
175 return '[^%s]' % fix_charclass(result)
176
177 def not_id_literal_(keep):
178 """ Make negated id_literal like char class """
179 match = _re.compile(id_literal_(keep)).match
180 result = ''.join([
181 chr(c) for c in xrange(127) if not match(chr(c))
182 ])
183 return r'[%s]' % fix_charclass(result)
184
185 not_id_literal = not_id_literal_(r'[a-zA-Z0-9_$]')
186 preregex1 = r'[(,=:\[!&|?{};\r\n+*-]'
187 preregex2 = r'%(not_id_literal)sreturn' % locals()
188
189 id_literal = id_literal_(r'[a-zA-Z0-9_$]')
190 id_literal_open = id_literal_(r'[a-zA-Z0-9_${\[(!+-]')
191 id_literal_close = id_literal_(r'[a-zA-Z0-9_$}\])"\047\140+-]')
192 post_regex_off = id_literal_(r'[^\000-\040}\])?:|,;.&=+-]')
193
194 dull = r'[^\047"\140/\000-\040]'
195
196 space_sub_simple = _re.compile((
197 # noqa pylint: disable = bad-option-value, bad-continuation
198
199 r'(%(dull)s+)' # 0
200 r'|(%(strings)s%(dull)s*)' # 1
201 r'|(?<=[)])'
202 r'%(space)s*(?:%(newline)s%(space)s*)*'
203 r'(%(regex)s)' # 2
204 r'(?=%(space)s*(?:%(newline)s%(space)s*)*'
205 r'\.'
206 r'%(space)s*(?:%(newline)s%(space)s*)*[a-z])'
207 r'|(?<=%(preregex1)s)'
208 r'%(space)s*(?:%(newline)s%(space)s*)*'
209 r'(%(regex)s)' # 3
210 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 4
211 r'(?=%(post_regex_off)s))?'
212 r'|(?<=%(preregex2)s)'
213 r'%(space)s*(?:(%(newline)s)%(space)s*)*' # 5
214 r'(%(regex)s)' # 6
215 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 7
216 r'(?=%(post_regex_off)s))?'
217 r'|(?<=%(id_literal_close)s)'
218 r'%(space)s*(?:(%(newline)s)%(space)s*)+' # 8
219 r'(?=%(id_literal_open)s)'
220 r'|(?<=%(id_literal)s)(%(space)s)+(?=%(id_literal)s)' # 9
221 r'|(?<=\+)(%(space)s)+(?=\+)' # 10
222 r'|(?<=-)(%(space)s)+(?=-)' # 11
223 r'|%(space)s+'
224 r'|(?:%(newline)s%(space)s*)+'
225 ) % locals()).sub
226
227 # print(space_sub_simple.__self__.pattern)
228
229 def space_subber_simple(match):
230 """ Substitution callback """
231 # pylint: disable = too-many-return-statements
232
233 groups = match.groups()
234 if groups[0]:
235 return groups[0]
236 elif groups[1]:
237 return groups[1]
238 elif groups[2]:
239 return groups[2]
240 elif groups[3]:
241 if groups[4]:
242 return groups[3] + '\n'
243 return groups[3]
244 elif groups[6]:
245 return "%s%s%s" % (
246 groups[5] and '\n' or '',
247 groups[6],
248 groups[7] and '\n' or '',
249 )
250 elif groups[8]:
251 return '\n'
252 elif groups[9] or groups[10] or groups[11]:
253 return ' '
254 else:
255 return ''
256
257 space_sub_banged = _re.compile((
258 # noqa pylint: disable = bad-option-value, bad-continuation
259
260 r'(%(dull)s+)' # 0
261 r'|(%(strings)s%(dull)s*)' # 1
262 r'|(?<=[)])'
263 r'(%(space)s*(?:%(newline)s%(space)s*)*)' # 2
264 r'(%(regex)s)' # 3
265 r'(?=%(space)s*(?:%(newline)s%(space)s*)*'
266 r'\.'
267 r'%(space)s*(?:%(newline)s%(space)s*)*[a-z])'
268 r'|(?<=%(preregex1)s)'
269 r'(%(space)s*(?:%(newline)s%(space)s*)*)' # 4
270 r'(%(regex)s)' # 5
271 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 6
272 r'(?=%(post_regex_off)s))?'
273 r'|(?<=%(preregex2)s)'
274 r'(%(space)s*(?:(%(newline)s)%(space)s*)*)' # 7, 8
275 r'(%(regex)s)' # 9
276 r'(%(space)s*(?:%(newline)s%(space)s*)+' # 10
277 r'(?=%(post_regex_off)s))?'
278 r'|(?<=%(id_literal_close)s)'
279 r'(%(space)s*(?:%(newline)s%(space)s*)+)' # 11
280 r'(?=%(id_literal_open)s)'
281 r'|(?<=%(id_literal)s)(%(space)s+)(?=%(id_literal)s)' # 12
282 r'|(?<=\+)(%(space)s+)(?=\+)' # 13
283 r'|(?<=-)(%(space)s+)(?=-)' # 14
284 r'|(%(space)s+)' # 15
285 r'|((?:%(newline)s%(space)s*)+)' # 16
286 ) % locals()).sub
287
288 # print(space_sub_banged.__self__.pattern)
289
290 keep = _re.compile((
291 r'%(space_chars)s+|%(space_comment_nobang)s+|%(newline)s+'
292 r'|(%(bang_comment)s+)'
293 ) % locals()).sub
294 keeper = lambda m: m.groups()[0] or ''
295
296 # print(keep.__self__.pattern)
297
298 def space_subber_banged(match):
299 """ Substitution callback """
300 # pylint: disable = too-many-return-statements
301
302 groups = match.groups()
303 if groups[0]:
304 return groups[0]
305 elif groups[1]:
306 return groups[1]
307 elif groups[3]:
308 return "%s%s" % (
309 keep(keeper, groups[2]),
310 groups[3],
311 )
312 elif groups[5]:
313 return "%s%s%s%s" % (
314 keep(keeper, groups[4]),
315 groups[5],
316 keep(keeper, groups[6] or ''),
317 groups[6] and '\n' or '',
318 )
319 elif groups[9]:
320 return "%s%s%s%s%s" % (
321 keep(keeper, groups[7]),
322 groups[8] and '\n' or '',
323 groups[9],
324 keep(keeper, groups[10] or ''),
325 groups[10] and '\n' or '',
326 )
327 elif groups[11]:
328 return keep(keeper, groups[11]) + '\n'
329 elif groups[12] or groups[13] or groups[14]:
330 return keep(keeper, groups[12] or groups[13] or groups[14]) or ' '
331 else:
332 return keep(keeper, groups[15] or groups[16])
333
334 banged = _ft.partial(space_sub_banged, space_subber_banged)
335 simple = _ft.partial(space_sub_simple, space_subber_simple)
336
337 def jsmin(script, keep_bang_comments=False):
338 r"""
339 Minify javascript based on `jsmin.c by Douglas Crockford`_\.
340
341 Instead of parsing the stream char by char, it uses a regular
342 expression approach which minifies the whole script with one big
343 substitution regex.
344
345 .. _jsmin.c by Douglas Crockford:
346 http://www.crockford.com/javascript/jsmin.c
347
348 Parameters:
349 script (str):
350 Script to minify
351
352 keep_bang_comments (bool):
353 Keep comments starting with an exclamation mark? (``/*!...*/``)
354
355 Returns:
356 str: Minified script
357 """
358 # pylint: disable = redefined-outer-name
359
360 is_bytes, script = _as_str(script)
361 script = (banged if keep_bang_comments else simple)(
362 '\n%s\n' % script
363 ).strip()
364 if is_bytes:
365 script = script.encode('latin-1')
366 if is_bytes == 2:
367 script = bytearray(script)
368 return script
369
370 return jsmin
371
372 jsmin = _make_jsmin()
373
374
375 def _as_str(script):
376 """ Make sure the script is a text string """
377 is_bytes = False
378 if str is bytes:
379 if not isinstance(script, basestring): # noqa pylint: disable = undefined-variable
380 raise TypeError("Unexpected type")
381 elif isinstance(script, bytes):
382 is_bytes = True
383 script = script.decode('latin-1')
384 elif isinstance(script, bytearray):
385 is_bytes = 2
386 script = script.decode('latin-1')
387 elif not isinstance(script, str):
388 raise TypeError("Unexpected type")
389
390 return is_bytes, script
391
392
393 def jsmin_for_posers(script, keep_bang_comments=False):
394 r"""
395 Minify javascript based on `jsmin.c by Douglas Crockford`_\.
396
397 Instead of parsing the stream char by char, it uses a regular
398 expression approach which minifies the whole script with one big
399 substitution regex.
400
401 .. _jsmin.c by Douglas Crockford:
402 http://www.crockford.com/javascript/jsmin.c
403
404 :Warning: This function is the digest of a _make_jsmin() call. It just
405 utilizes the resulting regexes. It's here for fun and may
406 vanish any time. Use the `jsmin` function instead.
407
408 Parameters:
409 script (str):
410 Script to minify
411
412 keep_bang_comments (bool):
413 Keep comments starting with an exclamation mark? (``/*!...*/``)
414
415 Returns:
416 str: Minified script
417 """
418 if not keep_bang_comments:
419 rex = (
420 r'([^\047"\140/\000-\040]+)|((?:(?:\047[^\047\\\r\n]*(?:\\(?:[^'
421 r'\r\n]|\r?\n|\r)[^\047\\\r\n]*)*\047)|(?:"[^"\\\r\n]*(?:\\(?:[^'
422 r'\r\n]|\r?\n|\r)[^"\\\r\n]*)*")|(?:\140[^\140\\]*(?:\\(?:[^\r\n'
423 r']|\r?\n|\r)[^\140\\]*)*\140))[^\047"\140/\000-\040]*)|(?<=[)])'
424 r'(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+'
425 r')*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\0'
426 r'40]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*((?:/(?![\r\n/*])[^/'
427 r'\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]'
428 r'\r\n]*)*\]))[^/\\\[\r\n]*)*/[a-z]*))(?=(?:[\000-\011\013\014\0'
429 r'16-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n'
430 r']*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^'
431 r'/*][^*]*\*+)*/))*)*\.(?:[\000-\011\013\014\016-\040]|(?:/\*[^*'
432 r']*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\00'
433 r'0-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)'
434 r'*[a-z])|(?<=[(,=:\[!&|?{};\r\n+*-])(?:[\000-\011\013\014\016-'
435 r'\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)'
436 r'?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*]'
437 r'[^*]*\*+)*/))*)*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|'
438 r'(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*'
439 r'/[a-z]*))((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/'
440 r'*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013'
441 r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000'
442 r'-\040&)+,.:;=?\]|}-]))?|(?<=[\000-#%-,./:-@\[-^\140{-~-]return'
443 r')(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*'
444 r'+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013\014\016'
445 r'-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*((?:/(?![\r\n/*])'
446 r'[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^'
447 r'\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/[a-z]*))((?:[\000-\011\013\014'
448 r'\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r'
449 r'\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:'
450 r'[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=[^\0'
451 r'00-!#%&(*,./:-@\[\\^{|~])(?:[\000-\011\013\014\016-\040]|(?:/'
452 r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))'
453 r'(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+'
454 r')*/))*)+(?=[^\000-\040"#%-\047)*,./:-@\\-^\140|-~])|(?<=[^\000'
455 r'-#%-,./:-@\[-^\140{-~-])((?:[\000-\011\013\014\016-\040]|(?:/'
456 r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=[^\000-#%-,./:-@\[-^\140{-'
457 r'~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:'
458 r'[^/*][^*]*\*+)*/)))+(?=\+)|(?<=-)((?:[\000-\011\013\014\016-\0'
459 r'40]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=-)|(?:[\000-\011\0'
460 r'13\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+|(?:(?:(?'
461 r'://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]'
462 r'*\*+(?:[^/*][^*]*\*+)*/))*)+'
463 )
464
465 def subber(match):
466 """ Substitution callback """
467 groups = match.groups()
468 return (
469 groups[0] or
470 groups[1] or
471 groups[2] or
472 (groups[4] and (groups[3] + '\n')) or
473 groups[3] or
474 (groups[6] and "%s%s%s" % (
475 groups[5] and '\n' or '',
476 groups[6],
477 groups[7] and '\n' or '',
478 )) or
479 (groups[8] and '\n') or
480 (groups[9] and ' ') or
481 (groups[10] and ' ') or
482 (groups[11] and ' ') or
483 ''
484 )
485 else:
486 rex = (
487 r'([^\047"\140/\000-\040]+)|((?:(?:\047[^\047\\\r\n]*(?:\\(?:[^'
488 r'\r\n]|\r?\n|\r)[^\047\\\r\n]*)*\047)|(?:"[^"\\\r\n]*(?:\\(?:[^'
489 r'\r\n]|\r?\n|\r)[^"\\\r\n]*)*")|(?:\140[^\140\\]*(?:\\(?:[^\r\n'
490 r']|\r?\n|\r)[^\140\\]*)*\140))[^\047"\140/\000-\040]*)|(?<=[)])'
491 r'((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*'
492 r'+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-'
493 r'\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*)((?:/(?![\r\n/*])'
494 r'[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^'
495 r'\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/[a-z]*))(?=(?:[\000-\011\013\0'
496 r'14\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^'
497 r'\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+('
498 r'?:[^/*][^*]*\*+)*/))*)*\.(?:[\000-\011\013\014\016-\040]|(?:/'
499 r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?'
500 r':[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*'
501 r'/))*)*[a-z])|(?<=[(,=:\[!&|?{};\r\n+*-])((?:[\000-\011\013\014'
502 r'\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r'
503 r'\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:'
504 r'[^/*][^*]*\*+)*/))*)*)((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^'
505 r'\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r'
506 r'\n]*)*/[a-z]*))((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+'
507 r'(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\01'
508 r'1\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=['
509 r'^\000-\040&)+,.:;=?\]|}-]))?|(?<=[\000-#%-,./:-@\[-^\140{-~-]r'
510 r'eturn)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*]['
511 r'^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013\0'
512 r'14\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*)((?:/(?!['
513 r'\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^'
514 r'\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/[a-z]*))((?:[\000-\011'
515 r'\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:('
516 r'?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*'
517 r']*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|'
518 r'(?<=[^\000-!#%&(*,./:-@\[\\^{|~])((?:[\000-\011\013\014\016-\0'
519 r'40]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?['
520 r'\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^'
521 r'*]*\*+)*/))*)+)(?=[^\000-\040"#%-\047)*,./:-@\\-^\140|-~])|(?<'
522 r'=[^\000-#%-,./:-@\[-^\140{-~-])((?:[\000-\011\013\014\016-\040'
523 r']|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)(?=[^\000-#%-,./:-@\[-^'
524 r'\140{-~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*'
525 r'\*+(?:[^/*][^*]*\*+)*/))+)(?=\+)|(?<=-)((?:[\000-\011\013\014'
526 r'\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)(?=-)|((?:[\00'
527 r'0-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)'
528 r'|((?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|'
529 r'(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+)'
530 )
531
532 keep = _re.compile(
533 r'[\000-\011\013\014\016-\040]+|(?:/\*(?!!)[^*]*\*+(?:[^/*][^*]*'
534 r'\*+)*/)+|(?:(?://[^\r\n]*)?[\r\n])+|((?:/\*![^*]*\*+(?:[^/*][^'
535 r'*]*\*+)*/)+)'
536 ).sub
537 keeper = lambda m: m.groups()[0] or ''
538
539 def subber(match):
540 """ Substitution callback """
541 groups = match.groups()
542 return (
543 groups[0] or
544 groups[1] or
545 groups[3] and "%s%s" % (
546 keep(keeper, groups[2]),
547 groups[3],
548 ) or
549 groups[5] and "%s%s%s%s" % (
550 keep(keeper, groups[4]),
551 groups[5],
552 keep(keeper, groups[6] or ''),
553 groups[6] and '\n' or '',
554 ) or
555 groups[9] and "%s%s%s%s%s" % (
556 keep(keeper, groups[7]),
557 groups[8] and '\n' or '',
558 groups[9],
559 keep(keeper, groups[10] or ''),
560 groups[10] and '\n' or '',
561 ) or
562 groups[11] and (keep(keeper, groups[11]) + '\n') or
563 groups[12] and (keep(keeper, groups[12]) or ' ') or
564 groups[13] and (keep(keeper, groups[13]) or ' ') or
565 groups[14] and (keep(keeper, groups[14]) or ' ') or
566 keep(keeper, groups[15] or groups[16])
567 )
568
569 is_bytes, script = _as_str(script)
570 script = _re.sub(rex, subber, '\n%s\n' % script).strip()
571 if is_bytes:
572 script = script.encode('latin-1')
573 if is_bytes == 2:
574 script = bytearray(script)
575 return script
576
577
578 if __name__ == '__main__':
579 def main():
580 """ Main """
581 import sys as _sys # pylint: disable = import-outside-toplevel
582
583 argv = _sys.argv[1:]
584 keep_bang_comments = '-b' in argv or '-bp' in argv or '-pb' in argv
585 if '-p' in argv or '-bp' in argv or '-pb' in argv:
586 xjsmin = _make_jsmin(python_only=True)
587 else:
588 xjsmin = jsmin
589
590 _sys.stdout.write(xjsmin(
591 _sys.stdin.read(), keep_bang_comments=keep_bang_comments
592 ))
593
594 main()