Subject: 29.0.50; [PATCH] New function `xwidget-webkit-load-html'

From: Po Lu <[email-masked]>
Date: Tue, 09 Nov 2021 18:26:17 +0800

Hopefully this allows to reduce the amount of JavaScript hacks needed to
load HTML into a widget.  It also lets you customize the base URI WebKit
uses to find web resources, which you can't do from JavaScript.

Thanks.

>From f368cd47dd5a2d232024ef1fb0f95f4b0c15e566 Mon Sep 17 00:00:00 2001
From: Po Lu <[email-masked]>
Date: Tue, 9 Nov 2021 18:24:03 +0800
Subject: [PATCH] Add `xwidget-webkit-load-html'

* doc/lispref/display.texi (Xwidgets): Document new function.
* etc/NEWS: Announce new function.
* src/xwidget.c (Fxwidget_webkit_load_html): New function.
(syms_of_xwidget): Define new subr.
---
 doc/lispref/display.texi | 10 ++++++++++
 etc/NEWS                 |  5 +++++
 src/xwidget.c            | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+)

diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi
index b6bd14f887..3ccc755391 100644
--- a/doc/lispref/display.texi
+++ b/doc/lispref/display.texi
@@ -6943,6 +6943,16 @@ Xwidgets
 signals an error.
 @end defun
 
+@defun xwidget-webkit-load-html xwidget text base-uri
+Load @var{text} into @var{xwidget}, which should be a WebKit xwidget.
+@var{text} will be treated as HTML markup, and rendered by
+@var{xwidget}.
+
+For the purpose of controlling the location where web resources will
+be found, you can optionally specify a base URI as a string in
+@var{base-uri}, which defaults to @samp{about:blank}.
+@end defun
+
 @node Buttons
 @section Buttons
 @cindex buttons in buffers
diff --git a/etc/NEWS b/etc/NEWS
index 807f31fa33..93c161025b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -792,6 +792,11 @@ what the widget will actually receive.
 
 On GTK+, only key and function key events are implemented.
 
++++
+*** New function 'xwidget-webkit-load-html'.
+This function is used to load HTML text into WebKit xwidgets, without
+having to create a temporary file or URL.
+
 +++
 *** New functions for performing searches on WebKit xwidgets.
 Some new functions, such as 'xwidget-webkit-search', have been added
diff --git a/src/xwidget.c b/src/xwidget.c
index fc76ce307e..22893bfe2e 100644
--- a/src/xwidget.c
+++ b/src/xwidget.c
@@ -2139,6 +2139,43 @@ DEFUN ("xwidget-webkit-finish-search", Fxwidget_webkit_finish_search,
   return Qnil;
 }
 
+DEFUN ("xwidget-webkit-load-html", Fxwidget_webkit_load_html,
+       Sxwidget_webkit_load_html, 2, 3, 0,
+       doc: /* Make XWIDGET's WebKit widget render text.
+XWIDGET should be a WebKit xwidget, that will receive TEXT.  TEXT
+should be a string that will be displayed by XWIDGET as plain text.
+BASE_URI will be a URI that is used to fetch resources, and if not
+specified, is treated as equivalent to `about:blank'.  */)
+  (Lisp_Object xwidget, Lisp_Object text, Lisp_Object base_uri)
+{
+  struct xwidget *xw;
+#ifdef USE_GTK
+  WebKitWebView *webview;
+  char *data, *uri;
+#endif
+
+  CHECK_XWIDGET (xwidget);
+  CHECK_STRING (text);
+  if (NILP (base_uri))
+    base_uri = build_string ("about:blank");
+  CHECK_STRING (base_uri);
+  base_uri = ENCODE_UTF_8 (base_uri);
+  text = ENCODE_UTF_8 (text);
+  xw = XXWIDGET (xwidget);
+
+#ifdef USE_GTK
+  data = SSDATA (text);
+  uri = SSDATA (base_uri);
+  webview = WEBKIT_WEB_VIEW (xw->widget_osr);
+
+  block_input ();
+  webkit_web_view_load_html (webview, data, uri);
+  unblock_input ();
+#endif
+
+  return Qnil;
+}
+
 void
 syms_of_xwidget (void)
 {
@@ -2177,6 +2214,7 @@ syms_of_xwidget (void)
   defsubr (&Sxwidget_webkit_next_result);
   defsubr (&Sxwidget_webkit_previous_result);
   defsubr (&Sset_xwidget_buffer);
+  defsubr (&Sxwidget_webkit_load_html);
 
   DEFSYM (QCxwidget, ":xwidget");
   DEFSYM (QCtitle, ":title");
-- 
2.31.1