aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/javascriptminifier.php
blob: 35a57a631152c9e28c1e1ed04831f16e38e86cf9 (plain)
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
<?php namespace HashOver;

// Copyright (C) 2015 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class JavaScriptMinifier
{
	// Array for locking minification
	protected $lock = array (
		'status' => false,
		'char' => ''
	);

	// JavaScript minification function
	public function minify ($js, $level = 4)
	{
		if ($level <= 0) {
			return $js;
		}

		if ($level >= 1) {
			// Remove single-line code comments
			$js = preg_replace ('/^[\t ]*?\/\/.*\s?/m', '', $js);

			// Remove end-of-line code comments
			$js = preg_replace ('/([\s;})]+)\/\/.*/m', '\\1', $js);

			// Remove multi-line code comments
			$js = preg_replace ('/\/\*[\s\S]*?\*\//', '', $js);
		}

		if ($level >= 2) {
			// Remove whitespace
			$js = preg_replace ('/^\s*/m', '', $js);

			// Replace multiple tabs with a single space
			$js = preg_replace ('/\t+/m', ' ', $js);
		}

		if ($level >= 3) {
			// Remove newlines
			$js = preg_replace ('/[\r\n]+/', '', $js);
		}

		if ($level >= 4) {
			// Split input JavaScript by single and double quotes
			$js_substrings = preg_split ('/([\'"])/', $js, -1, PREG_SPLIT_DELIM_CAPTURE);

			// Empty variable for minified JavaScript
			$js = '';

			foreach ($js_substrings as $substring) {
				// Check if substring is split delimiter
				if ($substring === '\'' or $substring === '"') {
					// If so, check whether minification is unlocked
					if ($this->lock['status'] === false) {
						// If so, lock it and set lock character
						$this->lock['status'] = true;
						$this->lock['char'] = $substring;
					} else {
						// If not, check if substring is lock character
						if ($substring === $this->lock['char']) {
							// If so, unlock minification
							$this->lock['status'] = false;
							$this->lock['char'] = '';
						}
					}

					// Add substring to minified output
					$js .= $substring;

					continue;
				}

				// Minify current substring if minification is unlocked
				if ($this->lock['status'] === false) {
					// Remove unnecessary semicolons
					$substring = str_replace (';}', '}', $substring);

					// Remove spaces round operators
					$substring = preg_replace ('/ *([<>=+\-!\|{(},;&:?]+) */', '\\1', $substring);
				}

				// Add substring to minified output
				$js .= $substring;
			}
		}

		// Get URL add "unminified" URL query
		$unminified_url = 'http' . (isset ($_SERVER['HTTPS']) ? 's' : '') . '://';
		$unminified_url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		$unminified_url .= '&hashover-unminified';

		// Copyright notice and URL to unminified code
		$copyright = array (
			'// Copyright (C) 2015 Jacob Barkdull',
			'// Under the terms of the GNU Affero General Public License.',
			'//',
			'// Non-minified JavaScript:',
			'//',
			'//     ' . $unminified_url . PHP_EOL . PHP_EOL
		);

		// Return final minified JavaScript
		return implode (PHP_EOL, $copyright) . trim ($js);
	}
}