--- chimera/ContentClickListener.mm 2002/03/08 01:43:01 1.3 +++ chimera/ContentClickListener.mm 2002/04/14 08:43:03 1.9 @@ -46,9 +46,9 @@ #include "nsIPrefBranch.h" #include "nsIDOMMouseEvent.h" #include "nsEmbedAPI.h" -#include "nsIDOMHTMLAnchorElement.h" -#include "nsIDOMHTMLAreaElement.h" -#include "nsIDOMHTMLLinkElement.h" + +// Common helper routines (also used by the context menu code) +#include "GeckoUtils.h" NS_IMPL_ISUPPORTS2(ContentClickListener, nsIDOMMouseListener, nsIDOMEventListener); @@ -70,74 +70,17 @@ ContentClickListener::MouseClick(nsIDOME aEvent->GetTarget(getter_AddRefs(target)); if (!target) return NS_OK; - nsCOMPtr content(do_QueryInterface(target)); - nsAutoString localName; - if (content) - content->GetLocalName(localName); - + nsCOMPtr content(do_QueryInterface(target)); + nsCOMPtr linkContent; - ToLowerCase(localName); nsAutoString href; - if (localName.Equals(NS_LITERAL_STRING("a")) || - localName.Equals(NS_LITERAL_STRING("area")) || - localName.Equals(NS_LITERAL_STRING("link"))) { - PRBool hasAttr; - content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr); - if (hasAttr) { - linkContent = content; - nsCOMPtr anchor(do_QueryInterface(linkContent)); - if (anchor) - anchor->GetHref(href); - else { - nsCOMPtr area(do_QueryInterface(linkContent)); - if (area) - area->GetHref(href); - else { - nsCOMPtr link(do_QueryInterface(linkContent)); - if (link) - link->GetHref(href); - } - } - } - } - else { - // XXXdwh Handle prefilling of forms (input fields) on a click. - nsCOMPtr curr(do_QueryInterface(target)); - nsCOMPtr temp = curr; - temp->GetParentNode(getter_AddRefs(curr)); - while (curr) { - content = do_QueryInterface(curr); - if (!content) - break; - content->GetLocalName(localName); - ToLowerCase(localName); - if (localName.Equals(NS_LITERAL_STRING("a"))) { - PRBool hasAttr; - content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr); - if (hasAttr) { - linkContent = content; - nsCOMPtr anchor(do_QueryInterface(linkContent)); - if (anchor) - anchor->GetHref(href); - } - else - linkContent = nsnull; // Links can't be nested. - break; - } - - temp = curr; - temp->GetParentNode(getter_AddRefs(curr)); - } - } + GeckoUtils::GetEnclosingLinkElementAndHref(content, getter_AddRefs(linkContent), href); // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who // really uses these anyway? :) if (!linkContent || href.IsEmpty()) return NS_OK; - NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())]; - NSURL* urlToLoad = [NSURL URLWithString: hrefStr]; - nsCOMPtr pref(do_GetService("@mozilla.org/preferences-service;1")); if (!pref) return NS_OK; // Something bad happened if we can't get prefs. @@ -145,27 +88,37 @@ ContentClickListener::MouseClick(nsIDOME PRUint16 button; nsCOMPtr mouseEvent(do_QueryInterface(aEvent)); mouseEvent->GetButton(&button); - switch (button) { - case 0: { - PRBool metaKey, shiftKey; - mouseEvent->GetMetaKey(&metaKey); - mouseEvent->GetShiftKey(&shiftKey); - if (metaKey) { - // The command key is down. Open the link in a new window or tab. - PRBool useTab; - pref->GetBoolPref("browser.tabs.opentabfor.middleclick", &useTab); - PRBool loadInBackground; - pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground); - if (shiftKey) - loadInBackground = !loadInBackground; - if (useTab) { - - } - else - [mBrowserController openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground]; - } - } + + PRBool metaKey, shiftKey, altKey; + mouseEvent->GetMetaKey(&metaKey); + mouseEvent->GetShiftKey(&shiftKey); + mouseEvent->GetAltKey(&altKey); + + NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())]; + NSURL* linkURL = [NSURL URLWithString: hrefStr]; + + if ((metaKey && button == 0) || button == 1) { + // The command key is down or we got a middle click. Open the link in a new window or tab. + PRBool useTab; + pref->GetBoolPref("browser.tabs.opentabfor.middleclick", &useTab); + PRBool loadInBackground; + pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground); + if (shiftKey) + loadInBackground = !loadInBackground; + if (useTab) + [mBrowserController openNewTabWithURL: linkURL loadInBackground: loadInBackground]; + else + [mBrowserController openNewWindowWithURL: linkURL loadInBackground: loadInBackground]; } - + else if (altKey) { + // The user wants to save this link. + nsAutoString text; + GeckoUtils::GatherTextUnder(content, text); + + [mBrowserController saveURL: nil filterList: nil + url: linkURL suggestedFilename: [NSString stringWithCharacters: text.get() + length: nsCRT::strlen(text.get())]]; + } + return NS_OK; }