dvViewportApply function

String dvViewportApply(
  1. String html
)

Ensure html carries a viewport meta.

A page that already declares one is left alone: a developer who wrote their own meant it, and two viewport metas is not additive -- which one the browser honours is not something to guess at.

Implementation

String dvViewportApply(String html) {
  if (RegExp(r"""<meta\s+name=["']viewport["']""", caseSensitive: false)
      .hasMatch(html)) {
    return html;
  }
  final int at = html.indexOf('</head>');
  // No head to put it in. Returning the page unchanged beats inventing
  // structure around someone's template.
  if (at < 0) return html;
  return '${html.substring(0, at)}  $dvViewportMeta\n${html.substring(at)}';
}