Пример #1
0
        private void parseCombinartor()
        {
            combinatorSelector combineSelector = new combinatorSelector();

            combinatorClause clause = new combinatorClause();

            clause.combineType = combinator.unknown;

            char current = ReadNext();

            while (!stop)
            {
                if (current == '>')
                {
                    if (_buffer.Length > 0)
                    {
                        clause.selectorText = AppendCleanBuffer(_buffer);

                        clause.selector = new simpleSelectorParser(clause.selectorText).Parse();
                        combineSelector.item.Add(clause);

                        clause             = new combinatorClause();
                        clause.combineType = combinator.Child;

                        current = ReadNext();
                        continue;
                    }

                    if (clause.combineType == combinator.unknown || clause.combineType == combinator.Descendant)
                    {
                        clause.combineType = combinator.Child;
                    }

                    current = ReadNext();
                    continue;
                }
                else if (current == '+')
                {
                    if (_buffer.Length > 0)
                    {
                        clause.selectorText = AppendCleanBuffer(_buffer);

                        clause.selector = new simpleSelectorParser(clause.selectorText).Parse();
                        combineSelector.item.Add(clause);

                        clause             = new combinatorClause();
                        clause.combineType = combinator.AdjacentSibling;

                        current = ReadNext();
                        continue;
                    }

                    if (clause.combineType == combinator.unknown || clause.combineType == combinator.Descendant)
                    {
                        clause.combineType = combinator.AdjacentSibling;
                    }
                    current = ReadNext();
                    continue;
                }
                else if (current == '~')
                {
                    if (_buffer.Length > 0)
                    {
                        clause.selectorText = AppendCleanBuffer(_buffer);

                        clause.selector = new simpleSelectorParser(clause.selectorText).Parse();
                        combineSelector.item.Add(clause);

                        clause             = new combinatorClause();
                        clause.combineType = combinator.Sibling;
                        current            = ReadNext();
                        continue;
                    }

                    if (clause.combineType == combinator.unknown || clause.combineType == combinator.Descendant)
                    {
                        clause.combineType = combinator.Sibling;
                    }
                    current = ReadNext();
                    continue;
                }
                else if (current.isCSSWhiteSpace())
                {
                    if (_buffer.Length > 0)
                    {
                        clause.selectorText = AppendCleanBuffer(_buffer);

                        clause.selector = new simpleSelectorParser(clause.selectorText).Parse();
                        combineSelector.item.Add(clause);

                        clause             = new combinatorClause();
                        clause.combineType = combinator.Descendant;
                    }

                    if (clause.combineType == combinator.unknown)
                    {
                        clause.combineType = combinator.Descendant;
                    }

                    current = ReadNext();
                    continue;
                }
                else
                {
                    _buffer.Append(current);
                    current = ReadNext();
                }
            }

            if (_buffer.Length > 0)
            {
                clause.selectorText = AppendCleanBuffer(_buffer);

                clause.selector = new simpleSelectorParser(clause.selectorText).Parse();
                combineSelector.item.Add(clause);
            }

            simple = combineSelector;
        }
Пример #2
0
        private static bool matchCombinator(Element element, combinatorSelector combineSelector)
        {
            int            index   = combineSelector.item.Count() - 1;
            Element        matched = element;
            simpleSelector upMatchSelector;

            combinatorClause clause = combineSelector.item[index];

            if (!matchOneSelector(matched, clause.selector))
            {
                return(false);
            }

            for (int i = index; i >= 0; i--)
            {
                if (i == 0)
                {
                    return(true);
                }

                combinator combinetype = combineSelector.item[i].combineType;

                upMatchSelector = combineSelector.item[i - 1].selector;

                switch (combinetype)
                {
                case combinator.Descendant:
                {
                    Element parentElement = matched.parentElement;
                    bool    matchfound    = false;
                    while (parentElement != null && parentElement.nodeType == enumNodeType.ELEMENT)
                    {
                        if (selectorMatch.matchOneSelector(parentElement, upMatchSelector))
                        {
                            matched    = parentElement;
                            matchfound = true;
                            break;
                        }
                        parentElement = parentElement.parentElement;
                    }
                    if (matchfound)
                    {
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }

                case combinator.Child:
                {
                    Element parentElement = matched.parentElement;

                    if (parentElement != null && parentElement.nodeType == enumNodeType.ELEMENT && selectorMatch.matchOneSelector(parentElement, upMatchSelector))
                    {
                        matched = parentElement;
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }

                case combinator.AdjacentSibling:
                {
                    Node    previousnode    = matched.previousSibling();
                    Element previouselement = null;

                    while (previousnode != null)
                    {
                        if (previousnode.nodeType == enumNodeType.ELEMENT)
                        {
                            previouselement = (Element)previousnode;
                            break;
                        }
                        previousnode = previousnode.previousSibling();
                    }

                    if (previouselement == null)
                    {
                        return(false);
                    }
                    else
                    {
                        if (selectorMatch.matchOneSelector(previouselement, upMatchSelector))
                        {
                            matched = previouselement;
                            continue;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }

                case combinator.Sibling:
                {
                    Node previousnode = matched.previousSibling();
                    bool matchedfound = false;


                    while (previousnode != null)
                    {
                        if (previousnode.nodeType == enumNodeType.ELEMENT)
                        {
                            Element previouselement = (Element)previousnode;
                            if (selectorMatch.matchOneSelector(previouselement, upMatchSelector))
                            {
                                matchedfound = true;
                                matched      = previouselement;
                                break;
                            }
                        }

                        previousnode = previousnode.previousSibling();
                    }

                    if (matchedfound)
                    {
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }


                default:
                    return(false);
                }
            }

            return(true);
        }