Example #1
0
        protected internal HTMLElementNode InsertChild(int index, IHTMLElement nodeData)
        {
            var newNode = new HTMLElementNode(this, nodeData);

            Children.Insert(index, newNode);
            return(newNode);
        }
Example #2
0
        protected internal HTMLElementNode AddChild(IHTMLElement nodeData)
        {
            var newNode = new HTMLElementNode(this, nodeData);

            Children.Add(newNode);
            return(newNode);
        }
Example #3
0
        /// <summary>
        /// Returns a collection containing this element and all descendant elements.
        /// </summary>
        public static IEnumerable <HTMLElementNode> DescendantsAndSelf(this HTMLElementNode adapter)
        {
            yield return(adapter);

            foreach (HTMLElementNode child in adapter.Descendants())
            {
                yield return(child);
            }
        }
Example #4
0
        /// <summary>
        /// Returns a collection of ancestor elements.
        /// </summary>
        public static IEnumerable <HTMLElementNode> AncestorsAndSelf(this HTMLElementNode adapter)
        {
            yield return(adapter);

            foreach (HTMLElementNode child in adapter.Ancestors())
            {
                yield return(child);
            }
        }
Example #5
0
 /// <summary>
 /// Returns a collection of child elements.
 /// </summary>
 public static IEnumerable <HTMLElementNode> Elements(this HTMLElementNode adapter)
 {
     if (adapter == null)
     {
         throw new ArgumentNullException(nameof(adapter));
     }
     foreach (HTMLElementNode child in adapter.Children)
     {
         yield return(child);
     }
 }
Example #6
0
        public HTMLElementNode(HTMLElementNode parent, IHTMLElement nodeData)
        {
            Element = nodeData;
            if (parent == null)
            {
                Parent = this;
            }
            else
            {
                Parent = parent;
            }

            Children = new List <HTMLElementNode>();
        }
Example #7
0
        /// <summary>
        /// Returns a collection of ancestor elements.
        /// </summary>
        public static IEnumerable <HTMLElementNode> Ancestors(this HTMLElementNode adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }
            var parent = adapter.Parent;

            while (parent != null && parent != parent.Parent)
            {
                yield return(parent);

                parent = parent.Parent;
            }
        }
Example #8
0
 protected internal void RemoveChild(HTMLElementNode nodeData)
 {
     Children.Remove(nodeData);
 }
Example #9
0
 /// <summary>
 /// Returns a collection of descendant elements.
 /// </summary>
 public static IEnumerable <HTMLElementNode> Descendants <T>(this HTMLElementNode adapter)
 {
     return(adapter.Descendants().Where(i => i is T));
 }
Example #10
0
 public HTMLBuilder Parent()
 {
     lastNode = lastNode.Parent;
     return(this);
 }
Example #11
0
        private void WriteTree(StringBuilder sb, HTMLElementNode root, int nestedLevel = 0)
        {
            if (root != null)
            {
                string content     = "";
                var    elementType = root.Element.GetType();

                if (elementType == typeof(HTMLEmpty) || !Settings.WriteComments & elementType == typeof(HTMLComment))
                {
                    foreach (HTMLElementNode child in root.Children)
                    {
                        WriteTree(sb, child, nestedLevel);
                    }
                }
                else if (root.Children.Count == 0 && string.IsNullOrEmpty(root.Element.Content) && HTMLTags.SelfClosing.Contains(root.Element.Tag().ToLowerInvariant()))
                {
                    sb.AppendLine(new string(' ', nestedLevel * Settings.TabSize) + root.Element.Empty(Settings.EnforceProperCase));
                }
                else
                {
                    sb.Append(new string(' ', nestedLevel * Settings.TabSize) + root.Element.Open(Settings.EnforceProperCase));
                    if (root.Children.Count > 0 | root.Element.IsMultiLine)
                    {
                        sb.AppendLine();
                    }

                    if (!string.IsNullOrEmpty(root.Element.Content))
                    {
                        if (root.Children.Count > 0 | root.Element.IsMultiLine)
                        {
                            using (var sr = new System.IO.StringReader(root.Element.Content))
                            {
                                while (sr.Peek() != -1)
                                {
                                    content += new string(' ', (nestedLevel + 1) * Settings.TabSize) + sr.ReadLine() + Environment.NewLine;
                                }
                            }
                        }
                        else
                        {
                            content += root.Element.Content;
                        }

                        if (root.Element.ContentPosition == (int)ContentPosition.BeforeElements)
                        {
                            sb.Append(content);
                        }
                    }
                    foreach (HTMLElementNode child in root.Children)
                    {
                        if (Settings.IndentHeaderAndBodyTags || !Settings.IndentHeaderAndBodyTags && !((root.Element.Tag(true) ?? "") == "html"))
                        {
                            nestedLevel += 1;
                        }

                        WriteTree(sb, child, nestedLevel);
                        if (Settings.IndentHeaderAndBodyTags || !Settings.IndentHeaderAndBodyTags && !((root.Element.Tag(true) ?? "") == "html"))
                        {
                            nestedLevel -= 1;
                        }
                    }
                    if ((int)root.Element.ContentPosition == (int)ContentPosition.AfterAlements && !string.IsNullOrEmpty(content))
                    {
                        sb.Append(content);
                    }

                    if (root.Children.Count == 0 && !root.Element.IsMultiLine)
                    {
                        sb.AppendLine(root.Element.Close(Settings.EnforceProperCase));
                    }
                    else
                    {
                        sb.AppendLine(new string(' ', nestedLevel * Settings.TabSize) + root.Element.Close(Settings.EnforceProperCase));
                    }
                }
            }
        }