FromBounds() 공개 정적인 메소드

public static FromBounds ( HtmlTokenType type, int start, int end ) : HtmlToken
type HtmlTokenType
start int
end int
리턴 HtmlToken
예제 #1
0
        // This is the most complex case (see comments above)
        public IHtmlToken[] GetComment()
        {
            int start  = _cs.Position;
            var tokens = new List <IHtmlToken>();

            Debug.Assert(_cs.CurrentChar == '<' && _cs.LookAhead(1) == '!' && _cs.LookAhead(2) == '-' && _cs.LookAhead(3) == '-');
            _cs.Advance(4);

            bool continueLoop = true;

            while (continueLoop)
            {
                if (_cs.IsEndOfStream())
                {
                    // If we hit end of stream, the comment is not well formed. We are going to add
                    // final token even if it is zero-length so we can expand it when user types
                    // at the end of the file. The token though should be marked as malformed.

                    tokens.Add(HtmlToken.FromBounds(HtmlTokenType.Comment, start, _cs.Position, wellFormed: false));
                    break;
                }

                if (_cs.CurrentChar == '-' && _cs.NextChar == '-' && _cs.LookAhead(2) == '>')
                {
                    _cs.Advance(3);

                    tokens.Add(HtmlToken.FromBounds(HtmlTokenType.Comment, start, _cs.Position, wellFormed: true));
                    break;
                }

                _cs.MoveToNextChar();
            }

            return(tokens.ToArray());
        }
예제 #2
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));
            }
        }
예제 #3
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);
        }