Accented urls - Angular Js
HTML
<body ng-app="myApp" ng-controller="myCtrl">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div>
<h1>Example from model json</h1>
<p>Link: <a href="{{htmlEnDeCode.htmlDecode(item1.url)}}">{{htmlEnDeCode.htmlDecode(item1.url)}}</a></p>
</div>
<div>
<h1>Example from ng-init</h1>
<div ng-init="item2 = {'url':'http://www.example.com/file-áúó.pdf'}"></div>
<p>Go to <a ng-href="{{item2.url}}">{{item2.url}}</a> to learn!</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', function($scope) {
$scope.item1 = {
'url': 'http://www.example.com/file-áúó.pdf'
};
$scope.htmlEnDeCode = (function() {
var charToEntityRegex,
entityToCharRegex,
charToEntity,
entityToChar;
function resetCharacterEntities() {
charToEntity = {};
entityToChar = {};
// add the default set
addCharacterEntities({
'&' : '&',
'>' : '>',
'<' : '<',
'"' : '"',
''' : "'"
});
}
function addCharacterEntities(newEntities) {
var charKeys = [],
entityKeys = [],
key, echar;
for (key in newEntities) {
echar = newEntities[key];
entityToChar[key] = echar;
charToEntity[echar] = key;
charKeys.push(echar);
entityKeys.push(key);
}
charToEntityRegex = new RegExp('(' + charKeys.join('|') + ')', 'g');
entityToCharRegex = new RegExp('(' + entityKeys.join('|') + '|&#[0-9]{1,5};' + ')', 'g');
}
function htmlDecode(value) {
var htmlDecodeReplaceFn = function(match, capture) {
return (capture in entityToChar) ? entityToChar[capture] : String.fromCharCode(parseInt(capture.substr(2), 10));
};
...