Example #1
0
 public void TextTest()
 {
     string text = "text";
     HtmlText textNode = new HtmlText("root");
     textNode.Text = text;
     StringAssert.Equals(textNode.Text, text);
 }
Example #2
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;
        }
Example #3
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;
        }
Example #4
0
 public void HtmlTest()
 {
     HtmlNode target = new HtmlText("Test");
     string actual = "Test";
     Assert.AreEqual(target.Html, actual);
 }
Example #5
0
 public void SetParentTest()
 {
     HtmlNode node = new HtmlText("Test");
     HtmlElement parent = new HtmlElement("Parent");
     node.SetParent(parent);
     Assert.AreEqual(parent, node.Parent);
 }
Example #6
0
        public void IsTextTest()
        {
            HtmlNode target = new HtmlText("Test");
            Assert.IsTrue(target.IsText());

            target = new HtmlElement("Test");
            Assert.IsFalse(target.IsText());
        }
Example #7
0
 public void ToStringTest()
 {
     string text = "text";
     HtmlText textNode = new HtmlText(text);
     StringAssert.Equals(textNode.ToString(), text);
 }