Skip to content

Commit

Permalink
Added media parameter for css bundling in html link creation
Browse files Browse the repository at this point in the history
  • Loading branch information
phillip-haydon committed Aug 13, 2012
1 parent cda7ac6 commit 5391f7f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
51 changes: 26 additions & 25 deletions NuGet/content/Mvc.Bundler.cs
Expand Up @@ -252,38 +252,39 @@ public static MvcHtmlString RenderJsBundle(this HtmlHelper html, string bundlePa
}); });
} }


public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified) public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified, string media = null)
{ {
if (string.IsNullOrEmpty(bundlePath)) if (string.IsNullOrEmpty(bundlePath))
return MvcHtmlString.Empty; return MvcHtmlString.Empty;


if (!CachePaths()) BundleCache.SafeClear(); if (!CachePaths()) BundleCache.SafeClear();


return BundleCache.GetOrAdd(bundlePath, str => { return BundleCache.GetOrAdd(bundlePath, str =>
var filePath = HostingEnvironment.MapPath(bundlePath); {
var filePath = HostingEnvironment.MapPath(bundlePath);
var baseUrl = VirtualPathUtility.GetDirectory(bundlePath); var baseUrl = VirtualPathUtility.GetDirectory(bundlePath);
if (options == BundleOptions.Combined) if (options == BundleOptions.Combined)
return html.Css(bundlePath.Replace(".bundle", ""), null, options); return html.Css(bundlePath.Replace(".bundle", ""), media, options);
if (options == BundleOptions.MinifiedAndCombined) if (options == BundleOptions.MinifiedAndCombined)
return html.Css(bundlePath.Replace(".css.bundle", ".min.css"), null, options); return html.Css(bundlePath.Replace(".css.bundle", ".min.css"), media, options);
var cssFiles = File.ReadAllLines(filePath); var cssFiles = File.ReadAllLines(filePath);
var styles = new StringBuilder(); var styles = new StringBuilder();
foreach (var file in cssFiles) foreach (var file in cssFiles)
{ {
var cssFile = file.Trim().Replace(".less", ".css"); var cssFile = file.Trim().Replace(".less", ".css");
var cssSrc = Path.Combine(baseUrl, cssFile); var cssSrc = Path.Combine(baseUrl, cssFile);
styles.AppendLine( styles.AppendLine(
html.Css(cssSrc, null, options).ToString() html.Css(cssSrc, media, options).ToString()
); );
} }
return styles.ToString().ToMvcHtmlString(); return styles.ToString().ToMvcHtmlString();
}); });
} }
} }
} }
8 changes: 4 additions & 4 deletions tests/Bootstrap.Mvc/Mvc.Bundler.cs
Expand Up @@ -252,7 +252,7 @@ public static MvcHtmlString RenderJsBundle(this HtmlHelper html, string bundlePa
}); });
} }


public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified) public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified, string media = null)
{ {
if (string.IsNullOrEmpty(bundlePath)) if (string.IsNullOrEmpty(bundlePath))
return MvcHtmlString.Empty; return MvcHtmlString.Empty;
Expand All @@ -265,9 +265,9 @@ public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundleP
var baseUrl = VirtualPathUtility.GetDirectory(bundlePath); var baseUrl = VirtualPathUtility.GetDirectory(bundlePath);
if (options == BundleOptions.Combined) if (options == BundleOptions.Combined)
return html.Css(bundlePath.Replace(".bundle", ""), null, options); return html.Css(bundlePath.Replace(".bundle", ""), media, options);
if (options == BundleOptions.MinifiedAndCombined) if (options == BundleOptions.MinifiedAndCombined)
return html.Css(bundlePath.Replace(".css.bundle", ".min.css"), null, options); return html.Css(bundlePath.Replace(".css.bundle", ".min.css"), media, options);
var cssFiles = File.ReadAllLines(filePath); var cssFiles = File.ReadAllLines(filePath);
Expand All @@ -278,7 +278,7 @@ public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundleP
var cssSrc = Path.Combine(baseUrl, cssFile); var cssSrc = Path.Combine(baseUrl, cssFile);
styles.AppendLine( styles.AppendLine(
html.Css(cssSrc, null, options).ToString() html.Css(cssSrc, media, options).ToString()
); );
} }
Expand Down
3 changes: 3 additions & 0 deletions tests/Bootstrap.Mvc/Views/Shared/_Layout.cshtml
Expand Up @@ -10,6 +10,9 @@


<!-- Le styles --> <!-- Le styles -->
@Html.RenderCssBundle("~/Content/app.css.bundle", BundleOptions.MinifiedAndCombined) @Html.RenderCssBundle("~/Content/app.css.bundle", BundleOptions.MinifiedAndCombined)

<!-- Print StyleSheet (should include media="print" -->
@Html.RenderCssBundle("~/Content/app.css.bundle", BundleOptions.MinifiedAndCombined, "print")


<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --> <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]> <!--[if lt IE 9]>
Expand Down
Binary file modified tests/Bootstrap.Mvc/bin/Bootstrap.Mvc.dll
Binary file not shown.

0 comments on commit 5391f7f

Please sign in to comment.