Exemplo n.º 1
0
        private static string GetTagNameCore(RazorBlockSyntax tagBlock)
        {
            if (tagBlock == null)
            {
                throw new ArgumentNullException(nameof(tagBlock));
            }

            MarkupTextLiteralSyntax nameLiteral = null;
            var isBangTag = false;

            if (tagBlock.Children.Count > 0 && tagBlock.Children[0] is MarkupTextLiteralSyntax firstChild)
            {
                if (firstChild.GetContent().StartsWith("<") &&
                    tagBlock.Children.Count >= 3 &&
                    tagBlock.Children[1] is RazorMetaCodeSyntax &&
                    tagBlock.Children[2] is MarkupTextLiteralSyntax potentialBangTagName)
                {
                    isBangTag   = true;
                    nameLiteral = potentialBangTagName;
                }
                else
                {
                    nameLiteral = firstChild;
                }
            }

            if (nameLiteral == null)
            {
                return(null);
            }

            SyntaxToken textToken = null;

            for (var i = 0; i < nameLiteral.LiteralTokens.Count; i++)
            {
                var token = nameLiteral.LiteralTokens[i];

                if (token != null &&
                    (token.Kind == SyntaxKind.Whitespace || token.Kind == SyntaxKind.Text))
                {
                    textToken = token;
                    break;
                }
            }

            if (textToken == null)
            {
                return(null);
            }

            var name = textToken.Kind == SyntaxKind.Whitespace ? null : textToken.Content;

            if (name != null && isBangTag)
            {
                name = "!" + name;
            }

            return(name);
        }
Exemplo n.º 2
0
        private static bool IsSelfClosingCore(RazorBlockSyntax tagBlock)
        {
            if (tagBlock == null)
            {
                throw new ArgumentNullException(nameof(tagBlock));
            }

            var lastChild = tagBlock.ChildNodes().LastOrDefault();

            return(lastChild?.GetContent().EndsWith("/>", StringComparison.Ordinal) ?? false);
        }
Exemplo n.º 3
0
        private static MarkupTagHelperAttributeValueSyntax RewriteAttributeValue(TryParseResult result, RazorBlockSyntax attributeValue)
        {
            var rewriter       = new AttributeValueRewriter(result);
            var rewrittenValue = attributeValue;

            if (result.IsBoundAttribute)
            {
                // If the attribute was requested by a tag helper but the corresponding property was not a
                // string, then treat its value as code. A non-string value can be any C# value so we need
                // to ensure the tree reflects that.
                rewrittenValue = (RazorBlockSyntax)rewriter.Visit(attributeValue);
            }

            return(SyntaxFactory.MarkupTagHelperAttributeValue(rewrittenValue.Children));
        }