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

From: Po Lu <[email-masked]>
Date: Wed, 10 Nov 2021 08:14:13 +0800

Lars Ingebrigtsen <[email-masked]> writes:

> Po Lu <[email-masked]> writes:
>
>>> Hmm... this is better, but at least I am still in the dark regarding
>>> the need for this optional argument.  E.g., what would happen if you
>>> use that tag, but don't specify the URI?
>>
>> I'm in the dark too.  I am not a web developer, and browser engines are
>> remarkably lax about these things, but hopefully Lars can explain better
>> (after all, shr must deal with these things too), so I added him to the
>> Ccs.
>
> The base URI is used when expanding relative links.  So if you have HTML
> that looks like
>
> <a href="foo.html"><img src="img.png"></a>
>
> then the browser can't resolve those two URLs without having the base
> URI.  So you'd pass in a base URI of https://example.com/foo/bar/zot if
> that's where the HTML snippet is from.
>
> When the URLs are absolute, the base URI is irrelevant.

Thanks, does this look OK to you?  And I'm afraid I'm not terribly good
at phrasing this into words, so I would really appreciate it if you
could help write that section of the manual.

>From 1d701339df5ed91ed730fb134976137bcf1e0ce6 Mon Sep 17 00:00:00 2001
From: Po Lu <[email-masked]>
Date: Tue, 9 Nov 2021 21:36:40 +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 | 11 +++++++++++
 etc/NEWS                 |  5 +++++
 src/xwidget.c            | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi
index b6bd14f887..aa7d64493c 100644
--- a/doc/lispref/display.texi
+++ b/doc/lispref/display.texi
@@ -6943,6 +6943,17 @@ Xwidgets
 signals an error.
 @end defun
 
+@defun xwidget-webkit-load-html xwidget text &optional base-uri
+Load @var{text}, a string, into @var{xwidget}, which should be a
+WebKit xwidget.  It treats @var{text} as HTML markup, which will be
+rendered by @var{xwidget}.
+
+Optional argument @var{base-uri}, which should be a string, specifies
+the location of web resources specified through relative links, such
+as the resource @samp{foo.png} in the HTML tag @samp{<img
+src="foo.png">}.  It defaults to @samp{about:blank}.
+@end defun
+
 @node Buttons
 @section Buttons
 @cindex buttons in buffers
diff --git a/etc/NEWS b/etc/NEWS
index 3cad0995ac..ae318be1c0 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -796,6 +796,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 to store the markup.
+
 +++
 *** 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..2587b658e7 100644
--- a/src/xwidget.c
+++ b/src/xwidget.c
@@ -2139,6 +2139,43 @@ DEFUN ("xwidget-webkit-finish-search", Fxwidget_webkit_finish_search,
   return Qnil;
 }
 
+#ifdef USE_GTK
+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 HTML markup.
+BASE_URI should be a string containing a URI that is used to fetch
+resources, and if not specified, defaults to `about:blank'.  */)
+  (Lisp_Object xwidget, Lisp_Object text, Lisp_Object base_uri)
+{
+  struct xwidget *xw;
+  WebKitWebView *webview;
+  char *data, *uri;
+
+  CHECK_XWIDGET (xwidget);
+  CHECK_STRING (text);
+  if (NILP (base_uri))
+    base_uri = build_string ("about:blank");
+  else
+    CHECK_STRING (base_uri);
+
+  base_uri = ENCODE_UTF_8 (base_uri);
+  text = ENCODE_UTF_8 (text);
+  xw = XXWIDGET (xwidget);
+
+  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 ();
+
+  return Qnil;
+}
+#endif
+
 void
 syms_of_xwidget (void)
 {
@@ -2177,6 +2214,9 @@ syms_of_xwidget (void)
   defsubr (&Sxwidget_webkit_next_result);
   defsubr (&Sxwidget_webkit_previous_result);
   defsubr (&Sset_xwidget_buffer);
+#ifdef USE_GTK
+  defsubr (&Sxwidget_webkit_load_html);
+#endif
 
   DEFSYM (QCxwidget, ":xwidget");
   DEFSYM (QCtitle, ":title");
-- 
2.31.1