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

View file

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

View file

@ -13,7 +13,7 @@
<img pfm-src-loader="comment.user.avatars.thumbnail" pfm-src-size="thumbnail" /> <img pfm-src-loader="comment.user.avatars.thumbnail" pfm-src-size="thumbnail" />
<div class="content"> <div class="content">
<a bo-href="comment.user.url" bo-text="comment.user.name"></a> <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 class="meta" bo-text="comment.created_at.date | momentFromNow">
</div> </div>
</div> </div>

View file

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

View file

@ -71,7 +71,7 @@
<div class="left"> <div class="left">
<div class="description" bo-show="track.description.length"> <div class="description" bo-show="track.description.length">
<h2>Description</h2> <h2>Description</h2>
<p bo-html="track.description | linky | newlines"></p> <p bo-html="track.description | linky:'_blank':{rel: 'nofollow'} | newlines"></p>
</div> </div>
<div bo-show="track.is_vocal && track.lyrics.length" class="lyrics-panel"> <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 * @license AngularJS v1.2.0
* (c) 2010-2012 Google, Inc. http://angularjs.org * (c) 2010-2012 Google, Inc. http://angularjs.org
* License: MIT * License: MIT
@ -537,41 +539,73 @@ angular.module('ngSanitize', []).value('$sanitize', $sanitize);
</doc:scenario> </doc:scenario>
</doc:example> </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 = var LINKY_URL_REGEXP =
/((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>]/, /((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i,
MAILTO_REGEXP = /^mailto:/; MAILTO_REGEXP = /^mailto:/i;
return function(text, target) { return function(text, target, attributes) {
if (!text) return text; if (!text) return text;
var match; var match;
var raw = text; var raw = text;
var html = []; var html = [];
// TODO(vojta): use $sanitize instead
var writer = htmlSanitizeWriter(html);
var url; var url;
var i; var i;
var properties = {};
if (angular.isDefined(target)) {
properties.target = target;
}
while ((match = raw.match(LINKY_URL_REGEXP))) { while ((match = raw.match(LINKY_URL_REGEXP))) {
// We can not end in these as they are sometimes found at the end of the sentence // We can not end in these as they are sometimes found at the end of the sentence
url = match[0]; url = match[0];
// if we did not match ftp/http/mailto then assume mailto // if we did not match ftp/http/www/mailto then assume mailto
if (match[2] == match[3]) url = 'mailto:' + url; if (!match[2] && !match[4]) {
url = (match[3] ? 'http://' : 'mailto:') + url;
}
i = match.index; i = match.index;
writer.chars(raw.substr(0, i)); addText(raw.substr(0, i));
properties.href = url; addLink(url, match[0].replace(MAILTO_REGEXP, ''));
writer.start('a', properties);
writer.chars(match[0].replace(MAILTO_REGEXP, ''));
writer.end('a');
raw = raw.substring(i + match[0].length); raw = raw.substring(i + match[0].length);
} }
writer.chars(raw); addText(raw);
return html.join(''); 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); })(window, window.angular);