public RequireSettings Combine(RequireSettings other) { var settings = (new RequireSettings { Name = Name, Type = Type }).AtLocation(Location).AtLocation(other.Location) .WithBasePath(BasePath).WithBasePath(other.BasePath) .UseCdn(CdnMode).UseCdn(other.CdnMode) .UseDebugMode(DebugMode).UseDebugMode(other.DebugMode) .UseCulture(Culture).UseCulture(other.Culture) .UseCondition(Condition).UseCondition(other.Condition) .UseVersion(Version).UseVersion(other.Version) .Define(InlineDefinition).Define(other.InlineDefinition); settings._attributes = MergeAttributes(other); return(settings); }
public void RenderStylesheet(IHtmlContentBuilder builder, RequireSettings settings) { var first = true; var styleSheets = this.GetRequiredResources("stylesheet"); foreach (var context in styleSheets) { if (!first) { builder.AppendHtml(Environment.NewLine); } first = false; builder.AppendHtml(context.GetHtmlContent(settings, "/")); } }
private Dictionary <string, string> MergeAttributes(RequireSettings other) { // efficiently merge the two dictionaries, taking into account that one or both may not exist // and that attributes in 'other' should overridde attributes in this, even if the value is null. if (_attributes == null) { return(other._attributes == null ? null : new Dictionary <string, string>(other._attributes)); } if (other._attributes == null) { return(new Dictionary <string, string>(_attributes)); } var mergedAttributes = new Dictionary <string, string>(_attributes); foreach (var pair in other._attributes) { mergedAttributes[pair.Key] = pair.Value; } return(mergedAttributes); }
public IHtmlContent GetHtmlContent(RequireSettings baseSettings, string appPath) { var combinedSettings = baseSettings == null ? Settings : baseSettings.Combine(Settings); var tagBuilder = Resource.GetTagBuilder(combinedSettings, appPath); if (String.IsNullOrEmpty(combinedSettings.Condition)) { return(tagBuilder); } var builder = new HtmlContentBuilder(); if (combinedSettings.Condition == NotIE) { builder.AppendHtml("<!--[if " + combinedSettings.Condition + "]>-->"); } else { builder.AppendHtml("<!--[if " + combinedSettings.Condition + "]>"); } builder.AppendHtml(tagBuilder); if (!string.IsNullOrEmpty(combinedSettings.Condition)) { if (combinedSettings.Condition == NotIE) { builder.AppendHtml("<!--<![endif]-->"); } else { builder.AppendHtml("<![endif]-->"); } } return(builder); }
private ResourceDefinition FindResource(RequireSettings settings, bool resolveInlineDefinitions) { // find the resource with the given type and name // that has at least the given version number. If multiple, // return the resource with the greatest version number. // If not found and an inlineDefinition is given, define the resource on the fly // using the action. var name = settings.Name ?? ""; var type = settings.Type; ResourceDefinition resource; var resources = (from p in ResourceManifests from r in p.GetResources(type) where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase) select r.Value).SelectMany(x => x); if (!String.IsNullOrEmpty(settings.Version)) { // Specific version, filter var upper = GetUpperBoundVersion(settings.Version); var lower = GetLowerBoundVersion(settings.Version); resources = from r in resources let version = r.Version != null ? new Version(r.Version) : null where lower <= version && version < upper select r; } // Use the highest version of all matches resource = (from r in resources let version = r.Version != null ? new Version(r.Version) : null orderby version descending select r).FirstOrDefault(); if (resource == null && _dynamicManifest != null) { resources = (from r in _dynamicManifest.GetResources(type) where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase) select r.Value).SelectMany(x => x); if (!String.IsNullOrEmpty(settings.Version)) { // Specific version, filter var upper = GetUpperBoundVersion(settings.Version); var lower = GetLowerBoundVersion(settings.Version); resources = from r in resources let version = r.Version != null ? new Version(r.Version) : null where lower <= version && version < upper select r; } // Use the highest version of all matches resource = (from r in resources let version = r.Version != null ? new Version(r.Version) : null orderby version descending select r).FirstOrDefault(); } if (resolveInlineDefinitions && resource == null) { // Does not seem to exist, but it's possible it is being // defined by a Define() from a RequireSettings somewhere. if (ResolveInlineDefinitions(settings.Type)) { // if any were defined, now try to find it resource = FindResource(settings, false); } } return(resource); }
public ResourceDefinition FindResource(RequireSettings settings) { return(FindResource(settings, true)); }
public TagBuilder GetTagBuilder(RequireSettings settings, string applicationPath) { string url; // Url priority: if (settings.DebugMode) { url = settings.CdnMode ? Coalesce(UrlCdnDebug, UrlDebug, UrlCdn, Url) : Coalesce(UrlDebug, Url, UrlCdnDebug, UrlCdn); } else { url = settings.CdnMode ? Coalesce(UrlCdn, Url, UrlCdnDebug, UrlDebug) : Coalesce(Url, UrlDebug, UrlCdn, UrlCdnDebug); } if (String.IsNullOrEmpty(url)) { url = null; } if (!String.IsNullOrEmpty(settings.Culture)) { string nearestCulture = FindNearestCulture(settings.Culture); if (!String.IsNullOrEmpty(nearestCulture)) { url = Path.ChangeExtension(url, nearestCulture + Path.GetExtension(url)); } } if (url.StartsWith("~/", StringComparison.Ordinal)) { // For tilde slash paths, drop the leading ~ to make it work with the underlying IFileProvider. url = url.Substring(1); } var tagBuilder = new TagBuilder(TagName); if (!String.IsNullOrEmpty(CdnIntegrity) && url != null && url == UrlCdn) { tagBuilder.Attributes["integrity"] = CdnIntegrity; tagBuilder.Attributes["crossorigin"] = "anonymous"; } else if (!String.IsNullOrEmpty(CdnDebugIntegrity) && url != null && url == UrlCdnDebug) { tagBuilder.Attributes["integrity"] = CdnDebugIntegrity; tagBuilder.Attributes["crossorigin"] = "anonymous"; } if (_resourceAttributes.ContainsKey(Type)) { tagBuilder.MergeAttributes(_resourceAttributes[Type]); } if (Attributes != null) { tagBuilder.MergeAttributes(Attributes); } if (settings.HasAttributes) { tagBuilder.MergeAttributes(settings.Attributes); } if (!String.IsNullOrEmpty(FilePathAttributeName)) { if (!String.IsNullOrEmpty(url)) { tagBuilder.MergeAttribute(FilePathAttributeName, url, true); } } return(tagBuilder); }