Пример #1
0
        /// <summary>
        /// Creates an attribute selector using the current state.
        /// </summary>
        /// <returns>The created selector.</returns>
        SimpleSelector CreateAttrSelector()
        {
            switch (attrOp)
            {
            case "=":
                return(SimpleSelector.AttrMatch(attrName, attrValue, attrNs));

            case "~=":
                return(SimpleSelector.AttrList(attrName, attrValue, attrNs));

            case "|=":
                return(SimpleSelector.AttrHyphen(attrName, attrValue, attrNs));

            case "^=":
                return(SimpleSelector.AttrBegins(attrName, attrValue, attrNs));

            case "$=":
                return(SimpleSelector.AttrEnds(attrName, attrValue, attrNs));

            case "*=":
                return(SimpleSelector.AttrContains(attrName, attrValue, attrNs));

            case "!=":
                return(SimpleSelector.AttrNotMatch(attrName, attrValue, attrNs));

            default:
                return(SimpleSelector.AttrAvailable(attrName, attrNs));
            }
        }
        /// <summary>
        /// Invoked once a square bracket has been found in the token enumerator.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns>True if no error occurred, otherwise false.</returns>
        Boolean OnAttributeEnd(CssToken token)
        {
            if (token.Type == CssTokenType.Whitespace)
            {
                return(true);
            }

            state = State.Data;

            if (token.Type == CssTokenType.SquareBracketClose)
            {
                switch (attrOp)
                {
                case "=":
                    Insert(SimpleSelector.AttrMatch(attrName, attrValue));
                    break;

                case "~=":
                    Insert(SimpleSelector.AttrList(attrName, attrValue));
                    break;

                case "|=":
                    Insert(SimpleSelector.AttrHyphen(attrName, attrValue));
                    break;

                case "^=":
                    Insert(SimpleSelector.AttrBegins(attrName, attrValue));
                    break;

                case "$=":
                    Insert(SimpleSelector.AttrEnds(attrName, attrValue));
                    break;

                case "*=":
                    Insert(SimpleSelector.AttrContains(attrName, attrValue));
                    break;

                case "!=":
                    Insert(SimpleSelector.AttrNotMatch(attrName, attrValue));
                    break;

                default:
                    Insert(SimpleSelector.AttrAvailable(attrName));
                    break;
                }

                return(true);
            }

            return(false);
        }
Пример #3
0
        /// <summary>
        /// Gets the attribute selector for the specified sequence of tokens.
        /// </summary>
        /// <param name="tokens">The token source.</param>
        /// <returns>The created selector.</returns>
        public SimpleSelector GetAttributeSelector(IEnumerator <CssToken> tokens)
        {
            var      values = new List <string>();
            CssToken op     = null;

            while (tokens.MoveNext())
            {
                if (tokens.Current.Type == CssTokenType.SquareBracketClose)
                {
                    break;
                }
                else if (tokens.Current.Type == CssTokenType.Ident)
                {
                    values.Add(((CssKeywordToken)tokens.Current).Data);
                }
                else if (tokens.Current.Type == CssTokenType.String)
                {
                    values.Add(((CssStringToken)tokens.Current).Data);
                }
                else if (tokens.Current.Type == CssTokenType.Number)
                {
                    values.Add(((CssNumberToken)tokens.Current).Data.ToString());
                }
                else if (op == null && (tokens.Current is CssMatchToken || tokens.Current.Type == CssTokenType.Delim))
                {
                    op = tokens.Current;
                }
                else if (tokens.Current.Type != CssTokenType.Whitespace)
                {
                    if (!ignoreErrors)
                    {
                        throw new DOMException(ErrorCode.SyntaxError);
                    }
                    return(null);
                }
            }

            if ((op == null || values.Count != 2) && (op != null || values.Count != 1))
            {
                if (!ignoreErrors)
                {
                    throw new DOMException(ErrorCode.SyntaxError);
                }
                return(null);
            }

            if (op == null)
            {
                return(SimpleSelector.AttrAvailable(values[0]));
            }

            switch (op.ToValue())
            {
            case "=":
                return(SimpleSelector.AttrMatch(values[0], values[1]));

            case "~=":
                return(SimpleSelector.AttrList(values[0], values[1]));

            case "|=":
                return(SimpleSelector.AttrHyphen(values[0], values[1]));

            case "^=":
                return(SimpleSelector.AttrBegins(values[0], values[1]));

            case "$=":
                return(SimpleSelector.AttrEnds(values[0], values[1]));

            case "*=":
                return(SimpleSelector.AttrContains(values[0], values[1]));

            case "!=":
                return(SimpleSelector.AttrNotMatch(values[0], values[1]));
            }

            if (!ignoreErrors)
            {
                throw new DOMException(ErrorCode.SyntaxError);
            }
            return(null);
        }