Skip to content

Imports Reloaded #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
338 changes: 316 additions & 22 deletions CSSParser.php

Large diffs are not rendered by default.

252 changes: 252 additions & 0 deletions lib/CSSCharsetUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
<?php
/**
*
*/
class CSSCharsetUtils
{
static $CHARSET_DETECTION_MAP = array(
array(
'pattern' => '#^\xEF\xBB\xBF\x40\x63\x68\x61\x72\x73\x65\x74\x20\x22([\x20-\x7F]*)\x22\x3B#',
'charset' => null,
'endianness' => null
),
array(
'pattern' => '#^\xEF\xBB\xBF#',
'charset' => "UTF-8",
'endianness' => null
),
array(
'pattern' => '#^\x40\x63\x68\x61\x72\x73\x65\x74\x20\x22([\x20-\x7F]*)\x22\x3B#',
'charset' => null,
'endianness' => null
),
array(
'pattern' => '#^\xFE\xFF\x00\x40\x00\x63\x00\x68\x00\x61\x00\x72\x00\x73\x00\x65\x00\x74\x00\x20\x00\x22((?:\x00[\x20-\x7F])*)\x00\x22\x00\x3B#',
'charset' => null,
'endianness' => 'BE'
),
array(
'pattern' => '#^\x00\x40\x00\x63\x00\x68\x00\x61\x00\x72\x00\x73\x00\x65\x00\x74\x00\x20\x00\x22((?:\x00[\x20-\x7F])*)\x00\x22\x00\x3B#',
'charset' => null,
'endianness' => 'BE'
),
array(
'pattern' => '#^\xFF\xFE\x40\x00\x63\x00\x68\x00\x61\x00\x72\x00\x73\x00\x65\x00\x74\x00\x20\x00\x22\x00((?:\x00[\x20-\x7F])*)\x22\x00\x3B\x00#',
'charset' => null,
'endianness' => 'BE'
),
array(
'pattern' => '#^\x40\x00\x63\x00\x68\x00\x61\x00\x72\x00\x73\x00\x65\x00\x74\x00\x20\x00\x22\x00((?:\x00[\x20-\x7F])*)\x22\x00\x3B\x00#',
'charset' => null,
'endianness' => 'LE'
),
array(
'pattern' => '#^\x00\x00\xFE\xFF\x00\x00\x00\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22((?:\x00\x00\x00[\x20-\x7F])*)\x00\x00\x00\x22\x00\x00\x00\x3B#',
'charset' => null,
'endianness' => 'BE'
),
array(
'pattern' => '#^\x00\x00\x00\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22((?:\x00\x00\x00[\x20-\x7F])*)\x00\x00\x00\x22\x00\x00\x00\x3B#',
'charset' => null,
'endianness' => 'BE'
),
array(
'pattern' => '#^\x00\x00\xFF\xFE\x00\x00\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22\x00((?:\x00\x00[\x20-\x7F]\x00)*)\x00\x00\x22\x00\x00\x00\x3B\x00#',
'charset' => null,
'endianness' => '2143'
),
array(
'pattern' => '#^\x00\x00\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22\x00((?:\x00\x00[\x20-\x7F]\x00)*)\x00\x00\x22\x00\x00\x00\x3B\x00#',
'charset' => null,
'endianness' => '2143'
),
array(
'pattern' => '#^\xFE\xFF\x00\x00\x00\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22\x00\x00((?:\x00[\x20-\x7F]\x00\x00)*)\x00\x22\x00\x00\x00\x3B\x00\x00#',
'charset' => null,
'endianness' => '3412'
),
array(
'pattern' => '#^\x00\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22\x00\x00((?:\x00[\x20-\x7F]\x00\x00)*)\x00\x22\x00\x00\x00\x3B\x00\x00#',
'charset' => null,
'endianness' => '3412'
),
array(
'pattern' => '#^\xFF\xFE\x00\x00\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22\x00\x00\x00((?:[\x20-\x7F]\x00\x00\x00)*)\x22\x00\x00\x00\x3B\x00\x00\x00#',
'charset' => null,
'endianness' => 'LE'
),
array(
'pattern' => '#^\x40\x00\x00\x00\x63\x00\x00\x00\x68\x00\x00\x00\x61\x00\x00\x00\x72\x00\x00\x00\x73\x00\x00\x00\x65\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x22\x00\x00\x00((?:[\x20-\x7F]\x00\x00\x00)*)\x22\x00\x00\x00\x3B\x00\x00\x00#',
'charset' => null,
'endianness' => 'LE'
),
array(
'pattern' => '#^\x00\x00\xFE\xFF#',
'charset' => 'UTF-32BE',
'endianness' => null
),
array(
'pattern' => '#^\xFF\xFE\x00\x00#',
'charset' => 'UTF-32LE',
'endianness' => null
),
array(
'pattern' => '#^\x00\x00\xFF\xFE#',
'charset' => 'UTF-32-2143',
'endianness' => null
),
array(
'pattern' => '#^\xFE\xFF\x00\x00#',
'charset' => 'UTF-32-3412',
'endianness' => null
),
array(
'pattern' => '#^\xFE\xFF#',
'charset' => "UTF-16BE",
'endianness' => null
),
array(
'pattern' => '#^\xFF\xFE#',
'charset' => 'UTF-16LE',
'endianness' => null
),
/**
* These encodings are not supported by mbstring extension.
**/
//array(
//'pattern' => '/^\x7C\x83\x88\x81\x99\xA2\x85\xA3\x40\x7F(YY)*\x7F\x5E/',
//'charset' => null,
//'endianness' => null,
//'transcoded-from' => 'EBCDIC'
//),
//array(
//'pattern' => '/^\xAE\x83\x88\x81\x99\xA2\x85\xA3\x40\xFC(YY)*\xFC\x5E/',
//'charset' => null,
//'endianness' => null,
//'transcoded-from' => 'IBM1026'
//),
//array(
//'pattern' => '/^\x00\x63\x68\x61\x72\x73\x65\x74\x20\x22(YY)*\x22\x3B/',
//'charset' => null,
//'endianness' => null
//'transcoded-from' => 'GSM 03.38'
//),
);

static function detectCharset($sText) {
$aSupportedEncodings = mb_list_encodings();
foreach (self::$CHARSET_DETECTION_MAP as $aCharsetMap) {
$sPattern = $aCharsetMap['pattern'];
$aMatches = array();
if(preg_match($sPattern, $sText, $aMatches)) {
if($aCharsetMap['charset']) {
$sCharset = $aCharsetMap['charset'];
} else {
$sCharset = $aMatches[1];
}
return $sCharset;
}
}
return false;
}

static function convert($sSubject, $sFromCharset, $sToCharset) {
return mb_convert_encoding($sSubject, $sToCharset, $sFromCharset);
//return iconv($sFromCharset, $sToCharset, $sSubject);
}

static function removeBOM($sText) {
$iLen = strlen($sText);
if($iLen > 3) {
switch ($sText[0]) {
case "\xEF":
if(("\xBB" == $sText[1]) && ("\xBF" == $sText[2])) {
// EF BB BF UTF-) encoded BOM
return substr($sText, 3);
}
break;
case "\xFE":
if (("\xFF" == $sText[1]) && ("\x00" == $sText[2]) && ("\x00" == $sText[3])) {
// FE FF 00 00 UCS-4, unusual octet order BOM (3412)
return substr($sText, 4);
} else if ("\xFF" == $sText[1]) {
// FE FF UTF-16, big endian BOM
return substr($sText, 2);
}
break;
case "\x00":
if (("\x00" == $sText[1]) && ("\xFE" == $sText[2]) && ("\xFF" == $sText[3])) {
// 00 00 FE FF UTF-32, big-endian BOM
return substr($sText, 4);
} else if (("\x00" == $sText[1]) && ("\xFF" == $sText[2]) && ("\xFE" == $sText[3])) {
// 00 00 FF FE UCS-4, unusual octet order BOM (2143)
return substr($sText, 4);
}
break;
case "\xFF":
if (("\xFE" == $sText[1]) && ("\x00" == $sText[2]) && ("\x00" == $sText[3])) {
// FF FE 00 00 UTF-32, little-endian BOM
return substr($sText, 4);
} else if ("\xFE" == $sText[1]) {
// FF FE UTF-16, little endian BOM
return substr($sText, 2);
}
break;
}
}
return $sText;
}

static function checkForBOM($sText) {
$iLen = strlen($sText);
if($iLen > 3) {
switch ($sText[0]) {
case "\xEF":
if(("\xBB" == $sText[1]) && ("\xBF" == $sText[2])) {
// EF BB BF UTF-) encoded BOM
return 'UTF-8';
}
break;
case "\xFE":
if (("\xFF" == $sText[1]) && ("\x00" == $sText[2]) && ("\x00" == $sText[3])) {
// FE FF 00 00 UCS-4, unusual octet order BOM (3412)
return "X-ISO-10646-UCS-4-3412";
} else if ("\xFF" == $sText[1]) {
// FE FF UTF-16, big endian BOM
return "UTF-16BE";
}
break;
case "\x00":
if (("\x00" == $sText[1]) && ("\xFE" == $sText[2]) && ("\xFF" == $sText[3])) {
// 00 00 FE FF UTF-32, big-endian BOM
return "UTF-32BE";
} else if (("\x00" == $sText[1]) && ("\xFF" == $sText[2]) && ("\xFE" == $sText[3])) {
// 00 00 FF FE UCS-4, unusual octet order BOM (2143)
return "X-ISO-10646-UCS-4-2143";
}
break;
case "\xFF":
if (("\xFE" == $sText[1]) && ("\x00" == $sText[2]) && ("\x00" == $sText[3])) {
// FF FE 00 00 UTF-32, little-endian BOM
return "UTF-32LE";
} else if ("\xFE" == $sText[1]) {
// FF FE UTF-16, little endian BOM
return "UTF-16LE";
}
break;
}
}
return false;
}

static function printBytes($sString, $iLen=null)
{
if($iLen == null) $iLen = strlen($sString);
$aBytes = array();
for($i = 0; $i < $iLen; $i++) {
$aBytes[] = "0x".dechex(ord($sString[$i]));
}
return implode(' ', $aBytes);
}

}
24 changes: 23 additions & 1 deletion lib/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,29 @@ abstract class CSSList {
public function __construct() {
$this->aContents = array();
}

public function prepend($oItem)
{
array_unshift($this->aContents, $oItem);
}

public function append($oItem) {
$this->aContents[] = $oItem;
}

public function extend(Array $aItems) {
foreach ($aItems as $oItem) {
$this->aContents[] = $oItem;
}
}

public function removeItemAt($iIndex) {
unset($this->aContents[$iIndex]);
}

public function insertItemsAt(Array $oItems, $iIndex) {
array_splice($this->aContents, $iIndex, 0, $oItems);
}

public function __toString() {
$sResult = '';
Expand All @@ -25,7 +44,10 @@ public function __toString() {

public function getContents() {
return $this->aContents;
}
}
public function setContents(Array $aContents) {
$this->aContents = $aContents;
}

protected function allDeclarationBlocks(&$aResult) {
foreach($this->aContents as $mContent) {
Expand Down
12 changes: 8 additions & 4 deletions lib/CSSProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ public function __construct(CSSURL $oLocation, $sMediaQuery) {
}

public function setLocation($oLocation) {
$this->oLocation = $oLocation;
$this->oLocation = $oLocation;
}

public function getLocation() {
return $this->oLocation;
return $this->oLocation;
}

public function getMediaQuery() {
return $this->sMediaQuery;
}

public function __toString() {
return "@import ".$this->oLocation->__toString().($this->sMediaQuery === null ? '' : ' '.$this->sMediaQuery).';';
Expand All @@ -40,11 +44,11 @@ public function __construct($sCharset) {
}

public function setCharset($sCharset) {
$this->sCharset = $sCharset;
$this->sCharset = $sCharset;
}

public function getCharset() {
return $this->sCharset;
return $this->sCharset;
}

public function __toString() {
Expand Down
Loading