@@ -14,6 +14,13 @@ class CSSParser {
14
14
private $ sText ;
15
15
private $ iCurrentPosition ;
16
16
private $ iLength ;
17
+
18
+ /**
19
+ * Should we use mb_* string functions
20
+ *
21
+ * @var bool
22
+ */
23
+ private $ bUseMbFunctions = TRUE ;
17
24
18
25
public function __construct ($ sText , $ sDefaultCharset = 'utf-8 ' ) {
19
26
$ this ->sText = $ sText ;
@@ -23,7 +30,7 @@ public function __construct($sText, $sDefaultCharset = 'utf-8') {
23
30
24
31
public function setCharset ($ sCharset ) {
25
32
$ this ->sCharset = $ sCharset ;
26
- $ this ->iLength = mb_strlen ($ this ->sText , $ this ->sCharset );
33
+ $ this ->iLength = $ this -> strlen ($ this ->sText , $ this ->sCharset );
27
34
}
28
35
29
36
public function getCharset () {
@@ -35,7 +42,11 @@ public function parse() {
35
42
$ this ->parseDocument ($ oResult );
36
43
return $ oResult ;
37
44
}
38
-
45
+
46
+ public function setUseMbFlag ($ bFlag ){
47
+ $ this ->bUseMbFunctions = (bool ) $ bFlag ;
48
+ }
49
+
39
50
private function parseDocument (CSSDocument $ oDocument ) {
40
51
$ this ->consumeWhiteSpace ();
41
52
$ this ->parseList ($ oDocument , true );
@@ -157,7 +168,7 @@ private function parseCharacter($bIsForIdentifier) {
157
168
return $ this ->consume (1 );
158
169
}
159
170
$ sUnicode = $ this ->consumeExpression ('/^[0-9a-fA-F]{1,6}/u ' );
160
- if (mb_strlen ($ sUnicode , $ this ->sCharset ) < 6 ) {
171
+ if ($ this -> strlen ($ sUnicode , $ this ->sCharset ) < 6 ) {
161
172
//Consume whitespace after incomplete unicode escape
162
173
if (preg_match ('/ \\s/isSu ' , $ this ->peek ())) {
163
174
if ($ this ->comes ('\r\n ' )) {
@@ -345,15 +356,15 @@ private function parseColorValue() {
345
356
if ($ this ->comes ('# ' )) {
346
357
$ this ->consume ('# ' );
347
358
$ sValue = $ this ->parseIdentifier (false );
348
- if (mb_strlen ($ sValue , $ this ->sCharset ) === 3 ) {
359
+ if ($ this -> strlen ($ sValue , $ this ->sCharset ) === 3 ) {
349
360
$ sValue = $ sValue [0 ].$ sValue [0 ].$ sValue [1 ].$ sValue [1 ].$ sValue [2 ].$ sValue [2 ];
350
361
}
351
362
$ 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 ));
352
363
} else {
353
364
$ sColorMode = $ this ->parseIdentifier (false );
354
365
$ this ->consumeWhiteSpace ();
355
366
$ this ->consume ('( ' );
356
- $ iLength = mb_strlen ($ sColorMode , $ this ->sCharset );
367
+ $ iLength = $ this -> strlen ($ sColorMode , $ this ->sCharset );
357
368
for ($ i =0 ;$ i <$ iLength ;$ i ++) {
358
369
$ this ->consumeWhiteSpace ();
359
370
$ aColor [$ sColorMode [$ i ]] = $ this ->parseNumericValue (true );
@@ -395,27 +406,27 @@ private function peek($iLength = 1, $iOffset = 0) {
395
406
return '' ;
396
407
}
397
408
if (is_string ($ iLength )) {
398
- $ iLength = mb_strlen ($ iLength , $ this ->sCharset );
409
+ $ iLength = $ this -> strlen ($ iLength , $ this ->sCharset );
399
410
}
400
411
if (is_string ($ iOffset )) {
401
- $ iOffset = mb_strlen ($ iOffset , $ this ->sCharset );
412
+ $ iOffset = $ this -> strlen ($ iOffset , $ this ->sCharset );
402
413
}
403
- return mb_substr ($ this ->sText , $ this ->iCurrentPosition +$ iOffset , $ iLength , $ this ->sCharset );
414
+ return $ this -> substr ($ this ->sText , $ this ->iCurrentPosition +$ iOffset , $ iLength , $ this ->sCharset );
404
415
}
405
416
406
417
private function consume ($ mValue = 1 ) {
407
418
if (is_string ($ mValue )) {
408
- $ iLength = mb_strlen ($ mValue , $ this ->sCharset );
409
- if (mb_substr ($ this ->sText , $ this ->iCurrentPosition , $ iLength , $ this ->sCharset ) !== $ mValue ) {
419
+ $ iLength = $ this -> strlen ($ mValue , $ this ->sCharset );
420
+ if ($ this -> substr ($ this ->sText , $ this ->iCurrentPosition , $ iLength , $ this ->sCharset ) !== $ mValue ) {
410
421
throw new Exception ("Expected $ mValue, got " .$ this ->peek (5 ));
411
422
}
412
- $ this ->iCurrentPosition += mb_strlen ($ mValue , $ this ->sCharset );
423
+ $ this ->iCurrentPosition += $ this -> strlen ($ mValue , $ this ->sCharset );
413
424
return $ mValue ;
414
425
} else {
415
426
if ($ this ->iCurrentPosition +$ mValue > $ this ->iLength ) {
416
427
throw new Exception ("Tried to consume $ mValue chars, exceeded file end " );
417
428
}
418
- $ sResult = mb_substr ($ this ->sText , $ this ->iCurrentPosition , $ mValue , $ this ->sCharset );
429
+ $ sResult = $ this -> substr ($ this ->sText , $ this ->iCurrentPosition , $ mValue , $ this ->sCharset );
419
430
$ this ->iCurrentPosition += $ mValue ;
420
431
return $ sResult ;
421
432
}
@@ -459,7 +470,27 @@ private function consumeUntil($sEnd) {
459
470
}
460
471
461
472
private function inputLeft () {
462
- return mb_substr ($ this ->sText , $ this ->iCurrentPosition , -1 , $ this ->sCharset );
473
+ return $ this -> substr ($ this ->sText , $ this ->iCurrentPosition , -1 , $ this ->sCharset );
463
474
}
475
+
476
+ private function substr ($ string , $ start , $ length ){
477
+ if ($ this ->bUseMbFunctions ) {
478
+ return mb_substr ($ string , $ start , $ length , $ this ->sCharset );
479
+ }
480
+ else {
481
+ return substr ($ string , $ start , $ length );
482
+ }
483
+ }
484
+
485
+ private function strlen ($ text )
486
+ {
487
+ if ($ this ->bUseMbFunctions ) {
488
+ return mb_strlen ($ text , $ this ->sCharset );
489
+ }
490
+ else {
491
+ return strlen ($ text );
492
+ }
493
+
494
+ }
464
495
}
465
496
0 commit comments