PHP code example of bayareawebpro / laravel-dom-pipeline
1. Go to this page and download the library: Download bayareawebpro/laravel-dom-pipeline library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bayareawebpro / laravel-dom-pipeline example snippets
use BayAreaWebPro\DomPipeline\DomPipeline;
use My\Pipes\{
LazyLoadImageTags,
LazyLoadVideoTags,
BuildTableOfContents
};
$modified = DomPipeline::make($html, [
LazyLoadImageTags::class,
LazyLoadVideoTags::class,
BuildTableOfContents::class,
]);
use DOMDocument;
use DOMXPath;
use Closure;
class UpdateHeaders{
public function handle(DOMDocument $dom, Closure $next)
{
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//h1|//h2|//h3|//h4|//h5|//h6') as $node) {
// Change the header tags content.
$node->nodeValue = "This is a {$node->tagName} tag.";
}
return $next($dom);
}
}
declare(strict_types=1);
namespace App\Services\Html\Formatters;
use Closure;
use DOMElement;
use DOMDocument;
class LazyLoadImages
{
/**
* @param DOMDocument $dom
* @param Closure $next
* @return mixed
*/
public function handle(DOMDocument $dom, Closure $next)
{
foreach ($dom->getElementsByTagName('img') as $node) {
$this->lazyLoad($node);
}
return $next($dom);
}
/**
* @param DOMDocument $dom
* @param DOMElement $node
*/
protected function lazyLoad(DOMElement $node): void
{
if (!$node->hasAttribute('data-src')) {
// Set the data-src attribute.
$node->setAttribute('data-src', $node->getAttribute('src'));
// Set the src attribute to loading image.
$node->setAttribute('src', asset('images/loading.gif'));
// Merge the lazy load class into the class list.
$node->setAttribute('class',join(' ', [
$node->getAttribute('class'),
'lazy-load'
]));
}
}
}
declare(strict_types=1);
namespace App\Services\Html\Formatters;
use Closure;
use DOMElement;
use DOMDocument;
class LazyLoadVideos
{
/**
* @param DOMDocument $dom
* @param Closure $next
* @return mixed
*/
public function handle(DOMDocument $dom, Closure $next)
{
$xpath = new \DOMXPath($dom);
foreach($xpath->query('//iframe[@class="media"]') as $node){
$this->lazyLoad($dom, $node);
}
return $next($dom);
}
/**
* @param DOMDocument $dom
* @param DOMElement $node
*/
protected function lazyLoad(DOMDocument $dom, DOMElement $node): void
{
if(is_null($node->parentNode)) return;
// Match the YouTube Video ID.
// https://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i',
(string) $node->getAttribute('src'), $matches
);
if(isset($matches[1])){
// Create a new HTML fragment.
$fragment = $dom->createDocumentFragment();
$fragment->appendXML(<<<HTML
<v-video id="$matches[1]" label="Click to play..." :show-image="true"></v-video>
HTML);
// Replace Self with Fragment.
$node->parentNode->replaceChild($fragment, $node);
}
}
}
declare(strict_types=1);
namespace App\Services\Html\Formatters;
use DOMElement;
use Closure;
use StdClass;
use DOMDocument;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
class TableOfContents
{
/**
* @param DOMDocument $dom
* @param Closure $next
* @return mixed
*/
public function handle(DOMDocument $dom, Closure $next)
{
$nodes = Collection::make();
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//h1|//h2|//h3|//h4|//h5|//h6') as $node) {
$text = strip_tags(html_entity_decode((string) $node->nodeValue));
$nodes->push($this->makeBookmark($node, $text));
}
if($nodes->count() > 5){
View::share('tableOfContents', $nodes->take(25));
}
return $next($dom);
}
/**
* @param DOMElement $node
* @param string $text
* @return StdClass
*/
protected function makeBookmark(DOMElement $node, string $text): StdClass
{
// Create the bookmark item.
$bookmark = (object)[
'anchor' => Str::slug($text),
'text' => Str::title(Str::replaceLast('.', '', $text)),
];
// Link the bookmark to the header using the ID.
$node->setAttribute('id', $bookmark->anchor);
return $bookmark;
}
}
declare(strict_types=1);
namespace App\Services\Html\Formatters;
use Closure;
use DOMElement;
use DOMDocument;
class Tables
{
/**
* @param DOMDocument $dom
* @param \Closure $next
* @return mixed
*/
public function handle(DOMDocument $dom, Closure $next)
{
$xpath = new \DOMXPath($dom);
foreach($xpath->query('//table') as $node){
$this->makeResponsive($dom, $node);
}
return $next($dom);
}
/**
* Wrap Responsive
* @param DOMDocument $dom
* @param DOMElement $node
*/
protected function makeResponsive(DOMDocument $dom, DOMElement $node): void
{
if(is_null($node->parentNode)) return;
// Create the wrapper element.
$div = $dom->createElement('div');
// Apply classes to the element.
$div->setAttribute('class','table table-responsive table-striped table-hover');
// Clone and append the table to the element.
$div->appendChild($node->cloneNode(true));
// Swap the table for the new wrapped version.
$node->parentNode->replaceChild($div,$node);
}
}