Skip to content

Commit

Permalink
add simple html dom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabrisrp committed Jun 10, 2024
1 parent 34a03bf commit 43053a4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"require": {
"php": ">=7.3",
"cloudflare/cf-ip-rewrite": "^1.0",
"composer/installers": "^1.0 || ^2.0"
"composer/installers": "^1.0 || ^2.0",
"voku/simple_html_dom": "^4.8"
},
"require-dev": {
"php": "^7 || ^8",
Expand Down
2 changes: 2 additions & 0 deletions inc/Engine/Media/Lazyload/Content/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public function set_processor( $processor ) {
$this->processor = new Dom();
} elseif ( 'regex' === $processor ) {
$this->processor = new Regex();
} elseif ( 'simple_html_dom' === $processor ) {
$this->processor = new SimpleHtmlDom();
}
}

Expand Down
56 changes: 56 additions & 0 deletions inc/Engine/Media/Lazyload/Content/Processor/SimpleHtmlDom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Lazyload\Content\Processor;

use voku\helper\HtmlDomParser;
use voku\helper\SimpleHtmlDomBlank;

class SimpleHtmlDom {
public function add_locations_hash_to_html( $html ) {
$dom = HtmlDomParser::str_get_html( $html );

$body = $dom->getElementByTagName( 'body' );

if ( $body instanceof SimpleHtmlDomBlank ) {
return $html;
}

$this->add_hash_to_element( $body, 2 );

return $dom->save();
}

private function add_hash_to_element( $element, $depth ) {
if ( $depth < 0 ) {
return;
}

$skip_tags = [
'DIV',
'MAIN',
'FOOTER',
'SECTION',
'ARTICLE',
'HEADER',
];

foreach ( $element->childNodes() as $child ) {
if ( ! in_array( strtoupper( $child->getTag() ), $skip_tags, true ) ) {
continue;
}

// Calculate the hash of the opening tag.
$child_html = $child->html();
$opening_tag_html = strstr( $child_html, '>', true ) . '>';

$hash = md5( $opening_tag_html );

// Add the data-rocket-location-hash attribute.
$child->setAttribute( 'data-rocket-location-hash', $hash );

// Recursively process child elements.
$this->add_hash_to_element( $child, $depth - 1 );
}
}
}
2 changes: 1 addition & 1 deletion inc/Engine/Media/Lazyload/Content/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function get_subscribed_events(): array {
public function lazyload_content( string $buffer ): string {
$start = hrtime( true );

$this->processor->set_processor( 'regex' );
$this->processor->set_processor( 'simple_html_dom' );

$buffer = $this->processor->get_processor()->add_locations_hash_to_html( $buffer );

Expand Down

0 comments on commit 43053a4

Please sign in to comment.