Ejemplo n.º 1
0
        protected virtual string GenerateValue(string expression, object value, string format, bool useViewData)
        {
            var fullName       = DefaultHtmlGenerator.GetFullHtmlFieldName(ViewContext, expression);
            var attemptedValue =
                (string)DefaultHtmlGenerator.GetModelStateValue(ViewContext, fullName, typeof(string));

            string resolvedValue;

            if (attemptedValue != null)
            {
                // case 1: if ModelState has a value then it's already formatted so ignore format string
                resolvedValue = attemptedValue;
            }
            else if (useViewData)
            {
                // case 2: format the value from ViewData
                resolvedValue = DefaultHtmlGenerator.EvalString(ViewContext, expression, format);
            }
            else
            {
                // case 3: format the explicit value from ModelMetadata
                resolvedValue = FormatValue(value, format);
            }

            return(resolvedValue);
        }
Ejemplo n.º 2
0
        protected virtual string GenerateId(string expression)
        {
            var fullName = DefaultHtmlGenerator.GetFullHtmlFieldName(ViewContext, expression: expression);
            var id       = TagBuilder.CreateSanitizedId(fullName, IdAttributeDotReplacement);

            return(id);
        }
Ejemplo n.º 3
0
        private static HtmlHelper <TModel> GetHtmlHelper <TModel>(
            ViewDataDictionary <TModel> viewData,
            IUrlHelper urlHelper,
            ICompositeViewEngine viewEngine,
            IModelMetadataProvider provider,
            Func <IHtmlHelper, IHtmlHelper> innerHelperWrapper,
            IHtmlGenerator htmlGenerator)
        {
            var httpContext   = new DefaultHttpContext();
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            var bindingContext = new ActionBindingContext()
            {
                ValidatorProvider = new DataAnnotationsModelValidatorProvider(),
            };

            var bindingContextAccessor = new MockScopedInstance <ActionBindingContext>();

            bindingContextAccessor.Value = bindingContext;

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider
            .Setup(s => s.GetService(typeof(ICompositeViewEngine)))
            .Returns(viewEngine);
            serviceProvider
            .Setup(s => s.GetService(typeof(IUrlHelper)))
            .Returns(urlHelper);
            serviceProvider
            .Setup(s => s.GetService(typeof(IViewComponentHelper)))
            .Returns(new Mock <IViewComponentHelper>().Object);

            httpContext.RequestServices = serviceProvider.Object;
            if (htmlGenerator == null)
            {
                htmlGenerator = new DefaultHtmlGenerator(
                    GetAntiForgeryInstance(),
                    bindingContextAccessor,
                    provider,
                    urlHelper);
            }

            // TemplateRenderer will Contextualize this transient service.
            var innerHelper = (IHtmlHelper) new HtmlHelper(htmlGenerator, viewEngine, provider);

            if (innerHelperWrapper != null)
            {
                innerHelper = innerHelperWrapper(innerHelper);
            }
            serviceProvider
            .Setup(s => s.GetService(typeof(IHtmlHelper)))
            .Returns(() => innerHelper);

            var htmlHelper  = new HtmlHelper <TModel>(htmlGenerator, viewEngine, provider);
            var viewContext = new ViewContext(actionContext, Mock.Of <IView>(), viewData, new StringWriter());

            htmlHelper.Contextualize(viewContext);

            return(htmlHelper);
        }
Ejemplo n.º 4
0
        protected virtual string GenerateValue(string name, object value, string format, bool useViewData)
        {
            var fullName       = DefaultHtmlGenerator.GetFullHtmlFieldName(ViewContext, name);
            var attemptedValue =
                (string)DefaultHtmlGenerator.GetModelStateValue(ViewContext, fullName, typeof(string));

            string resolvedValue;

            if (attemptedValue != null)
            {
                // case 1: if ModelState has a value then it's already formatted so ignore format string
                resolvedValue = attemptedValue;
            }
            else if (useViewData)
            {
                if (string.IsNullOrEmpty(name))
                {
                    // case 2(a): format the value from ModelMetadata for the current model
                    var metadata = ViewData.ModelMetadata;
                    resolvedValue = FormatValue(metadata.Model, format);
                }
                else
                {
                    // case 2(b): format the value from ViewData
                    resolvedValue = DefaultHtmlGenerator.EvalString(ViewContext, name, format);
                }
            }
            else
            {
                // case 3: format the explicit value from ModelMetadata
                resolvedValue = FormatValue(value, format);
            }

            return(resolvedValue);
        }
Ejemplo n.º 5
0
        private static IHtmlContent BooleanTemplateDropDownList(IHtmlHelper htmlHelper, bool?value)
        {
            var selectTag = new TagBuilder("select");

            selectTag.AddCssClass("list-box");
            selectTag.AddCssClass("tri-state");
            selectTag.Attributes["disabled"] = "disabled";

            var content = new BufferedHtmlContent();

            foreach (var item in TriStateValues(value))
            {
                content.Append(DefaultHtmlGenerator.GenerateOption(item, item.Text));
            }

            selectTag.InnerHtml = content;
            return(selectTag);
        }
Ejemplo n.º 6
0
        private static string BooleanTemplateDropDownList(IHtmlHelper html, bool?value)
        {
            var selectTag = new TagBuilder("select");

            selectTag.AddCssClass("list-box");
            selectTag.AddCssClass("tri-state");
            selectTag.Attributes["disabled"] = "disabled";

            var builder = new StringBuilder();

            builder.Append(selectTag.ToString(TagRenderMode.StartTag));

            foreach (var item in TriStateValues(value))
            {
                var encodedText = html.Encode(item.Text);
                var option      = DefaultHtmlGenerator.GenerateOption(item, encodedText);
                builder.Append(option);
            }

            builder.Append(selectTag.ToString(TagRenderMode.EndTag));
            return(builder.ToString());
        }
Ejemplo n.º 7
0
        protected virtual string GenerateName(string expression)
        {
            var fullName = DefaultHtmlGenerator.GetFullHtmlFieldName(ViewContext, expression);

            return(fullName);
        }
Ejemplo n.º 8
0
        private static HtmlHelper <TModel> GetHtmlHelper <TModel>(
            ViewDataDictionary <TModel> viewData,
            IUrlHelper urlHelper,
            ICompositeViewEngine viewEngine,
            IModelMetadataProvider provider,
            Func <IHtmlHelper, IHtmlHelper> innerHelperWrapper,
            IHtmlGenerator htmlGenerator,
            string idAttributeDotReplacement)
        {
            var httpContext   = new DefaultHttpContext();
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            var options = new MvcOptions();

            if (!string.IsNullOrEmpty(idAttributeDotReplacement))
            {
                options.HtmlHelperOptions.IdAttributeDotReplacement = idAttributeDotReplacement;
            }
            options.ClientModelValidatorProviders.Add(new DataAnnotationsClientModelValidatorProvider());
            var optionsAccessor = new Mock <IOptions <MvcOptions> >();

            optionsAccessor
            .SetupGet(o => o.Options)
            .Returns(options);

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider
            .Setup(s => s.GetService(typeof(ICompositeViewEngine)))
            .Returns(viewEngine);
            serviceProvider
            .Setup(s => s.GetService(typeof(IUrlHelper)))
            .Returns(urlHelper);
            serviceProvider
            .Setup(s => s.GetService(typeof(IViewComponentHelper)))
            .Returns(new Mock <IViewComponentHelper>().Object);

            httpContext.RequestServices = serviceProvider.Object;
            if (htmlGenerator == null)
            {
                htmlGenerator = new DefaultHtmlGenerator(
                    GetAntiForgeryInstance(),
                    optionsAccessor.Object,
                    provider,
                    urlHelper,
                    new CommonTestEncoder());
            }

            // TemplateRenderer will Contextualize this transient service.
            var innerHelper = (IHtmlHelper) new HtmlHelper(
                htmlGenerator,
                viewEngine,
                provider,
                new CommonTestEncoder(),
                new UrlEncoder(),
                new JavaScriptStringEncoder());

            if (innerHelperWrapper != null)
            {
                innerHelper = innerHelperWrapper(innerHelper);
            }
            serviceProvider
            .Setup(s => s.GetService(typeof(IHtmlHelper)))
            .Returns(() => innerHelper);

            var htmlHelper = new HtmlHelper <TModel>(
                htmlGenerator,
                viewEngine,
                provider,
                new CommonTestEncoder(),
                new UrlEncoder(),
                new JavaScriptStringEncoder());

            var viewContext = new ViewContext(
                actionContext,
                Mock.Of <IView>(),
                viewData,
                null,
                new StringWriter(),
                options.HtmlHelperOptions);

            htmlHelper.Contextualize(viewContext);

            return(htmlHelper);
        }
        private static HtmlHelper <TModel> GetHtmlHelper <TModel>(
            ViewDataDictionary <TModel> viewData,
            IUrlHelper urlHelper,
            ICompositeViewEngine viewEngine,
            IModelMetadataProvider provider,
            Func <IHtmlHelper, IHtmlHelper> innerHelperWrapper,
            IHtmlGenerator htmlGenerator,
            string idAttributeDotReplacement)
        {
            var httpContext   = new DefaultHttpContext();
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            var options = new MvcViewOptions();

            if (!string.IsNullOrEmpty(idAttributeDotReplacement))
            {
                options.HtmlHelperOptions.IdAttributeDotReplacement = idAttributeDotReplacement;
            }
            var localizationOptionsAccesor = new Mock <IOptions <MvcDataAnnotationsLocalizationOptions> >();

            localizationOptionsAccesor.SetupGet(o => o.Value).Returns(new MvcDataAnnotationsLocalizationOptions());

            options.ClientModelValidatorProviders.Add(new DataAnnotationsClientModelValidatorProvider(
                                                          new ValidationAttributeAdapterProvider(),
                                                          localizationOptionsAccesor.Object,
                                                          stringLocalizerFactory: null));
            var optionsAccessor = new Mock <IOptions <MvcViewOptions> >();

            optionsAccessor
            .SetupGet(o => o.Value)
            .Returns(options);

            var urlHelperFactory = new Mock <IUrlHelperFactory>();

            urlHelperFactory
            .Setup(f => f.GetUrlHelper(It.IsAny <ActionContext>()))
            .Returns(urlHelper);

            if (htmlGenerator == null)
            {
                htmlGenerator = new DefaultHtmlGenerator(
                    Mock.Of <IAntiforgery>(),
                    optionsAccessor.Object,
                    provider,
                    urlHelperFactory.Object,
                    new HtmlTestEncoder());
            }

            // TemplateRenderer will Contextualize this transient service.
            var innerHelper = (IHtmlHelper) new HtmlHelper(
                htmlGenerator,
                viewEngine,
                provider,
                new TestViewBufferScope(),
                new HtmlTestEncoder(),
                UrlEncoder.Default,
                JavaScriptEncoder.Default);

            if (innerHelperWrapper != null)
            {
                innerHelper = innerHelperWrapper(innerHelper);
            }

            var serviceProvider = new ServiceCollection()
                                  .AddSingleton(viewEngine)
                                  .AddSingleton(urlHelperFactory.Object)
                                  .AddSingleton(Mock.Of <IViewComponentHelper>())
                                  .AddSingleton(innerHelper)
                                  .AddSingleton <IViewBufferScope, TestViewBufferScope>()
                                  .BuildServiceProvider();

            httpContext.RequestServices = serviceProvider;

            var htmlHelper = new HtmlHelper <TModel>(
                htmlGenerator,
                viewEngine,
                provider,
                new TestViewBufferScope(),
                new HtmlTestEncoder(),
                UrlEncoder.Default,
                JavaScriptEncoder.Default);

            var viewContext = new ViewContext(
                actionContext,
                Mock.Of <IView>(),
                viewData,
                new TempDataDictionary(
                    httpContext,
                    Mock.Of <ITempDataProvider>()),
                new StringWriter(),
                options.HtmlHelperOptions);

            htmlHelper.Contextualize(viewContext);

            return(htmlHelper);
        }
Ejemplo n.º 10
0
        private static HtmlHelper <TModel> GetHtmlHelper <TModel>(
            ViewDataDictionary <TModel> viewData,
            IUrlHelper urlHelper,
            ICompositeViewEngine viewEngine,
            IModelMetadataProvider provider,
            Func <IHtmlHelper, IHtmlHelper> innerHelperWrapper)
        {
            var httpContext = new Mock <HttpContext>();

            httpContext
            .Setup(o => o.Response)
            .Returns(Mock.Of <HttpResponse>());
            httpContext
            .Setup(o => o.Items)
            .Returns(new Dictionary <object, object>());

            var actionContext = new ActionContext(httpContext.Object,
                                                  new RouteData(),
                                                  new ActionDescriptor());

            var actionBindingContext = new ActionBindingContext(actionContext,
                                                                provider,
                                                                Mock.Of <IModelBinder>(),
                                                                Mock.Of <IValueProvider>(),
                                                                Mock.Of <IInputFormatterSelector>(),
                                                                new DataAnnotationsModelValidatorProvider());
            var actionBindingContextProvider = new Mock <IActionBindingContextProvider>();

            actionBindingContextProvider
            .Setup(c => c.GetActionBindingContextAsync(It.IsAny <ActionContext>()))
            .Returns(Task.FromResult(actionBindingContext));

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider
            .Setup(s => s.GetService(typeof(ICompositeViewEngine)))
            .Returns(viewEngine);
            serviceProvider
            .Setup(s => s.GetService(typeof(IUrlHelper)))
            .Returns(urlHelper);
            serviceProvider
            .Setup(s => s.GetService(typeof(IViewComponentHelper)))
            .Returns(new Mock <IViewComponentHelper>().Object);

            httpContext
            .Setup(o => o.RequestServices)
            .Returns(serviceProvider.Object);

            var htmlGenerator = new DefaultHtmlGenerator(
                actionBindingContextProvider.Object,
                GetAntiForgeryInstance(),
                provider,
                urlHelper);

            // TemplateRenderer will Contextualize this transient service.
            var innerHelper = (IHtmlHelper) new HtmlHelper(htmlGenerator, viewEngine, provider);

            if (innerHelperWrapper != null)
            {
                innerHelper = innerHelperWrapper(innerHelper);
            }
            serviceProvider
            .Setup(s => s.GetService(typeof(IHtmlHelper)))
            .Returns(() => innerHelper);

            var htmlHelper  = new HtmlHelper <TModel>(htmlGenerator, viewEngine, provider);
            var viewContext = new ViewContext(actionContext, Mock.Of <IView>(), viewData, new StringWriter());

            htmlHelper.Contextualize(viewContext);

            return(htmlHelper);
        }