Пример #1
0
        private CssItem BuildCssList(CssConvert.CssParser.CSSDocument css)
        {
            List <Element> listElem  = new List <Element>();
            List <Class>   listClass = new List <Class>();
            List <Id>      listId    = new List <Id>();

            try
            {
                foreach (CssConvert.CssParser.RuleSet rs in css.RuleSets)//this will be each item in the css
                {
                    #region StyleList
                    List <Style> listStyle = new List <Style>();
                    //here I have to create the Style List
                    foreach (CssConvert.CssParser.Declaration dec in rs.Declarations)
                    {
                        string s        = dec.ToString();
                        Style  newStyle = new Style(s);
                        listStyle.Add(newStyle);
                    }
                    #endregion

                    foreach (CssConvert.CssParser.Selector sel in rs.Selectors)
                    {
                        foreach (CssConvert.CssParser.SimpleSelector simp in sel.SimpleSelectors)
                        {
                            #region Class
                            if (sel.ToString().Contains("."))
                            {
                                string[] _class = sel.ToString().Split('.');
                                #region starts with period
                                if (sel.ToString().StartsWith("."))
                                {
                                    //need to check if there is an element
                                    Class newClass          = new Class();
                                    bool  containsClassName = false;

                                    foreach (Class c in listClass)
                                    {
                                        if (c.Name.Equals(_class[1]))
                                        {
                                            newClass          = c;
                                            containsClassName = true;
                                        }
                                    }
                                    #region Contains Space
                                    if (_class[1].Contains(" "))
                                    {
                                        //check for nested Element
                                        string[] nestedElement = _class[1].Split(' ');
                                        #region contains colon
                                        if (nestedElement[1].Contains(":"))
                                        {
                                            //have a psuedo on element
                                            string[] elementPsuedo = nestedElement[1].Split(':');
                                            //class
                                            if (containsClassName == false)
                                            {
                                                newClass.Name = nestedElement[0];
                                            }

                                            Element newElement = new Element();
                                            if (newClass.ListElement.Count > 0)
                                            {
                                                bool foundElement = false;
                                                foreach (Element e in newClass.ListElement)
                                                {
                                                    if (e.Name.Equals(elementPsuedo[0].ToString()))
                                                    {
                                                        newElement   = e;
                                                        foundElement = true;
                                                    }
                                                }
                                                if (foundElement == false)
                                                {
                                                    newElement.Name = elementPsuedo[0].ToString();
                                                }
                                            }
                                            else
                                            {
                                                newElement.Name = elementPsuedo[0].ToString();
                                            }

                                            Psuedo p = new Psuedo();
                                            p.Name      = elementPsuedo[1].ToString();
                                            p.ListStyle = listStyle;

                                            newElement.ListPsuedo.Add(p);

                                            newClass.ListElement.Add(newElement);
                                            if (containsClassName == false)
                                            {
                                                listClass.Add(newClass);
                                            }
                                        }
                                        #endregion
                                        #region No Colon
                                        else
                                        {
                                            Element ce = new Element();
                                            ce.Name             = nestedElement[1];
                                            ce.ListElementStyle = listStyle;

                                            newClass.ListElement.Add(ce);
                                            if (containsClassName == false)
                                            {
                                                newClass.Name = nestedElement[0];
                                                listClass.Add(newClass);
                                            }
                                        }
                                        #endregion
                                    }
                                    #endregion
                                    #region no space
                                    else
                                    {
                                        //this is just a class has no elements
                                        foreach (Class c in listClass)
                                        {
                                            if (c.Name.Equals(_class[1]))
                                            {
                                                containsClassName = true;
                                            }
                                        }
                                        if (containsClassName == false)
                                        {
                                            newClass.Name           = _class[1];
                                            newClass.ListClassStyle = listStyle;
                                            listClass.Add(newClass);
                                        }
                                    }
                                    #endregion
                                }
                                #endregion
                                #region does not start with period
                                else
                                {
                                    #region contains colon
                                    //now deal with the 0 index which will either be an element or an element with psuedo
                                    if (_class[0].Contains(":"))
                                    {
                                        //1 index will be the class
                                        Class c = new Class();
                                        c.Name = _class[1];
                                        listClass.Add(c);

                                        Element       e     = new Element();
                                        List <Psuedo> listP = new List <Psuedo>();
                                        string[]      ps    = _class[0].Split(':');
                                        for (int i = 0; i <= ps.Length - 1; i++)
                                        {
                                            //the 0 index will always be the element, each additional item will be a psuedo
                                            if (i == 0)
                                            {
                                                e.Name = ps[0];
                                            }
                                            else
                                            {
                                                Psuedo p = new Psuedo();
                                                p.ListStyle = listStyle;
                                                p.Name      = ps[i];
                                                listP.Add(p);
                                            }
                                        }
                                        e.ListPsuedo = listP;
                                        //listElem.Add(e);
                                        if (c.ListElement == null)
                                        {
                                            c.ListElement = new List <Element>();
                                        }
                                        c.ListElement.Add(e);
                                    }
                                    #endregion
                                    #region no colon
                                    else
                                    {
                                        //1 index will be the class
                                        Class c = new Class();
                                        c.Name = _class[1];
                                        listClass.Add(c);

                                        //we have just a plain element
                                        Element newElement = new Element();
                                        newElement.Name             = _class[0].ToString();
                                        newElement.ListElementStyle = listStyle;

                                        listElem.Add(newElement);
                                    }
                                    #endregion
                                }
                                #endregion
                            }
                            #endregion
                            #region Id
                            else if (sel.ToString().Contains("#"))
                            {
                                string[] id = sel.ToString().Split('#');
                                if (sel.ToString().StartsWith("#"))//means id[1] is the id
                                {
                                    //split the id name to see check for an element
                                    if (id[1].Contains(" "))
                                    {
                                        string[] nestedElement = id[1].Split(' ');//nestedElement[0] is the element
                                        if (nestedElement[0].Contains(":"))
                                        {
                                            string[] _psuedo = nestedElement[0].Split(':');//_psuedo[0] is the element, _psuedo[1] is the psuedo

                                            //we have a psuedo class
                                            Psuedo p = new Psuedo();
                                            p.Name      = _psuedo[1].ToString();
                                            p.ListStyle = listStyle;

                                            Element _element = new Element();
                                            _element.Name = _psuedo[0].ToString();
                                            _element.ListPsuedo.Add(p);

                                            Id i = new Id();
                                            i.Name = id[1].ToString();
                                            i.ListElement.Add(_element);
                                            listId.Add(i);
                                        }
                                        else
                                        {
                                            //no psuedo class
                                            Element newElement = new Element();
                                            newElement.Name             = nestedElement[0].ToString();
                                            newElement.ListElementStyle = listStyle;
                                            Id i = new Id();
                                            i.Name = id[1].ToString();
                                            i.ListElement.Add(newElement);
                                            listId.Add(i);
                                        }
                                    }
                                    else
                                    {
                                        //this is just a id
                                        Id i = new Id();
                                        i.Name        = id[1];
                                        i.ListIdStyle = listStyle;
                                        listId.Add(i);
                                    }
                                }
                            }
                            #endregion
                            #region Element
                            else
                            {
                                if (sel.ToString().Contains(" "))     //going to have multiple elements defined - possibly a nested element
                                {
                                    if (sel.ToString().Contains(",")) //this is going to be multiple elements
                                    {
                                    }
                                    else//this will be nested elements - having a parent
                                    {
                                        string[] parentElement = sel.ToString().Split(' ');//this could get deep
                                        int      peCounter     = parentElement.Length;

                                        while (peCounter >= 0)
                                        {
                                            //have to do this backward cause we need to set the parent
                                            if (parentElement[peCounter].Contains(":"))
                                            {
                                                //we have a psuedo
                                                string[] elementPsuedo = parentElement[peCounter].Split(':');//0 will be element 1 will be psuedo
                                                Psuedo   p             = new Psuedo();
                                                p.Name      = elementPsuedo[1].ToString();
                                                p.ListStyle = listStyle;

                                                Element newElement = new Element();
                                                newElement.Name = elementPsuedo[0].ToString();
                                                if (peCounter != parentElement.Length)
                                                {
                                                    newElement.Parent = listElem[listElem.Count - 1];// (listElem.Count - 1);
                                                }
                                                newElement.ListPsuedo.Add(p);
                                                //increment to the next element
                                                peCounter--;
                                            }
                                            else
                                            {
                                                Element newElement = new Element();
                                                newElement.Name = parentElement[peCounter].ToString();
                                                if (peCounter != parentElement.Length)
                                                {
                                                    newElement.Parent = listElem[listElem.Count - 1];// (listElem.Count - 1);
                                                }
                                                if (peCounter == parentElement.Length)
                                                {
                                                    newElement.ListElementStyle = listStyle;
                                                }

                                                listElem.Add(newElement);
                                                //increment to the next element
                                                peCounter--;
                                            }
                                        }
                                    }
                                }
                                else if (sel.ToString().Contains(":"))
                                {
                                    Element       e     = new Element();
                                    List <Psuedo> listP = new List <Psuedo>();
                                    string[]      ps    = sel.ToString().Split(':');
                                    for (int i = 0; i <= ps.Length - 1; i++)
                                    {
                                        //the 0 index will always be the element, each additional item will be a psuedo
                                        if (i == 0)
                                        {
                                            e.Name = ps[0];
                                        }
                                        else
                                        {
                                            Psuedo p = new Psuedo();
                                            p.ListStyle = listStyle;
                                            p.Name      = ps[i];
                                            listP.Add(p);
                                        }
                                    }
                                    e.ListPsuedo = listP;
                                    listElem.Add(e);
                                }
                                else
                                {
                                    //we have just a plain element
                                    Element newElement = new Element();
                                    newElement.Name             = sel.ToString();
                                    newElement.ListElementStyle = listStyle;

                                    listElem.Add(newElement);
                                }
                            }
                            #endregion
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string except = ex.ToString();
            }

            //create our new CssItem and add the list objects
            CssItem ci = new CssItem();

            ci.ListElement = listElem;
            ci.ListClass   = listClass;
            ci.ListId      = listId;
            ci.HasElement  = listElem.Count > 0 ? true : false;
            ci.HasClass    = listClass.Count > 0 ? true : false;
            ci.HasId       = listId.Count > 0 ? true : false;

            return(ci);
        }