Пример #1
0
    public function testCaseInsensitivity()
    {
        $sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "case-insensitivity.css";
        $oParser = new Parser(file_get_contents($sFile));
        $oResult = $oParser->parse();
        $this->assertSame('@charset "utf-8";
@import url("test.css");
@media screen {}
#myid {case: insensitive !important;frequency: 30Hz;font-size: 1em;color: #ff0;color: hsl(40,40%,30%);font-family: Arial;}', $oResult->render());
    }
Пример #2
0
 /**
  * Gets styles from <style> html tag - if there are any it will be merged with another styles added by addCss()
  * @return CSS\CSSList\Document
  * @throws \Exception
  */
 private function getCSS()
 {
     // get styles inside <style> tags in provided HTML
     foreach ($this->dom->getElementsByTagName('style') as $style) {
         $this->css .= $style->textContent;
     }
     $parser = new CSS\Parser($this->css);
     $css = $parser->parse();
     if (!$css) {
         throw new Exceptions\NoCssRulesException('There are no CSS rules provided.');
     }
     return $css;
 }
Пример #3
0
 public function testOverrideContents()
 {
     $sCss = '.thing { left: 10px; }';
     $oParser = new Parser($sCss);
     $oDoc = $oParser->parse();
     $aContents = $oDoc->getContents();
     $this->assertCount(1, $aContents);
     $sCss2 = '.otherthing { right: 10px; }';
     $oParser2 = new Parser($sCss);
     $oDoc2 = $oParser2->parse();
     $aContents2 = $oDoc2->getContents();
     $oDoc->setContents(array($aContents[0], $aContents2[0]));
     $aFinalContents = $oDoc->getContents();
     $this->assertCount(2, $aFinalContents);
 }
 public function testMediaQueries()
 {
     $sCss = '@media(min-width: 768px){.class{color:red}}';
     $oParser = new Parser($sCss);
     $oDoc = $oParser->parse();
     $aContents = $oDoc->getContents();
     $oMediaQuery = $aContents[0];
     $this->assertSame('media', $oMediaQuery->atRuleName(), 'Does not interpret the type as a function');
     $this->assertSame('(min-width: 768px)', $oMediaQuery->atRuleArgs(), 'The media query is the value');
     $sCss = '@media (min-width: 768px) {.class{color:red}}';
     $oParser = new Parser($sCss);
     $oDoc = $oParser->parse();
     $aContents = $oDoc->getContents();
     $oMediaQuery = $aContents[0];
     $this->assertSame('media', $oMediaQuery->atRuleName(), 'Does not interpret the type as a function');
     $this->assertSame('(min-width: 768px)', $oMediaQuery->atRuleArgs(), 'The media query is the value');
 }
Пример #5
0
 public function getPackedHTML()
 {
     $parser = new Parser($this->css);
     $cssDoc = $parser->parse();
     foreach ($cssDoc->getAllValues() as $value) {
         if ($value instanceof URL) {
             $client = new Client();
             $url = trim($value->getUrl(), '"');
             $fullSrc = strpos($url, "http") === 0 ? $url : $this->baseUrl . $url;
             try {
                 $response = $client->get($fullSrc);
                 $value->setURL(new String('data:' . $response->getHeader('Content-Type') . ';base64,' . base64_encode($response->getBody()->getContents())));
             } catch (ClientException $e) {
             }
         }
     }
     return $cssDoc->render();
 }
 public function testRuleInsertion()
 {
     $sCss = '.wrapper { left: 10px; text-align: left; }';
     $oParser = new Parser($sCss);
     $oDoc = $oParser->parse();
     $aContents = $oDoc->getContents();
     $oWrapper = $aContents[0];
     $oFirst = $oWrapper->getRules('left');
     $this->assertCount(1, $oFirst);
     $oFirst = $oFirst[0];
     $oSecond = $oWrapper->getRules('text-');
     $this->assertCount(1, $oSecond);
     $oSecond = $oSecond[0];
     $oBefore = new Rule('left');
     $oBefore->setValue(new Size(16, 'em'));
     $oMiddle = new Rule('text-align');
     $oMiddle->setValue(new Size(1));
     $oAfter = new Rule('border-bottom-width');
     $oAfter->setValue(new Size(1, 'px'));
     $oWrapper->addRule($oAfter);
     $oWrapper->addRule($oBefore, $oFirst);
     $oWrapper->addRule($oMiddle, $oSecond);
     $aRules = $oWrapper->getRules();
     $this->assertSame($oBefore, $aRules[0]);
     $this->assertSame($oFirst, $aRules[1]);
     $this->assertSame($oMiddle, $aRules[2]);
     $this->assertSame($oSecond, $aRules[3]);
     $this->assertSame($oAfter, $aRules[4]);
     $this->assertSame('.wrapper {left: 16em;left: 10px;text-align: 1;text-align: left;border-bottom-width: 1px;}', $oDoc->render());
 }
Пример #7
0
 /**
  * Use CssParser to go through and convert all relative paths to absolute
  *
  * @param string $content
  * @param string $originalFile
  *
  * @return string
  */
 protected function fixRelativePaths($content, $originalFile)
 {
     $cssParserSettings = CssSettings::create()->withMultibyteSupport(false);
     $cssParser = new CssParser($content, $cssParserSettings);
     $cssDocument = $cssParser->parse();
     $cssBlocks = $cssDocument->getAllValues();
     $this->fixUrls($cssBlocks, $originalFile);
     return $cssDocument->render();
 }
Пример #8
0
 /**
  * Constructor.
  *
  * @param string $css The CSS content.
  */
 public function __construct($css)
 {
     $settings = \Sabberworm\CSS\Settings::create();
     $settings->withLenientParsing();
     parent::__construct($css, $settings);
 }
Пример #9
0
 function parsedStructureForFile($sFileName, $oSettings = null)
 {
     $sFile = dirname(__FILE__) . '/../../files' . DIRECTORY_SEPARATOR . "{$sFileName}.css";
     $oParser = new Parser(file_get_contents($sFile), $oSettings);
     return $oParser->parse();
 }
Пример #10
0
 public function end()
 {
     $parser = new CSS\Parser($this->text);
     $this->document->appendStyleSheet($parser->parse());
 }
Пример #11
0
 /**
  * @dataProvider createBackgroundShorthandProvider
  * */
 public function testCreateBackgroundShorthand($sCss, $sExpected)
 {
     $oParser = new Parser($sCss);
     $oDoc = $oParser->parse();
     foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
         $oDeclaration->createBackgroundShorthand();
     }
     $this->assertSame(trim((string) $oDoc), $sExpected);
 }
Пример #12
0
 public function parseCSS()
 {
     if (!isset($this->cssString[$this->getDocumentID()])) {
         $this->cssString[$this->getDocumentID()] = file_get_contents(dirname(__FILE__) . '/Resources/default.css');
     }
     foreach (PhpQuery::pq('style', $this->getDocumentID()) as $style) {
         $this->cssString[$this->getDocumentID()] .= PhpQuery::pq($style)->text();
     }
     $CssParser = new CssParser($this->cssString[$this->getDocumentID()]);
     $CssDocument = $CssParser->parse();
     foreach ($CssDocument->getAllRuleSets() as $ruleset) {
         foreach ($ruleset->getSelector() as $selector) {
             $specificity = $selector->getSpecificity();
             foreach (PhpQuery::pq($selector->getSelector(), $this->getDocumentID()) as $el) {
                 $existing = pq($el)->data('phpquery_css');
                 if (PhpQuery::$enableCssShorthand) {
                     $ruleset->expandShorthands();
                 }
                 foreach ($ruleset->getRules() as $value) {
                     $rule = $value->getRule();
                     if (!isset($existing[$rule]) || $existing[$rule]['specificity'] <= $specificity) {
                         $value = $value->getValue();
                         $value = is_object($value) ? $value->__toString() : $value;
                         $existing[$rule] = array('specificity' => $specificity, 'value' => $value);
                     }
                 }
                 PhpQuery::pq($el)->data('phpquery_css', $existing);
                 $this->bubbleCSS(PhpQuery::pq($el));
             }
         }
     }
     foreach (PhpQuery::pq('*', $this->getDocumentID()) as $el) {
         $existing = pq($el)->data('phpquery_css');
         $style = pq($el)->attr('style');
         $style = strlen($style) ? explode(';', $style) : array();
         foreach ($this->attribute_css_mapping as $map => $css_equivalent) {
             if ($el->hasAttribute($map)) {
                 $style[] = $css_equivalent . ':' . pq($el)->attr($map);
             }
         }
         if (count($style)) {
             $CssParser = new CssParser('#ruleset {' . implode(';', $style) . '}');
             $CssDocument = $CssParser->parse();
             $ruleset = $CssDocument->getAllRulesets();
             $ruleset = reset($ruleset);
             if (PhpQuery::$enableCssShorthand) {
                 $ruleset->expandShorthands();
             }
             foreach ($ruleset->getRules() as $value) {
                 $rule = $value->getRule();
                 if (!isset($existing[$rule]) || 1000 >= $existing[$rule]['specificity']) {
                     $value = $value->getValue();
                     $value = is_object($value) ? $value->__toString() : $value;
                     $existing[$rule] = array('specificity' => 1000, 'value' => $value);
                 }
             }
             PhpQuery::pq($el)->data('phpquery_css', $existing);
             $this->bubbleCSS(PhpQuery::pq($el));
         }
     }
 }
Пример #13
0
 /**
  * Either sets the CSS property of an object or retrieves the
  * CSS property of a proejct.
  *
  * @param      $property_name
  * @param bool $value
  * @return string of css property value
  * @todo
  */
 public function css($property_name, $value = false)
 {
     if (!isset($this->cssIsParsed[$this->getDocumentID()]) || $this->cssIsParsed[$this->getDocumentID()] === false) {
         $this->parseCSS();
     }
     $data = PhpQuery::data($this->get(0), 'phpquery_css', null, $this->getDocumentID());
     if (!$value) {
         if (isset($data[$property_name])) {
             return $data[$property_name]['value'];
         }
         return null;
     }
     $specificity = isset($data[$property_name]) ? $data[$property_name]['specificity'] + 1 : 1000;
     $data[$property_name] = array('specificity' => $specificity, 'value' => $value);
     PhpQuery::data($this->get(0), 'phpquery_css', $data, $this->getDocumentID());
     $this->bubbleCSS(PhpQuery::pq($this->get(0), $this->getDocumentID()));
     if ($specificity >= 1000) {
         $styles = array();
         foreach ($this->data('phpquery_css') as $k => $v) {
             if ($v['specificity'] >= 1000) {
                 $styles[$k] = trim($k) . ':' . trim($v['value']);
             }
         }
         ksort($styles);
         if (empty($styles)) {
             $this->removeAttr('style');
         } elseif (PhpQuery::$enableCssShorthand) {
             $parser = new \Sabberworm\CSS\Parser('{' . join(';', $styles) . '}');
             $doc = $parser->parse();
             $doc->createShorthands();
             $style = trim($doc->__toString(), "\n\r\t {}");
         } else {
             $style = join(';', $styles);
         }
         $this->attr('style', $style);
     }
 }
 /**
  * Begins parsing the CSS file
  */
 public function parse()
 {
     $css = new Parser(file_get_contents($this->path));
     $this->leanCSS = $css->parse();
     $this->blubberCSS = clone $this->leanCSS;
 }
 /**
  * Saves the "lean" CSS to the filesystem	 
  */
 public function saveLean()
 {
     $fh = fopen($this->getDirname() . '/' . $this->getLeanName(), 'w');
     fwrite($fh, $this->leanCSS->render());
 }
Пример #16
0
 function testTopLevelCommentExtracting()
 {
     $parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;}');
     $doc = $parser->parse();
     $contents = $doc->getContents();
     $comments = $contents[0]->getComments();
     $this->assertCount(1, $comments);
     $this->assertEquals("Find Me!", $comments[0]->getComment());
 }
Пример #17
0
 /**
  * 
  * @param string $file css file path
  */
 function __construct($file = "style.css")
 {
     $this->origenl_css = file_get_contents($file);
     $this->parser = new CSS\Parser($this->origenl_css, CSS\Settings::create()->withMultibyteSupport(false));
     $this->document = $this->parser->parse();
 }