-1

Can anyone tell me why this is not encoding using htmlencode

any string that has < before the string ie

<something or &something 

is not being displayed back to the html page when looking at the encoding the < and & is not being encoded. I would have expected these characters to be encoded to < or &

edit: this is the code I use to encode the string:

var replacedHtml = Regex.Replace(html,
            @"</?(\w*)[^>]*>",
            me => AllowedTags.Any(s => s.Equals(me.Groups[1].Value, StringComparison.OrdinalIgnoreCase))
                ? me.Value
                : HttpUtility.HtmlEncode(me.Value), RegexOptions.Singleline);

        return replacedHtml;

edit: i think the issue is not on the server side but rather on the angular side. the ng-bind-html

<span ng-bind-html="ctl.linkGroup.Notes | TextToHtmlSafe">

angular.module('CPSCore.Filters')
.filter('TextToHtmlSafe', ['$sce',function ($sce) {
    return function (text) {
        if (!text)
            return text;

        var htmlText = text.replace(/\n/g, '<br />');
        return $sce.trustAsHtml(htmlText);
    };
}]);

is declaring that

<something 

without the closing tag is not safe and therefore removes it from the view

Dritzz
  • 159
  • 1
  • 1
  • 10

2 Answers2

0

Try System.Net.WebUtility.HtmlDecode to properly decode special characters. Using this, < changes to &lt; and & changes to &amp; which is properly displayed html pages.

Adersh M
  • 596
  • 3
  • 19
0

In HTML, the ampersand character (“&”) declares the beginning of an entity reference (a special character). If you want one to appear in text on a web page you should use the encoded named entity “&amp;”—more technical mumbo-jumbo at w3c.org. While most web browsers will let you get away without encoding them, stuff can get dicey in weird edge cases and fail completely in XML.

The other main characters to remember to encode are < (&lt;) and > (&gt;), you don’t want to confuse your browser about where HTML tags start and end

Kamruzzaman
  • 1,423
  • 1
  • 12
  • 14