/// <summary> /// Initializes a new instance of the TmxElement class. /// </summary> /// <param name="name">The name of this element.</param> public TmxElement(string name) { this.nodes = new TmxNodeCollection(this); this.attributes = new TmxAttributeCollection(this); this.name = name; this.isTerminated = false; }
/// <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); }
/// <summary> /// Initializes a new instance of the TmxDocument class. /// </summary> /// <param name="html">The HTML to parse.</param> internal TmxDocument(string html, bool wantSpaces, bool addLineBreaks) { this.nodes = new TmxNodeCollection(null); TmxParser parser = new TmxParser(); parser.RemoveEmptyElementText = !wantSpaces; this.nodes = parser.Parse(html, addLineBreaks); }
/// <summary> /// This allows you to directly access the first element in this colleciton with the given name. /// If the node does not exist, this will return null. /// </summary> public TmxNode this[string name] { get { TmxNodeCollection results = this.FindByName(name, false); if (results.Count > 0) { return(results[0]); } else { return(null); } } }
/// <summary> /// This will find the corresponding opening tag for the named one. This is identified as /// the most recently read node with the same name, but with no child nodes. /// </summary> /// <param name="nodes">The collection of nodes.</param> /// <param name="name">The name of the tag.</param> /// <returns>The index of the opening tag, or -1 if it was not found.</returns> private int FindTagOpenNodeIndex(TmxNodeCollection nodes, string name) { for (int index = nodes.Count - 1; index >= 0; index--) { if (nodes[index] is TmxElement) { if (Str.IsEqual(((TmxElement)nodes[index]).Name, name, true) && ((TmxElement)nodes[index]).Nodes.Count == 0 && ((TmxElement)nodes[index]).IsTerminated == false) { return(index); } } } return(-1); }
/// <summary> /// This will move all the nodes from the specified index to the new parent. /// </summary> /// <param name="nodes">The collection of nodes.</param> /// <param name="node_index">The index of the first node (in the above collection) to move.</param> /// <param name="new_parent">The node which will become the parent of the moved nodes.</param> private void MoveNodesDown(ref TmxNodeCollection nodes, int node_index, TmxElement new_parent) { for (int i = node_index; i < nodes.Count; i++) { ((TmxElement)new_parent).Nodes.Add(nodes[i]); nodes[i].SetParent(new_parent); } int c = nodes.Count; for (int i = node_index; i < c; i++) { nodes.RemoveAt(node_index); } new_parent.IsExplicitlyTerminated = true; }
/* * 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; }