/////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="index"></param>
        /// <param name="simpleSelector"></param>
        public void Insert(int index, DCssSimpleSelector simpleSelector)
        {
            System.Diagnostics.Debug.Assert(index >= 0 && index <= m_simpleSelectorList.Count);
            System.Diagnostics.Debug.Assert(simpleSelector != null);

            m_simpleSelectorList.Insert(index, simpleSelector);
        }
        /////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        internal void TransformCSS(StringBuilder writer)
        {
            for (int index = 0, count = m_simpleSelectorList.Count; index < count; ++index)
            {
                DCssSimpleSelector simpleSelector = m_simpleSelectorList[index];

                writer.Append(simpleSelector.CSS);
                switch (simpleSelector.Combinator)
                {
                case DCssSimpleSelector.Relation.Descendant:
                    writer.Append(" ");
                    break;

                case DCssSimpleSelector.Relation.Child:
                    writer.Append(" > ");
                    break;

                case DCssSimpleSelector.Relation.Sibling:
                    writer.Append(" + ");
                    break;

                default:
                    System.Diagnostics.Debug.Assert(false);
                    break;
                }
            }

            writer.Append("{ ");

            for (int index = 0, count = m_properties.Count; index < count; ++index)
            {
                writer.Append(m_properties[index].CSS + " ");
            }

            writer.Append("}");
        }
Example #3
0
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public DCssSelector ResolveSelector(string input)
        {
            System.Diagnostics.Debug.Assert(input != null);

            DCssSelector selector = new DCssSelector();

            string[] selectorList = input.Split(CHtmlUtil.WhiteSpaceCharsArray);

            StringBuilder builder         = new StringBuilder();
            bool          combinatorExist = false;

            for (int tokenIndex = 0, count = selectorList.Length; tokenIndex < count; ++tokenIndex)
            {
                string token = selectorList[tokenIndex];

                if (token.Equals('+')) // combinator +
                {
                    if (selector.Count != 0 && combinatorExist == false)
                    {
                        selector[selector.Count - 1].Combinator = DCssSimpleSelector.Relation.Sibling;
                        combinatorExist = true;
                    }
                    else
                    {
                        throw new ParseExeception();
                    }
                }
                else if (token.Equals('>')) // combinator >
                {
                    if (selector.Count != 0 && combinatorExist == false)
                    {
                        selector[selector.Count - 1].Combinator = DCssSimpleSelector.Relation.Child;
                        combinatorExist = true;
                    }
                    else
                    {
                        throw new ParseExeception();
                    }
                }
                else
                {
                    DCssSimpleSelector simpleSelector = new DCssSimpleSelector();

                    builder.Length = 0;
                    int index = 0;
                    while (index < token.Length)
                    {
                        if (token[index].Equals('#')) // id
                        {
                            index += 1;
                            if (index >= token.Length)
                            {
                                throw new ParseExeception();
                            }

                            int nextIndex = token.IndexOfAny("#.:[".ToCharArray(), index);
                            if (nextIndex != -1)
                            {
                                simpleSelector.ID = token.Substring(index, nextIndex - index);
                                index             = nextIndex;
                            }
                            else
                            {
                                simpleSelector.ID = token.Substring(index);
                                index             = token.Length;
                            }
                        }

                        else if (token[index].Equals('.')) // class
                        {
                            index += 1;
                            if (index >= token.Length)
                            {
                                throw new ParseExeception();
                            }

                            int nextIndex = token.IndexOfAny("#.:[".ToCharArray(), index);
                            if (nextIndex != -1)
                            {
                                simpleSelector.Class = token.Substring(index, nextIndex - index);
                                index = nextIndex;
                            }
                            else
                            {
                                simpleSelector.Class = token.Substring(index);
                                index = token.Length;
                            }
                        }
                        else if (token[index].Equals('[')) // attribute
                        {
                            System.Diagnostics.Debug.Assert(false);
                        }
                        else if (token[index].Equals(':')) // pseudo-x
                        {
                            index += 1;
                            if (index >= token.Length)
                            {
                                throw new ParseExeception();
                            }

                            int nextIndex = token.IndexOfAny("#.:[".ToCharArray(), index);
                            if (nextIndex != -1)
                            {
                                simpleSelector.Pseudo = token.Substring(index, nextIndex - index);
                                index = nextIndex;
                            }
                            else
                            {
                                simpleSelector.Pseudo = token.Substring(index);
                                index = token.Length;
                            }
                        }
                        else // element name
                        {
                            int nextIndex = token.IndexOfAny("#.[:".ToCharArray(), index);
                            if (nextIndex != -1)
                            {
                                simpleSelector.ElementNames = token.Substring(index, nextIndex - index);
                                index = nextIndex;
                            }
                            else
                            {
                                simpleSelector.ElementNames = token.Substring(index);
                                index = token.Length;
                            }
                        }
                    }

                    selector.Add(simpleSelector);
                    combinatorExist = false;
                }
            }

            return(selector);
        }
        /////////////////////////////////////////////////////////////////////////////////
        #region ¤ñ¹ï¾Þ?

        /////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public bool IsMatching(CHtmlElement element)
        {
            System.Diagnostics.Debug.Assert(element != null);
            System.Diagnostics.Debug.Assert(m_simpleSelectorList.Count > 0);

            bool result = false;

            DCssSimpleSelector.Relation combinator = DCssSimpleSelector.Relation.Descendant;

            int index = m_simpleSelectorList.Count - 1;
            DCssSimpleSelector simpleSelector = m_simpleSelectorList[index];

            if (simpleSelector.IsMatching(element) == true) // first matching
            {
                --index;

                if (index >= 0)
                {
                    simpleSelector = m_simpleSelectorList[index];
                    combinator     = simpleSelector.Combinator;
                    switch (combinator)
                    {
                    case DCssSimpleSelector.Relation.Child:
                    case DCssSimpleSelector.Relation.Descendant:
                        element = element.Parent;     // child to parent
                        break;

                    case DCssSimpleSelector.Relation.Sibling:
                        element = element.PreviousSibling as CHtmlElement;     // brother
                        break;

                    default: System.Diagnostics.Debug.Assert(false); break;
                    }
                }

                while (index >= 0 && element != null)
                {
                    simpleSelector = m_simpleSelectorList[index];
                    if (simpleSelector.IsMatching(element) == true) // simple selector matching
                    {
                        --index;                                    // match next simple selector

                        if (index >= 0)
                        {
                            combinator = simpleSelector.Combinator;
                            switch (combinator)
                            {
                            case DCssSimpleSelector.Relation.Child:
                            case DCssSimpleSelector.Relation.Descendant:
                                element = element.Parent;     // child to parent
                                break;

                            case DCssSimpleSelector.Relation.Sibling:
                                element = element.PreviousSibling as CHtmlElement;     // brother
                                break;

                            default: System.Diagnostics.Debug.Assert(false); break;
                            }
                        }
                    }
                    else
                    {
                        if (combinator == DCssSimpleSelector.Relation.Descendant)
                        {
                            element = element.Parent;// child to parent
                        }
                        else if (combinator == DCssSimpleSelector.Relation.Child ||
                                 combinator == DCssSimpleSelector.Relation.Sibling)
                        {
                            break;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(false);
                        }
                    }
                }

                if (index == -1)
                {
                    result = true;
                }
            }

            return(result);
        }
 /////////////////////////////////////////////////////////////////////////////////
 /// <summary>
 ///
 /// </summary>
 /// <param name="simpleSelector"></param>
 /// <returns></returns>
 public int IndexOf(DCssSimpleSelector simpleSelector)
 {
     System.Diagnostics.Debug.Assert(simpleSelector != null);
     return(m_simpleSelectorList.IndexOf(simpleSelector));
 }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="simpleSelector"></param>
        public void Add(DCssSimpleSelector simpleSelector)
        {
            System.Diagnostics.Debug.Assert(simpleSelector != null);

            m_simpleSelectorList.Add(simpleSelector);
        }