Biete im Fließtext Schriftgröße, Schriftfarbe und Ausrichtung an
Das Webprofil von FormattedText kennt zusätzlich Ausrichtung (zentriert, rechtsbündig), Schriftgröße (klein, groß, sehr groß) und Schriftfarbe (Marine, Grün, Rot, Grau) aus festen Werten. Verwaltung, Dokumente und PDF lassen sie weg. Der Editor des Fließtexts bekommt die Werkzeuge dafür (eigene Erweiterung für die Größe); auf der Seite werden Klassen und Tokens statt Inline-Stilen ausgegeben, Farben passen sich dunklen und grünen Flächen an. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
28bd5590bc
commit
57f3a18b82
18 files changed
+565
-34
No files matched your search
@@ -34,7 +34,16 @@ final class FormattedText
|
||||
/** Angaben der Redaktion, die formatiert gespeichert werden. Suchbegriffe, Titel und Teaser bleiben Klartext. */
|
||||
public const FIELDS = ['summary', 'description'];
|
||||
|
||||
/** @var array<string, HtmlSanitizer> je Linkregel eine Bereinigung (siehe sanitizer()) */
|
||||
/** Schriftgrößen der Website (Schlüssel => Bezeichnung); nur im Profil `$site`. Die Werte stehen in `widgets.css`. */
|
||||
public const SITE_SIZES = ['small' => 'Klein', 'large' => 'Groß', 'xlarge' => 'Sehr groß'];
|
||||
|
||||
/** Schriftfarben der Website (Schlüssel => Bezeichnung); nur im Profil `$site`. Die Farbwerte stehen in `widgets.css`. */
|
||||
public const SITE_COLORS = ['marine' => 'Marine', 'gruen' => 'Grün', 'rot' => 'Rot', 'grau' => 'Grau'];
|
||||
|
||||
/** Ausrichtungen der Website außer der Vorgabe „linksbündig“ (so schreibt der Editor sie: `end` heißt rechtsbündig). */
|
||||
private const SITE_ALIGNMENTS = ['center', 'end'];
|
||||
|
||||
/** @var array<string, HtmlSanitizer> je Profil eine Bereinigung (siehe sanitizer()) */
|
||||
private static array $sanitizers = [];
|
||||
|
||||
/** Symbolleiste der Editoren; muss zur Auswahl oben passen. */
|
||||
@@ -55,11 +64,13 @@ public static function isHtml(?string $value): bool
|
||||
/**
|
||||
* Kanonische Form eines gespeicherten oder eingegebenen Werts (HTML oder Klartext). Leer, wenn kein Text übrig ist.
|
||||
*
|
||||
* `$internalLinks`: Links auf Seiten der eigenen Website (`/materialien`, `#anker`) bleiben erhalten. Das gilt für
|
||||
* Texte der Website (CMS); Verwaltung und PDF kennen nur ausdrückliche Adressen (http, https, mailto). Die Form
|
||||
* gehört zum Text: Wer mit `true` speichert, liest auch mit `true`.
|
||||
* `$site` ist das Profil der Website (CMS): Links auf Seiten der eigenen Website (`/materialien`, `#anker`) bleiben
|
||||
* erhalten, dazu Ausrichtung (zentriert, rechtsbündig), Schriftgröße und Schriftfarbe aus festen Werten
|
||||
* ({@see self::SITE_SIZES}, {@see self::SITE_COLORS}). Verwaltung und PDF kennen nur ausdrückliche Adressen
|
||||
* (http, https, mailto) und keine dieser Auszeichnungen; sie fallen dort beim Speichern weg. Das Profil gehört zum Text:
|
||||
* Wer mit `true` speichert, liest auch mit `true`.
|
||||
*/
|
||||
public static function canonical(?string $value, bool $internalLinks = false): string
|
||||
public static function canonical(?string $value, bool $site = false): string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
|
||||
@@ -67,18 +78,18 @@ public static function canonical(?string $value, bool $internalLinks = false): s
|
||||
return '';
|
||||
}
|
||||
|
||||
return self::normalize(self::isHtml($value) ? $value : self::legacyToHtml($value), $internalLinks);
|
||||
return self::normalize(self::isHtml($value) ? $value : self::legacyToHtml($value), $site);
|
||||
}
|
||||
|
||||
/**
|
||||
* Kanonische Form eines Werts, der sicher HTML ist (zum Beispiel aus der Quelle Shinkan), auch wenn er nicht mit
|
||||
* einem Blockelement beginnt.
|
||||
*/
|
||||
public static function canonicalHtml(?string $html, bool $internalLinks = false): string
|
||||
public static function canonicalHtml(?string $html, bool $site = false): string
|
||||
{
|
||||
$html = trim((string) $html);
|
||||
|
||||
return $html === '' ? '' : self::normalize($html, $internalLinks);
|
||||
return $html === '' ? '' : self::normalize($html, $site);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,9 +101,9 @@ public static function canonicalHtml(?string $html, bool $internalLinks = false)
|
||||
*
|
||||
* @param (callable(int): ?array{src?: string, alt?: string, note?: string})|null $images
|
||||
*/
|
||||
public static function html(?string $value, ?callable $images = null, bool $internalLinks = false): string
|
||||
public static function html(?string $value, ?callable $images = null, bool $site = false): string
|
||||
{
|
||||
return self::renderImages(self::canonical($value, $internalLinks), $images);
|
||||
return self::renderImages(self::canonical($value, $site), $images);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -459,10 +470,10 @@ public static function legacyToHtml(?string $text): string
|
||||
return $html;
|
||||
}
|
||||
|
||||
private static function normalize(string $html, bool $internalLinks = false): string
|
||||
private static function normalize(string $html, bool $site = false): string
|
||||
{
|
||||
$html = self::mapLegacyTags($html);
|
||||
$html = trim(self::sanitizer($internalLinks)->sanitize($html));
|
||||
$html = trim(self::sanitizer($site)->sanitize($html));
|
||||
|
||||
if ($html === '') {
|
||||
return '';
|
||||
@@ -471,6 +482,10 @@ private static function normalize(string $html, bool $internalLinks = false): st
|
||||
$document = HTMLDocument::createFromString('<body>'.$html.'</body>', LIBXML_NOERROR);
|
||||
$body = $document->body;
|
||||
|
||||
if ($site) {
|
||||
self::siteMarks($document);
|
||||
}
|
||||
|
||||
// Ein Bild steht zwischen den Absätzen, in einem Absatz (so setzt der Editor es) oder in einer Tabellenzelle und braucht
|
||||
// seine Bildfassung. Anderswo (Liste, Zitat, Überschrift) entfällt es.
|
||||
foreach (iterator_to_array($document->querySelectorAll('img')) as $image) {
|
||||
@@ -533,7 +548,7 @@ private static function block(Node $node): string
|
||||
|
||||
$inner = self::inner($child);
|
||||
|
||||
if (! str_contains($inner, '<p>')) {
|
||||
if (preg_match('/<p[\s>]/', $inner) !== 1) {
|
||||
$inner = '<p>'.$inner.'</p>';
|
||||
}
|
||||
|
||||
@@ -547,11 +562,67 @@ private static function block(Node $node): string
|
||||
|
||||
$inner = self::inner($node);
|
||||
|
||||
if ($name === 'blockquote' && ! str_contains($inner, '<p>')) {
|
||||
if ($name === 'blockquote' && preg_match('/<p[\s>]/', $inner) !== 1) {
|
||||
$inner = '<p>'.$inner.'</p>';
|
||||
}
|
||||
|
||||
return self::hasContent($inner) ? '<'.$name.'>'.$inner.'</'.$name.'>' : '';
|
||||
// Die Ausrichtung ist im Webprofil schon auf `text-align: center|end` zurückgeführt (siehe siteMarks()).
|
||||
$align = $name !== 'blockquote' && preg_match('/^text-align: (?:'.implode('|', self::SITE_ALIGNMENTS).')$/', (string) $node->getAttribute('style')) === 1
|
||||
? ' style="'.$node->getAttribute('style').'"'
|
||||
: '';
|
||||
|
||||
return self::hasContent($inner) ? '<'.$name.$align.'>'.$inner.'</'.$name.'>' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Webprofil: Ausrichtung, Größe und Farbe auf feste Werte zurückführen. Stil und Klassen aus dem Editor oder von Hand
|
||||
* sind nie maßgeblich: Ein Span trägt nur noch `class="color" data-color` (bekannter Schlüssel) und/oder `data-size`
|
||||
* (bekannter Schlüssel), sonst entfällt er; Absatz und Zwischenüberschrift nur `style="text-align: center|end"`. So
|
||||
* liest der Editor den Wert beim Öffnen wieder, und ein beliebiger Stil kommt nie auf die Seite.
|
||||
*/
|
||||
private static function siteMarks(HTMLDocument $document): void
|
||||
{
|
||||
foreach (iterator_to_array($document->querySelectorAll('span')) as $span) {
|
||||
$color = (string) $span->getAttribute('data-color');
|
||||
$size = (string) $span->getAttribute('data-size');
|
||||
$color = array_key_exists($color, self::SITE_COLORS) ? $color : '';
|
||||
$size = array_key_exists($size, self::SITE_SIZES) ? $size : '';
|
||||
|
||||
if ($color === '' && $size === '') {
|
||||
while ($span->firstChild !== null) {
|
||||
$span->parentNode->insertBefore($span->firstChild, $span);
|
||||
}
|
||||
|
||||
$span->remove();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (iterator_to_array($span->attributes) as $attribute) {
|
||||
$span->removeAttribute($attribute->name);
|
||||
}
|
||||
|
||||
if ($color !== '') {
|
||||
$span->setAttribute('class', 'color');
|
||||
$span->setAttribute('data-color', $color);
|
||||
}
|
||||
|
||||
if ($size !== '') {
|
||||
$span->setAttribute('data-size', $size);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($document->querySelectorAll('p, h3, h4, h5, h6') as $block) {
|
||||
$align = preg_match('/(?:^|;)\s*text-align\s*:\s*(center|end|right)\s*(?:;|$)/i', (string) $block->getAttribute('style'), $match) === 1
|
||||
? (strtolower($match[1]) === 'center' ? 'center' : 'end')
|
||||
: null;
|
||||
|
||||
$block->removeAttribute('style');
|
||||
|
||||
if ($align !== null) {
|
||||
$block->setAttribute('style', 'text-align: '.$align);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -699,16 +770,29 @@ private static function runsText(array $runs): string
|
||||
return trim(implode('', array_map(fn (array $run): string => $run['text'], $runs)));
|
||||
}
|
||||
|
||||
private static function sanitizer(bool $internalLinks = false): HtmlSanitizer
|
||||
private static function sanitizer(bool $site = false): HtmlSanitizer
|
||||
{
|
||||
return self::$sanitizers[$internalLinks ? 'site' : 'catalog'] ??= new HtmlSanitizer(
|
||||
(new HtmlSanitizerConfig)
|
||||
->allowElement('p')
|
||||
if (isset(self::$sanitizers[$site ? 'site' : 'catalog'])) {
|
||||
return self::$sanitizers[$site ? 'site' : 'catalog'];
|
||||
}
|
||||
|
||||
// Das Webprofil lässt zusätzlich Spans (Größe, Farbe) und den Stil an Absätzen und Überschriften zu; siteMarks()
|
||||
// führt beides danach auf feste Werte zurück. Verwaltung und PDF kennen weder Spans noch Stile.
|
||||
$text = $site ? ['style'] : [];
|
||||
$config = (new HtmlSanitizerConfig)
|
||||
->allowElement('p', $text)
|
||||
->allowElement('h3', $text)
|
||||
->allowElement('h4', $text)
|
||||
->allowElement('h5', $text)
|
||||
->allowElement('h6', $text);
|
||||
|
||||
if ($site) {
|
||||
$config = $config->allowElement('span', ['class', 'data-color', 'data-size']);
|
||||
}
|
||||
|
||||
return self::$sanitizers[$site ? 'site' : 'catalog'] = new HtmlSanitizer(
|
||||
$config
|
||||
->allowElement('br')
|
||||
->allowElement('h3')
|
||||
->allowElement('h4')
|
||||
->allowElement('h5')
|
||||
->allowElement('h6')
|
||||
->allowElement('strong')
|
||||
->allowElement('em')
|
||||
->allowElement('ul')
|
||||
@@ -725,7 +809,7 @@ private static function sanitizer(bool $internalLinks = false): HtmlSanitizer
|
||||
->allowElement('img', ['data-id', 'alt'])
|
||||
->allowElement('a', ['href'])
|
||||
->allowLinkSchemes(['http', 'https', 'mailto'])
|
||||
->allowRelativeLinks($internalLinks)
|
||||
->allowRelativeLinks($site)
|
||||
->forceAttribute('a', 'rel', 'noopener noreferrer')
|
||||
->withMaxInputLength(100000),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user