protected string ExtractStyles(string renderedTemplate) { var wrapLog = Log.Call <string>(); var styleMatches = StyleDetection.Matches(renderedTemplate); var styleMatchesToRemove = new List <Match>(); Log.Add($"Found {styleMatches.Count} external styles"); foreach (Match match in styleMatches) { var posInPage = "head"; // default for styles var priority = CssDefaultPriority; var optMatch = OptimizeDetection.Match(match.Value); // todo: ATM the priority and type is only detected in the Regex which expects "enable-optimizations" // ...so to improve this code, we would have to use 2 regexs - one for detecting "enable-optimizations" // ...and another for the priority etc. // skip if not matched and setting only wants matches if (ExtractOnlyEnableOptimization) { if (!optMatch.Success) { continue; } // skip if not stylesheet if (!StyleRelDetect.IsMatch(match.Value)) { continue; } posInPage = optMatch.Groups["Position"]?.Value ?? posInPage; priority = GetPriority(optMatch, priority); // don't register/remove if not within specs if (priority <= 0) { continue; } } // Register, then remember to remove later on var url = FixUrlWithSpaces(match.Groups["Src"].Value); Assets.Add(new ClientAssetInfo { IsJs = false, PosInPage = posInPage, Priority = priority, Url = url }); styleMatchesToRemove.Add(match); } styleMatchesToRemove.Reverse(); styleMatchesToRemove.ForEach(p => renderedTemplate = renderedTemplate.Remove(p.Index, p.Length)); return(wrapLog(null, renderedTemplate)); }
public override Tuple <string, bool> Process(string renderedTemplate) { if (HttpContext.Current == null || HttpContext.Current.CurrentHandler == null || !(HttpContext.Current.CurrentHandler is Page)) { return(new Tuple <string, bool>(renderedTemplate, false)); } var page = (HttpContext.Current.CurrentHandler as Page); var include2SxcJs = false; #region Handle Client Dependency injection #region Scripts var scriptMatches = ScriptDetection.Matches(renderedTemplate); var scriptMatchesToRemove = new List <Match>(); foreach (Match match in scriptMatches) { // always remove 2sxc JS requests from template and ensure it's added the standard way var url = FixUrlWithSpaces(match.Groups["Src"].Value); if (Is2SxcApiJs(url)) { include2SxcJs = true; scriptMatchesToRemove.Add(match); continue; } var optMatch = OptimizeDetection.Match(match.Value); if (!optMatch.Success) { continue; } var providerName = GetProviderName(optMatch, "body"); var prio = GetPriority(optMatch, (int)FileOrder.Js.DefaultPriority); if (prio <= 0) { continue; // don't register/remove if not within specs } #region Register, then add to remove-queue //if (!Is2SxcApiJs(url)) //{ ClientResourceManager.RegisterScript(page, url, prio, providerName); scriptMatchesToRemove.Add(match); //} #endregion } // remove in reverse order, so that the indexes don't change scriptMatchesToRemove.Reverse(); scriptMatchesToRemove.ForEach(p => renderedTemplate = renderedTemplate.Remove(p.Index, p.Length)); #endregion #region Styles var styleMatches = StyleDetection.Matches(renderedTemplate); var styleMatchesToRemove = new List <Match>(); foreach (Match match in styleMatches) { var optMatch = OptimizeDetection.Match(match.Value); if (!optMatch.Success) { continue; } // skip If the Rel attribute is not stylesheet if (!StyleRelDetect.IsMatch(match.Value)) { continue; } var providerName = GetProviderName(optMatch, "head"); var prio = GetPriority(optMatch, (int)FileOrder.Css.DefaultPriority); if (prio <= 0) { continue; // don't register/remove if not within specs } // Register, then remember to remove later on var url = FixUrlWithSpaces(match.Groups["Src"].Value); ClientResourceManager.RegisterStyleSheet(page, url, prio, providerName); styleMatchesToRemove.Add(match); } styleMatchesToRemove.Reverse(); styleMatchesToRemove.ForEach(p => renderedTemplate = renderedTemplate.Remove(p.Index, p.Length)); #endregion #endregion return(new Tuple <string, bool>(renderedTemplate, include2SxcJs)); }
public override string Process(string renderedTemplate) { if (HttpContext.Current == null || HttpContext.Current.CurrentHandler == null || !(HttpContext.Current.CurrentHandler is Page)) { return(renderedTemplate); } var page = (HttpContext.Current.CurrentHandler as Page); #region Handle Client Dependency injection #region Scripts var scriptMatches = ScriptDetection.Matches(renderedTemplate); var scriptMatchesToRemove = new List <Match>(); foreach (Match match in scriptMatches) { var optMatch = OptimizeDetection.Match(match.Value); if (!optMatch.Success) { continue; } var providerName = GetProviderName(optMatch, "body"); var prio = GetPriority(optMatch, (int)FileOrder.Js.DefaultPriority); if (prio <= 0) { continue; // don't register/remove if not within specs } // Register, then remember to remove later on ClientResourceManager.RegisterScript(page, match.Groups["Src"].Value, prio, providerName); scriptMatchesToRemove.Add(match); } scriptMatchesToRemove.Reverse(); scriptMatchesToRemove.ForEach(p => renderedTemplate = renderedTemplate.Remove(p.Index, p.Length)); #endregion #region Styles var styleMatches = StyleDetection.Matches(renderedTemplate); var styleMatchesToRemove = new List <Match>(); foreach (Match match in styleMatches) { var optMatch = OptimizeDetection.Match(match.Value); if (!optMatch.Success) { continue; } // skip If the Rel attribute is not stylesheet if (!StyleRelDetect.IsMatch(match.Value)) { continue; } var providerName = GetProviderName(optMatch, "head"); var prio = GetPriority(optMatch, (int)FileOrder.Css.DefaultPriority); if (prio <= 0) { continue; // don't register/remove if not within specs } // Register, then remember to remove later on ClientResourceManager.RegisterStyleSheet(page, match.Groups["Src"].Value, prio, providerName); styleMatchesToRemove.Add(match); } styleMatchesToRemove.Reverse(); styleMatchesToRemove.ForEach(p => renderedTemplate = renderedTemplate.Remove(p.Index, p.Length)); #endregion #endregion return(renderedTemplate); }