Exemplo n.º 1
0
 /**
  * Checks if node matches css query filter ":only-of-type"
  * @return bool
  * @see match()
  * @access private
  */
 protected function filter_onlytype()
 {
     if ($this->parent === null) {
         return false;
     } else {
         return count($this->parent->getChildrenByTag($this->tag, 'total', false)) === 1;
     }
 }
Exemplo n.º 2
0
 /**
  * Minifies javascript using JSMin+
  * @param HTML_Node $root
  * @param string $indent_string
  * @param bool $wrap_comment Wrap javascript in HTML comments (<!-- ~text~ //-->)
  * @param bool $recursive
  * @return bool|array Array of errors on failure, true on succes
  */
 static function minify_javascript(&$root, $indent_string = ' ', $wrap_comment = true, $recursive = true)
 {
     #php4 JSMin+ doesn't support PHP4
     #return true;
     #php4e
     #php5
     include_once 'third party/jsminplus.php';
     $errors = array();
     foreach ($root->select('script:not-empty > "~text~"', false, $recursive, true) as $c) {
         try {
             $text = $c->text;
             while ($text) {
                 $text = trim($text);
                 //Remove comment/CDATA tags at begin and end
                 if (substr($text, 0, 4) === '<!--') {
                     $text = substr($text, 5);
                     continue;
                 } elseif (strtolower(substr($text, 0, 9)) === '<![cdata[') {
                     $text = substr($text, 10);
                     continue;
                 }
                 if (($end = substr($text, -3)) && ($end === '-->' || $end === ']]>')) {
                     $text = substr($text, 0, -3);
                     continue;
                 }
                 break;
             }
             if (trim($text)) {
                 $text = JSMinPlus::minify($text);
                 if ($wrap_comment) {
                     $text = "<!--\n" . $text . "\n//-->";
                 }
                 if ($indent_string && ($wrap_comment || strpos($text, "\n") !== false)) {
                     $text = indent_text("\n" . $text, $c->indent(), $indent_string);
                 }
             }
             $c->text = $text;
         } catch (Exception $e) {
             $errors[] = array($e, $c->parent->dumpLocation());
         }
     }
     return $errors ? $errors : true;
     #php5e
 }
Exemplo n.º 3
0
 /**
  * Evaluate root node using custom callback
  * @param array $conditions {@link parse_conditions()}
  * @param bool|int $recursive
  * @param bool $check_root
  * @return HTML_Node[]
  * @access private
  */
 protected function parse_callback($conditions, $recursive = true, $check_root = false)
 {
     return $this->result = $this->root->getChildrenByMatch($conditions, $recursive, $check_root, $this->custom_filter_map);
 }
Exemplo n.º 4
0
 /**
  * Performs a css select query on the root node
  * @param string $query
  * @param int|bool $index
  * @param bool $recursive
  * @param bool $check_self
  * @return HTML_Node|HTML_Node[]
  * @see HTML_Node::select()
  */
 public function select($query = '*', $index = false, $recursive = true, $check_self = false)
 {
     return $this->root->select($query, $index, $recursive, $check_self);
 }
Exemplo n.º 5
0
 /**
  * Minifies HTML / removes unneeded whitespace
  * @param HTML_Node $root
  * @param bool $strip_comments
  * @param bool $recursive
  */
 static function minify_html(&$root, $strip_comments = true, $recursive = true)
 {
     if ($strip_comments) {
         foreach ($root->select(':comment', false, $recursive, true) as $c) {
             $prev = $c->getSibling(-1);
             $next = $c->getSibling(1);
             $c->delete();
             if ($prev && $next && $prev->isText() && $next->isText()) {
                 $prev->text .= $next->text;
                 $next->delete();
             }
         }
     }
     foreach ($root->select('(!pre + !xmp + !style + !script + !"?php" + !"~text~" + !"~comment~"):not-empty > "~text~"', false, $recursive, true) as $c) {
         $c->text = preg_replace('`\\s+`', ' ', $c->text);
     }
 }