a11y: Report empty string as such

While the text data returned by the `get_contents`
function from the `GtkAccessibleTextInterface` does not
have to be NUL-terminated,
`gtk_accessible_text_get_contents` returns the
text contents as NUL-terminated UTF-8 data.

An empty string (returned as empty, i.e. size = 0,
but not NULL GBytes* data by `get_contents`) is valid, and
therefore also needs to be NUL-terminated, so do this.

Without this, e.g. querying the text of an empty paragraph
in the Gtk 4 variant of LibreOffice with the newly added
GtkAccessibleInterface implementation [1] gives an incorrect
result.

Previous sample use in Accerciser's IPython console:

    In [24]: acc.queryText().getText(0, -1)
    Out[24]: '[Invalid UTF-8]'

With this change in place, it now returns an empty
string as expected:

    In [25]: acc.queryText().getText(0, -1)
    Out[25]: ''

[1] https://git.libreoffice.org/core/commit/e268efd612d12ae9a459d6b9d0cb23220f025163
This commit is contained in:
Michael Weghorn 2024-02-22 17:19:09 +01:00
parent 840cd6f10f
commit e363fcca3c

View File

@ -80,12 +80,13 @@ nul_terminate_contents (GBytes *bytes)
gsize size;
data = g_bytes_get_data (bytes, &size);
if (size > 0 && data[size - 1] != '\0')
if (size == 0 || (size > 0 && data[size - 1] != '\0'))
{
guchar *copy;
copy = g_new (guchar, size + 1);
memcpy (copy, data, size);
if (size > 0)
memcpy (copy, data, size);
copy[size] = '\0';
g_bytes_unref (bytes);