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

From: Po Lu <[email-masked]>
Date: Tue, 09 Nov 2021 21:42:06 +0800

Eli Zaretskii <[email-masked]> writes:

>
> can you rephrase the last sentence not to use passive tense, please?
>
> But BASE-URI is not an &optional argument above.
>
> And also, this sentence is unnecessarily complex because you state the
> reason before the goal.  It is much better to do it the other way
> around, and while at that, make 2 simple sentences out of one complex
> one:
>
>   Optional argument @var{base-uri}, which should be a string,
>   specifies the location of the web resource.  It defaults to
>   @samp{about:blank}.
>
> (Of course, now this begs the question: what does it mean "web
> resource" for HTML text?  How about answering that in the text?)
>
> The second part is either unnecessary or too terse.  if it is
> important to explain that, please elaborate how temporary files are
> relevant here.
>
> The "plain text" part seems to contradict the text in the manual,
> which says it will be rendered as HTML markup?
>
> "will be" is confusing: why the future tense?
>
> Also, what kind of Lisp type is that?

It should be a string, thanks, I've resolved the concerns you listed
above.

> And I think explaining the importance of "fetching resources" would be
> beneficiary here.

I documented it in the manual the best I could.  I'm no web developer,
and the WebKitGTK documentation is vague on this subject, so I can't
think of anything better.

> And finally, "is treated as equivalent" is better reworded as "and
> defaults to ...", which is our style in these cases.

Thanks, done.

>> +  if (NILP (base_uri))
>> +    base_uri = build_string ("about:blank");
>> +  CHECK_STRING (base_uri);
>
> That last line should better be under 'else', right?

Done, thanks.

>> +  base_uri = ENCODE_UTF_8 (base_uri);

> Is it a good idea to produce non-ASCII URIs?

I personally think it's not, but the uuencoding of the URI is done by
WebKitGTK, which expects strings passed as the `uri' parameter to be
encoded as UTF-8 as opposed to uuencoded.

>> +  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

> Hmm... if we only use TEXT and BASE-URI in the GTK build, why do we
> encode them in the other builds?  Isn't that a waste of cycles?  IOW,
> what does this function do if USE_GTK is not defined?

This feature isn't supported on macOS, but it's my hope that someone
will port it to macOS in short order.

>From 4d4b1013813bebf9d473cc04f73cf784fb759091 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..07b9f1a743 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} 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, 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 807f31fa33..b55630336e 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 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..37e4fd94c1 100644
--- a/src/xwidget.c
+++ b/src/xwidget.c
@@ -2139,6 +2139,45 @@ 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 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;
+#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");
+  else
+    CHECK_STRING (base_uri);
+
+#ifdef USE_GTK
+  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 ();
+#endif
+
+  return Qnil;
+}
+
 void
 syms_of_xwidget (void)
 {
@@ -2177,6 +2216,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