From d42607998482e36865c1d8a443f92a457f6b2f3b Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sat, 15 Mar 2014 15:11:12 -0300 Subject: Squashed 'mod/minify/' content from commit 5949018 git-subtree-dir: mod/minify git-subtree-split: 594901859d5afc1fa364322428bd7a6d5c6a3f96 --- vendors/min/lib/Minify/CommentPreserver.php | 90 +++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 vendors/min/lib/Minify/CommentPreserver.php (limited to 'vendors/min/lib/Minify/CommentPreserver.php') diff --git a/vendors/min/lib/Minify/CommentPreserver.php b/vendors/min/lib/Minify/CommentPreserver.php new file mode 100644 index 000000000..f56eb3461 --- /dev/null +++ b/vendors/min/lib/Minify/CommentPreserver.php @@ -0,0 +1,90 @@ + + */ +class Minify_CommentPreserver { + + /** + * String to be prepended to each preserved comment + * + * @var string + */ + public static $prepend = "\n"; + + /** + * String to be appended to each preserved comment + * + * @var string + */ + public static $append = "\n"; + + /** + * Process a string outside of C-style comments that begin with "/*!" + * + * On each non-empty string outside these comments, the given processor + * function will be called. The first "!" will be removed from the + * preserved comments, and the comments will be surrounded by + * Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append. + * + * @param string $content + * @param callback $processor function + * @param array $args array of extra arguments to pass to the processor + * function (default = array()) + * @return string + */ + public static function process($content, $processor, $args = array()) + { + $ret = ''; + while (true) { + list($beforeComment, $comment, $afterComment) = self::_nextComment($content); + if ('' !== $beforeComment) { + $callArgs = $args; + array_unshift($callArgs, $beforeComment); + $ret .= call_user_func_array($processor, $callArgs); + } + if (false === $comment) { + break; + } + $ret .= $comment; + $content = $afterComment; + } + return $ret; + } + + /** + * Extract comments that YUI Compressor preserves. + * + * @param string $in input + * + * @return array 3 elements are returned. If a YUI comment is found, the + * 2nd element is the comment and the 1st and 2nd are the surrounding + * strings. If no comment is found, the entire string is returned as the + * 1st element and the other two are false. + */ + private static function _nextComment($in) + { + if ( + false === ($start = strpos($in, '/*!')) + || false === ($end = strpos($in, '*/', $start + 3)) + ) { + return array($in, false, false); + } + $ret = array( + substr($in, 0, $start) + ,self::$prepend . '/*' . substr($in, $start + 3, $end - $start - 1) . self::$append + ); + $endChars = (strlen($in) - $end - 2); + $ret[] = (0 === $endChars) + ? '' + : substr($in, -$endChars); + return $ret; + } +} -- cgit v1.2.3