public string BuildUrl(SafeDictionary <string> attributes = null) { if (attributes == null) { attributes = new SafeDictionary <string>(); } Func <string, Func <string>, string> getValue = (key, func) => { var value = attributes[key] ?? func(); attributes.Remove(key); return(value); }; var url = Url; if (Type == LinkType.Anchor) { url = string.Empty; } UrlBuilder builder = new UrlBuilder(url); var query = getValue("query", () => Query); var anchor = getValue("anchor", () => Anchor); if (query.HasValue()) { builder.AddToQueryString(query); } return(UrlFormat.Formatted(builder.ToString(), anchor.IsNullOrEmpty() ? "" : "#" + anchor)); }
public void AddToQueryString_AddsCompleteString_ReturnsValueUrl(string url, string expected, string query) { //Arrange var builder = new UrlBuilder(url); //Act builder.AddToQueryString(query); //Assert Assert.AreEqual(expected, builder.ToString()); }
public void AddToQueryString_AddsCompleteString_ReturnsValueUrl(string url, string expected, string query) { //Arrange var builder = new UrlBuilder(url); //Act builder.AddToQueryString(query); //Assert Assert.AreEqual(expected, builder.ToString()); }
/// <summary> /// Renders HTML for an image /// </summary> /// <param name="image">The image to render</param> /// <param name="attributes">Additional parameters to add. Do not include alt or src</param> /// <param name="outputHeightWidth">Indicates if the height and width attributes should be output when rendering the image</param> /// <returns>An img HTML element</returns> public virtual string RenderImage( Image image, SafeDictionary <string> attributes, bool outputHeightWidth = false ) { if (image == null) { return(string.Empty); } if (attributes == null) { attributes = new SafeDictionary <string>(); } var origionalKeys = attributes.Keys.ToList(); //should there be some warning about these removals? AttributeCheck(attributes, ImageParameterKeys.CLASS, image.Class); if (!attributes.ContainsKey(ImageParameterKeys.ALT)) { attributes[ImageParameterKeys.ALT] = image.Alt; } AttributeCheck(attributes, ImageParameterKeys.BORDER, image.Border); if (image.HSpace > 0) { AttributeCheck(attributes, ImageParameterKeys.HSPACE, image.HSpace.ToString(CultureInfo.InvariantCulture)); } if (image.VSpace > 0) { AttributeCheck(attributes, ImageParameterKeys.VSPACE, image.VSpace.ToString(CultureInfo.InvariantCulture)); } if (image.Width > 0) { AttributeCheck(attributes, ImageParameterKeys.WIDTHHTML, image.Width.ToString(CultureInfo.InvariantCulture)); } if (image.Height > 0) { AttributeCheck(attributes, ImageParameterKeys.HEIGHTHTML, image.Height.ToString(CultureInfo.InvariantCulture)); } var urlParams = new SafeDictionary <string>(); var htmlParams = new SafeDictionary <string>(); /* * ME - This method is used to render images rather than going back to the fieldrender * because it stops another call having to be passed to Sitecore. */ if (image == null || image.Src.IsNullOrWhiteSpace()) { return(String.Empty); } if (attributes == null) { attributes = new SafeDictionary <string>(); } Action <string> remove = key => attributes.Remove(key); Action <string> url = key => { urlParams.Add(key, attributes[key]); remove(key); }; Action <string> html = key => { htmlParams.Add(key, attributes[key]); remove(key); }; Action <string> both = key => { htmlParams.Add(key, attributes[key]); urlParams.Add(key, attributes[key]); remove(key); }; var keys = attributes.Keys.ToList(); foreach (var key in keys) { //if we have not config we just add it to both if (SitecoreContext.Config == null) { both(key); } else { bool found = false; if (SitecoreContext.Config.ImageAttributes.Contains(key)) { html(key); found = true; } if (SitecoreContext.Config.ImageQueryString.Contains(key)) { url(key); found = true; } if (!found) { html(key); } } } //copy width and height across to url if (!urlParams.ContainsKey(ImageParameterKeys.WIDTH) && !urlParams.ContainsKey(ImageParameterKeys.HEIGHT)) { if (origionalKeys.Contains(ImageParameterKeys.WIDTHHTML)) { urlParams[ImageParameterKeys.WIDTH] = htmlParams[ImageParameterKeys.WIDTHHTML]; } if (origionalKeys.Contains(ImageParameterKeys.HEIGHTHTML)) { urlParams[ImageParameterKeys.HEIGHT] = htmlParams[ImageParameterKeys.HEIGHTHTML]; } } if (!urlParams.ContainsKey(ImageParameterKeys.LANGUAGE) && image.Language != null) { urlParams[ImageParameterKeys.LANGUAGE] = image.Language.Name; } //calculate size var finalSize = Utilities.ResizeImage( image.Width, image.Height, urlParams[ImageParameterKeys.SCALE].ToFlaot(), urlParams[ImageParameterKeys.WIDTH].ToInt(), urlParams[ImageParameterKeys.HEIGHT].ToInt(), urlParams[ImageParameterKeys.MAX_WIDTH].ToInt(), urlParams[ImageParameterKeys.MAX_HEIGHT].ToInt()); if (finalSize.Height > 0) { urlParams[ImageParameterKeys.HEIGHT] = finalSize.Height.ToString(); } if (finalSize.Width > 0) { urlParams[ImageParameterKeys.WIDTH] = finalSize.Width.ToString(); } Action <string, string> originalAttributeClean = (exists, missing) => { if (origionalKeys.Contains(exists) && !origionalKeys.Contains(missing)) { urlParams.Remove(missing); htmlParams.Remove(missing); } }; //we do some smart clean up originalAttributeClean(ImageParameterKeys.WIDTHHTML, ImageParameterKeys.HEIGHTHTML); originalAttributeClean(ImageParameterKeys.HEIGHTHTML, ImageParameterKeys.WIDTHHTML); if (!outputHeightWidth) { htmlParams.Remove(ImageParameterKeys.WIDTHHTML); htmlParams.Remove(ImageParameterKeys.HEIGHTHTML); } var builder = new UrlBuilder(image.Src); foreach (var key in urlParams.Keys) { builder.AddToQueryString(key, urlParams[key]); } string mediaUrl = builder.ToString(); #if (SC81 || SC80 || SC75 || SC82) mediaUrl = ProtectMediaUrl(mediaUrl); #endif mediaUrl = HttpUtility.HtmlEncode(mediaUrl); return(ImageTagFormat.Formatted(mediaUrl, Utilities.ConvertAttributes(htmlParams, QuotationMark), QuotationMark)); }
/// <summary> /// Renders HTML for an image /// </summary> /// <param name="image">The image to render</param> /// <param name="attributes">Additional parameters to add. Do not include alt or src</param> /// <param name="outputHeightWidth">Indicates if the height and width attributes should be output when rendering the image</param> /// <returns>An img HTML element</returns> public virtual string RenderImage( Fields.Image image, SafeDictionary <string> attributes, bool outputHeightWidth = false ) { if (image == null) { return(string.Empty); } if (attributes == null) { attributes = new SafeDictionary <string>(); } var origionalKeys = attributes.Keys.ToList(); //should there be some warning about these removals? AttributeCheck(attributes, ImageParameterKeys.CLASS, image.Class); AttributeCheck(attributes, ImageParameterKeys.ALT, image.Alt); AttributeCheck(attributes, ImageParameterKeys.BORDER, image.Border); if (image.HSpace > 0) { AttributeCheck(attributes, ImageParameterKeys.HSPACE, image.HSpace.ToString(CultureInfo.InvariantCulture)); } if (image.VSpace > 0) { AttributeCheck(attributes, ImageParameterKeys.VSPACE, image.VSpace.ToString(CultureInfo.InvariantCulture)); } if (image.Width > 0) { AttributeCheck(attributes, ImageParameterKeys.WIDTHHTML, image.Width.ToString(CultureInfo.InvariantCulture)); } if (image.Height > 0) { AttributeCheck(attributes, ImageParameterKeys.HEIGHTHTML, image.Height.ToString(CultureInfo.InvariantCulture)); } var urlParams = new SafeDictionary <string>(); var htmlParams = new SafeDictionary <string>(); /* * ME - This method is used to render images rather than going back to the fieldrender * because it stops another call having to be passed to Sitecore. */ if (image == null || image.Src.IsNullOrWhiteSpace()) { return(""); } if (attributes == null) { attributes = new SafeDictionary <string>(); } Action <string> remove = key => attributes.Remove(key); Action <string> url = key => { urlParams.Add(key, attributes[key]); remove(key); }; Action <string> html = key => { htmlParams.Add(key, attributes[key]); remove(key); }; Action <string> both = key => { htmlParams.Add(key, attributes[key]); urlParams.Add(key, attributes[key]); remove(key); }; var keys = attributes.Keys.ToList(); foreach (var key in keys) { switch (key) { case ImageParameterKeys.BORDER: case ImageParameterKeys.ALT: case ImageParameterKeys.HSPACE: case ImageParameterKeys.VSPACE: case ImageParameterKeys.CLASS: case ImageParameterKeys.WIDTHHTML: case ImageParameterKeys.HEIGHTHTML: html(key); break; case ImageParameterKeys.OUTPUT_METHOD: case ImageParameterKeys.ALLOW_STRETCH: case ImageParameterKeys.IGNORE_ASPECT_RATIO: case ImageParameterKeys.SCALE: case ImageParameterKeys.MAX_WIDTH: case ImageParameterKeys.MAX_HEIGHT: case ImageParameterKeys.THUMBNAIL: case ImageParameterKeys.BACKGROUND_COLOR: case ImageParameterKeys.DATABASE: case ImageParameterKeys.LANGUAGE: case ImageParameterKeys.VERSION: case ImageParameterKeys.DISABLE_MEDIA_CACHE: case ImageParameterKeys.WIDTH: case ImageParameterKeys.HEIGHT: url(key); break; default: html(key); break; } } //copy width and height across to url if (!urlParams.ContainsKey(ImageParameterKeys.WIDTH) && !urlParams.ContainsKey(ImageParameterKeys.HEIGHT)) { if (origionalKeys.Contains(ImageParameterKeys.WIDTHHTML)) { urlParams[ImageParameterKeys.WIDTH] = htmlParams[ImageParameterKeys.WIDTHHTML]; } if (origionalKeys.Contains(ImageParameterKeys.HEIGHTHTML)) { urlParams[ImageParameterKeys.HEIGHT] = htmlParams[ImageParameterKeys.HEIGHTHTML]; } } if (!urlParams.ContainsKey(ImageParameterKeys.LANGUAGE) && image.Language != null) { urlParams[ImageParameterKeys.LANGUAGE] = image.Language.Name; } //calculate size var finalSize = Utilities.ResizeImage( image.Width, image.Height, urlParams[ImageParameterKeys.SCALE].ToFlaot(), urlParams[ImageParameterKeys.WIDTH].ToInt(), urlParams[ImageParameterKeys.HEIGHT].ToInt(), urlParams[ImageParameterKeys.MAX_WIDTH].ToInt(), urlParams[ImageParameterKeys.MAX_HEIGHT].ToInt()); urlParams[ImageParameterKeys.HEIGHT] = finalSize.Height.ToString(); urlParams[ImageParameterKeys.WIDTH] = finalSize.Width.ToString(); Action <string, string> originalAttributeClean = (exists, missing) => { if (origionalKeys.Contains(exists) && !origionalKeys.Contains(missing)) { urlParams.Remove(missing); htmlParams.Remove(missing); } }; //we do some smart clean up originalAttributeClean(ImageParameterKeys.WIDTHHTML, ImageParameterKeys.HEIGHTHTML); originalAttributeClean(ImageParameterKeys.HEIGHTHTML, ImageParameterKeys.WIDTHHTML); if (!outputHeightWidth) { htmlParams.Remove(ImageParameterKeys.WIDTHHTML); htmlParams.Remove(ImageParameterKeys.HEIGHTHTML); } var builder = new UrlBuilder(image.Src); foreach (var key in urlParams.Keys) { builder.AddToQueryString(key, urlParams[key]); } string mediaUrl = builder.ToString(); #if (SC80 || SC75) mediaUrl = ProtectMediaUrl(mediaUrl); #endif return(ImageTagFormat.Formatted(mediaUrl, Utilities.ConvertAttributes(htmlParams, QuotationMark), QuotationMark)); }
public string BuildUrl(SafeDictionary<string> attributes = null) { if (attributes == null) { attributes = new SafeDictionary<string>(); } Func<string, Func<string>, string> getValue = (key, func) => { var value = attributes[key] ?? func(); attributes.Remove(key); return value; }; var url = Url; if (Type == LinkType.Anchor) { url = string.Empty; } UrlBuilder builder = new UrlBuilder(url); var query = getValue("query", () => Query); var anchor = getValue("anchor", () => Anchor); if (query.IsNotNullOrEmpty()) builder.AddToQueryString(query); return UrlFormat.Formatted(builder.ToString(), anchor.IsNullOrEmpty() ? "" : "#" + anchor); }