Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Static Analysis

permissions:
contents: read
packages: read

on:
push:
branches:
- main
pull_request:

env:
PHP_VERSION: 8.4
PSALM_VERSION: 6.14.3

jobs:
Psalm:
name: Psalm
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHP_VERSION }}
coverage: none
tools: composer:v2
- uses: actions/checkout@v4
- run: composer install --no-interaction --no-progress --ansi --no-scripts
- name: Run Psalm
run: |
docker pull --quiet ghcr.io/webfactory/psalm:$PSALM_VERSION
docker run --tty -v $(pwd):/app -v $HOME/cache:/cache ghcr.io/webfactory/psalm:$PSALM_VERSION --show-info=false --stats --output-format=github
18 changes: 18 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
resolveFromConfigFile="true"
memoizeMethodCallResults="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
</projectFiles>

<issueHandlers>
<!-- Normal for libraries: classes/methods are used by consumers, not internally -->
<UnusedClass errorLevel="suppress" />
<PossiblyUnusedMethod errorLevel="suppress" />
</issueHandlers>
</psalm>
3 changes: 3 additions & 0 deletions src/Handler/BaseRewriteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
use Dom\Document;
use Dom\Element;
use Dom\XPath;
use Override;
use Webfactory\Html5TagRewriter\RewriteHandler;

/**
* Abstract base class for RewriteHandler implementations.
*/
abstract class BaseRewriteHandler implements RewriteHandler
{
#[Override]
public function match(Element $element): void
{
}

#[Override]
public function afterMatches(Document $document, XPath $xpath): void
{
}
Expand Down
17 changes: 14 additions & 3 deletions src/Implementation/Html5TagRewriter.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

declare(strict_types=1);

namespace Webfactory\Html5TagRewriter\Implementation;

use Dom\Document;
use Dom\Element;
use Dom\HTMLDocument;
use Dom\Node;
use Dom\XPath;
use Override;
use Webfactory\Html5TagRewriter\RewriteHandler;
use Webfactory\Html5TagRewriter\TagRewriter;

Expand All @@ -14,11 +18,13 @@ final class Html5TagRewriter implements TagRewriter
/** @var list<RewriteHandler> */
private array $rewriteHandlers = [];

#[Override]
public function register(RewriteHandler $handler): void
{
$this->rewriteHandlers[] = $handler;
}

#[Override]
public function process(string $html5): string
{
$document = HTMLDocument::createFromString($html5, LIBXML_NOERROR);
Expand All @@ -28,6 +34,7 @@ public function process(string $html5): string
return $this->cleanup($document->saveHtml());
}

#[Override]
public function processFragment(string $html5Fragment): string
{
$document = HTMLDocument::createEmpty();
Expand All @@ -37,13 +44,16 @@ public function processFragment(string $html5Fragment): string
$temp = $document->createElement('temp');
$temp->innerHTML = $html5Fragment;

while ($temp->firstChild) {
while ($temp->firstChild instanceof Node) {
$container->appendChild($temp->firstChild);
}

$this->applyHandlers($document, $container);

return $this->cleanup($container->innerHTML);
/** @var string */
$innerHTML = $container->innerHTML;

return $this->cleanup($innerHTML);
}

private function applyHandlers(Document $document, Node $context): void
Expand All @@ -54,6 +64,7 @@ private function applyHandlers(Document $document, Node $context): void
$xpath->registerNamespace('mathml', 'http://www.w3.org/1998/Math/MathML');

foreach ($this->rewriteHandlers as $handler) {
/** @var iterable<Element> */
$elements = $xpath->query($handler->appliesTo(), $context);
foreach ($elements as $element) {
$handler->match($element);
Expand All @@ -64,6 +75,6 @@ private function applyHandlers(Document $document, Node $context): void

private function cleanup(string $html): string
{
return preg_replace('#(<esi:([a-z]+)(?:[^>]*))></esi:\\2>#', '$1 />', $html);
return preg_replace('#(<esi:([a-z]+)(?:[^>]*))></esi:\\2>#', '$1 />', $html) ?? $html;
}
}
2 changes: 1 addition & 1 deletion src/TagRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

interface TagRewriter
{
public function register(RewriteHandler $handler);
public function register(RewriteHandler $handler): void;

public function process(string $html5): string;

Expand Down