[libdazzle] fuzzy: improve highlighting corner case scenarios



commit 7e1fb36ef61b02cbe5f61f776dba80d858ac09bf
Author: Christian Hergert <chergert redhat com>
Date:   Sun May 5 12:05:58 2019 -0700

    fuzzy: improve highlighting corner case scenarios
    
    This handles escaping <> and &apos; type data in the search corpus a bit
    better so we output proper Pango markup.
    
    I noticed this while searching across some epiphany issues with <Tab>
    in the title text.

 src/search/dzl-fuzzy-mutable-index.c | 37 +++++++++++++++++--------
 tests/meson.build                    |  9 +++++-
 tests/test-fuzzy-highlight.c         | 53 ++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 12 deletions(-)
---
diff --git a/src/search/dzl-fuzzy-mutable-index.c b/src/search/dzl-fuzzy-mutable-index.c
index d737406..5c16e74 100644
--- a/src/search/dzl-fuzzy-mutable-index.c
+++ b/src/search/dzl-fuzzy-mutable-index.c
@@ -595,16 +595,21 @@ dzl_fuzzy_highlight (const gchar *str,
 
       if (str_ch == '&')
         {
-          if (0 == strncmp (str, "&amp;", 5))
-            {
-              str += 4;
-              g_string_append (ret, "&amp;");
-              continue;
-            }
-          else if (0 == strncmp (str, "&apos;", 6))
+          const gchar *entity_end = strchr (str, ';');
+
+          if (entity_end != NULL)
             {
-              str += 5;
-              g_string_append (ret, "&apos;");
+              gsize len = entity_end - str;
+
+              if (element_open)
+                {
+                  g_string_append (ret, end);
+                  element_open = FALSE;
+                }
+
+              g_string_append_len (ret, str, len + 1);
+              str += len;
+
               continue;
             }
         }
@@ -618,7 +623,12 @@ dzl_fuzzy_highlight (const gchar *str,
               element_open = TRUE;
             }
 
-          g_string_append_unichar (ret, str_ch);
+          if (str_ch == '<')
+            g_string_append (ret, "&lt;");
+          else if (str_ch == '>')
+            g_string_append (ret, "&gt;");
+          else
+            g_string_append_unichar (ret, str_ch);
 
           /* TODO: We could seek to the next char and append in a batch. */
           match = g_utf8_next_char (match);
@@ -631,7 +641,12 @@ dzl_fuzzy_highlight (const gchar *str,
               element_open = FALSE;
             }
 
-          g_string_append_unichar (ret, str_ch);
+          if (str_ch == '<')
+            g_string_append (ret, "&lt;");
+          else if (str_ch == '>')
+            g_string_append (ret, "&gt;");
+          else
+            g_string_append_unichar (ret, str_ch);
         }
     }
 
diff --git a/tests/meson.build b/tests/meson.build
index 30e325e..b28841b 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -398,4 +398,11 @@ test_shortcut_tooltip = executable('test-shortcut-tooltip', 'test-shortcut-toolt
   dependencies: libdazzle_deps + [libdazzle_dep],
 )
 
-endif
\ No newline at end of file
+test_fuzzy_highlight = executable('test-fuzzy-highlight', 'test-fuzzy-highlight.c',
+        c_args: test_cflags,
+     link_args: test_link_args,
+  dependencies: libdazzle_deps + [libdazzle_dep],
+)
+test('test-fuzzy-highlight', test_fuzzy_highlight, env: test_env)
+
+endif
diff --git a/tests/test-fuzzy-highlight.c b/tests/test-fuzzy-highlight.c
new file mode 100644
index 0000000..5889bd0
--- /dev/null
+++ b/tests/test-fuzzy-highlight.c
@@ -0,0 +1,53 @@
+/* test-fuzzy-highlight.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <dazzle.h>
+
+static void
+test_highlight (void)
+{
+  static const struct {
+    const gchar *needle;
+    const gchar *haystack;
+    const gchar *expected;
+  } tests[] = {
+    { "with tab", "with <Tab>", "<b>with </b>&lt;<b>Tab</b>&gt;" },
+    { "with t", "with <Tab>", "<b>with </b>&lt;<b>T</b>ab&gt;" },
+    { "with tuff", "with &apos; stuff", "<b>with </b>&apos; s<b>tuff</b>" },
+    { "gtkwdg", "gtk_widget_show", "<b>gtk</b>_<b>w</b>i<b>dg</b>et_show" },
+  };
+
+  for (guint i = 0; i < G_N_ELEMENTS (tests); i++)
+    {
+      g_autofree gchar *highlight = dzl_fuzzy_highlight (tests[i].haystack,
+                                                         tests[i].needle,
+                                                         FALSE);
+      g_assert_cmpstr (highlight, ==, tests[i].expected);
+    }
+}
+
+gint
+main (gint argc,
+      gchar *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  g_test_add_func ("/Dazzle/Fuzzy/highlight", test_highlight);
+  return g_test_run ();
+}


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