]*)>(.*?)<\/h\1\s*>/is';
const ID_ATTR_PATTERN = '/(^|\s)id\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s"\'>]+))/i';
const SLOT_PREFIX = '/';
/**
* The placeholder a TOC renders where its list goes.
*
* @param int $start Shallowest heading level listed.
* @param int $end Deepest heading level listed.
* @param int $min Fewest headings worth a list.
*
* @return string
*/
public static function slot($start, $end, $min)
{
return sprintf('%1$s%2$d:%3$d:%4$d-->', self::SLOT_PREFIX, (int) $start, (int) $end, max(1, (int) $min));
}
/**
* Does this HTML hold a TOC slot?
*
* @param mixed $html Content.
*
* @return bool
*/
public static function has_slot($html)
{
return is_string($html) && false !== strpos($html, self::SLOT_PREFIX);
}
/**
* Index the headings and fill every TOC slot in the HTML.
*
* @param string $html Rendered content.
*
* @return string
*/
public static function fill($html)
{
list($html, $items) = self::index($html);
if (!self::has_slot($html)) {
return $html;
}
$filled = preg_replace_callback(
self::SLOT_PATTERN,
function ($m) use ($items) {
$start = min((int) $m[1], (int) $m[2]);
$end = max((int) $m[1], (int) $m[2]);
return self::render_list($items, $start, $end, (int) $m[3]);
},
$html
);
return null === $filled ? $html : $filled;
}
/**
* Give every non-empty heading an id and list them.
*
* @param string $html Rendered content.
*
* @return array{0: string, 1: array} The HTML with ids added, and the
* headings as [level, text, id] in order.
*/
public static function index($html)
{
$html = (string) $html;
if (false === stripos($html, '%4$s',
$level,
esc_attr($id),
self::without_id_attr($m[2]),
$m[3]
);
}
$items[] = ['level' => $level, 'text' => $text, 'id' => $id];
return $tag;
},
$html
);
if (null === $out) {
// PCRE gave up (backtrack limit). Leave the content alone rather
// than return ids for headings that did not get them.
return [$html, []];
}
return [$out, $items];
}
/**
* Slugify heading text into an id no other element in the HTML uses.
*
* @param string $text Heading text, entities decoded.
* @param array $used Ids taken so far; the new one is added.
*
* @return string
*/
public static function slugify($text, array &$used)
{
$slug = function_exists('mb_strtolower') ? mb_strtolower((string) $text, 'UTF-8') : strtolower((string) $text);
$slug = (string) preg_replace('/[^\p{L}\p{M}\p{N}\s_-]/u', '', $slug);
$slug = (string) preg_replace('/[\s_]+/u', '-', $slug);
$slug = (string) preg_replace('/-+/', '-', $slug);
$slug = trim($slug, '-');
if ('' === $slug) {
$slug = 'section';
}
$base = $slug;
$n = 2;
while (isset($used[$slug])) {
$slug = $base . '-' . $n;
$n++;
}
$used[$slug] = true;
return $slug;
}
/**
* A heading's text, as the browser's textContent gives it.
*
* @param string $inner Heading inner HTML.
*
* @return string
*/
public static function heading_text($inner)
{
$text = html_entity_decode(wp_strip_all_tags((string) $inner), ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Unicode-aware trim, like String.prototype.trim(): a heading that
// holds only is empty on both sides.
return (string) preg_replace('/^\s+|\s+$/u', '', $text);
}
/**
* The id a heading already has, or '' when it has none (or an empty one).
*
* @param string $attrs The heading's attribute string.
*
* @return string
*/
private static function id_attr($attrs)
{
if (!preg_match(self::ID_ATTR_PATTERN, (string) $attrs, $m)) {
return '';
}
$raw = ($m[2] ?? '') . ($m[3] ?? '') . ($m[4] ?? '');
return trim(html_entity_decode($raw, ENT_QUOTES | ENT_HTML5, 'UTF-8'));
}
/**
* Drop an empty id attribute before ours is added, so the tag never ends
* up with two.
*
* @param string $attrs The heading's attribute string.
*
* @return string
*/
private static function without_id_attr($attrs)
{
return (string) preg_replace(self::ID_ATTR_PATTERN, '', (string) $attrs, 1);
}
/**
* Every id already in the HTML, so a generated slug avoids them all.
*
* @param string $html Rendered content.
*
* @return array
*/
private static function reserved_ids($html)
{
$used = [];
if (preg_match_all(self::ID_ATTR_PATTERN, $html, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
$id = trim(html_entity_decode(($m[2] ?? '') . ($m[3] ?? '') . ($m[4] ?? ''), ENT_QUOTES | ENT_HTML5, 'UTF-8'));
if ('' !== $id) {
$used[$id] = true;
}
}
}
return $used;
}
/**
* The nested markup for one TOC.
*
* @param array $items Indexed headings.
* @param int $start Shallowest level listed.
* @param int $end Deepest level listed.
* @param int $min Fewest headings worth a list.
*
* @return string '' when there are too few, so the front end decides.
*/
public static function render_list(array $items, $start, $end, $min)
{
$items = array_values(
array_filter(
$items,
function ($item) use ($start, $end) {
return $item['level'] >= $start && $item['level'] <= $end;
}
)
);
if (count($items) < max(1, (int) $min)) {
return '';
}
$i = 0;
return self::render_items($items, $i, min(array_column($items, 'level')));
}
/**
* Recursively render the items at a given heading level. Always emits
* balanced /- markup regardless of the heading hierarchy (skipped
* or out-of-order levels). $i is advanced by reference as items are
* consumed.
*
* @param array $items Headings in range.
* @param int $i Cursor.
* @param int $level Level rendered by this call.
*
* @return string
*/
private static function render_items(array $items, &$i, $level)
{
$html = '';
$count = count($items);
while ($i < $count) {
$cur = (int) $items[$i]['level'];
// Belongs to a shallower level: hand control back to the caller.
if ($cur < $level) {
break;
}
$item = $items[$i];
$i++;
$link = sprintf(
'%2$s',
esc_attr($item['id']),
esc_html($item['text'])
);
// Nest any deeper following items inside this
- .
$children = '';
if ($i < $count && (int) $items[$i]['level'] > $cur) {
$children = '
' . self::render_items($items, $i, (int) $items[$i]['level']) . '
';
}
$html .= ' - ' . $link . $children . '
';
}
return $html;
}
}