Manage dynamic blocks and expand the editor library

This commit is contained in:
2026-11-22 00:59:59 +08:00
parent 828dfc09f9
commit 92354d7e4a
63 changed files with 671 additions and 188 deletions
+17 -1
View File
@@ -88,7 +88,7 @@ final class Html
return $result;
}
public static function sanitize(string $html): string
public static function sanitize(string $html, ?string $anchorPrefix = null): string
{
$document = new DOMDocument('1.0', 'UTF-8');
$document->loadHTML('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>' . $html . '</body></html>', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
@@ -97,6 +97,22 @@ final class Html
return '';
}
self::sanitizeChildren($body);
if ($anchorPrefix !== null) {
$ids = [];
foreach ($body->getElementsByTagName('*') as $element) {
$id = $element->getAttribute('id');
if ($id !== '') {
$ids[$id] = $anchorPrefix . '-' . $id;
$element->setAttribute('id', $ids[$id]);
}
}
foreach ($body->getElementsByTagName('a') as $link) {
$href = $link->getAttribute('href');
if (str_starts_with($href, '#') && isset($ids[substr($href, 1)])) {
$link->setAttribute('href', '#' . $ids[substr($href, 1)]);
}
}
}
$output = '';
foreach ($body->childNodes as $child) {
$output .= $document->saveHTML($child);