forked from laysense/laysense.cn
Manage dynamic blocks and expand the editor library
This commit is contained in:
+17
-1
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user