Skip to content

Commit d40613d

Browse files
committed
Fix non-utf-8 multibyte charset
Split text on charset changing
1 parent c5a44a9 commit d40613d

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 20 additions & 5 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,16 +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-
$this->aText = str_split($sText);
47-
}
4845
$this->blockRules = explode('/', AtRule::BLOCK_RULES);
4946

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

6057
public function setCharset($sCharset) {
6158
$this->sCharset = $sCharset;
59+
$this->aText = $this->strsplit($this->sText);
6260
$this->iLength = count($this->aText);
6361
}
6462

@@ -622,6 +620,23 @@ private function strtolower($sString) {
622620
}
623621
}
624622

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

0 commit comments

Comments
 (0)