Ejemplo n.º 1
0
        /// <summary>
        /// This will search though this collection of nodes for all elements with the
        /// specified name. If you want to search the subnodes recursively, you should
        /// pass True as the parameter in searchChildren. This search is guaranteed to
        /// return nodes in the order in which they are found in the document.
        /// </summary>
        /// <param name="name">The name of the element to find.</param>
        /// <param name="searchChildren">True if you want to search sub-nodes, False to
        /// only search this collection.</param>
        /// <returns>A collection of all the nodes that match.</returns>
        public TmxNodeCollection FindByName(string name, bool searchChildren)
        {
            TmxNodeCollection results = new TmxNodeCollection(null);

            foreach (TmxNode node in this.List)
            {
                if (node is TmxElement)
                {
                    if (((TmxElement)node).Name.ToLower().Equals(name.ToLower()))
                    {
                        results.Add(node);
                    }

                    if (searchChildren)
                    {
                        foreach (TmxNode matchedChild in ((TmxElement)node).Nodes.FindByName(name, searchChildren))
                        {
                            results.Add(matchedChild);
                        }
                    }
                }
            }

            return(results);
        }
        /*
         * This method contains valid code. Commented for code coverage purpose.
         *
         * /// <summary>
         * /// This will parse a string containing HTML and will produce a domain tree.
         * /// </summary>
         * /// <param name="html">The HTML to be parsed.</param>
         * /// <returns>A tree representing the elements.</returns>
         * public TmxNodeCollection Parse(string html)
         * {
         *  return this.Parse(html, false);
         * }
         */

        public TmxNodeCollection Parse(string html, bool addLineBreaks)
        {
            TmxNodeCollection nodes = new TmxNodeCollection(null);

            html = this.PreprocessScript(html, Token.SCRIPT);
            html = this.PreprocessScript(html, Token.STYLE);

            html = this.RemoveComments(html);

            StringCollection tokens = this.GetTokens(html);

            int        index   = 0;
            TmxElement element = null;

            while (index < tokens.Count)
            {
                if (Str.IsLT(tokens[index]))
                {
                    // Read open tag
                    index++;
                    if (index >= tokens.Count)
                    {
                        break;
                    }

                    string tag_name = tokens[index];
                    index++;
                    element = new TmxElement(tag_name);

                    if (addLineBreaks)
                    {
                        element.AddLineBreaks = true;
                    }

                    // Read the attributes and values.
                    while (index < tokens.Count && !Str.IsGT(tokens[index]) && !Str.IsSLGT(tokens[index]))
                    {
                        string attribute_name = tokens[index];
                        index++;

                        if (index < tokens.Count && Str.IsEQ(tokens[index]))
                        {
                            index++;
                            string attribute_value;
                            if (index < tokens.Count)
                            {
                                attribute_value = tokens[index];
                            }
                            else
                            {
                                attribute_value = null;
                            }

                            index++;

                            TmxAttribute attribute = new TmxAttribute(attribute_name, attribute_value);
                            element.Attributes.Add(attribute);
                        }
                        else if (index < tokens.Count)
                        {
                            // Null-value attribute
                            TmxAttribute attribute = new TmxAttribute(attribute_name, null);
                            element.Attributes.Add(attribute);
                        }
                    }

                    nodes.Add(element);
                    if (index < tokens.Count && Str.IsSLGT(tokens[index]))
                    {
                        element.IsTerminated = true;
                        index++;
                        element = null;
                    }
                    else if (index < tokens.Count && Str.IsGT(tokens[index]))
                    {
                        index++;
                    }
                }
                else if (Str.IsGT(tokens[index]))
                {
                    index++;
                }
                else if (Str.IsLTSL(tokens[index]))
                {
                    // Read close tag
                    index++;
                    if (index >= tokens.Count)
                    {
                        break;
                    }

                    string tag_name = tokens[index];
                    index++;

                    int open_index = this.FindTagOpenNodeIndex(nodes, tag_name);
                    if (open_index != -1)
                    {
                        this.MoveNodesDown(ref nodes, open_index + 1, (TmxElement)nodes[open_index]);
                    }
                    else
                    {
                        // Er, there is a close tag without an opening tag!!
                    }

                    // Skip to the end of this tag
                    while (index < tokens.Count && !Str.IsGT(tokens[index]))
                    {
                        index++;
                    }

                    if (index < tokens.Count && Str.IsGT(tokens[index]))
                    {
                        index++;
                    }

                    element = null;
                }
                else
                {
                    // Read text
                    string value = tokens[index];

                    if (this.isRemoveEmptyElementText)
                    {
                        value = this.RemoveWhitespace(value);
                    }

                    value = DecodeScript(value);

                    if (this.isRemoveEmptyElementText && value.Length == 0)
                    {
                        // We do nothing
                    }
                    else
                    {
                        TmxText node = new TmxText(value);
                        nodes.Add(node);
                    }

                    index++;
                }
            }

            return(nodes);
        }
        /// <summary>
        /// This will search though this collection of nodes for all elements with the
        /// specified name. If you want to search the subnodes recursively, you should
        /// pass True as the parameter in searchChildren. This search is guaranteed to
        /// return nodes in the order in which they are found in the document.
        /// </summary>
        /// <param name="name">The name of the element to find.</param>
        /// <param name="searchChildren">True if you want to search sub-nodes, False to
        /// only search this collection.</param>
        /// <returns>A collection of all the nodes that match.</returns>
        public TmxNodeCollection FindByName(string name, bool searchChildren)
        {
            TmxNodeCollection results = new TmxNodeCollection(null);

            foreach (TmxNode node in this.List)
            {
                if (node is TmxElement)
                {
                    if (((TmxElement)node).Name.ToLower().Equals(name.ToLower()))
                    {
                        results.Add(node);
                    }

                    if (searchChildren)
                    {
                        foreach (TmxNode matchedChild in ((TmxElement)node).Nodes.FindByName(name, searchChildren))
                        {
                            results.Add(matchedChild);
                        }
                    }
                }
            }

            return results;
        }