public void HtmlAttributeCollectionConstructorTest()
        {
            HtmlElement element = new HtmlElement("root");
            HtmlAttributeCollection target = new HtmlAttributeCollection(element);

            StringAssert.Equals(target.HtmlElement.Name, "root");
        }
 public void IsExplicitlyTerminatedTest()
 {
     string name = "root";
     HtmlElement target = new HtmlElement(name);
     target.IsExplicitlyTerminated = true;
     Assert.IsTrue(target.IsExplicitlyTerminated);
 }
 public void HtmlElementConstructorTest()
 {
     string name = "root";
     HtmlElement target = new HtmlElement(name);
     StringAssert.Equals(target.Name, name);
     Assert.IsNotNull(target.Nodes);
     Assert.IsNotNull(target.Attributes);
 }
        public void AddTest()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlNodeCollection target = new HtmlNodeCollection(root);
            HtmlElement child = new HtmlElement("child");
            int index = target.Add(child);
            Assert.AreEqual(root, child.Parent);
            Assert.AreEqual(index, 0);

            target.Add(null);
        }
Beispiel #5
0
        public void GetCommonAncestorTest()
        {
            HtmlNode target = CreateTreeStructure();
            HtmlNode child1 = target.FirstChild.FirstChild;
            HtmlNode child2 = target.LastChild.LastChild;

            HtmlNode actual = child1.GetCommonAncestor(child2);

            Assert.AreEqual(target, actual);

            HtmlElement element = new HtmlElement("NoCommonAncestor");

            actual = child2.GetCommonAncestor(element);
            Assert.IsNull(actual);
        }
        public void FindByAttributeNameTest()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlNodeCollection target = new HtmlNodeCollection(root);
            target.Add(new HtmlElement("first"));
            target.Add(new HtmlElement("second"));
            target.Add(new HtmlElement("third"));
            ((HtmlElement)target[0]).Nodes.Add(new HtmlElement("secondchild"));

            ((HtmlElement)target[1]).Attributes.Add(new HtmlAttribute("firstattribute"));
            ((HtmlElement)target[1]).Attributes.Add(new HtmlAttribute("secondattribute"));
            ((HtmlElement)target[2]).Attributes.Add(new HtmlAttribute("firstattribute"));

            Assert.AreEqual(target.FindByAttributeName("firstattribute").Count, 2);

            ((HtmlElement)((HtmlElement)target[0]).Nodes[0]).Attributes.Add(new HtmlAttribute("firstattribute"));
            Assert.AreEqual(target.FindByAttributeName("firstattribute", false).Count, 2);
            Assert.AreEqual(target.FindByAttributeName("firstattribute", true).Count, 3);
        }
        public void ItemByNameTest()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlNodeCollection target = new HtmlNodeCollection(root);
            target.Add(new HtmlElement("first"));
            target.Add(new HtmlElement("second"));
            target.Add(new HtmlElement("second"));

            Assert.IsNotNull(target["second"]);

            Assert.IsNull(target["anyname"]);
        }
        public void ItemByIndexTest()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlNodeCollection target = new HtmlNodeCollection(root);
            target.Add(new HtmlElement("first"));
            target.Add(new HtmlElement("second"));
            target.Add(new HtmlElement("third"));

            Assert.AreEqual(target[1], target["second"]);

            target[2] = new HtmlElement("another");
            target[0] = null;

            StringAssert.Contains(target[2].ToString(), "another");
        }
        public void InsertTest()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlNodeCollection target = new HtmlNodeCollection(root);
            HtmlElement child = new HtmlElement("child");
            target.Add(child);

            child = new HtmlElement("second");
            target.Insert(0, child);
            Assert.AreEqual(root, child.Parent);
            Assert.AreEqual(target.IndexOf(child), 0);

            target.Insert(0, null);
        }
 public void HtmlNodeCollectionConstructorTest()
 {
     HtmlElement root = new HtmlElement("root");
     HtmlNodeCollection target = new HtmlNodeCollection(root);
     HtmlElement child = new HtmlElement("child");
     target.Add(child);
     Assert.AreEqual(root, child.Parent);
 }
        public void GetByNameTest()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlNodeCollection target = new HtmlNodeCollection(root);
            target.Add(new HtmlElement("first"));
            target.Add(new HtmlElement("second"));
            target.Add(new HtmlElement("second"));

            Assert.AreEqual(target.GetByName("second").Count, 2);

            ((HtmlElement)target[0]).Nodes.Add(new HtmlElement("second"));

            Assert.AreEqual(target.GetByName("second", false).Count, 2);

            Assert.AreEqual(target.GetByName("second").Count, 3);
        }
Beispiel #12
0
 public void SetParentTest()
 {
     HtmlNode node = new HtmlText("Test");
     HtmlElement parent = new HtmlElement("Parent");
     node.SetParent(parent);
     Assert.AreEqual(parent, node.Parent);
 }
Beispiel #13
0
        /// <summary>
        /// This will move all the nodes from the specified index to the new parent.
        /// </summary>
        private static void MoveNodesDown(ref HtmlNodeCollection nodes, int index, HtmlElement newParent)
        {
            int count = nodes.Count;

            for (int i = index; i < count; i++)
            {
                ((HtmlElement)newParent).Nodes.Add(nodes[i]);
                nodes[i].SetParent(newParent);
            }

            for (int i = index; i < count; i++)
            {
                nodes.RemoveAt(index);
            }
            newParent.IsExplicitlyTerminated = true;
        }
Beispiel #14
0
        private HtmlElement CreateElementStructure()
        {
            HtmlElement root = new HtmlElement("root");
            root.Attributes.Add(new HtmlAttribute("name", "value"));
            HtmlElement child = new HtmlElement("child");
            child.Attributes.Add(new HtmlAttribute("name", "value"));
            child.Attributes.Add(new HtmlAttribute("secondname", "secondvalue"));
            child.IsTerminated = true;
            root.Nodes.Add(child);
            child = new HtmlElement("anotherchild");
            child.Attributes.Add(new HtmlAttribute("name", "value"));
            child.IsExplicitlyTerminated = true;
            root.Nodes.Add(child);

            HtmlText textNode = new HtmlText("text");
            child.Nodes.Add(textNode);
            return root;
        }
Beispiel #15
0
 /// <summary>
 /// Internal method to maintain the identity of the parent nodes.
 /// </summary>
 /// <param attributeName="parentNode">The parent nodes of this one</param>
 internal void SetParent(HtmlElement parent)
 {
     this.parent = parent;
 }
Beispiel #16
0
 internal virtual HtmlNode CreateTreeStructure()
 {
     // |-Root
     //   |-FirstChild
     //   |  |-FirstFirstChild
     //   |  |-FirstSecondChild
     //   |-SecondChild
     //      |-SecondFirstChild
     //      |-SecondSecondChild
     //
     HtmlElement target = new HtmlElement("Root");
     HtmlElement child = new HtmlElement("FirstChild");
     child.Nodes.Add(new HtmlElement("FirstFirstChild"));
     child.Nodes.Add(new HtmlElement("FirstSecondChild"));
     target.Nodes.Add(child);
     child = new HtmlElement("SecondChild");
     child.Nodes.Add(new HtmlElement("SecondFirstChild"));
     child.Nodes.Add(new HtmlElement("SecondSecondChild"));
     target.Nodes.Add(child);
     return target;
 }
        internal HtmlAttributeCollection CreateTestCollection()
        {
            HtmlElement root = new HtmlElement("root");
            HtmlAttributeCollection target = new HtmlAttributeCollection(root);

            HtmlAttribute attribute = new HtmlAttribute("first");
            target.Add(attribute);
            attribute = new HtmlAttribute("second", "value");
            target.Add(attribute);
            attribute = new HtmlAttribute("third", "\"another value\"");
            target.Add(attribute);

            return target;
        }
Beispiel #18
0
        private static HtmlNodeCollection BuildNodeCollection(Queue<string> tokens)
        {
            HtmlNodeCollection nodes = new HtmlNodeCollection(null);
            HtmlElement element = null;
            string current;

            while (tokens.Count > 0)
            {
                current = tokens.Dequeue();
                switch (current)
                {
                    case ("<"):
                        // Read open tag

                        if (tokens.Count == 0)
                            break;

                        current = tokens.Dequeue();
                        element = new HtmlElement(current);

                        // read the attributes and values
                        while (tokens.Count > 0 && (current = tokens.Dequeue()) != ">" && current != "/>")
                        {
                            string attribute_name = current;
                            if (tokens.Count > 0 && tokens.Peek() == "=")
                            {
                                tokens.Dequeue();
                                current = (tokens.Count > 0) ? tokens.Dequeue() : null;
                                HtmlAttribute attribute = new HtmlAttribute(attribute_name, HttpUtility.HtmlDecode(current));
                                element.Attributes.Add(attribute);
                            }
                            else //if (tokens.Count == 0)
                            {
                                // Null-attributeValue attribute
                                HtmlAttribute attribute = new HtmlAttribute(attribute_name);
                                element.Attributes.Add(attribute);
                            }
                        }
                        nodes.Add(element);

                        if (current == "/>")
                        {
                            element.IsTerminated = true;
                            element = null; //could not have any sub elements
                        }
                        else if (current == ">")
                        {
                            continue;
                        }
                        break;
                    case (">"):
                        continue;
                    case ("</"):
                        // Read close tag

                        if (tokens.Count == 0)
                            break;

                        current = tokens.Dequeue();

                        int open_index = FindTagOpenNodeIndex(nodes, current);
                        if (open_index != -1)
                        {
                            MoveNodesDown(ref nodes, open_index + 1, (HtmlElement)nodes[open_index]);
                        }

                        // Skip to the end of this tag
                        while (tokens.Count > 0 && (current = tokens.Dequeue()) != ">")
                        {
                            //shouldn't happen
                        }
                        element = null;
                        break;
                    default:
                        HtmlText node = new HtmlText(current);
                        nodes.Add(node);
                        break;
                }
            }
            return nodes;
        }
Beispiel #19
0
        public void IsTextTest()
        {
            HtmlNode target = new HtmlText("Test");
            Assert.IsTrue(target.IsText());

            target = new HtmlElement("Test");
            Assert.IsFalse(target.IsText());
        }