Copied to clipboard

Flag this post as spam?

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


  • Arun 144 posts 407 karma points
    Apr 17, 2020 @ 11:43
    Arun
    0

    Empty paragraph tag in Rich Text Editor. How to remove this empty <p> tags

    Hi, I'm inserting List items inside Rich Text Editor and displaying it with

    @Html.Raw(HttpUtility.HtmlDecode(listItemDescription))

    The data get rendered as expected but on inspecting the code I can see an extra empty p tag like this <p></p> just below the </ul>

    I have tried replacing p tags with string.empty but seems not working for list items.

    How to remove this empty <p></p>

    Anybody knows the solution?

    update on June 8 2020
    I changed my rendering code from @Html.Raw(HttpUtility.HtmlDecode(listItemDescription)) to @Html.Raw(library.RemoveFirstParagraphTag(listItemDescription))
    Now i got my empty p tag in the begining and end got removed,
    But still the list items create empty p tag in mobile
    -Regards Arun

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Apr 17, 2020 @ 11:59
    Alex Skrypnyk
    0

    Hi Arun,

    Are you using Umbraco 7? in v7 you can use - umbraco.library.RemoveFirstParagraphTag

    In Umbraco 8 there is no remove first paragraph method but you can find custom helper methods here - https://our.umbraco.com/forum/using-umbraco-and-getting-started/96264-v8-removefirstparagraphtag

    Thanks,

    Alex

  • Arun 144 posts 407 karma points
    Jun 08, 2020 @ 05:31
    Arun
    0

    Hi Alex,
    I'm using Umbraco 7.12.3
    -Arun

  • Tim Miller 32 posts 252 karma points
    Apr 17, 2020 @ 17:07
    Tim Miller
    0

    I've dealt with this. I solved it by using an ApplicationEventHandler and removed the extra spacing during the saving event. I can't find the v7 documentation for this, but v8 is here: https://our.umbraco.com/documentation/Reference/Events/ContentService-Events

    Anyway, here's the relevant bits from our v7 class. As you'll see I'm doing a few more clean up items than just removing this space.

    using log4net;
    using System;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using Umbraco.Core;
    using Umbraco.Core.Services;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    
    namespace MyProject.EventHandlers
    {
        public class MyContentEvents : ApplicationEventHandler
        {
            private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Saving += ContentService_Saving;
            }
    
            private static void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
            {
                foreach (var node in e.SavedEntities)
                {
                    ContentCleanup(node);
                }
            }
    
            private static void ContentCleanup(IContent node)
            {
                try
                {
                    foreach (var prop in node.PropertyTypes)
                    {
                        CleanupTinyMce(node, prop);
                    }
                }
                catch (NullReferenceException ex)
                {
                    Log.Warn("Failed custom saving event.", ex);
                }
            }
    
            private static void CleanupTinyMce(IContent node, PropertyType prop)
            {
                if (prop.PropertyEditorAlias != "Umbraco.TinyMCEv3" || !prop.IsDirty()) return;
    
                var property = node.Properties[prop.Alias];
    
                if (property.Value == null) return;
    
                var revPropVal = DoRteCleanup(property.Value.ToString());
                property.Value = revPropVal;
            }
    
            private static string DoRteCleanup(string str2Cln)
            {
                var retStr = str2Cln;
    
                // Let's replace stuff that we know is weird and will cause problems with spaces. Like this screwy non-breaking space.
                retStr = Regex.Replace(retStr, @"(\xa0|&nbsp;)", " ");
                // Replace the empty paragraph tags inserted into TinyMCE for no good reason with spaces.
                retStr = Regex.Replace(retStr, @"<p[^>]*>(<br[^>]*>)*(\s|&nbsp;|\r|\n)*(<br[^>]*>)*(\s|&nbsp;|\r|\n)*</p>",
                    " ");
                // Now remove any combos of multiple spaces to clean things up
                retStr = Regex.Replace(retStr, @" {2,}", " ");
                // Detect and convert phone numbers to tel links.
                retStr = Regex.Replace(retStr,
                    @"(?<start>[a-zA-Z]""?>|\s|^)(?<OriginalNumber>(\+?(?<NatCode>1)\s*[-\/\.]?)?(?<AreaCode>\(\d{3}\)|\d{3})\s*[-\/\.]?\s*(?<Number1>\d{3})\s*[-\/\.]?\s*(?<Number2>\d{4})\s*?(.*?([xX]|[eE][xX][tT])\.?\s*(?<Ext>\d+))?)(?<end>\.|<|\s)",
                    "${start}<a href=\"tel:+1${AreaCode}${Number1}${Number2}\">${OriginalNumber}</a>${end}",
                    RegexOptions.IgnoreCase);
                // Remove __mcenew id that is added to images (in some cases)
                retStr = Regex.Replace(retStr, @"id=""__mcenew""", "");
    
                return retStr;
            }
        }
    }
    
  • Arun 144 posts 407 karma points
    Jun 08, 2020 @ 05:32
    Arun
    0

    Thanks Tim,
    I will check this
    -Arun

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies