cvs: pear /HTML_CSS CSS.php
| From: | Laurent Laville | Date: | Sat, 12 Jan 2008 18:04:21 +0000 |
| Subject: | cvs: pear /HTML_CSS CSS.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-50245@lists.php.net to get a copy of this message | ||
farell Sat Jan 12 18:04:21 2008 UTC
Modified files:
/pear/HTML_CSS CSS.php
Log:
parseString() can now parse styles with or without at-rules
http://cvs.php.net/viewvc.cgi/pear/HTML_CSS/CSS.php?r1=1.72&r2=1.73&diff_format=u
Index: pear/HTML_CSS/CSS.php
diff -u pear/HTML_CSS/CSS.php:1.72 pear/HTML_CSS/CSS.php:1.73
--- pear/HTML_CSS/CSS.php:1.72 Sat Jan 12 16:20:35 2008
+++ pear/HTML_CSS/CSS.php Sat Jan 12 18:04:20 2008
@@ -38,7 +38,7 @@
* @author Laurent Laville <pear@laurent-laville.org>
* @copyright 2003-2008 Klaus Guenther, Laurent Laville
* @license http://www.opensource.org/licenses/bsd-license.php
New BSD License
- * @version CVS: $Id: CSS.php,v 1.72 2008/01/12 16:20:35 farell Exp $
+ * @version CVS: $Id: CSS.php,v 1.73 2008/01/12 18:04:20 farell Exp $
* @link http://pear.php.net/package/HTML_CSS
* @since File available since Release 0.2.0
*/
@@ -1709,36 +1709,71 @@
$str = preg_replace('/^\s*@[a-z\-]+\s+.+;\s*$/m', '', $str);
}
- // Parse each element of csscode
- $parse = preg_split('/\{(.*)\}/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
-
$elements = array();
$properties = array();
- foreach ($parse as $i => $part) {
- if ($i%2 == 0) {
- $part = ltrim($part, "\r\n}");
- $pos = strpos($part, '{');
- if ($pos === false) {
- $elements[] = trim($part);
+ // Parse each element of csscode
+ $parts = explode("}", $str);
+ foreach ($parts as $part) {
+ $part = trim($part);
+ if (strlen($part) == 0) {
+ continue;
+ }
+ // prevent invalide css data structure
+ $pos = strpos($part, '{');
+ if (strpos($part, '{', $pos+1) !== false && $part{0} !==
'@') {
+
+ $context = debug_backtrace();
+ $context = @array_pop($context);
+ $function = strtolower($context['function']);
+ if ($function === 'parsestring') {
+ $var = 'str';
+ } elseif ($function === 'parsefile') {
+ $var = 'filename';
} else {
- // Remove eol
- $part = preg_replace("/\r?\n?/", '', $part);
+ $var = 'styles';
+ }
+
+ return $this->raiseError(HTML_CSS_ERROR_INVALID_INPUT, 'error',
+ array('var' => '$'.$var,
+ 'was' => 'invalid data source',
+ 'expected' => 'valid CSS structure',
+ 'paramnum' => 1));
+ }
+ $parse = preg_split('/\{(.*)\}/', "$part }", -1,
+ PREG_SPLIT_DELIM_CAPTURE);
- if (strpos($part, '}', $pos+1) === false) {
- // complex declaration block style (nested)
- list($keystr, $codestr) = explode("{", $part);
- $elements[] = trim($part);
+ if (count($parse) == 1) {
+ list($keystr, $codestr) = explode("{", $part);
+ $elements[] = trim($keystr);
+ $properties[] = trim($codestr);
+ } else {
+ for ($i = 0; $i < 2; $i++) {
+ if ($i == 0) {
+ $part = ltrim($parse[$i], "\r\n}");
+ $pos = strpos($part, '{');
+ if ($pos === false) {
+ $elements[] = trim($part);
+ } else {
+ // Remove eol
+ $part = preg_replace("/\r?\n?/", '', $part);
+
+ if (strpos($part, '}', $pos+1) === false) {
+ // complex declaration block style (nested)
+ list($keystr, $codestr) = explode("{", $part);
+ $elements[] = trim($part);
+ } else {
+ // simple declaration block style
+ $parse = preg_split('/\{(.*)\}/', "$part
}",
+ -1, PREG_SPLIT_DELIM_CAPTURE);
+ $elements[] = trim($parse[0]);
+ $properties[] = trim($parse[1]);
+ }
+ }
} else {
- // simple declaration block style
- $parse = preg_split('/\{(.*)\}/', $part, -1,
- PREG_SPLIT_DELIM_CAPTURE);
- $elements[] = trim($parse[0]);
- $properties[] = trim($parse[1]);
+ $properties[] = trim($parse[$i]);
}
}
- } else {
- $properties[] = trim($part);
}
}