コード例 #1
0
        /// <summary>
        /// Invoked once a square bracket has been found in the token enumerator.
        /// </summary>
        /// <param name="token">The token.</param>
        void OnAttribute(CssToken token)
        {
            if (token.Type == CssTokenType.Whitespace)
            {
                return;
            }

            if (token.Type == CssTokenType.Ident || token.Type == CssTokenType.String)
            {
                state    = State.AttributeOperator;
                attrName = token.Data;
            }
            else if (token.Type == CssTokenType.Delim && token.ToValue() == "|")
            {
                state  = State.Attribute;
                attrNs = String.Empty;
            }
            else if (token.Type == CssTokenType.Delim && token.ToValue() == "*")
            {
                state    = State.AttributeOperator;
                attrName = token.ToValue();
            }
            else
            {
                state = State.Data;
                valid = false;
            }
        }
コード例 #2
0
        /// <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 OnAttributeOperator(CssToken token)
        {
            if (token.Type == CssTokenType.Whitespace)
            {
                return(true);
            }

            state = State.AttributeValue;

            if (token.Type == CssTokenType.SquareBracketClose)
            {
                return(OnAttributeEnd(token));
            }
            else if (token is CssMatchToken || token.Type == CssTokenType.Delim)
            {
                attrOp = token.ToValue();
            }
            else
            {
                state = State.AttributeEnd;
                return(false);
            }

            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Invoked once a square bracket has been found in the token enumerator.
        /// </summary>
        /// <param name="token">The token.</param>
        void OnAttributeOperator(CssToken token)
        {
            if (token.Type == CssTokenType.Whitespace)
            {
                return;
            }

            if (token.Type == CssTokenType.SquareBracketClose)
            {
                state = State.AttributeValue;
                OnAttributeEnd(token);
            }
            else if (token.IsMatchToken() || token.Type == CssTokenType.Delim)
            {
                state  = State.AttributeValue;
                attrOp = token.ToValue();

                if (attrOp == "|")
                {
                    attrNs   = attrName;
                    attrName = null;
                    attrOp   = String.Empty;
                    state    = State.Attribute;
                }
            }
            else
            {
                state = State.AttributeEnd;
                valid = false;
            }
        }
コード例 #4
0
        void OnAttributeOperator(CssToken token)
        {
            if (token.Type != CssTokenType.Whitespace)
            {
                if (token.Type == CssTokenType.SquareBracketClose)
                {
                    _state = State.AttributeValue;
                    OnAttributeEnd(token);
                }
                else if (token.Type == CssTokenType.Match || token.Type == CssTokenType.Delim)
                {
                    _state  = State.AttributeValue;
                    _attrOp = token.ToValue();

                    if (_attrOp == CombinatorSymbols.Pipe)
                    {
                        _attrNs   = _attrName;
                        _attrName = null;
                        _attrOp   = String.Empty;
                        _state    = State.Attribute;
                    }
                }
                else
                {
                    _state = State.AttributeEnd;
                    _valid = false;
                }
            }
        }
コード例 #5
0
 void OnAttribute(CssToken token)
 {
     if (token.Type != CssTokenType.Whitespace)
     {
         if (token.Type == CssTokenType.Ident || token.Type == CssTokenType.String)
         {
             _state    = State.AttributeOperator;
             _attrName = token.Data;
         }
         else if (token.Type == CssTokenType.Delim && token.Data.Is(CombinatorSymbols.Pipe))
         {
             _state  = State.Attribute;
             _attrNs = String.Empty;
         }
         else if (token.Type == CssTokenType.Delim && token.Data.Is(Keywords.Asterisk))
         {
             _state    = State.AttributeOperator;
             _attrName = token.ToValue();
         }
         else
         {
             _state = State.Data;
             _valid = false;
         }
     }
 }
コード例 #6
0
ファイル: CssBuilder.cs プロジェクト: neelendu/AngleSharp
        void CollectTrivia(ref CssToken token)
        {
            var storeComments = _parser.Options.IsStoringTrivia;

            while (token.Type == CssTokenType.Whitespace || token.Type == CssTokenType.Comment || token.Type == CssTokenType.Cdc || token.Type == CssTokenType.Cdo)
            {
                if (storeComments && token.Type == CssTokenType.Comment)
                {
                    var current = _nodes.Peek();
                    var comment = new CssComment(token.Data);
                    var start   = token.Position;
                    var end     = start.After(token.ToValue());
                    comment.SourceCode = CreateView(start, end);
                    current.AppendChild(comment);
                }

                token = _tokenizer.Get();
            }
        }
コード例 #7
0
ファイル: CssBuilder.cs プロジェクト: mganss/AngleSharp
        /// <summary>
        /// Called before the property name has been detected.
        /// </summary>
        public CssProperty CreateDeclarationWith(Func <String, CssProperty> createProperty, ref CssToken token)
        {
            var property = default(CssProperty);

            CreateNewNode();

            var sb = Pool.NewStringBuilder();

            while (token.Type != CssTokenType.Eof &&
                   token.Type != CssTokenType.Colon &&
                   token.Type != CssTokenType.Whitespace &&
                   token.Type != CssTokenType.Comment &&
                   token.Type != CssTokenType.CurlyBracketOpen &&
                   token.Type != CssTokenType.Semicolon)
            {
                sb.Append(token.ToValue());
                token = NextToken();
            }

            var propertyName = sb.ToPool();

            if (propertyName.Length > 0)
            {
                property = _parser.Options.IsIncludingUnknownDeclarations ||
                           _parser.Options.IsToleratingInvalidValues ?
                           new CssUnknownProperty(propertyName) : createProperty(propertyName);

                if (property == null)
                {
                    RaiseErrorOccurred(CssParseError.UnknownDeclarationName, token);
                }

                CollectTrivia(ref token);

                if (token.Type == CssTokenType.Colon)
                {
                    var important = false;
                    var value     = CreateValue(CssTokenType.CurlyBracketClose, ref token, out important);

                    if (value == null)
                    {
                        RaiseErrorOccurred(CssParseError.ValueMissing, token);
                    }
                    else if (property != null && property.TrySetValue(value))
                    {
                        property.IsImportant = important;
                    }

                    CollectTrivia(ref token);
                }
                else
                {
                    RaiseErrorOccurred(CssParseError.ColonMissing, token);
                }

                JumpToDeclEnd(ref token);
            }
            else if (token.Type != CssTokenType.Eof)
            {
                RaiseErrorOccurred(CssParseError.IdentExpected, token);
                JumpToDeclEnd(ref token);
            }

            if (token.Type == CssTokenType.Semicolon)
            {
                token = NextToken();
            }

            return(CloseNode(property));
        }
コード例 #8
0
        /// <summary>
        /// Invoked once a pseudo class has been found in the token enumerator.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns>True if no error occurred, otherwise false.</returns>
        Boolean OnPseudoClassFunction(CssToken token)
        {
            if (token.Type == CssTokenType.Whitespace)
            {
                return(true);
            }

            switch (attrName)
            {
            case pseudoClassFunctionNthChild:
            case pseudoClassFunctionNthLastChild:
            {
                switch (token.Type)
                {
                case CssTokenType.Ident:
                case CssTokenType.Number:
                case CssTokenType.Dimension:
                    attrValue += token.ToValue();
                    return(true);

                case CssTokenType.Delim:
                    var chr = token.Data[0];

                    if (chr == Specification.Plus || chr == Specification.Minus)
                    {
                        attrValue += token.Data;
                        return(true);
                    }

                    break;
                }

                break;
            }

            case pseudoClassFunctionNot:
            {
                if (nested == null)
                {
                    nested = new CssSelectorConstructor();
                }

                if (token.Type != CssTokenType.RoundBracketClose || nested.state != State.Data)
                {
                    nested.Apply(token);
                    return(true);
                }

                break;
            }

            case pseudoClassFunctionDir:
            {
                if (token.Type == CssTokenType.Ident)
                {
                    attrValue = token.Data;
                }

                state = State.PseudoClassFunctionEnd;
                return(true);
            }

            case pseudoClassFunctionLang:
            {
                if (token.Type == CssTokenType.Ident)
                {
                    attrValue = token.Data;
                }

                state = State.PseudoClassFunctionEnd;
                return(true);
            }

            case pseudoClassFunctionContains:
            {
                if (token.Type == CssTokenType.String || token.Type == CssTokenType.Ident)
                {
                    attrValue = token.Data;
                }

                state = State.PseudoClassFunctionEnd;
                return(true);
            }
            }

            return(OnPseudoClassFunctionEnd(token));
        }