JS.php

Gesponsorde koppelingen

PHP script bestanden

  1. Minify.php
  2. CSS.php
  3. Exception.php
  4. JS.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
namespace MatthiasMullie\Minify;

/**
 * JavaScript minifier.
 *
 * Please report bugs on https://github.com/matthiasmullie/minify/issues
 *
 * @author Matthias Mullie <[email protected]>
 * @author Tijs Verkoyen <[email protected]>
 *
 * @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved.
 * @license MIT License
 */

class JS extends Minify
{
    /**
     * List of JavaScript operators that accept a <variable, value, ...> after
     * them. We'll insert semicolons if they're missing at EOL, but some
     * end of lines are not the end of a statement, like with these operators.
     *
     * Note: Most operators are fine, we've only removed !, ++ and --.
     * There can't be a newline separating ! and whatever it is negating.
     * ++ & -- have to be joined with the value they're in-/decrementing.
     *
     * Will be loaded from /data/js/operators_before.txt
     *
     * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
     * @var string[]
     */

    protected $operatorsBefore = array();

    /**
     * List of JavaScript operators that accept a <variable, value, ...> before
     * them. We'll insert semicolons if they're missing at EOL, but some end of
     * lines are not the end of a statement, like when continued by one of these
     * operators on the newline.
     *
     * Note: Most operators are fine, we've only removed ), ], ++ and --.
     * ++ & -- have to be joined with the value they're in-/decrementing.
     * ) & ] are "special" in that they have lots or usecases. () for example
     * is used for function calls, for grouping, in if () and for (), ...
     *
     * Will be loaded from /data/js/operators_after.txt
     *
     * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
     * @var string[]
     */

    protected $operatorsAfter = array();

    /**
     * List of JavaScript reserved words that accept a <variable, value, ...>
     * after them. We'll insert semicolons if they're missing at EOL, but some
     * end of lines are not the end of a statement, like with these keywords.
     *
     * E.g.: we shouldn't insert a ; after this else
     * else
     *     console.log('this is quite fine')
     *
     * Will be loaded from /data/js/reserved_before.txt
     *
     * @see https://mathiasbynens.be/notes/reserved-keywords
     * @var string[]
     */

    protected $keywordsBefore = array();

    /**
     * List of JavaScript reserved words that accept a <variable, value, ...>
     * before them. We'll insert semicolons if they're missing at EOL, but some
     * end of lines are not the end of a statement, like when continued by one
     * of these keywords on the newline.
     *
     * E.g.: we shouldn't insert a ; before this instanceof
     * variable
     *     instanceof String
     *
     * Will be loaded from /data/js/reserved_after.txt
     *
     * @see https://mathiasbynens.be/notes/reserved-keywords
     * @var string[]
     */

    protected $keywordsAfter = array();

    /**
     * {@inheritDoc}
     */

    public function __construct()
    {

        call_user_func_array(array('parent', '__construct'), func_get_args());

        $dataDir = __DIR__ . '/../data/js/';
        $options = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES;
        $this->keywordsBefore = file($dataDir . 'keywords_before.txt', $options);
        $this->keywordsAfter = file($dataDir . 'keywords_after.txt', $options);
        $this->operatorsBefore = file($dataDir . 'operators_before.txt', $options);
        $this->operatorsAfter = file($dataDir . 'operators_after.txt', $options);
    }


    /**
     * Minify the data.
     * Perform JS optimizations.
     *
     * @param  string[optional] $path Path to write the data to.
     * @return string           The minified data.
     */

    public function minify($path = null)
    {

        $content = '';

        // loop files
        foreach ($this->data as $source => $js) {
            // combine js (separate sources with semicolon)
            $content .= $js . ';';
        }


        /*
         * Let's first take out strings, comments and regular expressions.
         * All of these can contain JS code-like characters, and we should make
         * sure any further magic ignores anything inside of these.
         *
         * Consider this example, where we should not strip any whitespace:
         * var str = "a   test";
         *
         * Comments will be removed altogether, strings and regular expressions
         * will be replaced by placeholder text, which we'll restore later.
         */

        $this->extractStrings();
        $this->stripComments();
        $this->extractRegex();
        $content = $this->replace($content);

        $content = $this->stripWhitespace($content);

        /*
         * Earlier, we extracted strings & regular expressions and replaced them
         * with placeholder text. This will restore them.
         */

        $content = $this->restoreExtractedData($content);

        // save to path
        if ($path !== null) {
            $this->save($content, $path);
        }


        return $content;
    }


    /**
     * Strip comments from source code.
     */

    protected function stripComments()
    {

        // single-line comments
        $this->registerPattern('/\/\/.*$[\r\n]*/m', '');

        // multi-line comments
        $this->registerPattern('/\/\*.*?\*\//s', '');
    }


    /**
     * JS kan have /-delimited regular expressions, like: /ab+c/.match(string)
     *
     * The content inside the regex can contain characters that may be confused
     * for JS code: e.g. it could contain whitespace it needs to match & we
     * don't want to strip whitespace in there.
     *
     * The regex can be pretty simple: we don't have to care about comments,
     * (which also use slashes) because stripComments() will have stripped those
     * already.
     *
     * This method will replace all string content with simple REGEX#
     * placeholder text, so we've rid all regular expressions from characters
     * that may be misinterpreted. Original regex content will be saved in
     * $this->extracted and after doing all other minifying, we can restore the
     * original content via restoreRegex()
     */

    protected function extractRegex()
    {

        // PHP only supports $this inside anonymous functions since 5.4
        $minifier = $this;
        $callback = function ($match) use ($minifier) {
            $count = count($minifier->extracted);
            $placeholder = '/REGEX' . $count . '/';
            $minifier->extracted[$placeholder] = '/' . $match[1] . '/';

            return $placeholder;
        };


        // it's a regex if we can find an opening (not preceded by variable,
        // value or similar) & (non-escaped) closing /,

        $before = $this->getOperatorsForRegex($this->operatorsBefore, '/');
        $this->registerPattern('/^\s*\K\/(.*?(?<!\\\\)(\\\\\\\\)*)\//', $callback);
        $this->registerPattern('/(?:' . implode('|', $before) . ')\s*\K\/(.*?(?<!\\\\)(\\\\\\\\)*)\//', $callback);
    }


    /**
     * Strip whitespace.
     *
     * We won't strip *all* whitespace, but as much as possible. The thing that
     * we'll preserve are newlines we're unsure about.
     * JavaScript doesn't require statements to be terminated with a semicolon.
     * It will automatically fix missing semicolons with ASI (automatic semi-
     * colon insertion) at the end of line causing errors (without semicolon.)
     *
     * Because it's sometimes hard to tell if a newline is part of a statement
     * that should be terminated or not, we'll just leave some of them alone.
     *
     * @param  string $content The content to strip the whitespace for.
     * @return string
     */

    protected function stripWhitespace($content)
    {

        // uniform line endings, make them all line feed
        $content = str_replace(array("\r\n", "\r"), "\n", $content);

        // collapse all non-line feed whitespace into a single space
        $content = preg_replace('/[^\S\n]+/', ' ', $content);

        // strip leading & trailing whitespace
        $content = str_replace(array(" \n", "\n "), "\n", $content);

        // collapse consecutive line feeds into just 1
        $content = preg_replace('/\n+/', "\n", $content);

        // strip whitespace that ends in (or next line begin with) an operator
        // that allows statements to be broken up over multiple lines

        $before = $this->getOperatorsForRegex($this->operatorsBefore, '/');
        $after = $this->getOperatorsForRegex($this->operatorsAfter, '/');
        $content = preg_replace('/(' . implode('|', $before) . ')\s+/', '\\1', $content);
        $content = preg_replace('/\s+(' . implode('|', $after) . ')/', '\\1', $content);

        // make sure + and - can't be mistaken for, or joined into ++ and --
        $content = preg_replace('/(?<![\+\-])\s*([\+\-])/', '\\1', $content);
        $content = preg_replace('/([\+\-])\s*(?!\\1)/', '\\1', $content);

        // collapse whitespace around reserved words into single space
        $before = $this->getKeywordsForRegex($this->keywordsBefore, '/');
        $after = $this->getKeywordsForRegex($this->keywordsAfter, '/');
        $content = preg_replace('/(' . implode('|', $before) . ')\s+/', '\\1 ', $content);
        $content = preg_replace('/\s+(' . implode('|', $after) . ')/', ' \\1', $content);

        /*
         * We didn't strip whitespace after a couple of operators because they
         * could be used in different contexts and we can't be sure it's ok to
         * strip the newlines. However, we can safely strip any non-line feed
         * whitespace that follows them.
         */

        $operators = $this->getOperatorsForRegex($this->operatorsBefore + $this->operatorsAfter, '/');
        $content = preg_replace('/([\}\)\]])[^\S\n]+(?!' . implode('|', $operators) . ')/', '\\1', $content);

        /*
         * We also don't really want to terminate statements followed by closing
         * curly braces (which we've ignored completely up until now): ASI will
         * kick in here & we're all about minifying.
         */

        $content = preg_replace('/;\}/s', '}', $content);

        // get rid of remaining whitespace af beginning/end, as well as
        // semicolon, which doesn't make sense there: ASI will kick in here too

        return trim($content, "\n ;");
    }


    /**
     * We'll strip whitespace around certain operators with regular expressions.
     * This will prepare the given array by escaping all characters.
     *
     * @param  string[] $operators
     * @param  string   $delimiter
     * @return string[]
     */

    protected function getOperatorsForRegex(array $operators, $delimiter = '/')
    {

        // escape operators for use in regex
        $delimiter = array_fill(0, count($operators), $delimiter);
        $escaped = array_map('preg_quote', $operators, $delimiter);

        $operators = array_combine($operators, $escaped);

        // ignore + & - for now, they'll get special treatment
        unset($operators['+'], $operators['-']);

        // dot can not just immediately follow a number; it can be confused
        // between decimal point, or calling a method on it, e.g. 42 .toString()

        $operators['.'] = '(?<![0-9]\s)\.';

        return $operators;
    }


    /**
     * We'll strip whitespace around certain keywords with regular expressions.
     * This will prepare the given array by escaping all characters.
     *
     * @param  string[] $keywords
     * @param  string   $delimiter
     * @return string[]
     */

    protected function getKeywordsForRegex(array $keywords, $delimiter = '/')
    {

        // escape keywords for use in regex
        $delimiter = array_fill(0, count($keywords), $delimiter);
        $escaped = array_map('preg_quote', $keywords, $delimiter);

        // add word boundaries
        array_walk($keywords, function ($value) {
            return '\b' . $value . '\b';
        });


        $keywords = array_combine($keywords, $escaped);

        return $keywords;
    }
}

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.