Replace angular-sanitize.js with a customized version, add link attributes

This commit is contained in:
Zeusking19 2015-12-27 12:22:27 +00:00
parent f1d2095b0b
commit f418cad608
6 changed files with 59 additions and 25 deletions

View file

@ -52,7 +52,7 @@
<div class="left">
<div class="description" bo-show="album.description.length">
<h2>Description</h2>
<p bo-html="album.description | linky | newlines"></p>
<p bo-html="album.description | linky:'_blank':{rel: 'nofollow'} | newlines"></p>
</div>
<h2>Tracks</h2>

View file

@ -3,7 +3,7 @@
<div bo-show="artist.bio.trim().length">
<h2>Bio</h2>
<div class="description">
<p bo-html="artist.bio | linky | newlines"></p>
<p bo-html="artist.bio | linky:'_blank':{rel: 'nofollow'} | newlines"></p>
</div>
</div>

View file

@ -13,7 +13,7 @@
<img pfm-src-loader="comment.user.avatars.thumbnail" pfm-src-size="thumbnail" />
<div class="content">
<a bo-href="comment.user.url" bo-text="comment.user.name"></a>
<span bo-html="comment.content | linky"></span>
<span bo-html="comment.content | linky:'_blank':{rel: 'nofollow'}"></span>
<div class="meta" bo-text="comment.created_at.date | momentFromNow">
</div>
</div>

View file

@ -51,7 +51,7 @@
<div class="left">
<div class="description" bo-show="playlist.description.length">
<h2>Description</h2>
<p bo-html="playlist.description | linky | newlines"></p>
<p bo-html="playlist.description | linky:'_blank':{rel: 'nofollow'} | newlines"></p>
</div>
<h2>Tracks</h2>

View file

@ -71,7 +71,7 @@
<div class="left">
<div class="description" bo-show="track.description.length">
<h2>Description</h2>
<p bo-html="track.description | linky | newlines"></p>
<p bo-html="track.description | linky:'_blank':{rel: 'nofollow'} | newlines"></p>
</div>
<div bo-show="track.is_vocal && track.lyrics.length" class="lyrics-panel">

View file

@ -1,4 +1,6 @@
/**
* This is a customized version of ngSanitize for Pony.fm. The linky filter has been updated to the version found in Angular 1.5.0
*
* @license AngularJS v1.2.0
* (c) 2010-2012 Google, Inc. http://angularjs.org
* License: MIT
@ -537,41 +539,73 @@ angular.module('ngSanitize', []).value('$sanitize', $sanitize);
</doc:scenario>
</doc:example>
*/
angular.module('ngSanitize').filter('linky', function() {
function sanitizeText(chars) {
var buf = [];
var writer = htmlSanitizeWriter(buf, angular.noop);
writer.chars(chars);
return buf.join('');
}
angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
var LINKY_URL_REGEXP =
/((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>]/,
MAILTO_REGEXP = /^mailto:/;
/((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i,
MAILTO_REGEXP = /^mailto:/i;
return function(text, target) {
return function(text, target, attributes) {
if (!text) return text;
var match;
var raw = text;
var html = [];
// TODO(vojta): use $sanitize instead
var writer = htmlSanitizeWriter(html);
var url;
var i;
var properties = {};
if (angular.isDefined(target)) {
properties.target = target;
}
while ((match = raw.match(LINKY_URL_REGEXP))) {
// We can not end in these as they are sometimes found at the end of the sentence
url = match[0];
// if we did not match ftp/http/mailto then assume mailto
if (match[2] == match[3]) url = 'mailto:' + url;
// if we did not match ftp/http/www/mailto then assume mailto
if (!match[2] && !match[4]) {
url = (match[3] ? 'http://' : 'mailto:') + url;
}
i = match.index;
writer.chars(raw.substr(0, i));
properties.href = url;
writer.start('a', properties);
writer.chars(match[0].replace(MAILTO_REGEXP, ''));
writer.end('a');
addText(raw.substr(0, i));
addLink(url, match[0].replace(MAILTO_REGEXP, ''));
raw = raw.substring(i + match[0].length);
}
writer.chars(raw);
return html.join('');
addText(raw);
return $sanitize(html.join(''));
function addText(text) {
if (!text) {
return;
}
html.push(sanitizeText(text));
}
function addLink(url, text) {
var key;
html.push('<a ');
if (angular.isFunction(attributes)) {
attributes = attributes(url);
}
if (angular.isObject(attributes)) {
for (key in attributes) {
html.push(key + '="' + attributes[key] + '" ');
}
} else {
attributes = {};
}
if (angular.isDefined(target) && !('target' in attributes)) {
html.push('target="',
target,
'" ');
}
html.push('href="',
url.replace(/"/g, '&quot;'),
'">');
addText(text);
html.push('</a>');
}
};
});
}]);
})(window, window.angular);