From c170dd43ffaadc916f5fae7cbb329a9ee2ff5745 Mon Sep 17 00:00:00 2001 From: Tomas Popela Date: Wed, 29 Jun 2016 13:41:14 +0200 Subject: [PATCH] EHTMLEditorUtils - Correctly remove class when it is surrounded by spaces Avoid the leftover spaces to remain in the class attribute. --- e-util/e-html-editor-utils.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/e-util/e-html-editor-utils.c b/e-util/e-html-editor-utils.c index 2899e9247b..be5b8c99ff 100644 --- a/e-util/e-html-editor-utils.c +++ b/e-util/e-html-editor-utils.c @@ -253,8 +253,9 @@ void element_remove_class (WebKitDOMElement *element, const gchar* class) { - gchar *element_class; - GString *result; + gchar *element_class, *final_class; + GRegex *regex; + gchar *pattern = NULL; if (!WEBKIT_DOM_IS_ELEMENT (element)) return; @@ -264,19 +265,19 @@ element_remove_class (WebKitDOMElement *element, element_class = webkit_dom_element_get_class_name (element); - if (g_strcmp0 (element_class, class) == 0) { - webkit_dom_element_remove_attribute (element, "class"); - g_free (element_class); - return; - } + pattern = g_strconcat ("[\\s]*", class, "[\\s]*", NULL); + regex = g_regex_new (pattern, 0, 0, NULL); + final_class = g_regex_replace (regex, element_class, -1, 0, " ", 0, NULL); - result = e_str_replace_string (element_class, class, ""); - if (result) { - webkit_dom_element_set_class_name (element, result->str); - g_string_free (result, TRUE); - } + if (g_strcmp0 (final_class, " ") != 0) + webkit_dom_element_set_class_name (element, final_class); + else + webkit_dom_element_remove_attribute (element, "class"); g_free (element_class); + g_free (final_class); + g_free (pattern); + g_regex_unref (regex); } void