Skip to content

Commit

Permalink
Add missing classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jun 14, 2024
1 parent a9dc0cb commit de47fc8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/HtmlSanitizer/ReadmeImageSanitizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\HtmlSanitizer;

use Composer\Pcre\Preg;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
use Symfony\Component\HtmlSanitizer\Visitor\AttributeSanitizer\AttributeSanitizerInterface;

class ReadmeImageSanitizer implements AttributeSanitizerInterface
{
public function __construct(private string|null $host, private string $ownerRepo, private string $basePath)
{
}

public function getSupportedAttributes(): ?array
{
return ['src'];
}

public function getSupportedElements(): ?array
{
return ['img'];
}

public function sanitizeAttribute(string $element, string $attribute, string $value, HtmlSanitizerConfig $config): ?string
{
if (!str_contains($value, '//')) {
return match ($this->host) {
'github.com' => 'https://raw.github.com/'.$this->ownerRepo.'/HEAD/'.$this->basePath.$value,
'gitlab.com' => 'https://gitlab.com/'.$this->ownerRepo.'/-/raw/HEAD/'.$this->basePath.$value,
'bitbucket.org' => 'https://bitbucket.org/'.$this->ownerRepo.'/raw/HEAD/'.$this->basePath.$value,
default => $value,
};
}

if (str_starts_with($value, 'https://private-user-images.githubusercontent.com/')) {
return Preg::replace('{^https://private-}', 'https://', $value, 1);
}

return $value;
}
}
64 changes: 64 additions & 0 deletions src/HtmlSanitizer/ReadmeLinkSanitizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\HtmlSanitizer;

use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
use Symfony\Component\HtmlSanitizer\Visitor\AttributeSanitizer\AttributeSanitizerInterface;

class ReadmeLinkSanitizer implements AttributeSanitizerInterface
{
public function __construct(private string|null $host, private string $ownerRepo, private string $basePath)
{
}

public function getSupportedAttributes(): ?array
{
return ['href', 'target', 'id'];
}

public function getSupportedElements(): ?array
{
return ['a'];
}

/**
* @param 'href'|'target'|'id' $attribute
* @param string $value
*/
public function sanitizeAttribute(string $element, string $attribute, string $value, HtmlSanitizerConfig $config): ?string
{
if ($attribute === 'target') {
if ($value !== '') {
return '_blank';
}

return null;
}

if ($attribute === 'id') {
if (!str_starts_with($value, 'user-content-')) {
return 'user-content-'.$value;
}

return $value;
}

if (str_starts_with($value, '#') && !str_starts_with($value, '#user-content-')) {
return '#user-content-'.substr($value, 1);
}

if (str_starts_with($value, 'mailto:')) {
return $value;
}

if ($this->host === 'github.com' && !str_contains($value, '//')) {
return 'https://github.com/'.$this->ownerRepo.'/blob/HEAD/'.$this->basePath.$value;
}

if ($this->host === 'gitlab.com' && !str_contains($value, '//')) {
return 'https://gitlab.com/'.$this->ownerRepo.'/-/blob/HEAD/'.$this->basePath.$value;
}

return $value;
}
}

0 comments on commit de47fc8

Please sign in to comment.