private string BundleHtml() { var result = new StringBuilder(); var hash = ""; if (Scripts.Any()) { hash = GetHash(Scripts); } else if (Styles.Any()) { hash = GetHash(Styles); } var path = string.Format("~{0}{1}-{2}", BundleHelper.BUNDLE_VPATH, GetCategory(CategoryName), hash); var pathcss = path + ".css"; var pathjs = path + ".js"; var bundlecss = BundleHelper.GetCssBundle(pathcss); var bundlejs = BundleHelper.GetJsBundle(pathjs); if (bundlecss == null && bundlejs == null) { if (Styles.Any()) { bundlecss = BundleHelper.CssBundle(pathcss); foreach (var style in Styles) { bundlecss.Include(style); } BundleHelper.AddBundle(bundlecss); } if (Scripts.Any()) { bundlejs = BundleHelper.JsBundle(pathjs); foreach (var script in Scripts) { bundlejs.Include(script, true); } BundleHelper.AddBundle(bundlejs); } } if (bundlecss != null) { result.AppendLine(BundleHelper.HtmlLink(pathcss)); } if (bundlejs != null) { result.AppendLine(BundleHelper.HtmlScript(pathjs)); } return(result.ToString()); }
private string BundleHtml(string category, string html) { var result = new StringBuilder(); var hash = HttpServerUtility.UrlTokenEncode(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(html))); var path = string.Format("~{0}{1}-{2}", BundleHelper.BUNDLE_VPATH, GetCategory(category), hash); var pathcss = path + ".css"; var pathjs = path + ".js"; var bundlecss = BundleHelper.GetCssBundle(pathcss); var bundlejs = BundleHelper.GetJsBundle(pathjs); if (bundlecss == null && bundlejs == null) { var document = new HtmlDocument(); document.LoadHtml(html); if (bundlecss == null) { var styles = document.DocumentNode.SelectNodes("/style | /link[@rel='stylesheet'] | /link[@rel='stylesheet/less']"); if (styles != null && 0 < styles.Count) { bundlecss = BundleHelper.CssBundle(pathcss); foreach (var style in styles) { if (style.Name == "style" && !string.IsNullOrEmpty(style.InnerHtml)) { throw new NotSupportedException("Embedded styles not supported."); } bundlecss.Include(style.Attributes["href"].Value); } BundleHelper.AddBundle(bundlecss); } } if (bundlejs == null) { var scripts = document.DocumentNode.SelectNodes("/script"); if (scripts != null && 0 < scripts.Count) { bundlejs = BundleHelper.JsBundle(pathjs); foreach (var script in scripts) { if (script.Attributes["src"] == null && !string.IsNullOrEmpty(script.InnerHtml)) { throw new NotSupportedException("Embedded scripts not supported."); } bundlejs.Include(script.Attributes["src"].Value, script.Attributes["notobfuscate"] == null); } BundleHelper.AddBundle(bundlejs); } } } if (bundlecss != null) { result.AppendLine(BundleHelper.HtmlLink(pathcss)); } if (bundlejs != null) { result.AppendLine(BundleHelper.HtmlScript(pathjs)); } return(result.ToString()); }