Skip to content

Commit 76af69e

Browse files
committed
Merge remote-tracking branch 'origin/pr/99'
* origin/pr/99: Fix non-utf-8 multibyte charset Split text on charset changing
2 parents 7642a0e + d40613d commit 76af69e

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
class Parser {
2828

29+
private $sText;
2930
private $aText;
3031
private $iCurrentPosition;
3132
private $oParserSettings;
@@ -35,20 +36,12 @@ class Parser {
3536
private $aSizeUnits;
3637

3738
public function __construct($sText, Settings $oParserSettings = null) {
39+
$this->sText = $sText;
3840
$this->iCurrentPosition = 0;
3941
if ($oParserSettings === null) {
4042
$oParserSettings = Settings::create();
4143
}
4244
$this->oParserSettings = $oParserSettings;
43-
if ($this->oParserSettings->bMultibyteSupport) {
44-
$this->aText = preg_split('//u', $sText, null, PREG_SPLIT_NO_EMPTY);
45-
} else {
46-
if($sText === '') {
47-
$this->aText = array();
48-
} else {
49-
$this->aText = str_split($sText);
50-
}
51-
}
5245
$this->blockRules = explode('/', AtRule::BLOCK_RULES);
5346

5447
foreach (explode('/', Size::ABSOLUTE_SIZE_UNITS.'/'.Size::RELATIVE_SIZE_UNITS.'/'.Size::NON_SIZE_UNITS) as $val) {
@@ -63,6 +56,7 @@ public function __construct($sText, Settings $oParserSettings = null) {
6356

6457
public function setCharset($sCharset) {
6558
$this->sCharset = $sCharset;
59+
$this->aText = $this->strsplit($this->sText);
6660
$this->iLength = count($this->aText);
6761
}
6862

@@ -494,8 +488,7 @@ private function peek($iLength = 1, $iOffset = 0) {
494488
if ($iOffset >= $this->iLength) {
495489
return '';
496490
}
497-
$out = $this->substr($iOffset, $iLength);
498-
return $out;
491+
return $this->substr($iOffset, $iLength);
499492
}
500493

501494
private function consume($mValue = 1) {
@@ -593,13 +586,13 @@ private function substr($iStart, $iLength) {
593586
if ($iStart + $iLength > $this->iLength) {
594587
$iLength = $this->iLength - $iStart;
595588
}
596-
$out = '';
589+
$sResult = '';
597590
while ($iLength > 0) {
598-
$out .= $this->aText[$iStart];
591+
$sResult .= $this->aText[$iStart];
599592
$iStart++;
600593
$iLength--;
601594
}
602-
return $out;
595+
return $sResult;
603596
}
604597

605598
private function strlen($sString) {
@@ -626,6 +619,27 @@ private function strtolower($sString) {
626619
}
627620
}
628621

622+
private function strsplit($sString) {
623+
if ($this->oParserSettings->bMultibyteSupport) {
624+
if ($this->streql($this->sCharset, 'utf-8')) {
625+
return preg_split('//u', $sString, null, PREG_SPLIT_NO_EMPTY);
626+
} else {
627+
$iLength = mb_strlen($sString, $this->sCharset);
628+
$aResult = array();
629+
for ($i = 0; $i < $iLength; ++$i) {
630+
$aResult[] = mb_substr($sString, $i, 1, $this->sCharset);
631+
}
632+
return $aResult;
633+
}
634+
} else {
635+
if($sString === '') {
636+
return array();
637+
} else {
638+
return str_split($sString);
639+
}
640+
}
641+
}
642+
629643
private function strpos($sString, $sNeedle, $iOffset) {
630644
if ($this->oParserSettings->bMultibyteSupport) {
631645
return mb_strpos($sString, $sNeedle, $iOffset, $this->sCharset);

0 commit comments

Comments
 (0)