Files
pipelines/app/lib/features/artifacts/ui/artifact_html_links_web.dart
alan 4e0481fd8d Artifact viewer: full screen view; HTML scrolls, pans and zooms; links open safely
Full screen button in the viewer's bottom bar opens a root full-screen route
with the same bar and top bar; exit by the bar, Close, Escape or back. It
renders the already fetched bytes, never the system browser. The external
Open action is now labelled Open in browser.

Native web views claim their gestures (vertical drags inline, every pointer
in full screen) so the page swipe no longer starves them. On the web, full
screen adds zoom buttons that scale the live sandboxed frame. Links: in-page
anchors scroll, http(s)/mailto open outside, everything else is inert.

Capture kit: --touch, swipe=, pinch=, tabs=; html=1 fixture artifact.
2026-11-03 18:50:41 +00:00

53 lines
2.3 KiB
Dart

import 'dart:js_interop';
import 'package:web/web.dart' as web;
import '../domain/artifact_html_links.dart';
/// The iframe's sandbox: no script, no same-origin access, no forms and no
/// navigation of the app, but a link may open a new tab that is itself
/// unsandboxed (an ordinary website, as if typed into the address bar).
const String artifactHtmlSandbox =
'allow-popups allow-popups-to-escape-sandbox';
/// [html] with every link's behaviour fixed before the frame shows it
/// ([artifactHtmlLinkAction]): a `#place` stays in the frame and scrolls
/// there, an http(s)/mailto address opens a new tab (no opener, no
/// referrer), and every other link loses its `href`, so it is inert text.
/// A document's own `<base>` is dropped: the frame is not at the artifact's
/// address, and a relative link must not reach the app's origin.
///
/// The browser's `DOMParser` builds an inert document: nothing in it runs or
/// loads while it is rewritten.
String artifactHtmlWithInertLinks(String html) {
final doc = web.DOMParser().parseFromString(html.toJS, 'text/html');
final bases = doc.querySelectorAll('base');
for (var i = bases.length - 1; i >= 0; i--) {
(bases.item(i) as web.Element?)?.remove();
}
final links = doc.querySelectorAll('a[href], area[href]');
for (var i = 0; i < links.length; i++) {
final link = links.item(i) as web.Element?;
if (link == null) continue;
final href = link.getAttribute('href') ?? '';
switch (artifactHtmlLinkAction(href)) {
case ArtifactHtmlLinkAction.inDocument:
// A srcdoc document's base is the app page, so a bare `#place` would
// navigate the frame to the app; the document's own address keeps it
// a same-document jump.
final hash = href.indexOf('#');
final place = hash < 0 ? '' : href.substring(hash);
link.setAttribute('href', 'about:srcdoc$place');
link.setAttribute('target', '_self');
case ArtifactHtmlLinkAction.external:
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
case ArtifactHtmlLinkAction.blocked:
link.removeAttribute('href');
link.removeAttribute('target');
}
}
final doctype = doc.doctype == null ? '' : '<!DOCTYPE html>';
return '$doctype${doc.documentElement?.outerHTML ?? ''}';
}