Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($location): compatibility with SVG anchors #4098

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ function $LocationProvider(){
}
};

this.$get = ['$rootScope', '$browser', '$sniffer', '$rootElement',
function( $rootScope, $browser, $sniffer, $rootElement) {
this.$get = ['$rootScope', '$browser', '$sniffer', '$rootElement', '$window',
function( $rootScope, $browser, $sniffer, $rootElement, $window) {
var $location,
LocationMode,
baseHref = $browser.baseHref(), // if base[href] is undefined, it defaults to ''
Expand Down Expand Up @@ -548,6 +548,13 @@ function $LocationProvider(){
}

var absHref = elm.prop('href');

// If this was an anchor element in an SVG, it'll need to be unpacked.
if ($window.SVGAnimatedString &&
absHref instanceof $window.SVGAnimatedString) {
absHref = absHref.animVal;
}

var rewrittenUrl = $location.$$rewrite(absHref);

if (absHref && !elm.attr('target') && rewrittenUrl && !event.isDefaultPrevented()) {
Expand Down
44 changes: 44 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,50 @@ describe('$location', function() {
})
);

// don't run next tests on IE<9, as SVG isn't supported
if (!(msie < 9)) {
it('should listen on click events on href and prevent browser default in hashbang mode in svg', function() {
module(function() {
return function($rootElement, $compile, $rootScope) {
// innerHTML isn't available in SVGs and jqLite doesn't handle namespaced elements
$rootElement.html('');

var svgNs = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(svgNs, 'svg');
$rootElement[0].appendChild(svg);

var a = document.createElementNS(svgNs, 'a');
a.setAttributeNS('http://www.w3.org/1999/xlink', 'href', 'http://server/#/somePath');
a.textContent = 'link';
svg.appendChild(a);

$compile($rootElement)($rootScope);
jqLite(document.body).append($rootElement);
}
});

inject(function($location, $rootScope, $browser, $rootElement) {
var log = '',
link = $rootElement.find('a');


$rootScope.$on('$locationChangeStart', function(event) {
event.preventDefault();
log += '$locationChangeStart';
});
$rootScope.$on('$locationChangeSuccess', function() {
throw new Error('after cancellation in hashbang mode');
});

browserTrigger(link, 'click');

expect(log).toEqual('$locationChangeStart');
expect($browser.url()).toEqual('http://server/');

dealoc($rootElement);
});
});
}

it('should listen on click events on href and prevent browser default in hashbang mode', function() {
module(function() {
Expand Down