public void CopyHtmlAttribute_DoesNotOverrideAttributes()
        {
            // Arrange
            var attributeName = "hello";
            var tagHelperOutput = new TagHelperOutput(
                "p",
                attributes: new Dictionary<string, object>()
                {
                    { attributeName, "world2" }
                });
            var expectedAttribute = new KeyValuePair<string, object>(attributeName, "world2");
            var tagHelperContext = new TagHelperContext(
                allAttributes: new Dictionary<string, object>(StringComparer.Ordinal)
                {
                    { attributeName, "world" }
                },
                items: new Dictionary<object, object>(),
                uniqueId: "test",
                getChildContentAsync: () =>
                {
                    var tagHelperContent = new DefaultTagHelperContent();
                    tagHelperContent.Append("Something Else");
                    return Task.FromResult<TagHelperContent>(tagHelperContent);
                });

            // Act
            tagHelperOutput.CopyHtmlAttribute(attributeName, tagHelperContext);

            // Assert
            var attribute = Assert.Single(tagHelperOutput.Attributes);
            Assert.Equal(expectedAttribute, attribute);
        }
        public void CanAppendContent()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var expected = "HtmlEncode[[Hello World!]]";

            // Act
            tagHelperContent.Append("Hello World!");

            // Assert
            Assert.Equal(expected, tagHelperContent.GetContent(new CommonTestEncoder()));
        }
        public void CanAppendContent()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var expected = "Hello World!";

            // Act
            tagHelperContent.Append(expected);

            // Assert
            Assert.Equal(expected, tagHelperContent.GetContent());
        }
        public void IsEmpty_FalseAfterAppendTagHelper()
        {
            // Arrange
            var tagHelperContent       = new DefaultTagHelperContent();
            var copiedTagHelperContent = new DefaultTagHelperContent();

            copiedTagHelperContent.SetContent("Hello");

            // Act
            tagHelperContent.Append(copiedTagHelperContent);

            // Assert
            Assert.False(tagHelperContent.IsEmpty);
        }
        public void WriteTo_WritesToATextWriter()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var writer           = new StringWriter();

            tagHelperContent.SetContent("Hello ");
            tagHelperContent.Append("World");

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

            // Assert
            Assert.Equal("Hello World", writer.ToString());
        }
        public void GetEnumerator_EnumeratesThroughBuffer()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var expected         = new string[] { "Hello", "World" };

            tagHelperContent.SetContent(expected[0]);
            tagHelperContent.Append(expected[1]);
            var i = 0;

            // Act & Assert
            foreach (var val in tagHelperContent)
            {
                Assert.Equal(expected[i++], val);
            }
        }
 public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
 {
     if (ShowDismiss)
     {
         output.PreContent.AppendFormat(@"<button type='button' class='btn btn-default' data-dismiss='modal'>{0}</button>", DismissText);
     }
     var childContent = await context.GetChildContentAsync();
     var footerContent = new DefaultTagHelperContent();
     if (ShowDismiss)
     {
         footerContent.AppendFormat(@"<button type='button' class='btn btn-default' data-dismiss='modal'>{0}</button>", DismissText);
     }
     footerContent.Append(childContent);
     var modalContext = (ModalContext)context.Items[typeof(ModalTagHelper)];
     modalContext.Footer = footerContent;
     output.SuppressOutput();
 }
        public void Append_WithTagHelperContent_MultipleAppends()
        {
            // Arrange
            var tagHelperContent       = new DefaultTagHelperContent();
            var copiedTagHelperContent = new DefaultTagHelperContent();
            var text1    = "Hello";
            var text2    = " World!";
            var expected = text1 + text2;

            tagHelperContent.Append(text1);
            tagHelperContent.Append(text2);

            // Act
            copiedTagHelperContent.Append(tagHelperContent);

            // Assert
            Assert.Equal(expected, copiedTagHelperContent.GetContent());
        }
        public void Append_WithTagHelperContent_MultipleAppends()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var copiedTagHelperContent = new DefaultTagHelperContent();
            var text1 = "Hello";
            var text2 = " World!";
            var expected = text1 + text2;
            tagHelperContent.Append(text1);
            tagHelperContent.Append(text2);

            // Act
            copiedTagHelperContent.Append(tagHelperContent);

            // Assert
            Assert.Equal(expected, copiedTagHelperContent.GetContent());
            Assert.Equal(new[] { text1, text2 }, copiedTagHelperContent.ToArray());
        }
        public void GetEnumerator_EnumeratesThroughBuffer()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var expected = new string[] { "Hello", "World" };
            tagHelperContent.SetContent(expected[0]);
            tagHelperContent.Append(expected[1]);
            var i = 0;

            // Act & Assert
            foreach (var val in tagHelperContent)
            {
                Assert.Equal(expected[i++], val);
            }
        }
Ejemplo n.º 11
0
        private void BuildFallbackBlock(TagHelperAttributeList attributes, DefaultTagHelperContent builder)
        {
            EnsureGlobbingUrlBuilder();
            EnsureFileVersionProvider();

            var fallbackSrcs = GlobbingUrlBuilder.BuildUrlList(FallbackSrc, FallbackSrcInclude, FallbackSrcExclude);
            if (fallbackSrcs.Any())
            {
                // Build the <script> tag that checks the test method and if it fails, renders the extra script.
                builder.Append(Environment.NewLine)
                       .Append("<script>(")
                       .Append(FallbackTestExpression)
                       .Append("||document.write(\"");

                // May have no "src" attribute in the dictionary e.g. if Src and SrcInclude were not bound.
                if (!attributes.ContainsName(SrcAttributeName))
                {
                    // Need this entry to place each fallback source.
                    attributes.Add(new TagHelperAttribute(SrcAttributeName, value: null));
                }

                foreach (var src in fallbackSrcs)
                {
                    // Fallback "src" values come from bound attributes and globbing. Must always be non-null.
                    Debug.Assert(src != null);

                    builder.Append("<script");

                    foreach (var attribute in attributes)
                    {
                        if (!attribute.Name.Equals(SrcAttributeName, StringComparison.OrdinalIgnoreCase))
                        {
                            var encodedKey = JavaScriptEncoder.JavaScriptStringEncode(attribute.Name);
                            var attributeValue = attribute.Value.ToString();
                            var encodedValue = JavaScriptEncoder.JavaScriptStringEncode(attributeValue);

                            AppendAttribute(builder, encodedKey, encodedValue, escapeQuotes: true);
                        }
                        else
                        {
                            // Ignore attribute.Value; use src instead.
                            var attributeValue = src;
                            if (FileVersion == true)
                            {
                                attributeValue = _fileVersionProvider.AddFileVersionToPath(attributeValue);
                            }

                            // attribute.Key ("src") does not need to be JavaScript-encoded.
                            var encodedValue = JavaScriptEncoder.JavaScriptStringEncode(attributeValue);

                            AppendAttribute(builder, attribute.Name, encodedValue, escapeQuotes: true);
                        }
                    }

                    builder.Append("><\\/script>");
                }

                builder.Append("\"));</script>");
            }
        }
        public void IsEmpty_TrueAfterAppendEmptyTagHelperContent()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var copiedTagHelperContent = new DefaultTagHelperContent();

            // Act
            tagHelperContent.Append(copiedTagHelperContent);
            tagHelperContent.Append(string.Empty);

            // Assert
            Assert.True(tagHelperContent.IsEmpty);
        }
        public void IsEmpty_FalseAfterAppendTagHelper()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var copiedTagHelperContent = new DefaultTagHelperContent();
            copiedTagHelperContent.SetContent("Hello");

            // Act
            tagHelperContent.Append(copiedTagHelperContent);

            // Assert
            Assert.False(tagHelperContent.IsEmpty);
        }
        public void CanIdentifyWhiteSpace(string data)
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();

            // Act
            tagHelperContent.SetContent("  ");
            tagHelperContent.Append(data);

            // Assert
            Assert.True(tagHelperContent.IsWhiteSpace);
        }
        public void CanIdentifyWhiteSpace_WithoutIgnoringStrings()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();

            // Act
            tagHelperContent.SetContent("  ");
            tagHelperContent.Append("Hello");

            // Assert
            Assert.False(tagHelperContent.IsWhiteSpace);
        }
        public void IsModified_TrueAfterAppend()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();

            // Act
            tagHelperContent.Append(string.Empty);

            // Assert
            Assert.True(tagHelperContent.IsModified);
        }
        public void IsModified_TrueIfAppendedNull()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            TagHelperContent NullContent = null;

            // Act
            tagHelperContent.Append(NullContent);

            // Assert
            Assert.True(tagHelperContent.IsModified);
        }
Ejemplo n.º 18
0
        private static TagHelperContext MakeTagHelperContext(
            TagHelperAttributeList attributes = null,
            string content = null)
        {
            attributes = attributes ?? new TagHelperAttributeList();

            return new TagHelperContext(
                attributes,
                items: new Dictionary<object, object>(),
                uniqueId: Guid.NewGuid().ToString("N"),
                getChildContentAsync: useCachedResult =>
                {
                    var tagHelperContent = new DefaultTagHelperContent();
                    tagHelperContent.Append(content);
                    return Task.FromResult((TagHelperContent)tagHelperContent);
                });
        }
Ejemplo n.º 19
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var viewDictionary = modelContext.CurrentData;
            if (viewDictionary == null)
            {
                await base.ProcessAsync(context, output);
                return;
            }

            foreach (var attribute in BindAttributes)
            {
                if (
                    attribute.Key.StartsWith("bindattr-") ||
                    forbiddenForAny.Contains(attribute.Key) ||
                    (output.TagName.ToLower() == "view" && forbiddenForView.Contains(attribute.Key))
                )
                {
                    continue;
                }

                var value = modelContext.Value(attribute.Value);
                output.Attributes.Add($"bindattr-{attribute.Key}", attribute.Value);

                if (true.Equals(value))
                {
                    output.Attributes.Add(attribute.Key, string.Empty);
                }
                else if (!false.Equals(value))
                {
                    output.Attributes.Add(attribute.Key, value);
                }
            }

            if (!string.IsNullOrWhiteSpace(BindCount))
            {
                output.Attributes.Add("bindcount", BindCount);

                var value = modelContext.Value(BindCount) as IEnumerable;

                if (value == null)
                {
                    output.Content.SetContent(0.ToString());
                }
                else
                {
                    output.Content.SetContent(value.Cast<object>().Count().ToString());
                }
            }
            else if (!string.IsNullOrWhiteSpace(BindText))
            {
                output.Attributes.Add("bindtext", BindText);

                var value = modelContext.Value(BindText);
                output.Content.SetContent(helper.Encode(value));
            }
            else if (!string.IsNullOrWhiteSpace(BindHtml))
            {
                output.Attributes.Add("bindhtml", BindHtml);

                var value = modelContext.Value(BindHtml);
                output.Content.SetContent(helper.Raw(value).ToString());
            }
            else if (!string.IsNullOrWhiteSpace(BindSome))
            {
                output.Attributes.Add("bindsome", BindSome);
                var value = modelContext.Value(BindSome) as IEnumerable;

                if (value != null && value.Cast<object>().Any())
                {
                    ViewDataDictionary originalData;
                    TagHelperContent template;
                    TagHelperContent buffer;

                    originalData = modelContext.CurrentData;
                    modelContext.Initialize(new object());
                    buffer = new DefaultTagHelperContent();
                    template = await context.GetChildContentAsync();

                    // commit a8fd85d to aspnet/Razor will make this cleaner:
                    //   buffer.Append(await context.GetChildContentAsync(useCachedResult: false));
                    // for now just use reflection to hack it up
                    var delegateField = context.GetType().GetRuntimeFields().Single(f => f.Name == "_getChildContentAsync");
                    var @delegate = delegateField.GetValue(context);
                    var targetField = delegateField.FieldType.GetRuntimeProperties().Single(p => p.Name == "Target");
                    var @target = targetField.GetValue(@delegate);
                    var hackField = @target.GetType().GetRuntimeFields().Single(f => f.Name == "_childContent");

                    foreach (var item in value)
                    {
                        hackField.SetValue(@target, null);
                        modelContext.Initialize(item);
                        buffer.Append(await context.GetChildContentAsync());
                    }

                    output.Content.SetContent(buffer);
                    modelContext.CurrentData = originalData;
                }
                else
                {
                    output.Attributes.Add("hidden", true);
                }
            }
            else if (!string.IsNullOrWhiteSpace(BindNone))
            {
                output.Attributes.Add("bindnone", BindNone);
                var value = modelContext.Value(BindNone) as IEnumerable;

                if (value == null || value.Cast<object>().Any())
                {
                    output.Attributes.Add("hidden", true);
                }
            }

            await base.ProcessAsync(context, output);
        }
Ejemplo n.º 20
0
        public void Append_WrittenAsEncoded()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            tagHelperContent.Append("Hi");

            var writer = new StringWriter();

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

            // Assert
            Assert.Equal("HtmlEncode[[Hi]]", writer.ToString());
        }
Ejemplo n.º 21
0
        public void WriteTo_WritesToATextWriter()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var writer = new StringWriter();
            tagHelperContent.SetContent("Hello ");
            tagHelperContent.Append("World");

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

            // Assert
            Assert.Equal("HtmlEncode[[Hello ]]HtmlEncode[[World]]", writer.ToString());
        }
Ejemplo n.º 22
0
        public void Append_WithTagHelperContent_MultipleAppends()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var copiedTagHelperContent = new DefaultTagHelperContent();
            var text1 = "Hello";
            var text2 = " World!";
            var expected = "HtmlEncode[[Hello]]HtmlEncode[[ World!]]";
            tagHelperContent.Append(text1);
            tagHelperContent.Append(text2);

            // Act
            copiedTagHelperContent.Append(tagHelperContent);

            // Assert
            Assert.Equal(expected, copiedTagHelperContent.GetContent(new CommonTestEncoder()));
        }