1

J'essaie de mettre en œuvre un désinfectant HTML Whitelist en utilisant HtmlAgilityPack. Je veux créer une Helt Html réutilisable qui me permet de l'utiliser. J'ai d'autres aides Html personnalisées que j'utilise qui fonctionnent très bien, mais pour une raison quelconque, celle-ci ne fonctionnera pas. Chaque fois que j'essaie de l'appeler à partir d'une vue, il est incapable de trouver la méthode Sanitize. L'appel que je suis en train de faire est une vue partielle et ressemble à ceci:Custom Heltml Helper ne devient pas disponible dans la vue (Razor/Asp.Net MVC3)

@Html.Raw(Html.Sanitize(Model.Body)) 

Mon Html Classe d'assistance:

using System; 
using System.Text; 
using System.Web.Mvc; 
using System.Collections.Generic; 
using System.Linq; 
using HtmlAgilityPack; 

namespace ProjectX.WebUI.HtmlHelpers 
{ 
    public static class HtmlSanitizeHelpers 
    { 
     private static readonly IDictionary<string, string[]> Whitelist; 
     private static List<string> DeletableNodesXpath = new List<string>(); 

     static HtmlSanitizeHelpers() 
     { 
      Whitelist = new Dictionary<string, string[]> { 
       { "a", new[] { "href" } }, 
       { "strong", null }, 
       { "em", null }, 
       { "blockquote", null }, 
       { "b", null}, 
       { "p", null}, 
       { "ul", null}, 
       { "ol", null}, 
       { "li", null}, 
       { "div", new[] { "align" } }, 
       { "strike", null}, 
       { "u", null},     
       { "sub", null}, 
       { "sup", null}, 
       { "table", null }, 
       { "tr", null }, 
       { "td", null }, 
       { "th", null } 
       }; 
     } 

     public static MvcHtmlString Sanitize(string input) 
     { 
      if (input.Trim().Length < 1) 
       return MvcHtmlString.Empty; 
      var htmlDocument = new HtmlDocument(); 

      htmlDocument.LoadHtml(input);    
      SanitizeNode(htmlDocument.DocumentNode); 
      string xPath = HtmlSanitizeHelpers.CreateXPath(); 

      return MvcHtmlString.Create(StripHtml(htmlDocument.DocumentNode.WriteTo().Trim(), xPath)); 
     } 

     private static void SanitizeChildren(HtmlNode parentNode) 
     { 
      for (int i = parentNode.ChildNodes.Count - 1; i >= 0; i--) 
      { 
       SanitizeNode(parentNode.ChildNodes[i]); 
      } 
     } 

     private static void SanitizeNode(HtmlNode node) 
     { 
      if (node.NodeType == HtmlNodeType.Element) 
      { 
       if (!Whitelist.ContainsKey(node.Name)) 
       { 
        if (!DeletableNodesXpath.Contains(node.Name)) 
        {      
         //DeletableNodesXpath.Add(node.Name.Replace("?","")); 
         node.Name = "removeableNode"; 
         DeletableNodesXpath.Add(node.Name); 
        } 
        if (node.HasChildNodes) 
        { 
         SanitizeChildren(node); 
        }     

        return; 
       } 

       if (node.HasAttributes) 
       { 
        for (int i = node.Attributes.Count - 1; i >= 0; i--) 
        { 
         HtmlAttribute currentAttribute = node.Attributes[i]; 
         string[] allowedAttributes = Whitelist[node.Name]; 
         if (allowedAttributes != null) 
         { 
          if (!allowedAttributes.Contains(currentAttribute.Name)) 
          { 
           node.Attributes.Remove(currentAttribute); 
          } 
         } 
         else 
         { 
          node.Attributes.Remove(currentAttribute); 
         } 
        } 
       } 
      } 

      if (node.HasChildNodes) 
      { 
       SanitizeChildren(node); 
      } 
     } 

     private static string StripHtml(string html, string xPath) 
     { 
      HtmlDocument htmlDoc = new HtmlDocument(); 
      htmlDoc.LoadHtml(html); 
      if (xPath.Length > 0) 
      { 
       HtmlNodeCollection invalidNodes = htmlDoc.DocumentNode.SelectNodes(@xPath); 
       foreach (HtmlNode node in invalidNodes) 
       { 
        node.ParentNode.RemoveChild(node, true); 
       } 
      } 
      return htmlDoc.DocumentNode.WriteContentTo(); ; 
     } 

     private static string CreateXPath() 
     { 
      string _xPath = string.Empty; 
      for (int i = 0; i < DeletableNodesXpath.Count; i++) 
      { 
       if (i != DeletableNodesXpath.Count - 1) 
       { 
        _xPath += string.Format("//{0}|", DeletableNodesXpath[i].ToString()); 
       } 
       else _xPath += string.Format("//{0}", DeletableNodesXpath[i].ToString()); 
      } 
      return _xPath; 
     } 
    } 
} 

Crédits pour la plupart de ce code va à la réponse à this posting.

choses que j'ai déjà vérifié:

  1. Namespace a été correctement défini dans le fichier web.config. (Je le sais aussi puisque d'autres dans l'espace de noms fonctionnent déjà)
  2. Avoir fait une construction propre du projet.
  3. REStarted Visual Studio 2010.

Réflexions sur la raison pour laquelle je ne peux pas sembler appeler la méthode de la classe?

Répondre

1

Il ne semble pas que vous ayez correctement étendu le HtmlHelper.

Il vous manque ce qui suit dans la définition de votre fonction:

public static MvcHtmlString Sanitize(this HtmlHelper helper, string input) 

Vérifiez ce post sur Using Class Extensions to simplify your code with the UrlHelper

+0

Ce fut en effet la réponse. Je vous remercie. Cela m'a complètement échappé. –

Questions connexes