4 var svgDirectives = {};
21 svgDirectives[attr] = [
27 'computeSVGAttrValue',
39 link: function(scope, element, attrs) {
42 //Only apply to svg elements to avoid unnecessary observing
43 //Check that is in html5Mode and that history is supported
44 if ((!svgAttrExpressions.SVG_ELEMENT.test(element[0] &&
45 element[0].toString())) ||
47 !$sniffer.history) return;
49 //Assumes no expressions, since svg is unforgiving of xml violations
50 initialUrl = attrs[attr];
51 attrs.$observe(attr, updateValue);
52 $rootScope.$on('$locationChangeSuccess', updateValue);
54 function updateValue () {
55 var newVal = computeSVGAttrValue(initialUrl);
56 //Prevent recursive updating
57 if (newVal && attrs[attr] !== newVal) attrs.$set(attr, newVal);
64 angular.module('ngSVGAttributes', []).
65 factory('urlResolve', [function() {
66 //Duplicate of urlResolve & urlParsingNode in angular core
67 var urlParsingNode = document.createElement('a');
68 return function urlResolve(url) {
69 urlParsingNode.setAttribute('href', url);
70 return urlParsingNode;
73 value('svgAttrExpressions', {
74 FUNC_URI: /^url\((.*)\)$/,
75 SVG_ELEMENT: /SVG[a-zA-Z]*Element/,
78 factory('computeSVGAttrValue', [
79 '$location', '$sniffer', 'svgAttrExpressions', 'urlResolve',
80 function($location, $sniffer, svgAttrExpressions, urlResolve) {
81 return function computeSVGAttrValue(url) {
83 if (match = svgAttrExpressions.FUNC_URI.exec(url)) {
84 //hash in html5Mode, forces to be relative to current url instead of base
85 if (match[1].indexOf('#') === 0) {
86 fullUrl = $location.absUrl().
87 replace(svgAttrExpressions.HASH_PART, '') +
90 //Presumably links to external SVG document
92 fullUrl = urlResolve(match[1]);
95 return fullUrl ? 'url(' + fullUrl + ')' : null;
100 directive(svgDirectives);