1
0
mirror of https://github.com/git/git.git synced 2024-10-01 06:01:27 +02:00
git/gitweb/static/js/javascript-detection.js
Peter Stuge 2b07ff3ffa gitweb: Fix links to lines in blobs when javascript-actions are enabled
The fixLinks() function adds 'js=1' to each link that does not already
have 'js' query parameter specified. This is used to signal to gitweb
that the browser can actually do javascript when these links are used.

There are two problems with the existing code:

  1. URIs with fragment and 'js' query parameter, like e.g.

        ...foo?js=0#l199

     were not recognized as having 'js' query parameter already.

  2. The 'js' query parameter, in the form of either '?js=1' or ';js=1'
     was appended at the end of URI, even if it included a fragment
     (had a hash part).  This lead to the incorrect links like this

        ...foo#l199?js=1

     instead of adding query parameter as last part of query, but
     before the fragment part, i.e.

        ...foo?js=1#l199

Signed-off-by: Peter Stuge <peter@stuge.se>
Acked-by: Jakub Narebski <jnareb@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-09-27 09:34:37 -07:00

44 lines
1.2 KiB
JavaScript

// Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
// 2007, Petr Baudis <pasky@suse.cz>
// 2008-2011, Jakub Narebski <jnareb@gmail.com>
/**
* @fileOverview Detect if JavaScript is enabled, and pass it to server-side
* @license GPLv2 or later
*/
/* ============================================================ */
/* Manipulating links */
/**
* used to check if link has 'js' query parameter already (at end),
* and other reasons to not add 'js=1' param at the end of link
* @constant
*/
var jsExceptionsRe = /[;?]js=[01](#.*)?$/;
/**
* Add '?js=1' or ';js=1' to the end of every link in the document
* that doesn't have 'js' query parameter set already.
*
* Links with 'js=1' lead to JavaScript version of given action, if it
* exists (currently there is only 'blame_incremental' for 'blame')
*
* To be used as `window.onload` handler
*
* @globals jsExceptionsRe
*/
function fixLinks() {
var allLinks = document.getElementsByTagName("a") || document.links;
for (var i = 0, len = allLinks.length; i < len; i++) {
var link = allLinks[i];
if (!jsExceptionsRe.test(link)) {
link.href = link.href.replace(/(#|$)/,
(link.href.indexOf('?') === -1 ? '?' : ';') + 'js=1$1');
}
}
}
/* end of javascript-detection.js */