From: Marcel Werk Date: Sun, 28 Mar 2021 10:48:19 +0000 (+0200) Subject: Made sure ids in toc are unique X-Git-Tag: 5.3.6~43 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8d77e09bc110b523b9e7fef218dae8b0898771d5;p=GitHub%2FWoltLab%2FWCF.git Made sure ids in toc are unique --- diff --git a/wcfsetup/install/files/lib/system/html/toc/HtmlToc.class.php b/wcfsetup/install/files/lib/system/html/toc/HtmlToc.class.php index 436b1c1945..5d21c4ac4b 100644 --- a/wcfsetup/install/files/lib/system/html/toc/HtmlToc.class.php +++ b/wcfsetup/install/files/lib/system/html/toc/HtmlToc.class.php @@ -23,6 +23,7 @@ class HtmlToc { $titleRegex = new Regex('[^\p{L}\p{N}]+', Regex::UTF_8); // fetch all headings in their order of appearance + $usedIDs = []; /** @var HtmlTocItem[] $headings */ $headings = []; /** @var \DOMElement $hElement */ @@ -38,7 +39,16 @@ class HtmlToc { // trim to 80 characters $id = rtrim(mb_substr($id, 0, 80), '-'); $id = mb_strtolower($id); - + if (isset($usedIDs[$id])) { + $i = 2; + do { + $newID = $id . '--' . ($i++); + } + while (isset($usedIDs[$newID])); + $id = $newID; + } + $usedIDs[$id] = $id; + // Using the 'normalized' title allows for human-readable anchors that will remain // valid even when new headings are being added or existing ones are removed. This // also covers the relocation of a heading to a new position.