示例#1
0
        public void WriteAttributeTo_WritesAsExpected(AttributeValue[] attributeValues, string expectedOutput)
        {
            // Arrange
            var page = CreatePage(p => { });
            page.HtmlEncoder = new CommonTestEncoder();
            var writer = new StringWriter();
            var prefix = new PositionTagged<string>("someattr=", 0);
            var suffix = new PositionTagged<string>(string.Empty, 0);

            // Act
            page.WriteAttributeTo(writer, "someattr", prefix, suffix, attributeValues);

            // Assert
            Assert.Equal(expectedOutput, writer.ToString());
        }
示例#2
0
        public void AddHtmlAttributeValues_AddsToHtmlAttributesAsExpected(
            AttributeValue[] attributeValues,
            string expectedValue)
        {
            // Arrange
            var page = CreatePage(p => { });
            page.HtmlEncoder = new CommonTestEncoder();
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: () => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            page.AddHtmlAttributeValues("someattr", executionContext, attributeValues);

            // Assert
            var htmlAttribute = Assert.Single(executionContext.HTMLAttributes);
            Assert.Equal("someattr", htmlAttribute.Name, StringComparer.Ordinal);
            Assert.IsType<HtmlString>(htmlAttribute.Value);
            Assert.Equal(expectedValue, HtmlContentUtilities.HtmlContentToString((IHtmlContent)htmlAttribute.Value));
            Assert.False(htmlAttribute.Minimized);
            var allAttribute = Assert.Single(executionContext.AllAttributes);
            Assert.Equal("someattr", allAttribute.Name, StringComparer.Ordinal);
            Assert.IsType<HtmlString>(allAttribute.Value);
            Assert.Equal(expectedValue, allAttribute.Value.ToString(), StringComparer.Ordinal);
            Assert.False(allAttribute.Minimized);
        }
示例#3
0
        public virtual void WriteAttributeTo(
            [NotNull] TextWriter writer,
            string name,
            [NotNull] PositionTagged <string> prefix,
            [NotNull] PositionTagged <string> suffix,
            params AttributeValue[] values)
        {
            if (values.Length == 0)
            {
                // Explicitly empty attribute, so write the prefix
                WritePositionTaggedLiteral(writer, prefix);
            }
            else if (IsSingleBoolFalseOrNullValue(values))
            {
                // Value is either null or the bool 'false' with no prefix; don't render the attribute.
                return;
            }
            else if (UseAttributeNameAsValue(values))
            {
                var attributeValue = values[0];
                var positionTaggedAttributeValue = attributeValue.Value;

                WritePositionTaggedLiteral(writer, prefix);

                var sourceLength       = suffix.Position - positionTaggedAttributeValue.Position;
                var nameAttributeValue = new AttributeValue(
                    attributeValue.Prefix,
                    new PositionTagged <object>(name, attributeValue.Value.Position),
                    literal: attributeValue.Literal);

                // The value is just the bool 'true', write the attribute name instead of the string 'True'.
                WriteAttributeValue(writer, nameAttributeValue, sourceLength);
            }
            else
            {
                // This block handles two cases.
                // 1. Single value with prefix.
                // 2. Multiple values with or without prefix.
                WritePositionTaggedLiteral(writer, prefix);
                for (var i = 0; i < values.Length; i++)
                {
                    var attributeValue = values[i];
                    var positionTaggedAttributeValue = attributeValue.Value;

                    if (positionTaggedAttributeValue.Value == null)
                    {
                        // Nothing to write
                        continue;
                    }

                    var next = i == values.Length - 1 ?
                               suffix :              // End of the list, grab the suffix
                               values[i + 1].Prefix; // Still in the list, grab the next prefix

                    // Calculate length of the source span by the position of the next value (or suffix)
                    var sourceLength = next.Position - attributeValue.Value.Position;

                    WriteAttributeValue(writer, attributeValue, sourceLength);
                }
            }

            WritePositionTaggedLiteral(writer, suffix);
        }