Inheritance: BaseHtmlToken, IHtmlAttributeValueToken
示例#1
0
        /// <summary>
        /// Retrieves quoted attribute value token at the current character stream position
        /// </summary>
        /// <param name="isScript">True if attribute is a script, like onclick</param>
        /// <returns>Attribute value token</returns>
        public IHtmlAttributeValueToken GetQuotedAttributeValue(bool isScript, int tagEnd)
        {
            Debug.Assert(_cs.IsAtString());

            int  start     = _cs.Position;
            char openQuote = _cs.CurrentChar;

            int stringEnd = _stringClosure.GetStringClosureLocation(tagEnd);

            _cs.Position = start;

            var valueTokens = new List <IHtmlToken>();

            valueTokens.Add(HtmlToken.FromBounds(HtmlTokenType.String, _cs.Position, stringEnd));

            char closeQuote = '\0';

            if (openQuote != '\0' && stringEnd - 1 != start)
            {
                closeQuote = _cs[stringEnd - 1];
                if (closeQuote != openQuote)
                {
                    closeQuote = '\0';
                }
            }

            _cs.Position = stringEnd;

            if (valueTokens.Count < 2)
            {
                if (isScript)
                {
                    return(new ScriptAttributeValueToken(valueTokens[0], openQuote, closeQuote));
                }
                else
                {
                    return(AttributeValueToken.Create(valueTokens[0], openQuote, closeQuote));
                }
            }
            else
            {
                return(new CompositeAttributeValueToken(valueTokens.ToArray(), openQuote, closeQuote, isScript));
            }
        }
示例#2
0
        /// <summary>
        /// Retrieves unquoted attribute value
        /// </summary>
        /// <returns>Attribute value token</returns>
        public IHtmlAttributeValueToken GetUnquotedAttributeValue(int tagEnd)
        {
            // Need to handle <a b=c=d /> and figure out that c=d is a valid pair while
            // b= is missing a value. One way is to check if 'c' happens to be
            // one of the known HTML attributes, but it only works for plain HTML
            // and won't work for ASP.NET controls that can define any attributes
            // via .NET properties.

            // Options (value is unquoted)
            //      a. Attribute value is either a sequence of characters or a number
            //         except if it is ID or CLASS.
            //      b. Attribute name is typically a sequence of characters and does not include digits
            //      c. Attribute value is not normally followed by =

            Debug.Assert(!_cs.IsAtString());

            AttributeValueToken token = null;
            int start = _cs.Position;
            int end   = _cs.Position;

            ITextRange    nextTokenRange;
            NextTokenType nextTokenType = NextToken.PeekNextToken(_cs, tagEnd, out nextTokenRange);

            switch (nextTokenType)
            {
            case NextTokenType.None:        // attrName={EOF}
            case NextTokenType.Tag:         // attName=<foo
            case NextTokenType.Equals:      // attrName==
                return(null);

            case NextTokenType.Number:
            // attrName = 1.0
            case NextTokenType.Unknown:
            // id=#
            case NextTokenType.Identifier:
                // attrName = foo12. There are no know attributes of this form.
                end = nextTokenRange.End;
                break;

            case NextTokenType.Letters:
                // Legal value. Need to check against attribute names to be sure.
                bool isKnownAttribute = AttributeTable.IsKnownAttribute(_cs.GetSubstringAt(nextTokenRange.Start, nextTokenRange.Length));
                if (!isKnownAttribute)
                {
                    end = nextTokenRange.End;
                }
                break;
            }

            char closeQuote = '\0';

            if (end > start)
            {
                closeQuote = _cs[end - 1];
                if (closeQuote != '\'' && closeQuote != '\"')
                {
                    closeQuote = '\0';
                }
            }

            token        = AttributeValueToken.Create(HtmlToken.FromBounds(start, end), '\0', closeQuote);
            _cs.Position = end;

            return(token);
        }