Exemplo n.º 1
0
 /**
  * Extract the css files from a html code
  *
  * @param  string $html
  *
  * @return string
  */
 public function extractStyle($html)
 {
     // the CSS content
     $style = ' ';
     // extract the link tags, and remove them in the html code
     preg_match_all('/<link([^>]*)>/isU', $html, $match);
     $html = preg_replace('/<link[^>]*>/isU', '', $html);
     $html = preg_replace('/<\\/link[^>]*>/isU', '', $html);
     // analyse each link tag
     foreach ($match[1] as $code) {
         $tmp = $this->tagParser->extractTagAttributes($code);
         // if type text/css => we keep it
         if (isset($tmp['type']) && strtolower($tmp['type']) == 'text/css' && isset($tmp['href'])) {
             // get the href
             $url = $tmp['href'];
             // get the content of the css file
             $content = @file_get_contents($url);
             // if "http://" in the url
             if (strpos($url, 'http://') !== false) {
                 // get the domain "http://xxx/"
                 $url = str_replace('http://', '', $url);
                 $url = explode('/', $url);
                 $urlMain = 'http://' . $url[0] . '/';
                 // get the absolute url of the path
                 $urlSelf = $url;
                 unset($urlSelf[count($urlSelf) - 1]);
                 $urlSelf = 'http://' . implode('/', $urlSelf) . '/';
                 // adapt the url in the css content
                 $content = preg_replace('/url\\(([^\\\\][^)]*)\\)/isU', 'url(' . $urlSelf . '$1)', $content);
                 $content = preg_replace('/url\\((\\\\[^)]*)\\)/isU', 'url(' . $urlMain . '$1)', $content);
             } else {
                 // @TODO correction on url in absolute on a local css content
                 // $content = preg_replace('/url\(([^)]*)\)/isU', 'url('.dirname($url).'/$1)', $content);
             }
             // add to the CSS content
             $style .= $content . "\n";
         }
     }
     // extract the style tags des tags style, and remove them in the html code
     preg_match_all('/<style[^>]*>(.*)<\\/style[^>]*>/isU', $html, $match);
     $html = preg_replace('/<style[^>]*>(.*)<\\/style[^>]*>/isU', '', $html);
     // analyse each style tags
     foreach ($match[1] as $code) {
         // add to the CSS content
         $code = str_replace('<!--', '', $code);
         $code = str_replace('-->', '', $code);
         $style .= $code . "\n";
     }
     //analyse the css content
     $this->analyseStyle($style);
     return $html;
 }
Exemplo n.º 2
0
 /**
  * @param string $code
  * @param array  $expected
  *
  * @dataProvider tagAttributesProvider
  */
 public function testExtractTagAttributes($code, $expected)
 {
     $result = $this->parser->extractTagAttributes($code);
     $this->assertEquals($expected, $result);
 }