Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pejman-hkh committed Oct 25, 2023
1 parent b8d4b4e commit 4a84ed6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
37 changes: 16 additions & 21 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) == '/' ) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -284,36 +279,34 @@ 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;
}

return $tag;
}

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;
}

Expand All @@ -323,6 +316,7 @@ function parse( &$parent = '' ) {
return $tags;
}


function find( $query = '', $index = [] ) {
$f = new Find();
return $f->find( $query, $this->document->childrens, $index );
Expand All @@ -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;

}
}

}
18 changes: 7 additions & 11 deletions src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -31,7 +28,6 @@ function find( $query, $index = []) {

function remove() {
unset( $this->parent->childrens[ $this->eq ] );
$this->updateParentsHtml( $this->parent );
}

private function notEmpty() {
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 4a84ed6

Please sign in to comment.