f02ff56dfe
show_html documents that declare dark support follow the app theme; others stay light. Tool description and skill docs say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.7 KiB
Dart
64 lines
2.7 KiB
Dart
/// How a recorded HTML document follows the app's light or dark theme.
|
|
///
|
|
/// A document opts into dark mode by saying it supports it: a
|
|
/// `prefers-color-scheme` media query, a `color-scheme` declaration (CSS or
|
|
/// `<meta>`) or the `light-dark()` colour function. Such a document is shown
|
|
/// in the app's scheme; any other document keeps the light scheme on a white
|
|
/// page, because its own colours were chosen for that and may be unreadable
|
|
/// on a dark canvas.
|
|
///
|
|
/// The frame's browser does not reliably pass the app's preference into a
|
|
/// sandboxed document (and a phone's web view knows only the system's), so
|
|
/// the scheme is written into the document itself: a leading
|
|
/// `<meta name="color-scheme">` naming only the chosen scheme (the browser
|
|
/// paints its default canvas and text, form controls and `light-dark()` in
|
|
/// it), and each `prefers-color-scheme` query is replaced by one that is
|
|
/// always true or always false for that scheme.
|
|
library;
|
|
|
|
final RegExp _darkAware = RegExp(
|
|
r'prefers-color-scheme|color-scheme|light-dark\(',
|
|
caseSensitive: false,
|
|
);
|
|
|
|
final RegExp _schemeQuery = RegExp(
|
|
r'\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)',
|
|
caseSensitive: false,
|
|
);
|
|
|
|
final RegExp _headOpen = RegExp(r'<head(\s[^>]*)?>', caseSensitive: false);
|
|
|
|
/// A media feature that always holds, and one that never does (a negative
|
|
/// length makes the query invalid, which evaluates to false).
|
|
const String _always = '(min-width: 0px)';
|
|
const String _never = '(max-width: -1px)';
|
|
|
|
/// Whether [html] declares that it styles itself for dark mode.
|
|
bool artifactHtmlSupportsDark(String html) => _darkAware.hasMatch(html);
|
|
|
|
/// The colour scheme [html] is shown in: `dark` only when the app is dark
|
|
/// ([dark]) and the document supports it, else `light`.
|
|
String artifactHtmlScheme(String html, {required bool dark}) =>
|
|
dark && artifactHtmlSupportsDark(html) ? 'dark' : 'light';
|
|
|
|
/// [html] fixed to its [artifactHtmlScheme] (see the library comment).
|
|
String artifactHtmlForScheme(String html, {required bool dark}) {
|
|
final scheme = artifactHtmlScheme(html, dark: dark);
|
|
final queried = html.replaceAllMapped(
|
|
_schemeQuery,
|
|
(match) => match[1]!.toLowerCase() == scheme ? _always : _never,
|
|
);
|
|
final meta = '<meta name="color-scheme" content="$scheme">';
|
|
final head = _headOpen.firstMatch(queried);
|
|
if (head != null) {
|
|
return queried.replaceRange(head.end, head.end, meta);
|
|
}
|
|
// Before everything else, but after a doctype: content ahead of the
|
|
// doctype would put the document in quirks mode.
|
|
final doctype = _doctype.firstMatch(queried);
|
|
final at = doctype?.end ?? 0;
|
|
return queried.replaceRange(at, at, meta);
|
|
}
|
|
|
|
final RegExp _doctype = RegExp(r'^\s*<!doctype[^>]*>', caseSensitive: false);
|