Exemplo n.º 1
0
        public CssSelector GetCombinatorSelector(string sel)
        {
            string newSel = sel.Replace("+", " + ").Replace("~", " ~ ").Replace(">", " > ");

            string[]      sep       = new string[] { " " };
            List <string> basicSels = newSel.Split(sep, StringSplitOptions.RemoveEmptyEntries).ToList();

            int j = basicSels.Count - 1;

            if (j == 0)
            {
                BasicCssSelector selector = (BasicCssSelector)GetBasicSelector(basicSels[j]);
                return(selector);
            }
            else
            {
                Stack <CssSelector> stack = new Stack <CssSelector>();
                while (j >= 0)
                {
                    CombinatorCssSelector current       = null;
                    CssSelector           selectorRight = null;
                    if (stack.Count == 0)
                    {
                        selectorRight = (BasicCssSelector)GetBasicSelector(basicSels[j]);
                        j--;
                    }
                    else
                    {
                        selectorRight = stack.Pop();
                    }

                    string symbol = basicSels[j];
                    current = (CombinatorCssSelector)GetCurrentSelector(symbol);
                    j--;
                    if (j < 0)
                    {
                        current.right = selectorRight;
                        break;
                    }
                    BasicCssSelector selectorLeft = (BasicCssSelector)GetBasicSelector(basicSels[j]);
                    current.left  = selectorLeft;
                    current.right = selectorRight;
                    stack.Push(current);
                    j--;
                }
                return(stack.Pop());
            }
        }
Exemplo n.º 2
0
        public CssSelector GetBasicSelector(string sel)
        {
            CssSelector     selector = null;
            CssSelectorType type     = CssSelectorType.Unkown;

            if (sel[0] == '#')
            {
                type     = CssSelectorType.IdSelector;
                selector = new BasicCssSelector(sel.Substring(1), type, "#");
            }
            else if (sel[0] == '.')
            {
                type     = CssSelectorType.ClassSelector;
                selector = new BasicCssSelector(sel.Substring(1), type, ".");
            }
            else if (sel[0] == '[')
            {
                type     = CssSelectorType.AttributeSelector;
                selector = new BasicCssSelector(sel.Substring(1, sel.Length - 2), type, "[");
            }
            else if (sel[0] == ':')
            {
                if (sel[1] == ':')
                {
                    type     = CssSelectorType.PseudoElement;
                    selector = new BasicCssSelector(sel.Substring(2), type, "::");
                }
                else
                {
                    type     = CssSelectorType.PseudoClass;
                    selector = new BasicCssSelector(sel.Substring(1), type, ":");
                }
            }
            else if (Char.IsLetter(sel[0]))
            {
                type     = CssSelectorType.ElementSelector;
                selector = new BasicCssSelector(sel.Substring(1), type, "");
            }
            return(selector);
        }