dvApplyFavicon function

String dvApplyFavicon(
  1. String html,
  2. String? href
)

html wearing href as its icon, replacing whatever the shell declared.

Replacing rather than appending: two <link rel="icon"> tags leave the browser to choose, and which one it chooses is not something the page gets to decide. A null href leaves the document alone, so a page that named no icon keeps the application's instead of losing it.

Here rather than in each renderer because both of them do this -- the web server per request, the static build once per page -- and a second copy of the rule is a second place for the two to disagree about what a page wears.

Implementation

String dvApplyFavicon(String html, String? href) {
  if (href == null || href.isEmpty) return html;
  final String escaped =
      const HtmlEscape(HtmlEscapeMode.attribute).convert(href);
  final RegExp icon = RegExp(r'<link[^>]*rel="(?:shortcut )?icon"[^>]*>');
  return icon.hasMatch(html)
      ? html.replaceAll(icon, '<link rel="icon" href="$escaped">')
      : html.replaceFirst(
          '</head>', '<link rel="icon" href="$escaped">\n</head>');
}