Skip to content

Charset issues #99

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

Merged
merged 1 commit into from
Apr 26, 2016
Merged
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
25 changes: 20 additions & 5 deletions lib/Sabberworm/CSS/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
class Parser {

private $sText;
private $aText;
private $iCurrentPosition;
private $oParserSettings;
Expand All @@ -35,16 +36,12 @@ class Parser {
private $aSizeUnits;

public function __construct($sText, Settings $oParserSettings = null) {
$this->sText = $sText;
$this->iCurrentPosition = 0;
if ($oParserSettings === null) {
$oParserSettings = Settings::create();
}
$this->oParserSettings = $oParserSettings;
if ($this->oParserSettings->bMultibyteSupport) {
$this->aText = preg_split('//u', $sText, null, PREG_SPLIT_NO_EMPTY);
} else {
$this->aText = str_split($sText);
}
$this->blockRules = explode('/', AtRule::BLOCK_RULES);

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

public function setCharset($sCharset) {
$this->sCharset = $sCharset;
$this->aText = $this->strsplit($this->sText);
$this->iLength = count($this->aText);
}

Expand Down Expand Up @@ -622,6 +620,23 @@ private function strtolower($sString) {
}
}

private function strsplit($sString) {
if ($this->oParserSettings->bMultibyteSupport) {
if ($this->streql($this->sCharset, 'utf-8')) {
return preg_split('//u', $sString, null, PREG_SPLIT_NO_EMPTY);
} else {
$iLength = mb_strlen($sString, $this->sCharset);
$out = [];
for ($i = 0; $i < $iLength; ++$i) {
$out[] = mb_substr($sString, $i, 1, $this->sCharset);
}
return $out;
}
} else {
return str_split($sString);
}
}

private function strpos($sString, $sNeedle, $iOffset) {
if ($this->oParserSettings->bMultibyteSupport) {
return mb_strpos($sString, $sNeedle, $iOffset, $this->sCharset);
Expand Down