Merge lp:~osomon/webbrowser-app/hyperlinks-blank-target into lp:webbrowser-app

Proposed by Olivier Tilloy
Status: Merged
Approved by: Ugo Riboni
Approved revision: 89
Merged at revision: 98
Proposed branch: lp:~osomon/webbrowser-app/hyperlinks-blank-target
Merge into: lp:webbrowser-app
Diff against target: 57 lines (+41/-1)
2 files modified
src/Ubuntu/Browser/Browser.qml (+2/-1)
src/Ubuntu/Browser/hyperlinks.js (+39/-0)
To merge this branch: bzr merge lp:~osomon/webbrowser-app/hyperlinks-blank-target
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Ubuntu Phablet Team Pending
Review via email: mp+155216@code.launchpad.net

Commit message

Use javascript event delegation to work around the lack of support for handling hyperlinks with a target attribute set to '_blank' in QtWebKit.

Description of the change

Note to the reviewer: bug #1129281 was originally reported against the twitter web app. Unfortunately, this MR doesn’t fix the original issue because the twitter web app does additional event handling that conflicts with my solution (see https://bugs.launchpad.net/manhattan/+bug/1129281/comments/7 for details).

However this patch does fix the handling of "normal" hyperlinks with target="_blank" (hopefully the majority of them out there), so I think it’s still valuable on its own. It can be tested against http://news.google.com (all links have target="_blank" on this page).

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ugo Riboni (uriboni) wrote :

A different approach would be to wait for the document to finish loading, then do document.getElementsByTagName("a") and add your click listener on those of them that have target = "_blank"

The advantage of that is that you don't have to walk up the tree for each click anywhere on the document (which probably happens often). The tradeoff is bviously of some more initial setup at page load, but it shouldn't be too bad.

Revision history for this message
Olivier Tilloy (osomon) wrote :

As discussed on IRC, the approach suggested by Ugo would probably consume more memory (for each onclick handler), as opposed to more CPU consumption for the patch I’m proposing. We’d need numbers to back this hypothesis though.

Regardless, this is a temporary solution, the proper way of fixing this issue is to patch QtWebKit to implement decidePolicyForNewWindowAction(…).

This patch improves user experience by enabling something that doesn’t work without it, so let’s get it in as is, and work on a proper fix asap.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Ubuntu/Browser/Browser.qml'
2--- src/Ubuntu/Browser/Browser.qml 2013-03-20 18:28:04 +0000
3+++ src/Ubuntu/Browser/Browser.qml 2013-03-25 12:57:10 +0000
4@@ -130,7 +130,8 @@
5 experimental.devicePixelRatio: browser.qtwebkitdpr
6 experimental.preferences.developerExtrasEnabled: browser.developerExtrasEnabled
7 experimental.preferences.navigatorQtObjectEnabled: true
8- experimental.userScripts: [Qt.resolvedUrl("selection.js")]
9+ experimental.userScripts: [Qt.resolvedUrl("hyperlinks.js"),
10+ Qt.resolvedUrl("selection.js")]
11 experimental.onMessageReceived: {
12 var data = null
13 try {
14
15=== added file 'src/Ubuntu/Browser/hyperlinks.js'
16--- src/Ubuntu/Browser/hyperlinks.js 1970-01-01 00:00:00 +0000
17+++ src/Ubuntu/Browser/hyperlinks.js 2013-03-25 12:57:10 +0000
18@@ -0,0 +1,39 @@
19+/*
20+ * Copyright 2013 Canonical Ltd.
21+ *
22+ * This file is part of ubuntu-browser.
23+ *
24+ * ubuntu-browser is free software; you can redistribute it and/or modify
25+ * it under the terms of the GNU General Public License as published by
26+ * the Free Software Foundation; version 3.
27+ *
28+ * ubuntu-browser is distributed in the hope that it will be useful,
29+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31+ * GNU General Public License for more details.
32+ *
33+ * You should have received a copy of the GNU General Public License
34+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35+ */
36+
37+var doc = document.documentElement;
38+
39+doc.addEventListener('click', function(event) {
40+ var node = event.target;
41+ while (node) {
42+ if (node.nodeName.toLowerCase() == 'a') {
43+ // Using event delegation to work around the lack of support for
44+ // handling hyperlinks with a target attribute set to '_blank' in
45+ // QtWebKit. See related upstream bug reports:
46+ // https://bugs.webkit.org/show_bug.cgi?id=76416
47+ // https://bugs.webkit.org/show_bug.cgi?id=91779
48+ if (node.hasAttribute('target')) {
49+ if (node.getAttribute('target').toLowerCase() == '_blank') {
50+ window.location = node.getAttribute('href');
51+ }
52+ }
53+ break;
54+ }
55+ node = node.parentNode;
56+ }
57+});

Subscribers

People subscribed via source and target branches

to status/vote changes: