An IHtmlContentBuilder implementation using an in memory list.
Inheritance: IHtmlContentBuilder
Ejemplo n.º 1
0
        public IHtmlContent Zone(dynamic Display, dynamic Shape)
        {
            var htmlContents = new List<IHtmlContent>();
            foreach (var item in Shape)
            {
                htmlContents.Add(Display(item));
            }

            var htmlContentBuilder = new HtmlContentBuilder();
            foreach (var htmlContent in htmlContents)
            {
                htmlContentBuilder.AppendHtml(htmlContent);
            }

            return htmlContentBuilder;
        }
Ejemplo n.º 2
0
        public IHtmlContent ContentZone(dynamic Display, dynamic Shape)
        {
            var htmlContents = new List<IHtmlContent>();

            var shapes = ((IEnumerable<dynamic>)Shape);
            var tabbed = shapes.GroupBy(x => (string)x.Metadata.Tab).ToList();

            if (tabbed.Count > 1)
            {
                foreach (var tab in tabbed)
                {
                    var tabName = String.IsNullOrWhiteSpace(tab.Key) ? "Content" : tab.Key;
                    var tabBuilder = new TagBuilder("div");
                    tabBuilder.Attributes["id"] = "tab-" + tabName.HtmlClassify();
                    tabBuilder.Attributes["data-tab"] = tabName;
                    foreach (var item in tab)
                    {
                        tabBuilder.InnerHtml.AppendHtml(Display(item));
                    }
                    htmlContents.Add(tabBuilder);
                }
            }
            else if (tabbed.Count > 0)
            {
                foreach (var item in tabbed[0])
                {
                    htmlContents.Add(Display(item));
                }
            }

            var htmlContentBuilder = new HtmlContentBuilder();
            foreach (var htmlContent in htmlContents)
            {
                htmlContentBuilder.AppendHtml(htmlContent);
            }

            return htmlContentBuilder;
        }
Ejemplo n.º 3
0
        public IHtmlContent GetHtmlContent(RequireSettings baseSettings, string appPath)
        {
            var combinedSettings = baseSettings == null ? Settings : baseSettings.Combine(Settings);

            var tagBuilder = Resource.GetTagBuilder(combinedSettings, appPath);

            if (String.IsNullOrEmpty(combinedSettings.Condition))
            {
                return tagBuilder;
            }

            var builder = new HtmlContentBuilder();

            if (combinedSettings.Condition == NotIE)
            {
                builder.AppendHtml("<!--[if " + combinedSettings.Condition + "]>-->");
            }
            else
            {
                builder.AppendHtml("<!--[if " + combinedSettings.Condition + "]>");
            }

            builder.AppendHtml(tagBuilder);

            if (!string.IsNullOrEmpty(combinedSettings.Condition))
            {
                if (combinedSettings.Condition == NotIE)
                {
                    builder.AppendHtml("<!--<![endif]-->");
                }
                else
                {
                    builder.AppendHtml("<![endif]-->");
                }
            }

            return builder;
        }
Ejemplo n.º 4
0
        public void MoveTo_FlattensAndClears()
        {
            // Arrange
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);

            var nestedItems = new List<object>();
            var nested = new HtmlContentBuilder(nestedItems);
            nested.AppendHtml("Hello");
            buffer.AppendHtml(nested);

            var destinationItems = new List<object>();
            var destination = new HtmlContentBuilder(destinationItems);

            // Act
            buffer.MoveTo(destination);

            // Assert
            Assert.Empty(nestedItems);
            Assert.Empty(buffer.Pages);
            Assert.Equal("Hello", Assert.IsType<HtmlString>(destinationItems[0]).Value);
        }
Ejemplo n.º 5
0
        public void MoveTo_DoesDeepMove()
        {
            // Arrange
            var source = new DefaultTagHelperContent();

            var nested = new DefaultTagHelperContent();
            source.AppendHtml(nested);
            nested.AppendHtml(new HtmlEncodedString("hello"));
            source.Append("Test");

            var items = new List<object>();
            var destination = new HtmlContentBuilder(items);
            destination.Append("some-content");

            // Act
            source.MoveTo(destination);

            // Assert
            Assert.Equal(string.Empty, source.GetContent());
            Assert.Equal(string.Empty, nested.GetContent());
            Assert.Equal(3, items.Count);

            Assert.Equal("some-content", Assert.IsType<string>(items[0]));
            Assert.Equal("hello", Assert.IsType<HtmlEncodedString>(items[1]).Value);
            Assert.Equal("Test", Assert.IsType<string>(items[2]));
        }
Ejemplo n.º 6
0
        public void CopyTo_CopiesAllItems()
        {
            // Arrange
            var source = new DefaultTagHelperContent();
            source.AppendHtml(new HtmlEncodedString("hello"));
            source.Append("Test");

            var items = new List<object>();
            var destination = new HtmlContentBuilder(items);
            destination.Append("some-content");

            // Act
            source.CopyTo(destination);

            // Assert
            Assert.Equal(3, items.Count);

            Assert.Equal("some-content", Assert.IsType<string>(items[0]));
            Assert.Equal("hello", Assert.IsType<HtmlEncodedString>(items[1]).Value);
            Assert.Equal("Test", Assert.IsType<string>(items[2]));
        }
        public void HtmlFormattableString_WithOtherIHtmlContent()
        {
            // Arrange
            var builder = new HtmlContentBuilder();
            builder.Append("First");

            var formattableString = new HtmlFormattableString("{0}!", builder);

            // Act
            var result = HtmlContentToString(formattableString);

            // Assert
            Assert.Equal("HtmlEncode[[First]]!", result);
        }
Ejemplo n.º 8
0
        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 HtmlContentBuilder();
                var viewEngine = serviceProvider.GetRequiredService<ICompositeViewEngine>();
                var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();

                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,
                        viewBufferScope,
                        htmlHelper.ViewContext,
                        htmlHelper.ViewData,
                        modelExplorer,
                        htmlFieldName: fieldName,
                        templateName: null,
                        readOnly: false,
                        additionalViewData: null);
                    result.AppendHtml(templateBuilder.Build());
                }

                return result;
            }
            finally
            {
                viewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
            }
        }
Ejemplo n.º 9
0
        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 viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();

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

                var templateBuilder = new TemplateBuilder(
                    viewEngine,
                    viewBufferScope,
                    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.SetHtmlContent(label);
                        content.AppendLine(labelTag);
                    }

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

                    valueDivTag.InnerHtml.AppendHtml(templateBuilderResult);
                    valueDivTag.InnerHtml.AppendHtml(" ");
                    valueDivTag.InnerHtml.AppendHtml(htmlHelper.ValidationMessage(
                        propertyMetadata.PropertyName,
                        message: null,
                        htmlAttributes: null,
                        tag: null));

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

            return content;
        }
Ejemplo n.º 10
0
        public void MoveTo_MovesToBuilder(TagHelperOutput output, string expected)
        {
            // Arrange
            var writer = new StringWriter();
            var testEncoder = new HtmlTestEncoder();

            var buffer = new HtmlContentBuilder();

            var tagHelperExecutionContext = new TagHelperExecutionContext(
                tagName: output.TagName,
                tagMode: output.TagMode,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());
            tagHelperExecutionContext.Output = output;

            // Act
            ((IHtmlContentContainer)output).MoveTo(buffer);

            // Assert
            buffer.WriteTo(writer, testEncoder);

            Assert.Equal(string.Empty, output.PreElement.GetContent());
            Assert.Equal(string.Empty, output.PreContent.GetContent());
            Assert.Equal(string.Empty, output.Content.GetContent());
            Assert.Equal(string.Empty, output.PostContent.GetContent());
            Assert.Equal(string.Empty, output.PostElement.GetContent());
            Assert.Empty(output.Attributes);

            Assert.Equal(expected, writer.ToString(), StringComparer.Ordinal);
        }
Ejemplo n.º 11
-1
        public IHtmlContent GenerateTitle()
        {
            if (_title != null)
            {
                return _title;
            }

            if (_titleSeparator == null)
            {
                _titleSeparator = new HtmlString(" - ");
            }

            _titleParts.Sort(FlatPositionComparer.Instance);

            var htmlContentBuilder = new HtmlContentBuilder();

            if (_titleParts.Count == 0)
            {
                return HtmlString.Empty;
            }

            for (var i = 0; i < _titleParts.Count; i++)
            {
                htmlContentBuilder.AppendHtml(_titleParts[i].Value);

                if (i < _titleParts.Count - 1)
                {
                    htmlContentBuilder.AppendHtml(_titleSeparator);
                }
            }

            _title = htmlContentBuilder;

            return _title;
        }
Ejemplo n.º 12
-1
        public IHtmlContent GetTag()
        {
            if (!string.IsNullOrEmpty(Condition))
            {
                var htmlBuilder = new HtmlContentBuilder();
                htmlBuilder.AppendHtml("<!--[if " + Condition + "]>");
                htmlBuilder.AppendHtml(_builder);
                htmlBuilder.AppendHtml("<![endif]-->");

                return htmlBuilder;
            }

            return _builder;
        }
Ejemplo n.º 13
-10
        public async Task<IHtmlContent> DisplayAsync(IEnumerable<object> shapes)
        {
            var htmlContents = new List<IHtmlContent>();
            var htmlContentBuilder = new HtmlContentBuilder();

            foreach (var shape in shapes)
            {
                htmlContentBuilder.AppendHtml(await DisplayAsync(shape));
            }

            return htmlContentBuilder;
        }
Ejemplo n.º 14
-11
        public static IHtmlContent HiddenInputTemplate(IHtmlHelper htmlHelper)
        {
            var viewData = htmlHelper.ViewData;
            var model = viewData.Model;

            var result = new HtmlContentBuilder();
            if (!viewData.ModelMetadata.HideSurroundingHtml)
            {
                result.AppendHtml(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.AppendHtml(hiddenResult);

            return result;
        }
Ejemplo n.º 15
-21
        public async Task<object> InvokeAsync(string name, INamedEnumerable<object> parameters)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return await ShapeTypeExecuteAsync(name, parameters);
            }

            if (parameters.Positional.Count() == 1)
            {
                return await ShapeExecuteAsync(parameters.Positional.First());
            }

            if (parameters.Positional.Any())
            {
                var htmlContents = await ShapeExecuteAsync(parameters.Positional);
                var htmlContentBuilder = new HtmlContentBuilder();
                foreach (var htmlContent in htmlContents)
                {
                    htmlContentBuilder.AppendHtml(htmlContent);
                }
            }

            // zero args - no display to execute
            return null;
        }