Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Walker 43 posts 66 karma points
    Sep 28, 2011 @ 15:57
    John Walker
    0

    Library.StripHtml v 4.7.1

    Hi,

    I've just attempted to use the code

    @Library.StripHtml(item.introduction)

    Below is the text from the database and what is retruned.

    <p>Lorem ipsum dolor <strong>sit amet,</strong> consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    "Lorem ipsum dolor sit amet,"

    As you can see it seems to be breaking after the strong tag closes. Has anyone else had this problem, or am I just doing something wrong?

    John

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 28, 2011 @ 16:04
    Sebastiaan Janssen
    1

    Confirmed, this doesn't work for me either. 

    Alternatively, you can use this @function: 

    @using HtmlAgilityPack
    
    @functions {
      public static string StripHtml(string html)
      {
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
        return htmlDoc.DocumentNode.InnerText;
      }
    }
  • John Walker 43 posts 66 karma points
    Sep 28, 2011 @ 17:13
    John Walker
    0

     

    Thanks Sebastiaan,

    In the end I created a extension to the library using a regex to do this.

    I want to add the code snippet but the style is not avaiable in my comment window!?

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 28, 2011 @ 17:16
    Sebastiaan Janssen
    0

    I would never ever recommend you do HTML sanitizing with regex, so many pitfalls there. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

    Just paste the code snippet, and format it with "Preformatted"!

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 28, 2011 @ 21:36
    Dan Diplo
    0

    I agree with Seb - regex are dangerous for anything but the most simple examples. Use the HtmlAgility pack - it's much more robust.

  • Gareth Evans 143 posts 335 karma points c-trib
    Oct 04, 2011 @ 22:29
    Gareth Evans
    0

    Could you please paste your original HTML including any open/closing tags?

    The StripHTML function should work for this (and it doesn't use regex)

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Oct 04, 2011 @ 22:34
    Sebastiaan Janssen
    0

    @Gareth I took that same HTML from the opening post with the <strong> mixed in and found the same problem.

  • Gareth Evans 143 posts 335 karma points c-trib
    Oct 04, 2011 @ 22:35
    Gareth Evans
    0

    So like this: "Lorem ipsum dolor sit amet,<strong>I am bold</strong> Lorem ipsum dolor sit amet"

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Oct 04, 2011 @ 22:36
    Sebastiaan Janssen
    0

    Yup, that should do the trick!

  • Gareth Evans 143 posts 335 karma points c-trib
    Oct 04, 2011 @ 23:36
    Gareth Evans
    0

    Please test this build http://dl.dropbox.com/u/2923715/LatestRazorMacroEngine-Only.zip

    I have rewritten @Library.StripHtml to use HtmlAgilityPack

  • John Walker 43 posts 66 karma points
    Oct 06, 2011 @ 12:57
    John Walker
    0

    Hi Gareth,

    Just tested the updated dll my results are below;

    Works fine:

    @Library.StripHtml("<strong>test</strong> goes here")

    Errors:

    @Library.StripHtml("test")

    So looks like your strip html is working but I would expect it also to be able to handle strings without html elements. Error messgae is below I hope this helps with your testing. John

    Error Loading Razor Script (file: Calendar List) Object reference not set to an instance of an object.    at umbraco.MacroEngines.Library.RazorLibraryCore.StripHtmlTags(String html, List`1 tags) in C:\Work Files\udev\umbraco.MacroEngines.Juno\RazorDynamicNode\RazorLibraryCore.cs:line 511
      at umbraco.MacroEngines.Library.RazorLibraryCore.StripHtml(String html) in C:\Work Files\udev\umbraco.MacroEngines.Juno\RazorDynamicNode\RazorLibraryCore.cs:line 474
      at ASP._Page_macroScripts_CalendarList_cshtml.Execute() in c:\git\s4l\Site\macroScripts\CalendarList.cshtml:line 66
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
      at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors)
      at System.Web.WebPages.WebPage.ExecutePageHierarchy()
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer)
      at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage) in C:\Work Files\udev\umbraco.MacroEngines.Juno\RazorCore\RazorMacroEngine.cs:line 113
      at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage) in C:\Work Files\udev\
    .MacroEngines.Juno\RazorCore\RazorMacroEngine.cs:line 160

  • Carlos 338 posts 472 karma points
    Aug 10, 2012 @ 21:07
    Carlos
    0

    Can stripHTML strip out object tags? 

    This is my current version. But it seems like it is stripping everything else out but the object tag, Making my Macro not render.

    @Library.Truncate(Library.StripHtml(@nodeItem.GetProperty("bodyText").Value"img""a""object""param""table""embed""iframe""strong""b""p"),300)

  • Gareth Evans 143 posts 335 karma points c-trib
    Aug 12, 2012 @ 23:20
    Gareth Evans
    0

    Hi Carlos

    That should work, but the Library.StripHtml method is based on HtmlAgilityPack - so if it's not being stripped that would suggest the fault lies there.
    Could you try without specifying the tags list to strip out?

    Gareth

  • Carlos 338 posts 472 karma points
    Aug 13, 2012 @ 17:33
    Carlos
    0

    Gareth

    I should have responded back to my own post.  For some reason, and we have had this issue before, is that some nodes suddenly became unpublished.  I did a batch publish of the file, but it did not actually publish each node.  Meaning my 'bodyText' property was not rendering. I think it had to rebuild the XML. Our site is over 2000 pages. Not sure if this is an issue with Umbraco or anything else.  But by republishing the nodes individually I was able to get the content to render, without the tags specified.  It all worked out in the end.

    Thanks.

    Carlos

  • Jacob 41 posts 90 karma points
    May 02, 2014 @ 19:02
    Jacob
    0

    This comment has been edited and moved to a more appropriate topic.

Please Sign in or register to post replies

Write your reply to:

Draft