1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2 
3 using System;
4 using System.Collections.Generic;
5 using System.Diagnostics.CodeAnalysis;
6 using System.Web.Mvc;
7 using Microsoft.Web.Mvc.Properties;
8 
9 namespace Microsoft.Web.Mvc
10 {
11     public static class ImageExtensions
12     {
13         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
Image(this HtmlHelper helper, string imageRelativeUrl)14         public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl)
15         {
16             return Image(helper, imageRelativeUrl, null, null);
17         }
18 
19         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "Required for Extension Method")]
Image(this HtmlHelper helper, string imageRelativeUrl, string alt)20         public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt)
21         {
22             return Image(helper, imageRelativeUrl, alt, null);
23         }
24 
25         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes)26         public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes)
27         {
28             return Image(helper, imageRelativeUrl, alt, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
29         }
30 
31         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
Image(this HtmlHelper helper, string imageRelativeUrl, object htmlAttributes)32         public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, object htmlAttributes)
33         {
34             return Image(helper, imageRelativeUrl, null, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
35         }
36 
37         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
Image(this HtmlHelper helper, string imageRelativeUrl, IDictionary<string, object> htmlAttributes)38         public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, IDictionary<string, object> htmlAttributes)
39         {
40             return Image(helper, imageRelativeUrl, null, htmlAttributes);
41         }
42 
43         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes)44         public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes)
45         {
46             if (String.IsNullOrEmpty(imageRelativeUrl))
47             {
48                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl");
49             }
50 
51             string imageUrl = UrlHelper.GenerateContentUrl(imageRelativeUrl, helper.ViewContext.HttpContext);
52             return MvcHtmlString.Create(Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing));
53         }
54 
55         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "The value is a not a regular URL since it may contain ~/ ASP.NET-specific characters")]
Image(string imageUrl, string alt, IDictionary<string, object> htmlAttributes)56         public static TagBuilder Image(string imageUrl, string alt, IDictionary<string, object> htmlAttributes)
57         {
58             if (String.IsNullOrEmpty(imageUrl))
59             {
60                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageUrl");
61             }
62 
63             TagBuilder imageTag = new TagBuilder("img");
64 
65             if (!String.IsNullOrEmpty(imageUrl))
66             {
67                 imageTag.MergeAttribute("src", imageUrl);
68             }
69 
70             if (!String.IsNullOrEmpty(alt))
71             {
72                 imageTag.MergeAttribute("alt", alt);
73             }
74 
75             imageTag.MergeAttributes(htmlAttributes, true);
76 
77             if (imageTag.Attributes.ContainsKey("alt") && !imageTag.Attributes.ContainsKey("title"))
78             {
79                 imageTag.MergeAttribute("title", (imageTag.Attributes["alt"] ?? String.Empty));
80             }
81             return imageTag;
82         }
83     }
84 }
85