Example #1
0
 public function resolve($imported_files = array())
 {
     if (empty($imported_files)) {
         $imported_files[] = $this->stylesheet->getHref();
     }
     $encoding = $this->stylesheet->getCharset();
     $lexer = new Lexer();
     $parser = new Parser($lexer);
     $url_resolver = new UrlResolver($this->stylesheet, $this->base_url);
     $url_resolver->resolve();
     foreach ($this->stylesheet->getRuleList()->getRules() as $rule) {
         if ($rule instanceof Rule\Import) {
             $url = $rule->getHref()->getUrl()->getString();
             // Take care of circular imports !
             if (in_array($url, $imported_files)) {
                 continue;
             } else {
                 $imported_files[] = $url;
             }
             try {
                 $source = Loader::load($url, $encoding);
             } catch (StyleSheetNotFoundException $e) {
                 // FIXME: should we remove the rule ?
                 continue;
             }
             // Do the parsing
             $lexer->setSource($source);
             $stylesheet = $parser->parseStyleSheet();
             // Remove charset rules
             $rule_list = $stylesheet->getRuleList();
             foreach ($rule_list->getItems() as $sub_rule) {
                 if ($sub_rule instanceof Rule\Charset) {
                     $stylesheet->getRuleList()->remove($sub_rule);
                     // Only one charset is allowed, so we can
                     break;
                 }
             }
             $rule_list->resetKeys();
             // Do the recurse
             $resolver = new ImportResolver($stylesheet);
             $resolver->resolve($imported_files);
             // Wrap into media query if needed
             if ($rule->getMediaQueryList() !== null && !$rule->getMediaQueryList()->isEmpty()) {
                 $media_query = new Rule\Media($rule->getMediaQueryList(), $rule_list);
                 $this->stylesheet->getRuleList()->replace($rule, $media_query);
             } else {
                 $this->stylesheet->getRuleList()->replace($rule, $rule_list->getItems());
             }
         }
     }
 }
Example #2
0
 private static function normalizeLineLength($input)
 {
     /*{{{*/
     // estimate average line length
     $len = strlen($input);
     $numlines = substr_count($input, "\n") + 1;
     $avg_line_length = round($len / $numlines);
     if ($avg_line_length < self::MAX_AVG_LINE_LENGTH) {
         return $input;
     }
     // quick & dirty tokenizer:
     // finds position of all '}' not inside a string literal
     $patterns = Lexer::getPatterns();
     $pos = 0;
     $tokens = array();
     while ($pos < $len) {
         $chr = $input[$pos];
         if ('"' === $chr || "'" === $chr) {
             if (preg_match('/\\G' . $patterns['string'] . '/iu', $input, $matches, 0, $pos)) {
                 $pos += strlen($matches[0]);
             } else {
                 preg_match('/\\G' . $patterns['badstring'] . '/iu', $input, $matches, 0, $pos);
                 $pos += strlen($matches[0]);
             }
         } else {
             if ('}' === $chr) {
                 $pos++;
                 $tokens[] = $pos;
                 $pos++;
             } else {
                 $pos++;
             }
         }
     }
     // add a newline character after each '}'
     $output = substr($input, 0, $tokens[0]) . "\n";
     foreach ($tokens as $i => $pos) {
         $next = $i + 1;
         if (!isset($tokens[$next])) {
             break;
         }
         $output .= substr($input, $tokens[$i], $tokens[$next] - $tokens[$i]) . "\n";
     }
     $last = end($tokens);
     $output .= substr($input, $last, $len - $last);
     return $output;
 }
Example #3
0
h1{  color: red; rotation: 77\$\$ }
p { color:green; color{;color:maroon} color:blue; color:yellow; border:none }
p{color:red}
p{ foo:bar; bar:calc(2 + 5 * (3-6)); baz:boo }

*/
/* Discards the rule til end of stylesheet, since no matching bracket can be found */
/*p{ foo:bar; foo{;bar("baz)};"; baz:boo }
h1{}*/
EOS;
$timer = new Benchmark_Timer();
//$nb_iterations = 1;
$timer->start();
$source = new Source\String($css);
$timer->setMarker(sprintf("Source init: %s", $source->getEncoding()));
$lexer = new Css\Lexer();
$lexer->setSource($source);
$timer->setMarker("Lexer init");
//$token = $lexer->nextToken();
//while ($token->type !== Css\Lexer::T_EOF) {
//echo $lexer->getLiteral($token) . PHP_EOL;
//$token = $lexer->nextToken();
//}
//echo $lexer->getLiteral($token) . PHP_EOL;
//$timer->setMarker("Tokenization end");
$parser = new Css\Parser($lexer);
$parser->setStrict(false);
$stylesheet = $parser->parseStyleSheet();
$timer->setMarker("Parsing end");
foreach ($parser->errors as $error) {
    echo "Start: " . $lexer->getLiteral($error['start']) . "\n";
Example #4
0
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use ju1ius\Css;
$lexer = new Css\Lexer();
$parser = new Css\Parser($lexer);
$css = <<<EOS
div {
  background: url(foo.png) top left / 12% 25% no-repeat fixed;
  border-radius: 1px 2px 3px / 1px 2px 3px;
}
EOS;
$lexer->setSource(Css\Loader::loadString($css));
$stylesheet = $parser->parseStyleSheet();
$value_list = $stylesheet->getFirstRule()->getStyleDeclaration()->getAppliedProperty('background')->getValueList();
var_dump($value_list);
$values = array('foo', ' ', 'bar', ' ', 'baz', '/', 'bidule', ',', 'boom', ' ', 'truc');
$delimiters = array('/' => 0, ',' => 2, ' ' => 1);
//$result = reduce($values, $delimiters);
//var_dump($result);
function reduce($values, $delimiters)
{
    if (count($values) === 1) {
        return $values[0];
    }
    foreach ($delimiters as $delimiter => $binds) {
        $start = null;
        $indexes = array_keys($values, $delimiter, true);
        if ($binds === 0) {
            foreach ($indexes as $idx) {
            }
Example #5
0
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
  </ul>
</root>
EOS;
$css = <<<EOS
h1:not(.foo)
EOS;
$timer = new Benchmark_Timer();
//$nb_iterations = 1;
$timer->start();
$source = new Source\String($css);
$timer->setMarker("Source init");
$lexer = new Css\Lexer();
$lexer->setSource($source);
$timer->setMarker("Lexer init");
$token = $lexer->nextToken();
while ($token->type !== Css\Lexer::T_EOF) {
    echo $lexer->getLiteral($token) . PHP_EOL;
    $token = $lexer->nextToken();
}
echo $lexer->getLiteral($token) . PHP_EOL;
$timer->setMarker("Tokenization end");
$parser = new Css\Parser($lexer);
$selector = $parser->parseSelector();
$timer->setMarker("Parsing end");
//var_dump($selector);
//echo $selector->toXpath() . PHP_EOL;
$dom = \DOMDocument::loadXML($xml);