Snippet:
- Filter |
- Source |
- Rendered |
+ Filter |
+ Source |
+ Rendered |
linky filter |
@@ -55,10 +54,19 @@
linky target |
- <div ng-bind-html="snippetWithTarget | linky:'_blank'"> </div>
+ <div ng-bind-html="snippetWithSingleURL | linky:'_blank'"> </div>
+ |
+
+
+ |
+
+
+ linky custom attributes |
+
+ <div ng-bind-html="snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}"> </div>
|
-
+
|
@@ -68,6 +76,18 @@
+
+ angular.module('linkyExample', ['ngSanitize'])
+ .controller('ExampleController', ['$scope', function($scope) {
+ $scope.snippet =
+ 'Pretty text with some links:\n'+
+ 'http://angularjs.org/,\n'+
+ 'mailto:us@somewhere.org,\n'+
+ 'another@somewhere.org,\n'+
+ 'and one more: ftp://127.0.0.1/.';
+ $scope.snippetWithSingleURL = 'http://angularjs.org/';
+ }]);
+
it('should linkify the snippet with urls', function() {
expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
@@ -95,10 +115,17 @@
it('should work with the target property', function() {
expect(element(by.id('linky-target')).
- element(by.binding("snippetWithTarget | linky:'_blank'")).getText()).
+ element(by.binding("snippetWithSingleURL | linky:'_blank'")).getText()).
toBe('http://angularjs.org/');
expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank');
});
+
+ it('should optionally add custom attributes', function() {
+ expect(element(by.id('linky-custom-attributes')).
+ element(by.binding("snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}")).getText()).
+ toBe('http://angularjs.org/');
+ expect(element(by.css('#linky-custom-attributes a')).getAttribute('rel')).toEqual('nofollow');
+ });
*/
@@ -107,7 +134,7 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
/((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;
@@ -137,8 +164,19 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
}
function addLink(url, text) {
+ var key;
html.push('
http://example.com',
'
http://example.com');
});
+
+ describe('custom attributes', function() {
+
+ it('should optionally add custom attributes', function() {
+ expect(linky("http://example.com", "_self", {rel: "nofollow"})).
+ toBeOneOf('
http://example.com',
+ '
http://example.com');
+ });
+
+
+ it('should override target parameter with custom attributes', function() {
+ expect(linky("http://example.com", "_self", {target: "_blank"})).
+ toBeOneOf('
http://example.com',
+ '
http://example.com');
+ });
+
+
+ it('should optionally add custom attributes from function', function() {
+ expect(linky("http://example.com", "_self", function(url) {return {"class": "blue"};})).
+ toBeOneOf('
http://example.com',
+ '
http://example.com',
+ '
http://example.com');
+ });
+
+
+ it('should pass url as parameter to custom attribute function', function() {
+ var linkParameters = jasmine.createSpy('linkParameters').andReturn({"class": "blue"});
+ linky("http://example.com", "_self", linkParameters);
+ expect(linkParameters).toHaveBeenCalledWith('http://example.com');
+ });
+
+
+ it('should strip unsafe attributes', function() {
+ expect(linky("http://example.com", "_self", {"class": "blue", "onclick": "alert('Hi')"})).
+ toBeOneOf('
http://example.com',
+ '
http://example.com',
+ '
http://example.com');
+ });
+ });
});