From 9d8fd9a553a114ef9b5d8ba559bde0899539a44b Mon Sep 17 00:00:00 2001 From: Xan Lopez Date: Fri, 16 Mar 2012 01:16:30 +0100 Subject: Fix bookmarklet's titles when they are created This was pretty much broken since the Gecko days. On top of that, catch the case where the bookmarklet title is in the child node of the anchor element, which we never got right before. https://bugzilla.gnome.org/show_bug.cgi?id=672194 --- embed/ephy-embed-event.c | 7 +++++ embed/ephy-embed-event.h | 29 +++++++++++---------- src/popup-commands.c | 68 ++++++++++++++++++++++++++---------------------- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/embed/ephy-embed-event.c b/embed/ephy-embed-event.c index bd3ca818d..f21b7a2f2 100644 --- a/embed/ephy-embed-event.c +++ b/embed/ephy-embed-event.c @@ -181,3 +181,10 @@ ephy_embed_event_has_property (EphyEmbedEvent *event, } +WebKitHitTestResult * +ephy_embed_event_get_hit_test_result (EphyEmbedEvent *event) +{ + g_return_val_if_fail (EPHY_IS_EMBED_EVENT (event), NULL); + + return event->priv->hit_test_result; +} diff --git a/embed/ephy-embed-event.h b/embed/ephy-embed-event.h index 6347be957..633104c07 100644 --- a/embed/ephy-embed-event.h +++ b/embed/ephy-embed-event.h @@ -56,20 +56,21 @@ struct EphyEmbedEventClass { }; -GType ephy_embed_event_get_type (void); -EphyEmbedEvent *ephy_embed_event_new (GdkEventButton *event, - WebKitHitTestResult *hit_test_result); -guint ephy_embed_event_get_context (EphyEmbedEvent *event); -guint ephy_embed_event_get_button (EphyEmbedEvent *event); -guint ephy_embed_event_get_modifier (EphyEmbedEvent *event); -void ephy_embed_event_get_coords (EphyEmbedEvent *event, - guint *x, - guint *y); -void ephy_embed_event_get_property (EphyEmbedEvent *event, - const char *name, - GValue *value); -gboolean ephy_embed_event_has_property (EphyEmbedEvent *event, - const char *name); +GType ephy_embed_event_get_type (void); +EphyEmbedEvent * ephy_embed_event_new (GdkEventButton *event, + WebKitHitTestResult *hit_test_result); +guint ephy_embed_event_get_context (EphyEmbedEvent *event); +guint ephy_embed_event_get_button (EphyEmbedEvent *event); +guint ephy_embed_event_get_modifier (EphyEmbedEvent *event); +void ephy_embed_event_get_coords (EphyEmbedEvent *event, + guint *x, + guint *y); +void ephy_embed_event_get_property (EphyEmbedEvent *event, + const char *name, + GValue *value); +gboolean ephy_embed_event_has_property (EphyEmbedEvent *event, + const char *name); +WebKitHitTestResult *ephy_embed_event_get_hit_test_result (EphyEmbedEvent *event); G_END_DECLS diff --git a/src/popup-commands.c b/src/popup-commands.c index 5860b461c..169062864 100644 --- a/src/popup-commands.c +++ b/src/popup-commands.c @@ -90,37 +90,51 @@ popup_cmd_bookmark_link (GtkAction *action, EphyWindow *window) { EphyEmbedEvent *event; - GValue link_title = { 0, }; - GValue link_rel = { 0, }; - GValue link = { 0, }; - GValue link_is_smart = { 0, }; - GValue linktext = { 0, }; - const char *title; - const char *location; - const char *rel; - gboolean is_smart; + char *title; + char *location; + guint context; + WebKitHitTestResult *result; + WebKitDOMNode *node, *first_child; event = ephy_window_get_context_event (window); g_return_if_fail (event != NULL); - /* FIXME: this is pretty much broken */ - ephy_embed_event_get_property (event, "link_is_smart", &link_is_smart); - ephy_embed_event_get_property (event, "link-uri", &link); - ephy_embed_event_get_property (event, "link_title", &link_title); - ephy_embed_event_get_property (event, "link_rel", &link_rel); - ephy_embed_event_get_property (event, "linktext", &linktext); + result = ephy_embed_event_get_hit_test_result (event); + g_object_get (result, "context", &context, NULL); + g_object_get (result, "inner-node", &node, NULL); - location = g_value_get_string (&link); - g_return_if_fail (location); + if (context != WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) + { + node = WEBKIT_DOM_NODE (webkit_dom_node_get_parent_element (WEBKIT_DOM_NODE (node))); + if (WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (node)) + location = webkit_dom_html_anchor_element_get_href (WEBKIT_DOM_HTML_ANCHOR_ELEMENT (node)); + } + else + { + g_object_get (result, "link-uri", &location, NULL); + } - rel = g_value_get_string (&link_rel); - is_smart = g_value_get_int (&link_is_smart); + if (!node || !location) + return; - title = g_value_get_string (&link_title); + title = webkit_dom_html_element_get_title (WEBKIT_DOM_HTML_ELEMENT (node)); + + if (title == NULL || title[0] == '\0') + { + title = webkit_dom_html_anchor_element_get_text (WEBKIT_DOM_HTML_ANCHOR_ELEMENT (node)); + } + /* Sometimes boorkmaklets are presented as images inside a + * link, try that. */ if (title == NULL || title[0] == '\0') { - title = g_value_get_string (&linktext); + first_child = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (node)); + if (first_child && WEBKIT_DOM_IS_HTML_IMAGE_ELEMENT (first_child)) + { + title = webkit_dom_html_image_element_get_alt (WEBKIT_DOM_HTML_IMAGE_ELEMENT (first_child)); + if (title == NULL || title[0] == '\0') + title = webkit_dom_html_image_element_get_name (WEBKIT_DOM_HTML_IMAGE_ELEMENT (first_child)); + } } if (title == NULL || title[0] == '\0') @@ -128,17 +142,9 @@ popup_cmd_bookmark_link (GtkAction *action, title = location; } - if (is_smart) - { - location = rel; - } - ephy_bookmarks_ui_add_bookmark (GTK_WINDOW (window), location, title); - g_value_unset (&link); - g_value_unset (&link_rel); - g_value_unset (&linktext); - g_value_unset (&link_title); - g_value_unset (&link_is_smart); + g_free (title); + g_free (location); } static void -- cgit v1.2.3