Skip to content

Commit 6ef872e

Browse files
author
ju1ius
committed
improved URL handling
1 parent d3af262 commit 6ef872e

File tree

5 files changed

+86
-20
lines changed

5 files changed

+86
-20
lines changed

CSSParser.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,21 @@ public function parseFile($sPath, $aLoadedFiles=array())
8585
if(!$this->aOptions['base_url']) {
8686
$this->aOptions['base_url'] = dirname($sPath);
8787
}
88-
if($this->aOptions['absolute_urls']) {
88+
if($this->aOptions['absolute_urls'] && !CSSUrlUtils::isAbsUrl($this->aOptions['base_url'])) {
8989
$this->aOptions['base_url'] = realpath($this->aOptions['base_url']);
9090
}
9191
$this->sImportMode = self::IMPORT_FILE;
9292
$sPath = realpath($sPath);
9393
$aLoadedFiles[] = $sPath;
9494
$this->aLoadedFiles = array_merge($this->aLoadedFiles, $aLoadedFiles);
9595
$sCss = file_get_contents($sPath);
96-
var_dump($sPath);
97-
9896
return $this->parseString($sCss);
9997
}
10098

10199
public function parseURL($sPath, $aLoadedFiles=array())
102100
{
103101
if(!$this->aOptions['base_url']) {
104-
$this->aOptions['base_url'] = dirname($sPath);
102+
$this->aOptions['base_url'] = CSSUrlUtils::dirname($sPath);
105103
}
106104
$this->sImportMode = self::IMPORT_URL;
107105
$aLoadedFiles[] =$sPath;
@@ -117,7 +115,7 @@ public function parseURL($sPath, $aLoadedFiles=array())
117115

118116

119117
public function parseString($sString, $sCharset=null) {
120-
echo "First 4 bytes: " . CSSCharsetUtils::printBytes($sString, 4) . PHP_EOL;
118+
//echo "First 4 bytes: " . CSSCharsetUtils::printBytes($sString, 4) . PHP_EOL;
121119
if(!$sCharset) {
122120
// detect charset from BOM and/or @charset rule
123121
$sCharset = CSSCharsetUtils::detectCharset($sString);

lib/CSSUrlUtils.php

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44

55
class CSSUrlUtils {
66

7-
static function loadURL($sURL) {
7+
/**
8+
* Requests the contents of an URL
9+
*
10+
* @param string $sURL the URL to fetch
11+
* @returns array an array in the form:
12+
* 'charset' => the charset of the response as specified by the
13+
* HTTP Content-Type header, if specified
14+
* 'response' => the response body
15+
**/
16+
static public function loadURL($sURL) {
817
$rCurl = curl_init();
918
curl_setopt($rCurl, CURLOPT_URL, $sURL);
1019
//curl_setopt($rCurl, CURLOPT_HEADER, true);
@@ -21,7 +30,7 @@ static function loadURL($sURL) {
2130
'response' => $mResponse
2231
);
2332
if($aInfos['content_type']) {
24-
if (preg_match('/charset=([a-zA-Z0-9-]*)/', $aInfos['content_type'], $aMatches)) {
33+
if(preg_match('/charset=([a-zA-Z0-9-]*)/', $aInfos['content_type'], $aMatches)) {
2534
$aResult['charset'] = $aMatches[0];
2635
}
2736
}
@@ -31,12 +40,11 @@ static function loadURL($sURL) {
3140
/**
3241
* CSSUrlUtils::joinPaths( string $head, string $tail [, string $...] )
3342
*
34-
* @param $head string the head component of the path
35-
* @param $tail string at least one path component
36-
* @returns string the resulting path
43+
* @param string $head the head component of the path
44+
* @param string $tail at least one path component
45+
* @returns string the resulting path
3746
**/
38-
static function joinPaths()
39-
{
47+
static public function joinPaths() {
4048
$num_args = func_num_args();
4149
if($num_args < 1) return '';
4250
$args = func_get_args();
@@ -54,12 +62,10 @@ static function joinPaths()
5462
/**
5563
* Returns boolean based on whether given path is absolute or not.
5664
*
57-
* @static
58-
* @access public
5965
* @param string $path Given path
60-
* @return boolean True if the path is absolute, false if it is not
66+
* @return boolean True if the path is absolute, false if it is not
6167
*/
62-
static function isAbsPath($sPath) {
68+
static public function isAbsPath($sPath) {
6369
if (preg_match('#(?:/|\\\)\.\.(?=/|$)#', $sPath)) {
6470
return false;
6571
}
@@ -69,8 +75,70 @@ static function isAbsPath($sPath) {
6975
return ($sPath[0] == '/') || ($sPath[0] == '~');
7076
}
7177

72-
static function isAbsURL($sPath)
78+
/**
79+
* Tests if an URL is absolute
80+
*
81+
* @param string $sURL
82+
* @return boolean
83+
**/
84+
static public function isAbsURL($sURL) {
85+
return preg_match('#^(http|https|ftp)://#', $sURL);
86+
}
87+
88+
/**
89+
* Returns the parent path of an URL or path
90+
*
91+
* @param string $sURL an URL
92+
* @returns string an URL
93+
**/
94+
static public function dirname($sURL) {
95+
$aURL = parse_url($sURL);
96+
if(isset($aURL['path'])) {
97+
$sPath = dirname($aURL['path']);
98+
if($sPath == '/') {
99+
unset($aURL['path']);
100+
} else {
101+
$aURL['path'] = $sPath;
102+
}
103+
}
104+
return self::buildURL($aURL);
105+
}
106+
107+
/**
108+
* Builds an URL from an array of URL parts
109+
*
110+
* @param array $aURL URL parts in the format returned by parse_url
111+
* @return string the builded URL
112+
* @see http://php.net/manual/function.parse-url.php
113+
**/
114+
static public function buildURL(array $aURL)
73115
{
74-
return preg_match('#^(http|https|ftp)://#', $sPath);
116+
if(isset($aURL['scheme'])) {
117+
$sURL .= $aURL['scheme'] . '://';
118+
}
119+
if(isset($aURL['user'])) {
120+
$sURL .= $aURL['user'];
121+
if(isset($aURL['pass'])) {
122+
$sURL .= ':' . $aURL['pass'];
123+
}
124+
$sURL .= '@';
125+
}
126+
if(isset($aURL['host'])) {
127+
$sURL .= $aURL['host'];
128+
}
129+
if(isset($aURL['port'])) {
130+
$sURL .= ':' . $aURL['port'];
131+
}
132+
if(isset($aURL['path'])) {
133+
if(strpos($aURL['path'], '/') !== 0) $sURL .= '/';
134+
$sURL .= $aURL['path'];
135+
}
136+
if(isset($aURL['query'])) {
137+
$sURL .= '?' . $aURL['query'];
138+
}
139+
if(isset($aURL['fragment'])) {
140+
$sURL .= '#' . $aURL['fragment'];
141+
}
142+
return $sURL;
75143
}
76144
}

tests/CSSImportTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function testAbsoluteUrlsProvider()
4040
public function testResolveImports()
4141
{
4242
$oParser = new CSSParser(array('resolve_imports' => true));
43-
$oDoc = $oParser->parseFile("files/import.css");
43+
$oDoc = $oParser->parseFile(dirname(__FILE__)."/files/import.css");
44+
echo $oDoc . PHP_EOL;
4445
}
4546

4647
}

tests/files/import.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
@import "imported.css";
22
@import "import/utf-16.css";
3-
@import "unicode.css" screen, print;
43

54
body#icomelast{
65
color: fuschia;

tests/files/import/utf-16.css

252 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)