PageRenderTime 92ms CodeModel.GetById 34ms RepoModel.GetById 8ms app.codeStats 0ms

/min_extras/tools/minifyTextarea.php

https://github.com/soitun/minify
PHP | 183 lines | 154 code | 17 blank | 12 comment | 26 complexity | 3ad63366abbfb7eb2c9dac0833cf6c0a MD5 | raw file
  1. <?php
  2. die('Disabled: use this only for testing');
  3. $app = (require __DIR__ . '/../../bootstrap.php');
  4. /* @var \Minify\App $app */
  5. // use FirePHP if not already setup
  6. if (!$app->config->errorLogger) {
  7. $app->config->errorLogger = true;
  8. }
  9. $app->cache = new Minify_Cache_Null();
  10. $env = $app->env;
  11. function h($txt)
  12. {
  13. return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8');
  14. }
  15. if ($env->post('textIn')) {
  16. $textIn = str_replace("\r\n", "\n", $env->post('textIn'));
  17. }
  18. if ($env->post('method') === 'Minify and serve') {
  19. $base = trim($env->post('base'));
  20. if ($base) {
  21. $textIn = preg_replace(
  22. '@(<head\\b[^>]*>)@i',
  23. '$1<base href="' . h($base) . '" />',
  24. $textIn
  25. );
  26. }
  27. $sourceSpec['content'] = $textIn;
  28. $sourceSpec['id'] = 'foo';
  29. if (isset($_POST['minJs'])) {
  30. $sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin\\JSMin', 'minify');
  31. }
  32. if (isset($_POST['minCss'])) {
  33. $sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSSmin', 'minify');
  34. }
  35. $source = new Minify_Source($sourceSpec);
  36. $controller = new Minify_Controller_Files($env, $app->sourceFactory, $app->logger);
  37. try {
  38. $app->minify->serve($controller, array(
  39. 'files' => $source,
  40. 'contentType' => Minify::TYPE_HTML,
  41. ));
  42. } catch (Exception $e) {
  43. echo h($e->getMessage());
  44. }
  45. exit;
  46. }
  47. $tpl = array();
  48. $tpl['classes'] = array('Minify_HTML', 'JSMin\\JSMin', 'Minify_CSS', 'Minify_Lines');
  49. if (in_array($env->post('method'), $tpl['classes'])) {
  50. $args = array($textIn);
  51. if ($env->post('method') === 'Minify_HTML') {
  52. $args[] = array(
  53. 'cssMinifier' => array('Minify_CSSmin', 'minify')
  54. ,'jsMinifier' => array('JSMin\\JSMin', 'minify')
  55. );
  56. }
  57. $func = array($env->post('method'), 'minify');
  58. $tpl['inBytes'] = strlen($textIn);
  59. $startTime = microtime(true);
  60. try {
  61. $tpl['output'] = call_user_func_array($func, $args);
  62. } catch (Exception $e) {
  63. $tpl['exceptionMsg'] = getExceptionMsg($e, $textIn);
  64. $tpl['output'] = $textIn;
  65. sendPage($tpl);
  66. }
  67. $tpl['time'] = microtime(true) - $startTime;
  68. $tpl['outBytes'] = strlen($tpl['output']);
  69. }
  70. sendPage($tpl);
  71. /**
  72. * @param Exception $e
  73. * @param string $input
  74. * @return string HTML
  75. */
  76. function getExceptionMsg(Exception $e, $input)
  77. {
  78. $msg = "<p>" . h($e->getMessage()) . "</p>";
  79. if (0 === strpos(get_class($e), 'JSMin_Unterminated')
  80. && preg_match('~byte (\d+)~', $e->getMessage(), $m)) {
  81. $msg .= "<pre>";
  82. if ($m[1] > 200) {
  83. $msg .= h(substr($input, ($m[1] - 200), 200));
  84. } else {
  85. $msg .= h(substr($input, 0, $m[1]));
  86. }
  87. $highlighted = isset($input[$m[1]]) ? h($input[$m[1]]) : '&#9220;';
  88. if ($highlighted === "\n") {
  89. $highlighted = "&#9166;\n";
  90. }
  91. $msg .= "<span style='background:#c00;color:#fff'>$highlighted</span>";
  92. $msg .= h(substr($input, $m[1] + 1, 200)) . "</span></pre>";
  93. }
  94. return $msg;
  95. }
  96. /**
  97. * Draw page
  98. *
  99. * @param array $vars
  100. */
  101. function sendPage($vars)
  102. {
  103. header('Content-Type: text/html; charset=utf-8'); ?>
  104. <!DOCTYPE html><head><title>minifyTextarea</title></head>
  105. <p><strong>Warning! Please do not place this application on a public site.</strong> This should be used only for testing.</p>
  106. <?php
  107. if (isset($vars['exceptionMsg'])) {
  108. echo $vars['exceptionMsg'];
  109. }
  110. if (isset($vars['time'])) {
  111. echo "
  112. <table>
  113. <tr><th>Bytes in</th><td>{$vars['inBytes']} (after line endings normalized to <code>\\n</code>)</td></tr>
  114. <tr><th>Bytes out</th><td>{$vars['outBytes']} (reduced " . round(100 - (100 * $vars['outBytes'] / $vars['inBytes'])) . "%)</td></tr>
  115. <tr><th>Time (s)</th><td>" . round($vars['time'], 5) . "</td></tr>
  116. </table>
  117. ";
  118. } ?>
  119. <form action="?2" method="post">
  120. <p><label>Content<br><textarea name="textIn" cols="80" rows="35" style="width:99%"><?php
  121. if (isset($vars['output'])) {
  122. echo h($vars['output']);
  123. } ?></textarea></label></p>
  124. <p>Minify with:
  125. <?php foreach ($vars['classes'] as $minClass): ?>
  126. <input type="submit" name="method" value="<?php echo $minClass; ?>">
  127. <?php endforeach; ?>
  128. </p>
  129. <p>...or <input type="submit" name="method" value="Minify and serve"> this HTML to the browser. Also minify:
  130. <label>CSS <input type="checkbox" name="minCss" checked></label> :
  131. <label>JS <input type="checkbox" name="minJs" checked></label>.
  132. <label>Insert BASE element w/ href: <input type="text" name="base" size="20"></label>
  133. </p>
  134. </form>
  135. <?php if (isset($vars['selectByte'])) { ?>
  136. <script>
  137. function selectText(el, begin, end) {
  138. var len = el.value.length;
  139. end = end || len;
  140. if (begin == null) {
  141. el.select();
  142. } else {
  143. if (el.setSelectionRange) {
  144. el.setSelectionRange(begin, end);
  145. } else {
  146. if (el.createTextRange) {
  147. var tr = el.createTextRange()
  148. ,c = "character";
  149. tr.moveStart(c, begin);
  150. tr.moveEnd(c, end - len);
  151. tr.select();
  152. } else {
  153. el.select();
  154. }
  155. }
  156. }
  157. el.focus();
  158. }
  159. window.onload = function () {
  160. var ta = document.querySelector('textarea[name="textIn"]');
  161. selectText(ta, <?= $vars['selectByte'] ?>, <?= ($vars['selectByte'] + 1) ?>);
  162. };
  163. </script>
  164. <?php }
  165. exit;
  166. }