[gnome-software: 6/10] gs-appstream: Handle self-closing elements in descriptions better




commit 12a95a64e06dcc54d80e63fefa985b77c90fb8a2
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Aug 20 12:18:48 2021 +0100

    gs-appstream: Handle self-closing elements in descriptions better
    
    Some oddly formatted appdata can contain descriptions like this one from
    ‘google Roboto Slab’:
    ```
    <description><p/><p/></description>
    ```
    
    After some testing, it appears that Firefox treats `<p/>` as if it
    doesn’t exist, and `<li/>` the same as `<li></li>` — so that’s what we
    should do.
    
    These changes mean that gnome-software no longer displays
    `(null)\n\n(null)` as the description in the UI for google Roboto Slab.
    The description is now hidden for that font.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 lib/gs-appstream.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
---
diff --git a/lib/gs-appstream.c b/lib/gs-appstream.c
index 86cfe16eb..756106078 100644
--- a/lib/gs-appstream.c
+++ b/lib/gs-appstream.c
@@ -55,14 +55,25 @@ gs_appstream_format_description (XbNode *root, GError **error)
 
                /* support <p>, <ul>, <ol> and <li>, ignore all else */
                if (g_strcmp0 (xb_node_get_element (n), "p") == 0) {
-                       g_string_append_printf (str, "%s\n\n", xb_node_get_text (n));
+                       const gchar *node_text = xb_node_get_text (n);
+
+                       /* Treat a self-closing paragraph (`<p/>`) as
+                        * nonexistent. This is consistent with Firefox. */
+                       if (node_text != NULL)
+                               g_string_append_printf (str, "%s\n\n", node_text);
                } else if (g_strcmp0 (xb_node_get_element (n), "ul") == 0) {
                        g_autoptr(GPtrArray) children = xb_node_get_children (n);
+
                        for (guint i = 0; i < children->len; i++) {
                                XbNode *nc = g_ptr_array_index (children, i);
                                if (g_strcmp0 (xb_node_get_element (nc), "li") == 0) {
+                                       const gchar *node_text = xb_node_get_text (nc);
+
+                                       /* Treat a self-closing `<li/>` as an empty
+                                        * list element (equivalent to `<li></li>`).
+                                        * This is consistent with Firefox. */
                                        g_string_append_printf (str, " • %s\n",
-                                                               xb_node_get_text (nc));
+                                                               (node_text != NULL) ? node_text : "");
                                }
                        }
                        g_string_append (str, "\n");
@@ -71,9 +82,12 @@ gs_appstream_format_description (XbNode *root, GError **error)
                        for (guint i = 0; i < children->len; i++) {
                                XbNode *nc = g_ptr_array_index (children, i);
                                if (g_strcmp0 (xb_node_get_element (nc), "li") == 0) {
+                                       const gchar *node_text = xb_node_get_text (nc);
+
+                                       /* Treat self-closing elements as with `<ul>` above. */
                                        g_string_append_printf (str, " %u. %s\n",
                                                                i + 1,
-                                                               xb_node_get_text (nc));
+                                                               (node_text != NULL) ? node_text : "");
                                }
                        }
                        g_string_append (str, "\n");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]