Emoji and other unicode characters not appearing c

2020-06-22 17:39发布

I have a web app that uses messages from Twitter, for example.

A feed of JSON is used to build a list of messages, but characters such as

2条回答
放荡不羁爱自由
2楼-- · 2020-06-22 18:19

I found that the regex above was breaking on some urls such as bit.yl shortlinks so i adapted another example i found somewhere:

twitterApp.filter('parseUrl', function($sce) {
    var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/gi;
    return function(text) {        
        angular.forEach(text.match(urlPattern), function(url) {
            text = text.replace(url, "<a target=\"_blank\" href="+ url + ">" + url +"</a>");
        });
        return $sce.trustAsHtml(text);        
    };
});

Usage:

<p ng-bind-html="tweet.text | parseUrl"></p>
查看更多
狗以群分
3楼-- · 2020-06-22 18:29

Note: This post does not display as intended on Chrome, as it doesn't support Emoji characters

It looks like Angular's $sanitize service attempts to convert the characters to their HTML-entity equivalent. However, for certain single emoji characters, it splits it to 2. As can be seen at http://plnkr.co/edit/fDDen3bnnrQUvx3JfKgw?p=preview,

$scope.sanitizedText = $sanitize('                                                                    
查看更多
登录 后发表回答