Author: Stephen Clay (steve@mrclay.org)
Author: Simon Schick (simonsimcity@gmail.com)
function test_Minify_ImportProcessor()
{
    global $thisDir;
    $linDir = $thisDir . '/_test_files/importProcessor';
    $expected = file_get_contents($linDir . '/css/output.css');
    $actual = Minify_ImportProcessor::process($linDir . '/css/input.css');
    $passed = assertTrue($expected === $actual, 'ImportProcessor');
    if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
        echo "\n---Output: " . countBytes($actual) . " bytes\n\n{$actual}\n\n";
        if (!$passed) {
            echo "---Expected: " . countBytes($expected) . " bytes\n\n{$expected}\n\n\n";
        }
    }
    $expectedIncludes = array(realpath($linDir . '/css/input.css'), realpath($linDir . '/css/adjacent.css'), realpath($linDir . '/../css/styles.css'), realpath($linDir . '/css/1/tv.css'), realpath($linDir . '/css/1/adjacent.css'), realpath($linDir . '/lib/css/example.css'));
    $passed = assertTrue($expectedIncludes === Minify_ImportProcessor::$filesIncluded, 'ImportProcessor : included right files in right order');
}
function test_Minify_ImportProcessor()
{
    global $thisDir;
    $linDir = $thisDir . '/_test_files/importProcessor';
    $testFilesUri = substr(realpath($thisDir . '/_test_files'), strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
    $testFilesUri = str_replace('\\', '/', $testFilesUri);
    $expected = str_replace('%TEST_FILES_URI%', $testFilesUri, file_get_contents($linDir . '/output.css'));
    $actual = Minify_ImportProcessor::process($linDir . '/input.css');
    $passed = assertTrue($expected === $actual, 'ImportProcessor');
    if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
        echo "\n---Output: " . strlen($actual) . " bytes\n\n{$actual}\n\n";
        if (!$passed) {
            echo "---Expected: " . strlen($expected) . " bytes\n\n{$expected}\n\n\n";
        }
    }
    $expectedIncludes = array(realpath($linDir . '/input.css'), realpath($linDir . '/adjacent.css'), realpath($linDir . '/../css/styles.css'), realpath($linDir . '/1/tv.css'), realpath($linDir . '/1/adjacent.css'));
    $passed = assertTrue($expectedIncludes === Minify_ImportProcessor::$filesIncluded, 'ImportProcessor : included right files in right order');
}
 private function _importCB($m)
 {
     $url = $m[1];
     $mediaList = preg_replace('/\\s+/', '', $m[2]);
     if (strpos($url, '://') > 0) {
         // protocol, leave in place for CSS, comment for JS
         return self::$_isCss ? $m[0] : "/* Minify_ImportProcessor will not include remote content */";
     }
     if ('/' === $url[0]) {
         // protocol-relative or root path
         $url = ltrim($url, '/');
         $file = realpath($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
     } else {
         // relative to current path
         $file = $this->_currentDir . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
     }
     $obj = new Minify_ImportProcessor(dirname($file));
     $content = $obj->_getContent($file);
     if ('' === $content) {
         // failed. leave in place for CSS, comment for JS
         return self::$_isCss ? $m[0] : "/* Minify_ImportProcessor could not fetch '{$file}' */";
     }
     return !self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList) ? $content : "@media {$mediaList} {\n{$content}\n}\n";
 }
Example #4
0
 /**
  * Get content
  *
  * @return string
  */
 public function getContent()
 {
     if (isset($this->minifyOptions['processCssImports']) && $this->minifyOptions['processCssImports']) {
         w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/ImportProcessor.php');
         $content = Minify_ImportProcessor::process($this->filepath);
     } else {
         $content = null !== $this->filepath ? file_get_contents($this->filepath) : (null !== $this->_content ? $this->_content : call_user_func($this->_getContentFunc, $this->_id));
     }
     // remove UTF-8 BOM if present
     return pack("CCC", 0xef, 0xbb, 0xbf) === substr($content, 0, 3) ? substr($content, 3) : $content;
 }
	/**
	 * This method parses the output content and saves any found css files or inline code
	 * into the "css" class property. The output content is cleaned up of the found results.
	 *
	 * @return void
	 */
	protected function getCSSfiles() {
		// filter pattern for the inDoc styles (fetches the content)
		$filterInDocumentPattern = '/' .
			'<style.*?>' . // This expression removes the opening style tag
			'(?:.*?\/\*<!\[CDATA\[\*\/)?' . // and the optionally prefixed CDATA string.
			'\s*(.*?)' . // We save the pure css content,
			'(?:\s*\/\*\]\]>\*\/)?' . // remove the possible closing CDATA string
			'\s*<\/style>' . // and closing style tag
			'/is';

		// parse all available css code inside link and style tags
		$cssTags = array();
		$pattern = '/' .
			'<(link|sty)' . // Parse any link and style tags.
			'(?=.+?(?:media="(.*?)"|>))' . // Fetch the media attribute
			'(?=.+?(?:href="(.*?)"|>))' . // and the href attribute
			'(?=.+?(?:rel="(.*?)"|>))' . // and the rel attribute
			'(?=.+?(?:title="(.*?)"|>))' . // and the title attribute of the tag.
			'(?:[^>]+?\.css[^>]+?\/?>' . // Continue parsing from \1 to the closing tag.
			'|le[^>]*?>[^>]+?<\/style>)\s*' .
			'/is';

		preg_match_all($pattern, $GLOBALS['TSFE']->content, $cssTags);
		if (!count($cssTags[0])) {
			return;
		}

		// remove any css code inside the output content
		$GLOBALS['TSFE']->content = preg_replace($pattern, '', $GLOBALS['TSFE']->content, count($cssTags[0]));

		// parse matches
		$amountOfResults = count($cssTags[0]);
		for ($i = 0; $i < $amountOfResults; ++$i) {
			$content = '';

			// get media attribute (all as default if it's empty)
			$media = (trim($cssTags[2][$i]) === '') ? 'all' : $cssTags[2][$i];
			$media = implode(',', array_map('trim', explode(',', $media)));

			// get rel attribute (stylesheet as default if it's empty)
			$relation = (trim($cssTags[4][$i]) === '') ? 'stylesheet' : $cssTags[4][$i];

			// get source attribute
			$source = $cssTags[3][$i];

			// get title attribute
			$title = trim($cssTags[5][$i]);

			// add basic entry
			$this->css[$relation][$media][$i]['minify-ignore'] = FALSE;
			$this->css[$relation][$media][$i]['compress-ignore'] = FALSE;
			$this->css[$relation][$media][$i]['merge-ignore'] = FALSE;
			$this->css[$relation][$media][$i]['file'] = $source;
			$this->css[$relation][$media][$i]['content'] = '';
			$this->css[$relation][$media][$i]['basename'] = '';
			$this->css[$relation][$media][$i]['title'] = $title;

			// styles which are added inside the document must be parsed again
			// to fetch the pure css code
			$cssTags[1][$i] = ($cssTags[1][$i] === 'sty' ? 'style' : $cssTags[1][$i]);
			if ($cssTags[1][$i] === 'style') {
				$cssContent = array();
				preg_match_all($filterInDocumentPattern, $cssTags[0][$i], $cssContent);

				// we doesn't need to continue if it was an empty style tag
				if ($cssContent[1][0] === '') {
					unset($this->css[$relation][$media][$i]);
					continue;
				}

				// save the content into a temporary file
				$hash = md5($cssContent[1][0]);
				$source = $this->tempDirectories['temp'] . 'inDocument-' . $hash;
				$tempFile = $source . '.css';
				if (!file_exists($source . '.css')) {
					t3lib_div::writeFile($tempFile, $cssContent[1][0]);
				}

				// try to resolve any @import occurrences
				/** @noinspection PhpUndefinedClassInspection */
				$content = Minify_ImportProcessor::process($tempFile);
				$this->css[$relation][$media][$i]['file'] = $tempFile;
				$this->css[$relation][$media][$i]['content'] = $content;
				$this->css[$relation][$media][$i]['basename'] = basename($source);
			} elseif ($source !== '') {
				// try to fetch the content of the css file
				$file = ($source{0} === '/' ? substr($source, 1) : $source);
				if ($GLOBALS['TSFE']->absRefPrefix !== '' && strpos($file, $GLOBALS['TSFE']->absRefPrefix) === 0) {
					$file = substr($file, strlen($GLOBALS['TSFE']->absRefPrefix) - 1);
				}
				if (file_exists(PATH_site . $file)) {
					$content = Minify_ImportProcessor::process(PATH_site . $file);
				} else {
					$tempFile = $this->getExternalFile($source);
					$content = Minify_ImportProcessor::process($tempFile);
				}

				// ignore this file if the content could not be fetched
				if ($content == '') {
					$this->css[$relation][$media][$i]['minify-ignore'] = TRUE;
					$this->css[$relation][$media][$i]['compress-ignore'] = TRUE;
					$this->css[$relation][$media][$i]['merge-ignore'] = TRUE;
					continue;
				}

				// check if the file should be ignored for some processes
				if ($this->extConfig['css.']['minify.']['ignore'] !== '') {
					if (preg_match($this->extConfig['css.']['minify.']['ignore'], $source)) {
						$this->css[$relation][$media][$i]['minify-ignore'] = TRUE;
					}
				}

				if ($this->extConfig['css.']['compress.']['ignore'] !== '') {
					if (preg_match($this->extConfig['css.']['compress.']['ignore'], $source)) {
						$this->css[$relation][$media][$i]['compress-ignore'] = TRUE;
					}
				}

				if ($this->extConfig['css.']['merge.']['ignore'] !== '') {
					if (preg_match($this->extConfig['css.']['merge.']['ignore'], $source)) {
						$this->css[$relation][$media][$i]['merge-ignore'] = TRUE;
					}
				}

				// set the css file with it's content
				$this->css[$relation][$media][$i]['content'] = $content;
			}

			// get base name for later usage
			// base name without file prefix and prefixed hash of the content
			$filename = basename($source);
			$hash = md5($content);
			$this->css[$relation][$media][$i]['basename'] =
				substr($filename, 0, strrpos($filename, '.')) . '-' . $hash;
		}
	}