Mokuji /www/libs/components/WebLoader/CssLoader.php

Language PHP Lines 124
MD5 Hash cad18fd904ab12638700ed52a2b6ec61
Repository https://github.com/bazo/Mokuji.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

/**
 * Css loader
 *
 * @author Jan Marek
 * @license MIT
 */
class CssLoader extends WebLoader
{
	/** @var string */
	public $media;

	/** @var bool */
	public $absolutizeUrls = true;

	/**
	 * Make relative url absolute
	 * @param string $url
	 * @param string $quote
	 * @param string $file
	 * @param string $sourceUri
	 * @return string
	 */
	public static function absolutizeUrl($url, $quote, $file, $sourceUri) {
		// is already absolute
		//($url, $quote, $file, $sourceUri);
		if (@ereg("^([a-z]+:/)?/", $url)) return $url;
		$lastPos = strrpos($file, "/");
		$fileFolder = $lastPos === false ? "" : "/" . substr($file, 0, $lastPos);
		//$path = $sourceUri . $fileFolder . "/" . $url;
		$path = $sourceUri . "/" . $url;
		$pathPieces = explode("/", $path);
		$piecesOut = array();
		//var_dump($pathPieces);exit;
		foreach ($pathPieces as $piece) {
			if ($piece === ".") continue;

			if ($piece === "..") {
				array_pop($piecesOut);
				continue;
			}

			$piecesOut[] = $piece;
		}

		$out = implode("/", $piecesOut);

		if ($quote === '"') $out = addslashes($out);

		return $out;
	}

	private function absolutizeUrls($s, $file) {
		// thanks to kravco
		//var_dump($file);exit;
		$regexp = '~
			(?<![a-z])
			url\(                                     ## url(
				\s*                                   ##   optional whitespace
				([\'"])?                              ##   optional single/double quote
				(   (?: (?:\\\\.)+                    ##     escape sequences
					|   [^\'"\\\\,()\s]+              ##     safe characters
					|   (?(1)   (?!\1)[\'"\\\\,() \t] ##       allowed special characters
						|       ^                     ##       (none, if not quoted)
						)
					)*                                ##     (greedy match)
				)
				(?(1)\1)                              ##   optional single/double quote
				\s*                                   ##   optional whitespace
			\)                                        ## )
		~xs';

		return preg_replace_callback(
			$regexp,
			create_function(
				'$matches',
				'return "url(\'" . CssLoader::absolutizeUrl($matches[2], $matches[1], "' .
				addslashes($file) . '", "' . addslashes($this->sourceUri) .
				'") . "\')";'
			),
			$s
		);
	}

	/**
	 * Load file
	 * @param string $path
	 * @return string
	 */
	protected function loadFile($file) {
		$content = parent::loadFile($file);
		//var_dump($this->absolutizeUrls, !empty($this->sourceUri));exit;
		if ($this->absolutizeUrls && !empty($this->sourceUri)) {
			$content = $this->absolutizeUrls($content, $file);
		}

		return $content;
	}

	/**
	 * Filename of generated CSS file
	 * @param array $files
	 * @return string
	 */
	public function getGeneratedFilename(array $files = null)
	{
		return parent::getGeneratedFilename($files) . ".css";
	}
	
	/**
	 * Get link element
	 * @param string $source
	 * @return Html
	 */
	public function getElement($source)
	{
		return Html::el("link")
			->rel("stylesheet")
			->type("text/css")
			->media($this->media)
			->href($source);
	}
}
Back to Top