Skip to content

Commit 0f8c0bb

Browse files
committed
fixed Unicode escape handling (thanks to cyberwolf)
1 parent c779825 commit 0f8c0bb

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

CSSParser.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private function parseCharacter($bIsForIdentifier) {
145145
if(preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
146146
return $this->consume(1);
147147
}
148-
$sUnicode = $this->consumeExpression('/[0-9a-fA-F]+/u');
148+
$sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u');
149149
if(mb_strlen($sUnicode, $this->sCharset) < 6) {
150150
//Consume whitespace after incomplete unicode escape
151151
if(preg_match('/\\s/isSu', $this->peek())) {
@@ -156,14 +156,13 @@ private function parseCharacter($bIsForIdentifier) {
156156
}
157157
}
158158
}
159-
$sUtf16 = '';
160-
if((strlen($sUnicode) % 2) === 1) {
161-
$sUnicode = "0$sUnicode";
159+
$iUnicode = intval($sUnicode, 16);
160+
$sUtf32 = "";
161+
for($i=0;$i<4;$i++) {
162+
$sUtf32 .= chr($iUnicode & 0xff);
163+
$iUnicode = $iUnicode >> 8;
162164
}
163-
for($i=0;$i<strlen($sUnicode);$i+=2) {
164-
$sUtf16 .= chr(intval($sUnicode[$i].$sUnicode[$i+1]));
165-
}
166-
return iconv('utf-16', $this->sCharset, $sUtf16);
165+
return iconv('utf-32le', $this->sCharset, $sUtf32);
167166
}
168167
if($bIsForIdentifier) {
169168
if(preg_match('/[a-zA-Z0-9]|-|_/u', $this->peek()) === 1) {
@@ -363,10 +362,8 @@ private function consume($mValue = 1) {
363362

364363
private function consumeExpression($mExpression) {
365364
$aMatches;
366-
if(preg_match($mExpression, $this->inputLeft(), $aMatches) === 1) {
367-
if($aMatches[0][1] === $this->iCurrentPosition) {
368-
return $this->consume($aMatches[0][0]);
369-
}
365+
if(preg_match($mExpression, $this->inputLeft(), $aMatches, PREG_OFFSET_CAPTURE) === 1) {
366+
return $this->consume($aMatches[0][0]);
370367
}
371368
throw new Exception("Expected pattern $mExpression not found, got: {$this->peek(5)}");
372369
}
@@ -695,11 +692,11 @@ public function getValues() {
695692
}
696693

697694
public function setIsImportant($bIsImportant) {
698-
$this->bIsImportant = $bIsImportant;
695+
$this->bIsImportant = $bIsImportant;
699696
}
700697

701698
public function getIsImportant() {
702-
return $this->bIsImportant;
699+
return $this->bIsImportant;
703700
}
704701
public function __toString() {
705702
$sResult = "{$this->sRule}: ";
@@ -730,19 +727,19 @@ public function __construct($fSize, $sUnit = null) {
730727
}
731728

732729
public function setUnit($sUnit) {
733-
$this->sUnit = $sUnit;
730+
$this->sUnit = $sUnit;
734731
}
735732

736733
public function getUnit() {
737-
return $this->sUnit;
734+
return $this->sUnit;
738735
}
739736

740737
public function setSize($fSize) {
741-
$this->fSize = floatval($fSize);
738+
$this->fSize = floatval($fSize);
742739
}
743740

744741
public function getSize() {
745-
return $this->fSize;
742+
return $this->fSize;
746743
}
747744

748745
public function isRelative() {
@@ -768,11 +765,11 @@ public function __construct($aColor) {
768765
}
769766

770767
public function setColor($aColor) {
771-
$this->aColor = $aColor;
768+
$this->aColor = $aColor;
772769
}
773770

774771
public function getColor() {
775-
return $this->aColor;
772+
return $this->aColor;
776773
}
777774

778775
public function getColorDescription() {
@@ -792,15 +789,17 @@ public function __construct($sString) {
792789
}
793790

794791
public function setString($sString) {
795-
$this->sString = $sString;
792+
$this->sString = $sString;
796793
}
797794

798795
public function getString() {
799-
return $this->sString;
796+
return $this->sString;
800797
}
801798

802799
public function __toString() {
803-
return '"'.addslashes($this->sString).'"';
800+
$sString = addslashes($this->sString);
801+
$sString = str_replace("\n", '\A', $sString);
802+
return '"'.$sString.'"';
804803
}
805804
}
806805

@@ -812,11 +811,11 @@ public function __construct(CSSString $oURL) {
812811
}
813812

814813
public function setURL(CSSString $oURL) {
815-
$this->oURL = $oURL;
814+
$this->oURL = $oURL;
816815
}
817816

818817
public function getURL() {
819-
return $this->oURL;
818+
return $this->oURL;
820819
}
821820

822821
public function __toString() {

0 commit comments

Comments
 (0)