Esempio n. 1
0
        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 JavaScript control to render the reCaptcha HTML control.
        /// </summary>
        /// <param name="htmlHelper"><c>HtmlHelper</c> instance.</param>
        /// <param name="src">JavaScript reference source.</param>
        /// <param name="options"><c>JsRenderingOptions</c> enum value.</param>
        /// <param name="parameters"><c>ResourceParameters</c> instance.</param>
        /// <returns>Returns the JavaScript control to render the reCaptcha HTML control.</returns>
        public static MvcHtmlString ReCaptchaApiJs(this HtmlHelper htmlHelper, string src, ApiJsRenderingOptions options, ResourceParameters parameters)
        {
            if (String.IsNullOrWhiteSpace(src))
            {
                throw new ArgumentNullException("src");
            }

            var htmlAttributes = new Dictionary <string, object>()
            {
                { "src", src },
            };

            return(htmlHelper.ReCaptchaApiJs(htmlAttributes, options, parameters));
        }
        /// <summary>
        /// Renders the JavaScript control to render the reCaptcha HTML control.
        /// </summary>
        /// <param name="htmlHelper"><c>HtmlHelper</c> instance.</param>
        /// <param name="src">JavaScript reference source.</param>
        /// <param name="options"><c>JsRenderingOptions</c> enum value.</param>
        /// <returns>Returns the JavaScript control to render the reCaptcha HTML control.</returns>
        public static MvcHtmlString ReCaptchaApiJs(this HtmlHelper htmlHelper, string src, ApiJsRenderingOptions options)
        {
            if (String.IsNullOrWhiteSpace(src))
            {
                throw new ArgumentNullException("src");
            }

            return(htmlHelper.ReCaptchaApiJs(src, options, null));
        }
        /// <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));
        }
        /// <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>
        /// <returns>Returns the reCaptcha HTML control.</returns>
        public static MvcHtmlString ReCaptchaApiJs(this HtmlHelper htmlHelper, IDictionary <string, object> htmlAttributes, ApiJsRenderingOptions options)
        {
            if (htmlAttributes == null)
            {
                throw new ArgumentNullException("htmlAttributes");
            }

            return(htmlHelper.ReCaptchaApiJs(htmlAttributes, options, null));
        }
        /// <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>
        /// <returns>Returns the reCaptcha HTML control.</returns>
        public static MvcHtmlString ReCaptchaApiJs(this HtmlHelper htmlHelper, object htmlAttributes, ApiJsRenderingOptions options)
        {
            if (htmlAttributes == null)
            {
                throw new ArgumentNullException("htmlAttributes");
            }

            return(htmlHelper.ReCaptchaApiJs(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), options, null));
        }