コード例 #1
0
        public static IList<ICssSelectorItem> CreateCssSelector(String selector) {
            IList<ICssSelectorItem> cssSelectorItems = new List<ICssSelectorItem>();
            Match itemMatcher = selectorPattern.Match(selector);
            bool isTagSelector = false;
            int crc = 0;
            while (itemMatcher.Success) {
                String selectorItem = itemMatcher.Groups[0].Value;
                crc += selectorItem.Length;
                switch (selectorItem[0]) {
                    case '#':
                        cssSelectorItems.Add(new CssIdSelector(selectorItem.Substring(1)));
                        break;
                    case '.':
                        cssSelectorItems.Add(new CssClassSelector(selectorItem.Substring(1)));
                        break;
                    case '[':
                        cssSelectorItems.Add(new CssAttributeSelector(selectorItem));
                        break;
                    case ':':
                        cssSelectorItems.Add(new CssPseudoSelector(selectorItem));
                        break;
                    case ' ':
                    case '+':
                    case '>':
                    case '~':
                        if (cssSelectorItems.Count == 0) return null;
                        ICssSelectorItem lastItem = cssSelectorItems[cssSelectorItems.Count - 1];
                        ICssSelectorItem currItem = new CssSeparatorSelector(selectorItem[0]);
                        if (lastItem is CssSeparatorSelector) {
                            if (selectorItem[0] == ' ')
                                break;
                            else if (lastItem.Separator == ' ')
                                cssSelectorItems[cssSelectorItems.Count - 1] = currItem;
                            else
                                return null;
                        } else {
                            cssSelectorItems.Add(currItem);
                            isTagSelector = false;
                        }
                        break;
                    default: //and case '*':
                        if (isTagSelector)
                            return null;
                        isTagSelector = true;
                        cssSelectorItems.Add(new CssTagSelector(selectorItem));
                        break;
                }
                itemMatcher = itemMatcher.NextMatch();
            }

            if (selector.Length == 0 || selector.Length != crc)
                return null;
            return cssSelectorItems;
        }
コード例 #2
0
        public static IList <ICssSelectorItem> CreateCssSelector(String selector)
        {
            IList <ICssSelectorItem> cssSelectorItems = new List <ICssSelectorItem>();
            Match itemMatcher   = selectorPattern.Match(selector);
            bool  isTagSelector = false;
            int   crc           = 0;

            while (itemMatcher.Success)
            {
                String selectorItem = itemMatcher.Groups[0].Value;
                crc += selectorItem.Length;
                switch (selectorItem[0])
                {
                case '#':
                    cssSelectorItems.Add(new CssIdSelector(selectorItem.Substring(1)));
                    break;

                case '.':
                    cssSelectorItems.Add(new CssClassSelector(selectorItem.Substring(1)));
                    break;

                case '[':
                    cssSelectorItems.Add(new CssAttributeSelector(selectorItem));
                    break;

                case ':':
                    cssSelectorItems.Add(new CssPseudoSelector(selectorItem));
                    break;

                case ' ':
                case '+':
                case '>':
                case '~':
                    if (cssSelectorItems.Count == 0)
                    {
                        return(null);
                    }
                    ICssSelectorItem lastItem = cssSelectorItems[cssSelectorItems.Count - 1];
                    ICssSelectorItem currItem = new CssSeparatorSelector(selectorItem[0]);
                    if (lastItem is CssSeparatorSelector)
                    {
                        if (selectorItem[0] == ' ')
                        {
                            break;
                        }
                        else if (lastItem.Separator == ' ')
                        {
                            cssSelectorItems[cssSelectorItems.Count - 1] = currItem;
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        cssSelectorItems.Add(currItem);
                        isTagSelector = false;
                    }
                    break;

                default:     //and case '*':
                    if (isTagSelector)
                    {
                        return(null);
                    }
                    isTagSelector = true;
                    cssSelectorItems.Add(new CssTagSelector(selectorItem));
                    break;
                }
                itemMatcher = itemMatcher.NextMatch();
            }

            if (selector.Length == 0 || selector.Length != crc)
            {
                return(null);
            }
            return(cssSelectorItems);
        }