Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

54 lines
2.0 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Web.Mvc;
using Microsoft.Web.Mvc.Properties;
namespace Microsoft.Web.Mvc
{
public static class ScriptExtensions
{
public static MvcHtmlString Script(this HtmlHelper helper, string releaseFile)
{
return Script(helper, releaseFile, releaseFile);
}
public static MvcHtmlString Script(this HtmlHelper helper, string releaseFile, string debugFile)
{
if (String.IsNullOrEmpty(releaseFile))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "releaseFile");
}
if (String.IsNullOrEmpty(debugFile))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "debugFile");
}
string src;
string file = helper.ViewContext.HttpContext.IsDebuggingEnabled ? debugFile : releaseFile;
if (IsRelativeToDefaultPath(file))
{
src = "~/Scripts/" + file;
}
else
{
src = file;
}
TagBuilder scriptTag = new TagBuilder("script");
scriptTag.MergeAttribute("type", "text/javascript");
scriptTag.MergeAttribute("src", UrlHelper.GenerateContentUrl(src, helper.ViewContext.HttpContext));
return MvcHtmlString.Create(scriptTag.ToString(TagRenderMode.Normal));
}
internal static bool IsRelativeToDefaultPath(string file)
{
return !(file.StartsWith("~", StringComparison.Ordinal) ||
file.StartsWith("../", StringComparison.Ordinal) ||
file.StartsWith("/", StringComparison.Ordinal) ||
file.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
file.StartsWith("https://", StringComparison.OrdinalIgnoreCase));
}
}
}