Bug 13618

Summary: HTMLAnchorElement should not force target to _blank for middle clicks
Product: WebKit Reporter: Peter Kasting <pkasting>
Component: WebCore Misc.Assignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal    
Priority: P2    
Version: 523.x (Safari 3)   
Hardware: PC   
OS: All   
Attachments:
Description Flags
patch v1
hyatt: review+
patch v2
mjs: review+
patch v3 none

Description Peter Kasting 2007-05-07 17:44:34 PDT
In HTMLAnchorElement.cpp, defaultEventHandler() sets the target of a link to "_blank" if the user middle-clicks the link.  This sort of thing should happen at the policy delegate level, not in the anchor element's event handler.

Patch coming shortly.
Comment 1 Peter Kasting 2007-05-07 17:50:47 PDT
Created attachment 14406 [details]
patch v1

Simple patch to fix.
Comment 2 Peter Kasting 2007-05-10 19:03:33 PDT
Created attachment 14480 [details]
patch v2

As discussed with Maciej on IRC, this patch adds a WebPreference to toggle the behavior.  On Mac OS X, the preference is set to do the old behavior (force target="_blank" on middle-click); otherwise, the preference defaults to the new behavior (don't change target, let embedders handle middle-clicks as they wish via the policy delegate functions).
Comment 3 Maciej Stachowiak 2007-05-11 11:37:27 PDT
Comment on attachment 14480 [details]
patch v2

r=me

Is there any way to make a test case for this? (Probably not).
Comment 4 Mark Rowe (bdash) 2007-05-11 13:57:53 PDT
Hrm, I think your API additions need to be added to WebPreferencesPrivate.h rather than WebPreferences.h until they go through an Apple API review and are approved for public API.  The middleClickShouldForceTargetToBlank/setMiddleClickShouldForceTargetToBlank: methods also have extraneous whitespace before arguments.
Comment 5 Peter Kasting 2007-05-11 19:31:31 PDT
Created attachment 14500 [details]
patch v3

This moves the functions to the Private section and fixes the whitespace issues.

Unlike the last patch, this one is untested (my access to a Mac is limited), so please make sure it compiles :)

I don't know of any way to make a testcase for this, but with the last patch I did at least use a debugger to verify the preference had been set correctly when stepping through the HTMLAnchorElement code.
Comment 6 Timothy Hatcher 2007-05-14 19:30:51 PDT
Comment on attachment 14500 [details]
patch v3

I think setMiddleClickShouldForceTargetToBlank should be named differently.

I think setMiddleClickShouldOpneNewWindow makes more sense to API users. An API user who isn't familiar with Web terms wont know what target blank is.
Comment 7 Timothy Hatcher 2007-05-14 19:31:35 PDT
That should read: setMiddleClickShouldOpenNewWindow
Comment 8 Peter Kasting 2007-05-15 00:36:04 PDT
(In reply to comment #6)
> (From update of attachment 14500 [details] [edit])
> I think setMiddleClickShouldForceTargetToBlank should be named differently.
> 
> I think setMiddleClickShouldOpneNewWindow makes more sense to API users. An API
> user who isn't familiar with Web terms wont know what target blank is.

I don't think that's a good idea.  It's misleading in multiple ways.

First, it doesn't force the link to open in a new window.  It's perfectly possible for links with target=_blank to _not_ open in new windows, based on what you do in DecidePolicyForNewWindowSomething() (can't remember the exact name, not looking at the source ATM).  And it's also possible to open a link in a new window _without_ setting its target to blank, by proper implementation inside DecidePolicyForNavigationAction().

Second, it implies that if you want middle clicks to open in new windows, you should turn it on.  But that isn't true.  If you want middle clicks to open in new windows, you _should_ do that in your policy delegates.  What _this_ flag does is maintain a broken behavior for backwards-compat reasons.  So if I weren't naming it middleClickShouldForceTargetToBlank, I'd name it hackAttributesOnLinksToMaintainCompatWithOldVersionsOfWebCore.

The name says exactly what it does.  If you don't know what it does, you shouldn't be toggling it back and forth.  This is not a pref that most people should be changing.  If that's not clear enough to embedders, I suggest that we change the pref to a linktime version check, which was one option when this patch got discussed in IRC.

Of course, backing off of that a bit, we could always just add voluminous comments about the effects of this flag somewhere.  I just don't happen to know where that place should be :)

(Incidentally, I'm not sure I buy your original assertion.  I haven't made a web page since 1998, can't write JS, and didn't even know what "DOM" stood for until a year ago, and I know what a blank target is.  Are people less knowledgable than me likely to be embedding WebKit _and_ flipping its prefs?)
Comment 9 Dave Hyatt 2007-05-15 00:45:44 PDT
My own opinion is that this blank-setting code could just be yanked with no compatibility fears.  We're talking about middle clicking here, which isn't exactly something to worry about on the Mac platform.

Comment 10 Peter Kasting 2007-05-15 01:19:25 PDT
Comment on attachment 14406 [details]
patch v1

I can't pass up endorsement like that, so I'll unobsolete v1 in case hyatt wants to r+ or land it :)
Comment 11 Dave Hyatt 2007-05-15 01:27:52 PDT
Comment on attachment 14500 [details]
patch v3

Someone who can run internal Safari should test/land this though, to verify that middle click still works as expected.
Comment 12 Dave Hyatt 2007-05-15 01:29:02 PDT
Comment on attachment 14406 [details]
patch v1

r=me
Comment 13 Sam Weinig 2007-07-17 23:30:39 PDT
Finally landed in r24401.