public static IHtmlContent HiddenInputTemplate(IHtmlHelper htmlHelper)
        {
            var viewData = htmlHelper.ViewData;
            var model    = viewData.Model;

            var result = new BufferedHtmlContent();

            if (!viewData.ModelMetadata.HideSurroundingHtml)
            {
                result.Append(DefaultDisplayTemplates.StringTemplate(htmlHelper));
            }

            // Special-case opaque values and arbitrary binary data.
            var modelAsByteArray = model as byte[];

            if (modelAsByteArray != null)
            {
                model = Convert.ToBase64String(modelAsByteArray);
            }

            var htmlAttributesObject = viewData[HtmlAttributeKey];
            var hiddenResult         = htmlHelper.Hidden(expression: null, value: model, htmlAttributes: htmlAttributesObject);

            result.Append(hiddenResult);

            return(result);
        }
Example #2
0
        /// <summary>
        /// Creates a new HTML tag that has the specified tag name.
        /// </summary>
        /// <param name="tagName">An HTML tag name.</param>
        public TagBuilder(string tagName)
        {
            if (string.IsNullOrEmpty(tagName))
            {
                throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(tagName));
            }

            TagName   = tagName;
            InnerHtml = new BufferedHtmlContent();
        }
Example #3
0
        public void AppendString_AppendsAString()
        {
            // Arrange
            var content = new BufferedHtmlContent();

            // Act
            content.Append("Hello");

            // Assert
            var result = Assert.Single(content.Entries);
            Assert.IsType(typeof(string), result);
        }
Example #4
0
        public void AppendCharArray_AppendsAsString()
        {
            // Arrange
            var content = new BufferedHtmlContent();

            // Act
            content.Append(new char[] { 'h', 'e', 'l', 'l', 'o' }, 0, 5);

            // Assert
            var result = Assert.Single(content.Entries);
            Assert.IsType(typeof(string), result);
        }
Example #5
0
        public TagBuilder(string tagName)
        {
            if (string.IsNullOrEmpty(tagName))
            {
                throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(tagName));
            }

            TagName    = tagName;
            Attributes = new SortedDictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            InnerHtml = new BufferedHtmlContent();
        }
Example #6
0
        public void AppendCharArray_AppendsAsString()
        {
            // Arrange
            var content = new BufferedHtmlContent();

            // Act
            content.Append(new char[] { 'h', 'e', 'l', 'l', 'o' }, 0, 5);

            // Assert
            var result = Assert.Single(content.Entries);

            Assert.IsType(typeof(string), result);
        }
Example #7
0
        public void AppendString_AppendsAString()
        {
            // Arrange
            var content = new BufferedHtmlContent();

            // Act
            content.Append("Hello");

            // Assert
            var result = Assert.Single(content.Entries);

            Assert.IsType(typeof(string), result);
        }
Example #8
0
        public void Clear_DeletesAllItems()
        {
            // Arrange
            var content = new BufferedHtmlContent();
            content.Append(new TestHtmlContent("hello"));
            content.Append("Test");

            // Act
            content.Clear();

            // Assert
            Assert.Equal(0, content.Entries.Count);
        }
Example #9
0
        public void CanAppendMultipleItems()
        {
            // Arrange
            var content = new BufferedHtmlContent();

            // Act
            content.Append(new TestHtmlContent("hello"));
            content.Append("Test");

            // Assert
            Assert.Equal(2, content.Entries.Count);
            Assert.Equal("Written from TestHtmlContent: hello", content.Entries[0].ToString());
            Assert.Equal("Test", content.Entries[1]);
        }
Example #10
0
        public void Clear_DeletesAllItems()
        {
            // Arrange
            var content = new BufferedHtmlContent();

            content.Append(new TestHtmlContent("hello"));
            content.Append("Test");

            // Act
            content.Clear();

            // Assert
            Assert.Equal(0, content.Entries.Count);
        }
Example #11
0
        public void CanAppendMultipleItems()
        {
            // Arrange
            var content = new BufferedHtmlContent();

            // Act
            content.Append(new TestHtmlContent("hello"));
            content.Append("Test");

            // Assert
            Assert.Equal(2, content.Entries.Count);
            Assert.Equal("Written from TestHtmlContent: hello", content.Entries[0].ToString());
            Assert.Equal("Test", content.Entries[1]);
        }
Example #12
0
        public void AppendIHtmlContent_AppendsAsIs()
        {
            // Arrange
            var content = new BufferedHtmlContent();
            var writer = new StringWriter();

            // Act
            content.Append(new TestHtmlContent("Hello"));

            // Assert
            var result = Assert.Single(content.Entries);
            var testHtmlContent = Assert.IsType<TestHtmlContent>(result);
            testHtmlContent.WriteTo(writer, new CommonTestEncoder());
            Assert.Equal("Written from TestHtmlContent: Hello", writer.ToString());
        }
Example #13
0
        public void WriteTo_WritesAllItems()
        {
            // Arrange
            var content = new BufferedHtmlContent();
            var writer  = new StringWriter();

            content.Append(new TestHtmlContent("Hello"));
            content.Append("Test");

            // Act
            content.WriteTo(writer, new CommonTestEncoder());

            // Assert
            Assert.Equal(2, content.Entries.Count);
            Assert.Equal("Written from TestHtmlContent: HelloTest", writer.ToString());
        }
Example #14
0
        public void AppendIHtmlContent_AppendsAsIs()
        {
            // Arrange
            var content = new BufferedHtmlContent();
            var writer  = new StringWriter();

            // Act
            content.Append(new TestHtmlContent("Hello"));

            // Assert
            var result          = Assert.Single(content.Entries);
            var testHtmlContent = Assert.IsType <TestHtmlContent>(result);

            testHtmlContent.WriteTo(writer, new CommonTestEncoder());
            Assert.Equal("Written from TestHtmlContent: Hello", writer.ToString());
        }
        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;
        }
Example #16
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);
        }
        public static IHtmlContent CollectionTemplate(IHtmlHelper htmlHelper)
        {
            var viewData = htmlHelper.ViewData;
            var model    = viewData.Model;

            if (model == null)
            {
                return(HtmlString.Empty);
            }

            var collection = model as IEnumerable;

            if (collection == null)
            {
                // Only way we could reach here is if user passed templateName: "Collection" to an Editor() overload.
                throw new InvalidOperationException(Resources.FormatTemplates_TypeMustImplementIEnumerable(
                                                        "Collection", model.GetType().FullName, typeof(IEnumerable).FullName));
            }

            var elementMetadata = htmlHelper.ViewData.ModelMetadata.ElementMetadata;

            Debug.Assert(elementMetadata != null);
            var typeInCollectionIsNullableValueType = elementMetadata.IsNullableValueType;

            var serviceProvider  = htmlHelper.ViewContext.HttpContext.RequestServices;
            var metadataProvider = serviceProvider.GetRequiredService <IModelMetadataProvider>();

            // Use typeof(string) instead of typeof(object) for IEnumerable collections. Neither type is Nullable<T>.
            if (elementMetadata.ModelType == typeof(object))
            {
                elementMetadata = metadataProvider.GetMetadataForType(typeof(string));
            }

            var oldPrefix = viewData.TemplateInfo.HtmlFieldPrefix;

            try
            {
                viewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

                var fieldNameBase = oldPrefix;
                var result        = new BufferedHtmlContent();
                var viewEngine    = serviceProvider.GetRequiredService <ICompositeViewEngine>();

                var index = 0;
                foreach (var item in collection)
                {
                    var itemMetadata = elementMetadata;
                    if (item != null && !typeInCollectionIsNullableValueType)
                    {
                        itemMetadata = metadataProvider.GetMetadataForType(item.GetType());
                    }

                    var modelExplorer = new ModelExplorer(
                        metadataProvider,
                        container: htmlHelper.ViewData.ModelExplorer,
                        metadata: itemMetadata,
                        model: item);
                    var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", fieldNameBase, index++);

                    var templateBuilder = new TemplateBuilder(
                        viewEngine,
                        htmlHelper.ViewContext,
                        htmlHelper.ViewData,
                        modelExplorer,
                        htmlFieldName: fieldName,
                        templateName: null,
                        readOnly: false,
                        additionalViewData: null);
                    result.Append(templateBuilder.Build());
                }

                return(result);
            }
            finally
            {
                viewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
            }
        }
        public static IHtmlContent CollectionTemplate(IHtmlHelper htmlHelper)
        {
            var model = htmlHelper.ViewData.Model;
            if (model == null)
            {
                return HtmlString.Empty;
            }

            var collection = model as IEnumerable;
            if (collection == null)
            {
                // Only way we could reach here is if user passed templateName: "Collection" to a Display() overload.
                throw new InvalidOperationException(Resources.FormatTemplates_TypeMustImplementIEnumerable(
                    "Collection", model.GetType().FullName, typeof(IEnumerable).FullName));
            }

            var elementMetadata = htmlHelper.ViewData.ModelMetadata.ElementMetadata;
            Debug.Assert(elementMetadata != null);
            var typeInCollectionIsNullableValueType = elementMetadata.IsNullableValueType;

            var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices;
            var metadataProvider = serviceProvider.GetRequiredService<IModelMetadataProvider>();

            // Use typeof(string) instead of typeof(object) for IEnumerable collections. Neither type is Nullable<T>.
            if (elementMetadata.ModelType == typeof(object))
            {
                elementMetadata = metadataProvider.GetMetadataForType(typeof(string));
            }

            var oldPrefix = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix;
            try
            {
                htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

                var fieldNameBase = oldPrefix;
                var result = new BufferedHtmlContent();
                var viewEngine = serviceProvider.GetRequiredService<ICompositeViewEngine>();

                var index = 0;
                foreach (var item in collection)
                {
                    var itemMetadata = elementMetadata;
                    if (item != null && !typeInCollectionIsNullableValueType)
                    {
                        itemMetadata = metadataProvider.GetMetadataForType(item.GetType());
                    }

                    var modelExplorer = new ModelExplorer(
                        metadataProvider,
                        container: htmlHelper.ViewData.ModelExplorer,
                        metadata: itemMetadata,
                        model: item);
                    var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", fieldNameBase, index++);

                    var templateBuilder = new TemplateBuilder(
                        viewEngine,
                        htmlHelper.ViewContext,
                        htmlHelper.ViewData,
                        modelExplorer,
                        htmlFieldName: fieldName,
                        templateName: null,
                        readOnly: true,
                        additionalViewData: null);
                    result.Append(templateBuilder.Build());
                }

                return result;
            }
            finally
            {
                htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
            }
        }
        public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
        {
            var viewData = htmlHelper.ViewData;
            var templateInfo = viewData.TemplateInfo;
            var modelExplorer = viewData.ModelExplorer;

            if (modelExplorer.Model == null)
            {
                return new HtmlString(modelExplorer.Metadata.NullDisplayText);
            }

            if (templateInfo.TemplateDepth > 1)
            {
                var text = modelExplorer.GetSimpleDisplayText();
                if (modelExplorer.Metadata.HtmlEncode)
                {
                    text = htmlHelper.Encode(text);
                }

                return new HtmlString(text);
            }

            var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices;
            var viewEngine = serviceProvider.GetRequiredService<ICompositeViewEngine>();

            var content = new BufferedHtmlContent();
            foreach (var propertyExplorer in modelExplorer.Properties)
            {
                var propertyMetadata = propertyExplorer.Metadata;
                if (!ShouldShow(propertyExplorer, templateInfo))
                {
                    continue;
                }

                var templateBuilder = new TemplateBuilder(
                    viewEngine,
                    htmlHelper.ViewContext,
                    htmlHelper.ViewData,
                    propertyExplorer,
                    htmlFieldName: propertyMetadata.PropertyName,
                    templateName: null,
                    readOnly: true,
                    additionalViewData: null);

                var templateBuilderResult = templateBuilder.Build();
                if (!propertyMetadata.HideSurroundingHtml)
                {
                    var label = propertyMetadata.GetDisplayName();
                    if (!string.IsNullOrEmpty(label))
                    {
                        var labelTag = new TagBuilder("div");
                        labelTag.InnerHtml.SetContent(label);
                        labelTag.AddCssClass("display-label");
                        content.AppendLine(labelTag);
                    }

                    var valueDivTag = new TagBuilder("div");
                    valueDivTag.AddCssClass("display-field");
                    valueDivTag.InnerHtml.SetContent(templateBuilderResult);
                    content.AppendLine(valueDivTag);
                }
                else
                {
                    content.Append(templateBuilderResult);
                }
            }

            return content;
        }
 /// <summary>
 /// Creates a new instance of <see cref="StringCollectionTextWriter"/>.
 /// </summary>
 /// <param name="encoding">The character <see cref="Encoding"/> in which the output is written.</param>
 public StringCollectionTextWriter(Encoding encoding)
 {
     _encoding = encoding;
     Content   = new BufferedHtmlContent();
 }
Example #21
0
        public static IHtmlContent HiddenInputTemplate(IHtmlHelper htmlHelper)
        {
            var viewData = htmlHelper.ViewData;
            var model = viewData.Model;

            var result = new BufferedHtmlContent();
            if (!viewData.ModelMetadata.HideSurroundingHtml)
            {
                result.Append(DefaultDisplayTemplates.StringTemplate(htmlHelper));
            }

            // Special-case opaque values and arbitrary binary data.
            var modelAsByteArray = model as byte[];
            if (modelAsByteArray != null)
            {
                model = Convert.ToBase64String(modelAsByteArray);
            }

            var htmlAttributesObject = viewData[HtmlAttributeKey];
            var hiddenResult = htmlHelper.Hidden(expression: null, value: model, htmlAttributes: htmlAttributesObject);
            result.Append(hiddenResult);

            return result;
        }
Example #22
0
        public void WriteTo_WritesAllItems()
        {
            // Arrange
            var content = new BufferedHtmlContent();
            var writer = new StringWriter();
            content.Append(new TestHtmlContent("Hello"));
            content.Append("Test");

            // Act
            content.WriteTo(writer, new CommonTestEncoder());

            // Assert
            Assert.Equal(2, content.Entries.Count);
            Assert.Equal("Written from TestHtmlContent: HelloTest", writer.ToString());
        }
 /// <summary>
 /// Creates a new instance of <see cref="StringCollectionTextWriter"/>.
 /// </summary>
 /// <param name="encoding">The character <see cref="Encoding"/> in which the output is written.</param>
 public StringCollectionTextWriter(Encoding encoding)
 {
     _encoding = encoding;
     Content = new BufferedHtmlContent();
 }
        public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
        {
            var viewData      = htmlHelper.ViewData;
            var templateInfo  = viewData.TemplateInfo;
            var modelExplorer = viewData.ModelExplorer;

            if (templateInfo.TemplateDepth > 1)
            {
                if (modelExplorer.Model == null)
                {
                    return(new HtmlString(modelExplorer.Metadata.NullDisplayText));
                }

                var text = modelExplorer.GetSimpleDisplayText();
                if (modelExplorer.Metadata.HtmlEncode)
                {
                    return(new StringHtmlContent(text));
                }

                return(new HtmlString(text));
            }

            var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices;
            var viewEngine      = serviceProvider.GetRequiredService <ICompositeViewEngine>();

            var content = new BufferedHtmlContent();

            foreach (var propertyExplorer in modelExplorer.Properties)
            {
                var propertyMetadata = propertyExplorer.Metadata;
                if (!ShouldShow(propertyExplorer, templateInfo))
                {
                    continue;
                }

                var templateBuilder = new TemplateBuilder(
                    viewEngine,
                    htmlHelper.ViewContext,
                    htmlHelper.ViewData,
                    propertyExplorer,
                    htmlFieldName: propertyMetadata.PropertyName,
                    templateName: null,
                    readOnly: false,
                    additionalViewData: null);

                var templateBuilderResult = templateBuilder.Build();
                if (!propertyMetadata.HideSurroundingHtml)
                {
                    var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null);
                    if (!string.IsNullOrEmpty(label.ToString()))
                    {
                        var labelTag = new TagBuilder("div");
                        labelTag.AddCssClass("editor-label");
                        labelTag.InnerHtml = label;
                        content.AppendLine(labelTag);
                    }

                    var valueDivTag = new TagBuilder("div");
                    valueDivTag.AddCssClass("editor-field");

                    var innerContent = new BufferedHtmlContent();
                    innerContent.Append(templateBuilderResult);
                    innerContent.Append(" ");
                    innerContent.Append(htmlHelper.ValidationMessage(
                                            propertyMetadata.PropertyName,
                                            message: null,
                                            htmlAttributes: null,
                                            tag: null));
                    valueDivTag.InnerHtml = innerContent;

                    content.AppendLine(valueDivTag);
                }
                else
                {
                    content.Append(templateBuilderResult);
                }
            }

            return(content);
        }