Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

An extended Html.Encode function for the Asp.Net Mvc HtmlHelper February 12, 2010

Filed under: Asp.Net,C#,MVC — Rupert Bates @ 2:52 pm

The built in Asp.Net Html encoding function misses out a large number of extended characters (including apostrophes). This extension method for the HtmlHelper class does a much more thorough job:

using System.Collections.Generic;
using System.Web.Mvc;

namespace KableDirect.Web.FrontEnd.Helpers
{
    public static class HtmlHelperExtensions
    {
        #region The List of replacements

        public static readonly Dictionary<string, string> Replacements = new Dictionary<string, string>
                                                                             {
                                                                                 {"–", "&#8211;"},
                                                                                 {"—", "&#8212;"},
                                                                                 {"‘", "&#8216;"},
                                                                                 {"’", "&#8217;"},
                                                                                 {"‚", "&#8218;"},
                                                                                 {"“", "&#8220;"},
                                                                                 {"”", "&#8221;"},
                                                                                 {"„", "&#8222;"},
                                                                                 {"†", "&#8224;"},
                                                                                 {"‡", "&#8225;"},
                                                                                 {"•", "&#8226;"},
                                                                                 {"…", "&#8230;"},
                                                                                 {"‰", "&#8240;"},
                                                                                 {"€", "&#8364;"},
                                                                                 {"™", "&#8482;"},
                                                                                 {"'", "&#39;"},
                                                                                 {"ą", "&#261;"},
                                                                                 {"Ą", "&#260;"},
                                                                                 {"ę", "&#281;"},
                                                                                 {"Ę", "&#280;"},
                                                                                 {"Ā", "&#256;"},
                                                                                 {"ā", "&#257;"},
                                                                                 {"Ă", "&#258;"},
                                                                                 {"ă", "&#259;"},
                                                                                 {"Ǟ", "&#478;"},
                                                                                 {"ǟ", "&#479;"},
                                                                                 {"Ǻ", "&#506;"},
                                                                                 {"ǻ", "&#507;"},
                                                                                 {"Ǽ", "&#508;"},
                                                                                 {"ǽ", "&#509;"},
                                                                                 {"Ḃ", "&#7682;"},
                                                                                 {"ḃ", "&#7683;"},
                                                                                 {"Ć", "&#262;"},
                                                                                 {"ć", "&#263;"},
                                                                                 {"Č", "&#268;"},
                                                                                 {"č", "&#269;"},
                                                                                 {"Ĉ", "&#264;"},
                                                                                 {"ĉ", "&#265;"},
                                                                                 {"Ċ", "&#266;"},
                                                                                 {"ċ", "&#267;"},
                                                                                 {"Ḑ", "&#7696;"},
                                                                                 {"ḑ", "&#7697;"},
                                                                                 {"Ď", "&#270;"},
                                                                                 {"ď", "&#271;"},
                                                                                 {"Ḋ", "&#7690;"},
                                                                                 {"ḋ", "&#7691;"},
                                                                                 {"Đ", "&#272;"},
                                                                                 {"đ", "&#273;"},
                                                                                 {"DZ", "&#497;"},
                                                                                 {"Dz", "&#498;"},
                                                                                 {"dz", "&#499;"},
                                                                                 {"DŽ", "&#452;"},
                                                                                 {"Dž", "&#453;"},
                                                                                 {"dž", "&#454;"},
                                                                                 {"Ě", "&#282;"},
                                                                                 {"ě", "&#283;"},
                                                                                 {"Ē", "&#274;"},
                                                                                 {"ē", "&#275;"},
                                                                                 {"Ĕ", "&#276;"},
                                                                                 {"ĕ", "&#277;"},
                                                                                 {"Ė", "&#278;"},
                                                                                 {"ė", "&#279;"},
                                                                                 {"Ʒ", "&#439;"},
                                                                                 {"ʒ", "&#658;"},
                                                                                 {"Ǯ", "&#494;"},
                                                                                 {"ǯ", "&#495;"},
                                                                                 {"Ḟ", "&#7710;"},
                                                                                 {"ḟ", "&#7711;"},
                                                                                 {"ƒ", "&#402;"},
                                                                                 {"ff", "&#64256;"},
                                                                                 {"fi", "&#64257;"},
                                                                                 {"fl", "&#64258;"},
                                                                                 {"ffi", "&#64259;"},
                                                                                 {"ffl", "&#64260;"},
                                                                                 {"ſt", "&#64261;"},
                                                                                 {"Ǵ", "&#500;"},
                                                                                 {"ǵ", "&#501;"},
                                                                                 {"Ģ", "&#290;"},
                                                                                 {"ģ", "&#291;"},
                                                                                 {"Ǧ", "&#486;"},
                                                                                 {"ǧ", "&#487;"},
                                                                                 {"Ĝ", "&#284;"},
                                                                                 {"ĝ", "&#285;"},
                                                                                 {"Ğ", "&#286;"},
                                                                                 {"ğ", "&#287;"},
                                                                                 {"Ġ", "&#288;"},
                                                                                 {"ġ", "&#289;"},
                                                                                 {"Ǥ", "&#484;"},
                                                                                 {"ǥ", "&#485;"},
                                                                                 {"Ĥ", "&#292;"},
                                                                                 {"ĥ", "&#293;"},
                                                                                 {"Ħ", "&#294;"},
                                                                                 {"ħ", "&#295;"},
                                                                                 {"Ĩ", "&#296;"},
                                                                                 {"ĩ", "&#297;"},
                                                                                 {"Ī", "&#298;"},
                                                                                 {"ī", "&#299;"},
                                                                                 {"Ĭ", "&#300;"},
                                                                                 {"ĭ", "&#301;"},
                                                                                 {"Į", "&#302;"},
                                                                                 {"į", "&#303;"},
                                                                                 {"İ", "&#304;"},
                                                                                 {"ı", "&#305;"},
                                                                                 {"IJ", "&#306;"},
                                                                                 {"ij", "&#307;"},
                                                                                 {"Ĵ", "&#308;"},
                                                                                 {"ĵ", "&#309;"},
                                                                                 {"Ḱ", "&#7728;"},
                                                                                 {"ḱ", "&#7729;"},
                                                                                 {"Ķ", "&#310;"},
                                                                                 {"ķ", "&#311;"},
                                                                                 {"Ǩ", "&#488;"},
                                                                                 {"ǩ", "&#489;"},
                                                                                 {"ĸ", "&#312;"},
                                                                                 {"Ĺ", "&#313;"},
                                                                                 {"ĺ", "&#314;"},
                                                                                 {"Ļ", "&#315;"},
                                                                                 {"ļ", "&#316;"},
                                                                                 {"Ľ", "&#317;"},
                                                                                 {"ľ", "&#318;"},
                                                                                 {"Ŀ", "&#319;"},
                                                                                 {"ŀ", "&#320;"},
                                                                                 {"Ł", "&#321;"},
                                                                                 {"ł", "&#322;"},
                                                                                 {"LJ", "&#455;"},
                                                                                 {"Lj", "&#456;"},
                                                                                 {"lj", "&#457;"},
                                                                                 {"Ṁ", "&#7744;"},
                                                                                 {"ṁ", "&#7745;"},
                                                                                 {"Ń", "&#323;"},
                                                                                 {"ń", "&#324;"},
                                                                                 {"Ņ", "&#325;"},
                                                                                 {"ņ", "&#326;"},
                                                                                 {"Ň", "&#327;"},
                                                                                 {"ň", "&#328;"},
                                                                                 {"ʼn", "&#329;"},
                                                                                 {"Ŋ", "&#330;"},
                                                                                 {"ŋ", "&#331;"},
                                                                                 {"NJ", "&#458;"},
                                                                                 {"Nj", "&#459;"},
                                                                                 {"nj", "&#460;"},
                                                                                 {"Ō", "&#332;"},
                                                                                 {"ō", "&#333;"},
                                                                                 {"Ŏ", "&#334;"},
                                                                                 {"ŏ", "&#335;"},
                                                                                 {"Ő", "&#336;"},
                                                                                 {"ő", "&#337;"},
                                                                                 {"Ǿ", "&#510;"},
                                                                                 {"ǿ", "&#511;"},
                                                                                 {"Œ", "&#338;"},
                                                                                 {"œ", "&#339;"},
                                                                                 {"Ṗ", "&#7766;"},
                                                                                 {"ṗ", "&#7767;"},
                                                                                 {"Ŕ", "&#340;"},
                                                                                 {"ŕ", "&#341;"},
                                                                                 {"Ŗ", "&#342;"},
                                                                                 {"ŗ", "&#343;"},
                                                                                 {"Ř", "&#344;"},
                                                                                 {"ř", "&#345;"},
                                                                                 {"ɼ", "&#636;"},
                                                                                 {"Ś", "&#346;"},
                                                                                 {"ś", "&#347;"},
                                                                                 {"Ş", "&#350;"},
                                                                                 {"ş", "&#351;"},
                                                                                 {"Š", "&#352;"},
                                                                                 {"š", "&#353;"},
                                                                                 {"Ŝ", "&#348;"},
                                                                                 {"ŝ", "&#349;"},
                                                                                 {"Ṡ", "&#7776;"},
                                                                                 {"ṡ", "&#7777;"},
                                                                                 {"ſ", "&#383;"},
                                                                                 {"Ţ", "&#354;"},
                                                                                 {"ţ", "&#355;"},
                                                                                 {"Ť", "&#356;"},
                                                                                 {"ť", "&#357;"},
                                                                                 {"Ṫ", "&#7786;"},
                                                                                 {"ṫ", "&#7787;"},
                                                                                 {"Ŧ", "&#358;"},
                                                                                 {"ŧ", "&#359;"},
                                                                                 {"Ũ", "&#360;"},
                                                                                 {"ũ", "&#361;"},
                                                                                 {"Ů", "&#366;"},
                                                                                 {"ů", "&#367;"},
                                                                                 {"Ū", "&#362;"},
                                                                                 {"ū", "&#363;"},
                                                                                 {"Ŭ", "&#364;"},
                                                                                 {"ŭ", "&#365;"},
                                                                                 {"Ų", "&#370;"},
                                                                                 {"ų", "&#371;"},
                                                                                 {"Ű", "&#368;"},
                                                                                 {"ű", "&#369;"},
                                                                                 {"Ẁ", "&#7808;"},
                                                                                 {"ẁ", "&#7809;"},
                                                                                 {"Ẃ", "&#7810;"},
                                                                                 {"ẃ", "&#7811;"},
                                                                                 {"Ŵ", "&#372;"},
                                                                                 {"ŵ", "&#373;"},
                                                                                 {"Ẅ", "&#7812;"},
                                                                                 {"ẅ", "&#7813;"},
                                                                                 {"Ỳ", "&#7922;"},
                                                                                 {"ỳ", "&#7923;"},
                                                                                 {"Ŷ", "&#374;"},
                                                                                 {"ŷ", "&#375;"},
                                                                                 {"Ÿ", "&#376;"},
                                                                                 {"Ź", "&#377;"},
                                                                                 {"ź", "&#378;"},
                                                                                 {"Ž", "&#381;"},
                                                                                 {"ž", "&#382;"},
                                                                                 {"Ż", "&#379;"},
                                                                                 {"ż", "&#380;"}
                                                                             };
        #endregion

        /// <summary>
        /// A fuller html encoding than the built in method, will encode a large number of characters ignored by the built in method including apostrophes.
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string EncodeFull(this HtmlHelper helper, string html)
        {
            var result = helper.Encode(html);
            foreach (var replacement in Replacements)
            {
                result = result.Replace(replacement.Key, replacement.Value);
            }
            return result;
        }
    }
}

And here is a unit test for it, this uses the HtmlHelperFactory class I described here:

using System.Linq;
using System.Web.Routing;
using KableDirect.Web.FrontEnd.Helpers;
using MbUnit.Framework;

namespace KableDirect.UnitTests
{
    [TestFixture]
    public class TestHtmlEncode
    {
        [Test]
        public void test_all_chars_are_encoded()
        {
            var html = HtmlHelperFactory.CreateInstance(new RouteData());
            var specials = "–—‘’‚“”„†‡•…‰€™ŒœŠšŸƒ’ÀàÁáÂâÃãÄäÅåĀāĂ㥹ǞǟǺǻÆæǼǽḂḃĆćÇçČčĈĉĊċḐḑĎďḊḋĐđÐðDZDzdzDŽDždžÈèÉéĚěÊêËëĒēĔĕĘęĖėƷʒǮǯḞḟƒfffiflffifflſtǴǵĢģǦǧĜĝĞğĠġǤǥĤĥĦħÌìÍíÎîĨĩÏïĪīĬĭĮįİıIJijĴĵḰḱĶķǨǩĸĹĺĻļĽľĿŀŁłLJLjljṀṁŃńŅņŇňÑñʼnŊŋNJNjnjÒòÓóÔôÕõÖöŌōŎŏØøŐőǾǿŒœṖṗŔŕŖŗŘřɼŚśŞşŠšŜŝṠṡſßŢţŤťṪṫŦŧÞþÙùÚúÛûŨũÜüŮůŪūŬŭŲųŰűẀẁẂẃŴŵẄẅỲỳÝýŶŷŸÿŹźŽžŻż";
            var encoded = html.EncodeFull(specials);
            var specialChars = specials.ToCharArray();
            var missedChars = encoded.ToCharArray().Intersect(specialChars);
            string output = "";
            foreach(var ch in missedChars)
            {
                output += "{\"" + ch + "\", \"&#" + ((int) ch) + ";\"},\n";
            }
            Assert.IsEmpty(output);
        }
    }
}
Advertisements
 

One Response to “An extended Html.Encode function for the Asp.Net Mvc HtmlHelper”

  1. […] to VoteAn extended Html.Encode function for the Asp.Net Mvc HtmlHelper (2/12/2010)Friday, February 12, 2010 from Rupert BatesThe built in Asp.Net Html encoding function misses out a […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s