/** * Parses the css code converting to a Css object with all selectors, properties and values. * * @param string $string_code The css code to parse * * @return Stylecow\Css The parsed css code */ private static function parse($string_code) { $Css = new Css(); while ($string_code) { $pos = strpos($string_code, '{'); $pos2 = strpos($string_code, ';'); if ($pos2 !== false && $pos2 < $pos) { $Css->addChild(new Css(Selector::createFromString(substr($string_code, 0, $pos2)))); $string_code = trim(substr($string_code, $pos2 + 1)); continue; } if ($pos === false) { break; } $selector = substr($string_code, 0, $pos); $string_code = trim(substr($string_code, $pos + 1)); $length = strlen($string_code); $in = 1; for ($n = 0; $n <= $length; $n++) { $letter = $string_code[$n]; if ($letter === '{') { $in++; continue; } if ($letter !== '}' || --$in) { continue; } $Child = $Css->addChild(new Css(Selector::createFromString($selector))); $string_piece = $n === 0 ? '' : trim(substr($string_code, 0, $n - 1)); $string_code = trim(substr($string_code, $n + 1)); $pos = strpos($string_piece, '{'); if ($pos === false) { $properties_string = $string_piece; $content_string = ''; } else { $pos = strrpos(substr($string_piece, 0, $pos), ';'); if ($pos !== false) { $properties_string = trim(substr($string_piece, 0, $pos + 1)); $content_string = trim(substr($string_piece, $pos + 1)); } else { $properties_string = ''; $content_string = $string_piece; } } if ($properties_string) { foreach (self::explodeTrim(';', $properties_string) as $property) { $Child->addProperty(Property::createFromString($property)); } } if ($content_string) { foreach (self::parse($content_string) as $child) { $Child->addChild($child); } } break; } } return $Css; }