Skip to content

Add ability to disable mb_* related string functions #40

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 2 commits into from Jun 28, 2012
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
57 changes: 44 additions & 13 deletions CSSParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class CSSParser {
private $sText;
private $iCurrentPosition;
private $iLength;

/**
* Should we use mb_* string functions
*
* @var bool
*/
private $bUseMbFunctions = TRUE;

public function __construct($sText, $sDefaultCharset = 'utf-8') {
$this->sText = $sText;
Expand All @@ -23,7 +30,7 @@ public function __construct($sText, $sDefaultCharset = 'utf-8') {

public function setCharset($sCharset) {
$this->sCharset = $sCharset;
$this->iLength = mb_strlen($this->sText, $this->sCharset);
$this->iLength = $this->strlen($this->sText);
}

public function getCharset() {
Expand All @@ -35,7 +42,11 @@ public function parse() {
$this->parseDocument($oResult);
return $oResult;
}


public function setUseMbFlag($bFlag){
$this->bUseMbFunctions = (bool) $bFlag;
}

private function parseDocument(CSSDocument $oDocument) {
$this->consumeWhiteSpace();
$this->parseList($oDocument, true);
Expand Down Expand Up @@ -157,7 +168,7 @@ private function parseCharacter($bIsForIdentifier) {
return $this->consume(1);
}
$sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u');
if(mb_strlen($sUnicode, $this->sCharset) < 6) {
if($this->strlen($sUnicode) < 6) {
//Consume whitespace after incomplete unicode escape
if(preg_match('/\\s/isSu', $this->peek())) {
if($this->comes('\r\n')) {
Expand Down Expand Up @@ -345,15 +356,15 @@ private function parseColorValue() {
if($this->comes('#')) {
$this->consume('#');
$sValue = $this->parseIdentifier(false);
if(mb_strlen($sValue, $this->sCharset) === 3) {
if($this->strlen($sValue) === 3) {
$sValue = $sValue[0].$sValue[0].$sValue[1].$sValue[1].$sValue[2].$sValue[2];
}
$aColor = array('r' => new CSSSize(intval($sValue[0].$sValue[1], 16), null, true), 'g' => new CSSSize(intval($sValue[2].$sValue[3], 16), null, true), 'b' => new CSSSize(intval($sValue[4].$sValue[5], 16), null, true));
} else {
$sColorMode = $this->parseIdentifier(false);
$this->consumeWhiteSpace();
$this->consume('(');
$iLength = mb_strlen($sColorMode, $this->sCharset);
$iLength = $this->strlen($sColorMode);
for($i=0;$i<$iLength;$i++) {
$this->consumeWhiteSpace();
$aColor[$sColorMode[$i]] = $this->parseNumericValue(true);
Expand Down Expand Up @@ -395,27 +406,27 @@ private function peek($iLength = 1, $iOffset = 0) {
return '';
}
if(is_string($iLength)) {
$iLength = mb_strlen($iLength, $this->sCharset);
$iLength = $this->strlen($iLength);
}
if(is_string($iOffset)) {
$iOffset = mb_strlen($iOffset, $this->sCharset);
$iOffset = $this->strlen($iOffset);
}
return mb_substr($this->sText, $this->iCurrentPosition+$iOffset, $iLength, $this->sCharset);
return $this->substr($this->sText, $this->iCurrentPosition+$iOffset, $iLength);
}

private function consume($mValue = 1) {
if(is_string($mValue)) {
$iLength = mb_strlen($mValue, $this->sCharset);
if(mb_substr($this->sText, $this->iCurrentPosition, $iLength, $this->sCharset) !== $mValue) {
$iLength = $this->strlen($mValue);
if($this->substr($this->sText, $this->iCurrentPosition, $iLength) !== $mValue) {
throw new Exception("Expected $mValue, got ".$this->peek(5));
}
$this->iCurrentPosition += mb_strlen($mValue, $this->sCharset);
$this->iCurrentPosition += $this->strlen($mValue);
return $mValue;
} else {
if($this->iCurrentPosition+$mValue > $this->iLength) {
throw new Exception("Tried to consume $mValue chars, exceeded file end");
}
$sResult = mb_substr($this->sText, $this->iCurrentPosition, $mValue, $this->sCharset);
$sResult = $this->substr($this->sText, $this->iCurrentPosition, $mValue);
$this->iCurrentPosition += $mValue;
return $sResult;
}
Expand Down Expand Up @@ -459,7 +470,27 @@ private function consumeUntil($sEnd) {
}

private function inputLeft() {
return mb_substr($this->sText, $this->iCurrentPosition, -1, $this->sCharset);
return $this->substr($this->sText, $this->iCurrentPosition, -1);
}

private function substr($string, $start, $length){
if($this->bUseMbFunctions) {
return mb_substr($string, $start, $length, $this->sCharset);
}
else {
return substr($string, $start, $length);
}
}

private function strlen($text)
{
if($this->bUseMbFunctions) {
return mb_strlen($text, $this->sCharset);
}
else {
return strlen($text);
}

}
}