diff --git a/src/Find.php b/src/Find.php index 1c41746..5528f41 100644 --- a/src/Find.php +++ b/src/Find.php @@ -8,7 +8,7 @@ function findAttr( $attrs = [], $tags ) { $f = true; foreach( $attrs as $attr => $value ) { if( $attr == 'class' ) { - $classes = @$tag->attrs?$tag->attrs->classes():[]; + $classes = isset($tag->attrs)?$tag->attrs->classes():[]; if( ! in_array($value, $classes ) ) $f = false; } else { @@ -22,7 +22,7 @@ function findAttr( $attrs = [], $tags ) { if( $f ) $ret[] = $tag; - if( @$tag->childrens ) { + if( isset($tag->childrens) ) { $found = $this->findAttr( $attrs, $tag->childrens ); foreach( $found as $a ) { $ret[] = $a; diff --git a/src/Parser.php b/src/Parser.php index 1b9834c..6786c18 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -75,7 +75,7 @@ function ParseTag() { if( @$this->html[ $this->i+1 ] == '/' ) $this->i++; $tag = ''; - while( $c1 = @$this->html[ $this->i++ ] ) { + while( ! $this->empty( $c1 = $this->nextTok() ) ) { if( $c1 == '>') { break; @@ -93,7 +93,7 @@ function ParseTag() { $ret = new Tag; $ret->tag = $tag; - if( @$attrs ) + if( isset($attrs) ) $ret->attrs = @$attrs; if( substr($tag,0,1) == '/' ) { @@ -142,8 +142,6 @@ function parseComment() { while( ! $this->empty( $c1 = $this->nextTok() ) ) { if( $this->isEqual('-->') ) break; - //if( $c1 == '-' && $this->html[ $this->i] == '-' && $this->html[ $this->i+1] == '>' ) - // break; $content .= $c1; } @@ -183,10 +181,6 @@ function next1() { if( $this->isEqual('!--') ) return $this->parseComment(); - //if( $this->isEqual('?') ) - //return $this->parsePhp(); - - if( $this->html[ $this->i ] == ' ' ) { $this->i++; $cn = $this->parseContents(); @@ -256,8 +250,9 @@ function parseCData() { function getTag() { $tag = $this->next(); - if( ! @$tag ) + if( ! isset($tag) ) return; + if( $tag->tag == 'cdata' ) { $tag->content = $this->parseCData(); return $tag; @@ -273,7 +268,7 @@ function getTag() { if( in_array( @$tag->tag, $hasNoEndTags ) ) return $tag; - if( @$tag->isEnd ) return $tag; + if( isset($tag->isEnd) ) return $tag; if( $tag->tag == 'script' ) { $content = $this->parseScriptInner(); @@ -284,12 +279,12 @@ function getTag() { $tag->childrens = $childrens; } - if( @$tag->tag == @$this->current->tag ) { + if( isset( $tag->tag ) && @$tag->tag == @$this->current->tag ) { return $tag; } while( $etag = $this->next() ) { - if( $tag->tag == @$etag->tag ) + if( isset( $etag->tag ) && $tag->tag == @$etag->tag ) break; } @@ -297,23 +292,21 @@ function getTag() { } function parse( &$parent = '' ) { - $tags = []; $stag = new Tag; $eq = 0; while( $tag = $this->getTag() ) { - if( @$tag->isEnd && @$parent->tag == @$tag->tag ) break; + if( isset($tag->isEnd) && @$parent->tag == @$tag->tag ) break; - if( @$tag->tag == 'empty' && empty( trim($tag->content) ) ) + if( isset( $tag->tag ) && $tag->tag == 'empty' && empty( trim($tag->content) ) ) continue; - if( ! @$tag->isEnd ) { + if( ! isset($tag->isEnd) ) { $tag->eq = $eq++; $tag->prev = $stag; $tag->parent = $parent; $stag->next = $tag; - //$tag->html = $tag->getHtml(); $tags[] = $tag; } @@ -323,6 +316,7 @@ function parse( &$parent = '' ) { return $tags; } + function find( $query = '', $index = [] ) { $f = new Find(); return $f->find( $query, $this->document->childrens, $index ); @@ -332,13 +326,14 @@ function __construct( $html ) { $this->tags = []; $this->html = $html; $this->i = 0; - $this->id = 0; - + $document = new Tag; $document->tag = 'document'; $document->childrens = $this->parse( $document ); - //$document->html = $document->getHtml(); + \Pejman\DomParser\PQuery::$document = $document; $this->document = $document; + } -} + +} \ No newline at end of file diff --git a/src/Tag.php b/src/Tag.php index 913a96c..125eb21 100644 --- a/src/Tag.php +++ b/src/Tag.php @@ -7,20 +7,17 @@ function __construct() { } function __get( $key ) { - if( method_exists( $this, 'get'.$key ) ) { - return $this->{'get'.$key}(); - } + if( $key == 'html' ) + return $this->getHtml(); - if( @$this->attrs->$key ) + if( isset($this->attrs->$key) ) return $this->attrs->$key; + return @$this->$key; } function __set( $key, $value ) { - if( @$this->attrs->$key ) - $this->attr($key, $value); - $this->$key = $value; } @@ -31,7 +28,6 @@ function find( $query, $index = []) { function remove() { unset( $this->parent->childrens[ $this->eq ] ); - $this->updateParentsHtml( $this->parent ); } private function notEmpty() { @@ -127,7 +123,7 @@ private function concatHtmls( $childrens ) { function updateParentsHtml( $parent ) { $parent->html = $parent->getHtml1(); - if( $parent->parent ) + if( isset($parent->parent) ) $this->updateParentsHtml( $parent->parent ); } @@ -167,10 +163,10 @@ function text() { private function concatTexts( $childrens ) { $html = ''; foreach( $childrens as $child ) { - if( @$child->content ) + if( isset($child->content) ) $html .= $child->content; - if( @$child->childrens ){ + if( isset($child->childrens) ){ if( $t = $this->concatTexts( $child->childrens ) ) $html .= $t; }