public void GetReCaptchaApiJs_GivenSrcAndOptionsAndParameters_Should_ReturnReCaptchaApiJsHtml(string src, ApiJsRenderingOptions options, params string[] parameters) { WidgetRenderType resultRenderType; var resourceParameters = new ResourceParameters() { OnLoad = parameters[0], Render = Enum.TryParse(parameters[1], true, out resultRenderType) ? resultRenderType : WidgetRenderType.Unknown, }; var reCaptchaApiJs = this._htmlHelper.ReCaptchaApiJs(src, options, resourceParameters); if (options.HasFlag(ApiJsRenderingOptions.Async)) { reCaptchaApiJs.ToHtmlString().Should().Contain("async"); } if (options.HasFlag(ApiJsRenderingOptions.Defer)) { reCaptchaApiJs.ToHtmlString().Should().Contain("defer"); } reCaptchaApiJs.ToHtmlString().Should().MatchRegex("src=.+" + src); reCaptchaApiJs.ToHtmlString().Should().Contain("onload=" + parameters[0]); reCaptchaApiJs.ToHtmlString().Should().Contain("render=" + parameters[1]); }
/// <summary> /// Renders the reCaptcha HTML control. /// </summary> /// <param name="htmlHelper"><c>HtmlHelper</c> instance.</param> /// <param name="htmlAttributes">List of HTML attributes.</param> /// <param name="options"><c>JsRenderingOptions</c> enum value.</param> /// <param name="parameters"><c>ResourceParameters</c> instance.</param> /// <returns>Returns the reCaptcha HTML control.</returns> public static MvcHtmlString ReCaptchaApiJs(this HtmlHelper htmlHelper, IDictionary <string, object> htmlAttributes, ApiJsRenderingOptions options, ResourceParameters parameters) { if (htmlAttributes == null) { throw new ArgumentNullException("htmlAttributes"); } if (options.HasFlag(ApiJsRenderingOptions.Async)) { var async = Convert.ToString(ApiJsRenderingOptions.Async).ToLower(); htmlAttributes.Add(async, async); } if (options.HasFlag(ApiJsRenderingOptions.Defer)) { var defer = Convert.ToString(ApiJsRenderingOptions.Defer).ToLower(); htmlAttributes.Add(defer, defer); } var builder = new TagBuilder("script"); builder.MergeAttributes(htmlAttributes); string src; if (parameters == null || !builder.Attributes.TryGetValue("src", out src)) { return(builder.ToMvcHtmlString(TagRenderMode.Normal)); } src += src.Contains("?") ? (src.EndsWith("?") ? null : "&") : "?"; src += parameters.ToDictionary <ResourceParameters, string, object>().Flatten(); builder.Attributes["src"] = src; return(builder.ToMvcHtmlString(TagRenderMode.Normal)); }