<!DOCTYPE html>
<html>
<head>
<!-- Jasmine -->
<link data-require="jasmine@*" data-semver="2.0.0" rel="stylesheet" href="//cdn.jsdelivr.net/jasmine/2.0.0/jasmine.css" />
<script data-require="jasmine@*" data-semver="2.0.0" src="//cdn.jsdelivr.net/jasmine/2.0.0/jasmine.js"></script>
<script data-require="jasmine@*" data-semver="2.0.0" src="//cdn.jsdelivr.net/jasmine/2.0.0/jasmine-html.js"></script>
<script data-require="jasmine@*" data-semver="2.0.0" src="//cdn.jsdelivr.net/jasmine/2.0.0/boot.js"></script>
<link rel="stylesheet" href="style.css" />
<!-- Angular -->
<script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.16/angular.js" data-semver="1.2.16"></script>
<script data-require="angular-mocks@*" data-semver="1.2.16" src="https://code.angularjs.org/1.2.16/angular-mocks.js"></script>
<script data-require="angular-sanitize@*" data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular-sanitize.js"></script>
<!-- Filter Def. -->
<script src="app.js"></script>
<!-- Jasmin test -->
<script src="app.spec.js"></script>
</head>
<body ng-app="myApp">
<!-- Usage -->
<div ng-init="text='http://step.pe'">
<p ng-bind-html="text | parseUrl"></p>
</div>
<!-- Unit tests are displayed here -->
<div id="HTMLReporter" class="jasmine_reporter"></div>
</body>
</html>
// Code goes here
angular.module('myApp', ['ngSanitize'])
.filter('parseUrl', function () {
var ptn = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
return function (input) {
return input.replace(ptn, '<a href=\"$1\" target=\"_blank\">$1</a>');
};
});
describe('Filter: parseUrl', function () {
// load the filter's module
beforeEach(module('myApp'));
// initialize a new instance of the filter before each test
var parseUrl;
beforeEach(inject(function ($filter) {
parseUrl = $filter('parseUrl');
}));
it('URL 1개를 추출한다.', function () {
var text = 'http://step.pe';
expect(parseUrl(text))
.toEqual('<a href="' + text + '" target="_blank">' + text + '</a>');
console.log(parseUrl(text));
});
it('URL 2개를 추출한다.', function () {
var url_1 = 'http://step.pe',
foo = ' foo ',
url_2 = 'https://google.com',
text = url_1 + foo + url_2;
expect(parseUrl(text))
.toEqual('<a href="' + url_1 + '" target="_blank">' + url_1 + '</a>' +
foo +
'<a href="' + url_2 + '" target="_blank">' + url_2 + '</a>');
console.log(parseUrl(text));
});
});