mirror of
https://github.com/mrclay/minify.git
synced 2025-08-06 14:16:28 +02:00
Tools for easier testing
This commit is contained in:
11
min_extras/README.txt
Normal file
11
min_extras/README.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
DO NOT leave this directory on a site in production. Some scripts within may be
|
||||
resource intensive and some allow file uploads.
|
||||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
This directory contains testing scripts and a few example applications built
|
||||
with the Minify library classes.
|
||||
|
||||
tools/
|
||||
Two utility web apps that upload a file, alter it, and send it back to the
|
||||
user. One applies HTTP encoding, the other minifies CSS/JS/HTML.
|
16
min_extras/config.php
Normal file
16
min_extras/config.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
// using same lib path and cache path specified in /min/config.php
|
||||
|
||||
require dirname(__FILE__) . '/../min/config.php';
|
||||
|
||||
set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
|
||||
|
||||
$minifyCachePath = isset($min_cachePath)
|
||||
? $min_cachePath
|
||||
: '';
|
||||
|
||||
function min_autoload($name) {
|
||||
require str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
|
||||
}
|
||||
spl_autoload_register('min_autoload');
|
97
min_extras/tools/minifyTextarea.php
Normal file
97
min_extras/tools/minifyTextarea.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
function getPost($key) {
|
||||
return get_magic_quotes_gpc()
|
||||
? stripslashes($_POST[$key])
|
||||
: $_POST[$key];
|
||||
}
|
||||
|
||||
if (isset($_POST['textIn'])) {
|
||||
require '../config.php';
|
||||
$textIn = str_replace("\r\n", "\n", getPost('textIn'));
|
||||
}
|
||||
|
||||
if (isset($_POST['method']) && $_POST['method'] === 'Minify and serve') {
|
||||
|
||||
$base = trim(getPost('base'));
|
||||
if ($base) {
|
||||
$textIn = preg_replace(
|
||||
'@(<head\\b[^>]*>)@i'
|
||||
,'$1<base href="' . htmlentities($base) . '" />'
|
||||
,$textIn
|
||||
);
|
||||
}
|
||||
|
||||
$sourceSpec['content'] = $textIn;
|
||||
$sourceSpec['id'] = 'foo';
|
||||
if (isset($_POST['minJs'])) {
|
||||
$sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin', 'minify');
|
||||
}
|
||||
if (isset($_POST['minCss'])) {
|
||||
$sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSS', 'minify');
|
||||
}
|
||||
$source = new Minify_Source($sourceSpec);
|
||||
Minify_Logger::setLogger(FirePHP::getInstance(true));
|
||||
try {
|
||||
Minify::serve('Files', array(
|
||||
'files' => $source
|
||||
,'contentType' => Minify::TYPE_HTML
|
||||
));
|
||||
} catch (Exception $e) {
|
||||
echo htmlspecialchars($e->getMessage());
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
$classes = array('Minify_HTML', 'Minify_CSS', 'JSMin', 'JSMinPlus');
|
||||
|
||||
if (isset($_POST['method']) && in_array($_POST['method'], $classes)) {
|
||||
|
||||
$args = array($textIn);
|
||||
if ($_POST['method'] === 'Minify_HTML') {
|
||||
$args[] = array(
|
||||
'cssMinifier' => array('Minify_CSS', 'minify')
|
||||
,'jsMinifier' => array('JSMin', 'minify')
|
||||
);
|
||||
}
|
||||
$func = array($_POST['method'], 'minify');
|
||||
$inOutBytes[0] = strlen($textIn);
|
||||
try {
|
||||
$textOut = call_user_func_array($func, $args);
|
||||
} catch (Exception $e) {
|
||||
echo htmlspecialchars($e->getMessage());
|
||||
exit;
|
||||
}
|
||||
$inOutBytes[1] = strlen($textOut);
|
||||
}
|
||||
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
?>
|
||||
<!DOCTYPE html><head><title>minifyTextarea</title></head>
|
||||
<?php
|
||||
if (isset($inOutBytes)) {
|
||||
echo "
|
||||
<table>
|
||||
<tr><th>Bytes in</th><td>{$inOutBytes[0]} (after line endings normalized to <code>\\n</code>)</td></tr>
|
||||
<tr><th>Bytes out</th><td>{$inOutBytes[1]} (reduced " . round(100 - (100 * $inOutBytes[1] / $inOutBytes[0])) . "%)</td></tr>
|
||||
</table>
|
||||
";
|
||||
}
|
||||
?>
|
||||
<form action="?2" method="post">
|
||||
<p><label>Content<br><textarea name="textIn" cols="80" rows="35" style="width:99%"><?php
|
||||
if (isset($textOut)) {
|
||||
echo htmlspecialchars($textOut);
|
||||
}
|
||||
?></textarea></label></p>
|
||||
<p>Minify with:
|
||||
<?php foreach ($classes as $minClass): ?>
|
||||
<input type="submit" name="method" value="<?php echo $minClass; ?>">
|
||||
<?php endForEach; ?>
|
||||
</p>
|
||||
<p>...or <input type="submit" name="method" value="Minify and serve"> this HTML to the browser. Also minify:
|
||||
<label>CSS <input type="checkbox" name="minCss" checked></label> :
|
||||
<label>JS <input type="checkbox" name="minJs" checked></label>.
|
||||
<label>Insert BASE element w/ href: <input type="text" name="base" size="20"></label>
|
||||
</p>
|
||||
</form>
|
164
min_extras/tools/minifyUrl.php
Normal file
164
min_extras/tools/minifyUrl.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
/**
|
||||
* Fetch and minify a URL (auto-detect HTML/JS/CSS)
|
||||
*/
|
||||
|
||||
function getPost($key) {
|
||||
if (! isset($_POST[$key])) {
|
||||
return null;
|
||||
}
|
||||
return get_magic_quotes_gpc()
|
||||
? stripslashes($_POST[$key])
|
||||
: $_POST[$key];
|
||||
}
|
||||
|
||||
function sniffType($headers) {
|
||||
$charset = 'utf-8';
|
||||
$type = null;
|
||||
$headers = "\n\n" . implode("\n\n", $headers) . "\n\n";
|
||||
if (preg_match(
|
||||
'@\\n\\nContent-Type: *([\\w/\\+-]+)( *; *charset *= *([\\w-]+))? *\\n\\n@i'
|
||||
,$headers
|
||||
,$m)) {
|
||||
$sentType = $m[1];
|
||||
if (isset($m[3])) {
|
||||
$charset = $m[3];
|
||||
}
|
||||
if (preg_match('@^(?:text|application)/(?:x-)?(?:java|ecma)script$@i', $sentType)) {
|
||||
$type = 'application/x-javascript';
|
||||
} elseif (preg_match('@^(?:text|application)/(?:html|xml|xhtml+xml)$@i', $sentType, $m)) {
|
||||
$type = 'text/html';
|
||||
} elseif ($sentType === 'text/css') {
|
||||
$type = $sentType;
|
||||
}
|
||||
}
|
||||
return array(
|
||||
'minify' => $type
|
||||
,'sent' => $sentType
|
||||
,'charset' => $charset
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($_POST['url'])) {
|
||||
|
||||
require '../config.php';
|
||||
|
||||
$url = trim(getPost('url'));
|
||||
$ua = trim(getPost('ua'));
|
||||
$cook = trim(getPost('cook'));
|
||||
|
||||
if (! preg_match('@^https?://@', $url)) {
|
||||
die('HTTP(s) only.');
|
||||
}
|
||||
|
||||
$httpOpts = array(
|
||||
'max_redirects' => 0
|
||||
,'timeout' => 3
|
||||
);
|
||||
if ($ua !== '') {
|
||||
$httpOpts['user_agent'] = $ua;
|
||||
}
|
||||
if ($cook !== '') {
|
||||
$httpOpts['header'] = "Cookie: {$cook}\r\n";
|
||||
}
|
||||
$ctx = stream_context_create(array(
|
||||
'http' => $httpOpts
|
||||
));
|
||||
|
||||
// fetch
|
||||
if (! ($fp = @fopen($url, 'r', false, $ctx))) {
|
||||
die('Couldn\'t open URL.');
|
||||
}
|
||||
$meta = stream_get_meta_data($fp);
|
||||
$content = stream_get_contents($fp);
|
||||
fclose($fp);
|
||||
|
||||
// get type info
|
||||
$type = sniffType($meta['wrapper_data']);
|
||||
if (! $type['minify']) {
|
||||
die('Unrecognized Content-Type: ' . $type['sent']);
|
||||
}
|
||||
|
||||
if ($type['minify'] === 'text/html'
|
||||
&& isset($_POST['addBase'])
|
||||
&& ! preg_match('@<base\\b@i', $content)) {
|
||||
$content = preg_replace(
|
||||
'@(<head\\b[^>]*>)@i'
|
||||
,'$1<base href="' . htmlentities($url) . '" />'
|
||||
,$content
|
||||
);
|
||||
}
|
||||
|
||||
$sourceSpec['content'] = $content;
|
||||
$sourceSpec['id'] = 'foo';
|
||||
|
||||
if ($type['minify'] === 'text/html') {
|
||||
if (isset($_POST['minJs'])) {
|
||||
$sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin', 'minify');
|
||||
}
|
||||
if (isset($_POST['minCss'])) {
|
||||
$sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSS', 'minify');
|
||||
}
|
||||
}
|
||||
|
||||
$source = new Minify_Source($sourceSpec);
|
||||
|
||||
$sendType = 'text/plain';
|
||||
if ($type['minify'] === 'text/html' && ! isset($_POST['asText'])) {
|
||||
$sendType = $type['sent'];
|
||||
}
|
||||
if ($type['charset']) {
|
||||
$sendType .= ';charset=' . $type['charset'];
|
||||
}
|
||||
header('Content-Type: ' . $sendType);
|
||||
// using combine instead of serve because it allows us to specify a
|
||||
// Content-Type like application/xhtml+xml IF we need to
|
||||
try {
|
||||
echo Minify::combine(array($source), array(
|
||||
'contentType' => $type['minify']
|
||||
));
|
||||
} catch (Exception $e) {
|
||||
header('Content-Type: text/html;charset=utf-8');
|
||||
echo htmlspecialchars($e->getMessage());
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
|
||||
$ua = get_magic_quotes_gpc()
|
||||
? stripslashes($_SERVER['HTTP_USER_AGENT'])
|
||||
: $_SERVER['HTTP_USER_AGENT'];
|
||||
|
||||
?>
|
||||
<!DOCTYPE html><head><title>Minify URL</title></head>
|
||||
|
||||
<p><strong>Warning! Please do not place this application on a public site.</strong> This should be used
|
||||
only for testing.</p>
|
||||
|
||||
<h1>Fetch and Minify a URL</h1>
|
||||
<p>This tool will retrieve the contents of a URL and minify it.
|
||||
The fetched resource Content-Type will determine the minifier used.</p>
|
||||
|
||||
<form action="?2" method="post">
|
||||
<p><label>URL: <input type="text" name="url" size="60"></label></p>
|
||||
<p><input type="submit" value="Fetch and minify"></p>
|
||||
|
||||
<fieldset><legend>HTML options</legend>
|
||||
<p>If the resource above is sent with an (x)HTML Content-Type, the following options will apply:</p>
|
||||
<ul>
|
||||
<li><label><input type="checkbox" name="asText" checked> Return plain text (o/w send the original content type)</label>
|
||||
<li><label><input type="checkbox" name="minCss" checked> Minify CSS</label>
|
||||
<li><label><input type="checkbox" name="minJs" checked> Minify JS</label>
|
||||
<li><label><input type="checkbox" name="addBase" checked> Add BASE element (if not present)</label>
|
||||
</ul>
|
||||
</fieldset>
|
||||
|
||||
<fieldset><legend>Retreival options</legend>
|
||||
<ul>
|
||||
<li><label>User-Agent: <input type="text" name="ua" size="60" value="<?php echo htmlspecialchars($ua); ?>"></label>
|
||||
<li><label>Cookie: <input type="text" name="cook" size="60"></label>
|
||||
</ul>
|
||||
</fieldset>
|
||||
|
||||
</form>
|
58
min_extras/tools/testRewriteUri.php
Normal file
58
min_extras/tools/testRewriteUri.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
header('Content-Type: text/html;charset=utf-8');
|
||||
|
||||
function h($str) { return htmlspecialchars($str, ENT_QUOTES); }
|
||||
|
||||
function getPost($name, $default = '') { return isset($_POST[$name]) ? $_POST[$name] : $default; }
|
||||
|
||||
function getInput($name, $default = '', $size = 50) {
|
||||
$val = h(isset($_POST[$name]) ? $_POST[$name] : $default);
|
||||
return "<input type='text' name='{$name}' value='{$val}' size='{$size}' />";
|
||||
}
|
||||
|
||||
// validate user POST (no arrays and fix slashes)
|
||||
if (! empty($_POST)) {
|
||||
foreach ($_POST as $name => $val) {
|
||||
if (! is_string($val)) {
|
||||
unset($_POST[$name]);
|
||||
continue;
|
||||
}
|
||||
if (get_magic_quotes_gpc()) {
|
||||
$_POST[$name] = stripslashes($val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$defaultCurrentDir = dirname(__FILE__);
|
||||
$defaultDocRoot = realpath($_SERVER['DOCUMENT_ROOT']);
|
||||
$defaultSymLink = '//symlinkPath';
|
||||
$defaultSymTarget = ($defaultCurrentDir[0] === '/') ? '/tmp' : 'C:\\WINDOWS\\Temp';
|
||||
$defaultCss = "url(hello.gif)\nurl(../hello.gif)\nurl(../../hello.gif)\nurl(up/hello.gif)";
|
||||
|
||||
$out = '';
|
||||
|
||||
if (isset($_POST['css'])) {
|
||||
require '../config.php';
|
||||
$symlinks = array();
|
||||
if ('' !== ($target = getPost('symTarget'))) {
|
||||
$symlinks[getPost('symLink')] = $target;
|
||||
}
|
||||
$css = Minify_CSS_UriRewriter::rewrite(
|
||||
getPost('css')
|
||||
, getPost('currentDir')
|
||||
, getPost('docRoot')
|
||||
, $symlinks
|
||||
);
|
||||
$out = "<hr /><pre><code>" . h($css) . '</code></pre>';
|
||||
}
|
||||
|
||||
?>
|
||||
<h1>Test <code>Minify_CSS_UriRewriter::rewrite()</code></h1>
|
||||
<form action="" method="post">
|
||||
<div><label>document root: <?php echo getInput('docRoot', $defaultDocRoot); ?></label></div>
|
||||
<div><label>symlink: <?php echo getInput('symLink', $defaultSymLink); ?> => <?php echo getInput('symTarget', $defaultSymTarget); ?></label></div>
|
||||
<div><label>current directory: <?php echo getInput('currentDir', $defaultCurrentDir); ?></label></div>
|
||||
<p><label>input CSS: <textarea name="css" cols="80" rows="5"><?php echo h(getPost('css', $defaultCss)); ?></textarea></label></p>
|
||||
<p><input type="submit" value="rewrite()" /></p>
|
||||
</form>
|
||||
<?php echo $out; ?>
|
Reference in New Issue
Block a user