PluginProbe
WP Super Minify • Minify, Compress and Cache HTML, CSS & JavaScript / trunk
WP Super Minify • Minify, Compress and Cache HTML, CSS & JavaScript vtrunk
trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.4 1.5 1.5.1 1.6 2.0 2.0.1
wp-super-minify / includes / min / lib / Minify / CommentPreserver.php

CommentPreserver.php in WP Super Minify • Minify, Compress and Cache HTML, CSS & JavaScript trunk, at includes/min/lib/Minify/CommentPreserver.php

88 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Minify_CommentPreserver
4 * @package Minify
5 */
6
7 /**
8 * Process a string in pieces preserving C-style comments that begin with "/*!"
9 *
10 * @package Minify
11 * @author Stephen Clay <steve@mrclay.org>
12 */
13 class Minify_CommentPreserver
14 {
15
16 /**
17 * String to be prepended to each preserved comment
18 *
19 * @var string
20 */
21 public static $prepend = "\n";
22
23 /**
24 * String to be appended to each preserved comment
25 *
26 * @var string
27 */
28 public static $append = "\n";
29
30 /**
31 * Process a string outside of C-style comments that begin with "/*!"
32 *
33 * On each non-empty string outside these comments, the given processor
34 * function will be called. The comments will be surrounded by
35 * Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append.
36 *
37 * @param string $content
38 * @param callback $processor function
39 * @param array $args array of extra arguments to pass to the processor
40 * function (default = array())
41 * @return string
42 */
43 public static function process($content, $processor, $args = array())
44 {
45 $ret = '';
46 while (true) {
47 list($beforeComment, $comment, $afterComment) = self::_nextComment($content);
48 if ('' !== $beforeComment) {
49 $callArgs = $args;
50 array_unshift($callArgs, $beforeComment);
51 $ret .= call_user_func_array($processor, $callArgs);
52 }
53 if (false === $comment) {
54 break;
55 }
56 $ret .= $comment;
57 $content = $afterComment;
58 }
59
60 return $ret;
61 }
62
63 /**
64 * Extract comments that YUI Compressor preserves.
65 *
66 * @param string $in input
67 *
68 * @return array 3 elements are returned. If a YUI comment is found, the
69 * 2nd element is the comment and the 1st and 3rd are the surrounding
70 * strings. If no comment is found, the entire string is returned as the
71 * 1st element and the other two are false.
72 */
73 private static function _nextComment($in)
74 {
75 if (false === ($start = strpos($in, '/*!')) || false === ($end = strpos($in, '*/', $start + 3))) {
76 return array($in, false, false);
77 }
78
79 $beforeComment = substr($in, 0, $start);
80 $comment = self::$prepend . '/*!' . substr($in, $start + 3, $end - $start - 1) . self::$append;
81
82 $endChars = (strlen($in) - $end - 2);
83 $afterComment = (0 === $endChars) ? '' : substr($in, -$endChars);
84
85 return array($beforeComment, $comment, $afterComment);
86 }
87 }
88