Advertisement
Guest User

KendoUI - ASP.NET MVC Defer Inline Scripts

a guest
Nov 22nd, 2012
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. public static class DeferHtmlHelperExtensions
  2. {
  3.     private const string DeferredScriptsKey = "deferredScripts";
  4.  
  5.     public static MvcHtmlString RenderDeferredScripts(this HtmlHelper htmlHelper)
  6.     {
  7.         var httpItems = htmlHelper.ViewContext.HttpContext.Items;
  8.  
  9.         if (!httpItems.Contains(DeferredScriptsKey)) return MvcHtmlString.Empty;
  10.  
  11.         var scripts = (IList<string>) httpItems[DeferredScriptsKey];
  12.  
  13.         var scriptBuilder = new StringBuilder(scripts.Sum(script => script.Length));
  14.  
  15.         foreach (var script in scripts.Where(script => !string.IsNullOrWhiteSpace(script)))
  16.             scriptBuilder.AppendLine(script);
  17.  
  18.         return new MvcHtmlString(scriptBuilder.ToString());
  19.     }
  20.  
  21.     public static MvcHtmlString Defer<TModel>(this HtmlHelper<TModel> htmlHelper, Func<HtmlHelper<TModel>, object> action)
  22.     {
  23.         var result = action(htmlHelper);
  24.  
  25.         string resultString = result is IHtmlString ? ((IHtmlString) result).ToHtmlString() : result.ToString();
  26.  
  27.         string html, script;
  28.  
  29.         ExtractScript(resultString, out html, out script);
  30.  
  31.         if (!String.IsNullOrWhiteSpace(script))
  32.         {
  33.             var httpItems = htmlHelper.ViewContext.HttpContext.Items;
  34.  
  35.             if (!httpItems.Contains(DeferredScriptsKey)) httpItems.Add(DeferredScriptsKey, new List<string>());
  36.  
  37.             var scripts = (IList<string>) httpItems[DeferredScriptsKey];
  38.  
  39.             scripts.Insert(0, script);
  40.         }
  41.  
  42.         return new MvcHtmlString(html);
  43.     }
  44.  
  45.     private static void ExtractScript(string input, out string html, out string script)
  46.     {
  47.         html = script = null;
  48.         if (String.IsNullOrWhiteSpace(input)) return;
  49.  
  50.         var htmlBuilder = new StringBuilder();
  51.         var scriptBuilder = new StringBuilder();
  52.  
  53.         bool isInScript = false;
  54.         const string scriptOpen = "<script";
  55.         const string scriptClose = "</script";
  56.  
  57.         for (int i = 0; i < input.Length; i++)
  58.         {
  59.             var c = input[i];
  60.  
  61.             if (c == '\r' || c == '\n') continue;
  62.  
  63.             if (isInScript)
  64.             {
  65.                 if (IsSequentialMatch(input, i, scriptClose))
  66.                 {
  67.                     scriptBuilder.Append("</script>");
  68.                     var closeIndex = input.IndexOf('>', i);
  69.                     if (closeIndex == -1) break;
  70.                     i = closeIndex;
  71.                     isInScript = false;
  72.                     continue;
  73.                 }
  74.  
  75.                 scriptBuilder.Append(c);
  76.                 continue;
  77.             }
  78.  
  79.             if (IsSequentialMatch(input, i, scriptOpen))
  80.             {
  81.                 isInScript = true;
  82.                 scriptBuilder.Append(c);
  83.                 continue;
  84.             }
  85.  
  86.             htmlBuilder.Append(c);
  87.         }
  88.  
  89.         html = htmlBuilder.ToString().Trim();
  90.         script = scriptBuilder.ToString().Trim();
  91.     }
  92.  
  93.     private static bool IsSequentialMatch(string input, int inputStartIndex, string toMatch)
  94.     {
  95.         if (input == null || toMatch == null || inputStartIndex < 0 || inputStartIndex >= input.Length) return false;
  96.  
  97.         for (int i = inputStartIndex; i < inputStartIndex + toMatch.Length; i++)
  98.             if (!input[i].Equals(toMatch[i - inputStartIndex])) return false;
  99.  
  100.         return true;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement