From 8d77e09bc110b523b9e7fef218dae8b0898771d5 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Sun, 28 Mar 2021 12:48:19 +0200 Subject: [PATCH] Made sure ids in toc are unique --- .../files/lib/system/html/toc/HtmlToc.class.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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. -- 2.20.1