/// <summary>
 /// This will add an element to the collection.
 /// </summary>
 /// <param name="attribute">The attribute to add.</param>
 /// <returns>The index at which it was added.</returns>
 public int Add(TmxAttribute attribute)
 {
     return this.List.Add(attribute);
 }
Esempio n. 2
0
 /// <summary>
 /// This will add an element to the collection.
 /// </summary>
 /// <param name="attribute">The attribute to add.</param>
 /// <returns>The index at which it was added.</returns>
 public int Add(TmxAttribute attribute)
 {
     return(this.List.Add(attribute));
 }
        /*
         * 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);
        }